public void TestGetFreeBlockWithSpare() { NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi")); NPOIFSMiniStore ministore = fs.GetMiniStore(); // Our 2nd SBAT block has spares Assert.AreEqual(false, ministore.GetBATBlockAndIndex(0).Block.HasFreeSectors); Assert.AreEqual(true, ministore.GetBATBlockAndIndex(128).Block.HasFreeSectors); // First free one at 181 Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(181)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(182)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(183)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(184)); // Ask, will get 181 Assert.AreEqual(181, ministore.GetFreeBlock()); // Ask again, will still get 181 as not written to Assert.AreEqual(181, ministore.GetFreeBlock()); // Allocate it, then ask again ministore.SetNextBlock(181, POIFSConstants.END_OF_CHAIN); Assert.AreEqual(182, ministore.GetFreeBlock()); fs.Close(); }
public void TestGetFreeBlockWithNonSpare() { NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi")); NPOIFSMiniStore ministore = fs.GetMiniStore(); // We've spare ones from 181 to 255 for (int i = 181; i < 256; i++) { Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(i)); } // Check our SBAT free stuff is correct Assert.AreEqual(false, ministore.GetBATBlockAndIndex(0).Block.HasFreeSectors); Assert.AreEqual(true, ministore.GetBATBlockAndIndex(128).Block.HasFreeSectors); // Allocate all the spare ones for (int i = 181; i < 256; i++) { ministore.SetNextBlock(i, POIFSConstants.END_OF_CHAIN); } // SBAT are now full, but there's only the two Assert.AreEqual(false, ministore.GetBATBlockAndIndex(0).Block.HasFreeSectors); Assert.AreEqual(false, ministore.GetBATBlockAndIndex(128).Block.HasFreeSectors); try { Assert.AreEqual(false, ministore.GetBATBlockAndIndex(256).Block.HasFreeSectors); Assert.Fail("Should only be two SBATs"); } catch (ArgumentOutOfRangeException) { } // Now ask for a free one, will need to extend the SBAT chain Assert.AreEqual(256, ministore.GetFreeBlock()); Assert.AreEqual(false, ministore.GetBATBlockAndIndex(0).Block.HasFreeSectors); Assert.AreEqual(false, ministore.GetBATBlockAndIndex(128).Block.HasFreeSectors); Assert.AreEqual(true, ministore.GetBATBlockAndIndex(256).Block.HasFreeSectors); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(254)); // 2nd SBAT Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(255)); // 2nd SBAT Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(256)); // 3rd SBAT Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(257)); // 3rd SBAT fs.Close(); }
public void TestMultiBlockStream() { byte[] data1B = new byte[63]; byte[] data2B = new byte[64 + 14]; for (int i = 0; i < data1B.Length; i++) { data1B[i] = (byte)(i + 2); } for (int i = 0; i < data2B.Length; i++) { data2B[i] = (byte)(i + 4); } // New filesystem and store to use NPOIFSFileSystem fs = new NPOIFSFileSystem(); NPOIFSMiniStore ministore = fs.GetMiniStore(); // Initially has Properties + BAT but nothing else Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(2)); // Store the 2 block one, should use 2 mini blocks, and request // the use of 2 big blocks ministore = fs.GetMiniStore(); fs.Root.CreateDocument("mini2", new ByteArrayInputStream(data2B)); // Check Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2)); // SBAT Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(3)); // Mini Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(4)); // First 2 Mini blocks will be used Assert.AreEqual(2, ministore.GetFreeBlock()); // Add one more mini-stream, and check fs.Root.CreateDocument("mini1", new ByteArrayInputStream(data1B)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2)); // SBAT Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(3)); // Mini Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(4)); // One more mini-block will be used Assert.AreEqual(3, ministore.GetFreeBlock()); // Check the contents too byte[] r1 = new byte[data1B.Length]; DocumentInputStream dis = fs.CreateDocumentInputStream("mini1"); IOUtils.ReadFully(dis, r1); dis.Close(); CollectionAssert.AreEqual(data1B, r1); byte[] r2 = new byte[data2B.Length]; dis = fs.CreateDocumentInputStream("mini2"); IOUtils.ReadFully(dis, r2); dis.Close(); CollectionAssert.AreEqual(data2B, r2); fs.Close(); }