PageRenderTime 57ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/unittests/LibraryTests/Partitions/GuidPartitionTableTest.cs

#
C# | 203 lines | 136 code | 39 blank | 28 comment | 0 complexity | b1afb3ed1ad30e68194802dfecb511a2 MD5 | raw file
  1. //
  2. // Copyright (c) 2008-2011, Kenneth Bell
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a
  5. // copy of this software and associated documentation files (the "Software"),
  6. // to deal in the Software without restriction, including without limitation
  7. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  8. // and/or sell copies of the Software, and to permit persons to whom the
  9. // Software is furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  20. // DEALINGS IN THE SOFTWARE.
  21. //
  22. using System.IO;
  23. using NUnit.Framework;
  24. using DiscUtils.Vdi;
  25. namespace DiscUtils.Partitions
  26. {
  27. [TestFixture]
  28. public class GuidPartitionTableTest
  29. {
  30. [Test]
  31. public void Initialize()
  32. {
  33. MemoryStream ms = new MemoryStream();
  34. using (Disk disk = Disk.InitializeDynamic(ms, Ownership.Dispose, 3 * 1024 * 1024))
  35. {
  36. GuidPartitionTable table = GuidPartitionTable.Initialize(disk);
  37. Assert.AreEqual(0, table.Count);
  38. }
  39. }
  40. [Test]
  41. public void CreateSmallWholeDisk()
  42. {
  43. MemoryStream ms = new MemoryStream();
  44. using (Disk disk = Disk.InitializeDynamic(ms, Ownership.Dispose, 3 * 1024 * 1024))
  45. {
  46. GuidPartitionTable table = GuidPartitionTable.Initialize(disk);
  47. int idx = table.Create(WellKnownPartitionType.WindowsFat, true);
  48. // Make sure the partition fills from first to last usable.
  49. Assert.AreEqual(table.FirstUsableSector, table[idx].FirstSector);
  50. Assert.AreEqual(table.LastUsableSector, table[idx].LastSector);
  51. }
  52. }
  53. [Test]
  54. public void CreateMediumWholeDisk()
  55. {
  56. MemoryStream ms = new MemoryStream();
  57. using (Disk disk = Disk.InitializeDynamic(ms, Ownership.Dispose, 2 * 1024L * 1024 * 1024))
  58. {
  59. GuidPartitionTable table = GuidPartitionTable.Initialize(disk);
  60. int idx = table.Create(WellKnownPartitionType.WindowsFat, true);
  61. Assert.AreEqual(2, table.Partitions.Count);
  62. Assert.AreEqual(GuidPartitionTypes.MicrosoftReserved, table[0].GuidType);
  63. Assert.AreEqual(32 * 1024 * 1024, table[0].SectorCount * 512);
  64. // Make sure the partition fills from first to last usable, allowing for MicrosoftReserved sector.
  65. Assert.AreEqual(table[0].LastSector + 1, table[idx].FirstSector);
  66. Assert.AreEqual(table.LastUsableSector, table[idx].LastSector);
  67. }
  68. }
  69. [Test]
  70. public void CreateLargeWholeDisk()
  71. {
  72. MemoryStream ms = new MemoryStream();
  73. using (Disk disk = Disk.InitializeDynamic(ms, Ownership.Dispose, 200 * 1024L * 1024 * 1024))
  74. {
  75. GuidPartitionTable table = GuidPartitionTable.Initialize(disk);
  76. int idx = table.Create(WellKnownPartitionType.WindowsFat, true);
  77. Assert.AreEqual(2, table.Partitions.Count);
  78. Assert.AreEqual(GuidPartitionTypes.MicrosoftReserved, table[0].GuidType);
  79. Assert.AreEqual(128 * 1024 * 1024, table[0].SectorCount * 512);
  80. // Make sure the partition fills from first to last usable, allowing for MicrosoftReserved sector.
  81. Assert.AreEqual(table[0].LastSector + 1, table[idx].FirstSector);
  82. Assert.AreEqual(table.LastUsableSector, table[idx].LastSector);
  83. }
  84. }
  85. [Test]
  86. public void CreateAlignedWholeDisk()
  87. {
  88. MemoryStream ms = new MemoryStream();
  89. using (Disk disk = Disk.InitializeDynamic(ms, Ownership.Dispose, 200 * 1024L * 1024 * 1024))
  90. {
  91. GuidPartitionTable table = GuidPartitionTable.Initialize(disk);
  92. int idx = table.CreateAligned(WellKnownPartitionType.WindowsFat, true, 1024 * 1024);
  93. Assert.AreEqual(2, table.Partitions.Count);
  94. Assert.AreEqual(GuidPartitionTypes.MicrosoftReserved, table[0].GuidType);
  95. Assert.AreEqual(128 * 1024 * 1024, table[0].SectorCount * 512);
  96. // Make sure the partition is aligned
  97. Assert.AreEqual(0, table[idx].FirstSector % 2048);
  98. Assert.AreEqual(0, (table[idx].LastSector + 1) % 2048);
  99. // Ensure partition fills most of the disk
  100. Assert.Greater((table[idx].SectorCount * 512), disk.Capacity * 0.9);
  101. }
  102. }
  103. [Test]
  104. public void CreateBySize()
  105. {
  106. MemoryStream ms = new MemoryStream();
  107. using (Disk disk = Disk.InitializeDynamic(ms, Ownership.Dispose, 3 * 1024 * 1024))
  108. {
  109. GuidPartitionTable table = GuidPartitionTable.Initialize(disk);
  110. int idx = table.Create(2 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false);
  111. // Make sure the partition is within 10% of the size requested.
  112. Assert.That((2 * 1024 * 2) * 0.9 < table[idx].SectorCount);
  113. Assert.AreEqual(table.FirstUsableSector, table[idx].FirstSector);
  114. }
  115. }
  116. [Test]
  117. public void CreateBySizeInGap()
  118. {
  119. MemoryStream ms = new MemoryStream();
  120. using (Disk disk = Disk.InitializeDynamic(ms, Ownership.Dispose, 300 * 1024 * 1024))
  121. {
  122. GuidPartitionTable table = GuidPartitionTable.Initialize(disk);
  123. table.Create(10 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false);
  124. table.Create((20 * 1024 * 1024) / 512, ((30 * 1024 * 1024) / 512) - 1, GuidPartitionTypes.WindowsBasicData, 0, "Data Partition");
  125. table.Create((60 * 1024 * 1024) / 512, ((70 * 1024 * 1024) / 512) - 1, GuidPartitionTypes.WindowsBasicData, 0, "Data Partition");
  126. int idx = table.Create(20 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false);
  127. Assert.AreEqual(((30 * 1024 * 1024) / 512), table[idx].FirstSector);
  128. Assert.AreEqual(((50 * 1024 * 1024) / 512) - 1, table[idx].LastSector);
  129. }
  130. }
  131. [Test]
  132. public void CreateBySizeInGapAligned()
  133. {
  134. MemoryStream ms = new MemoryStream();
  135. using (Disk disk = Disk.InitializeDynamic(ms, Ownership.Dispose, 300 * 1024 * 1024))
  136. {
  137. GuidPartitionTable table = GuidPartitionTable.Initialize(disk);
  138. table.Create(10 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false);
  139. // Note: end is unaligned
  140. table.Create((20 * 1024 * 1024) / 512, ((30 * 1024 * 1024) / 512) - 5, GuidPartitionTypes.WindowsBasicData, 0, "Data Partition");
  141. table.Create((60 * 1024 * 1024) / 512, ((70 * 1024 * 1024) / 512) - 1, GuidPartitionTypes.WindowsBasicData, 0, "Data Partition");
  142. int idx = table.CreateAligned(20 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false, 64 * 1024);
  143. Assert.AreEqual(((30 * 1024 * 1024) / 512), table[idx].FirstSector);
  144. Assert.AreEqual(((50 * 1024 * 1024) / 512) - 1, table[idx].LastSector);
  145. }
  146. }
  147. [Test]
  148. public void Delete()
  149. {
  150. MemoryStream ms = new MemoryStream();
  151. using (Disk disk = Disk.InitializeDynamic(ms, Ownership.Dispose, 10 * 1024 * 1024))
  152. {
  153. GuidPartitionTable table = GuidPartitionTable.Initialize(disk);
  154. Assert.AreEqual(0, table.Create(1 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false));
  155. Assert.AreEqual(1, table.Create(2 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false));
  156. Assert.AreEqual(2, table.Create(3 * 1024 * 1024, WellKnownPartitionType.WindowsFat, false));
  157. long[] sectorCount = new long[] { table[0].SectorCount, table[1].SectorCount, table[2].SectorCount };
  158. table.Delete(1);
  159. Assert.AreEqual(2, table.Count);
  160. Assert.AreEqual(sectorCount[2], table[1].SectorCount);
  161. }
  162. }
  163. }
  164. }