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 TestNextBlock() { // It's the same on 512 byte and 4096 byte block files! NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi")); NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi")); NPOIFSFileSystem fsC = new NPOIFSFileSystem(_inst.GetFile("BlockSize4096.zvi")); NPOIFSFileSystem fsD = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize4096.zvi")); foreach (NPOIFSFileSystem fs in new NPOIFSFileSystem[] { fsA, fsB, fsC, fsD }) { NPOIFSMiniStore ministore = fs.GetMiniStore(); // 0 -> 51 is one stream for (int i = 0; i < 50; i++) { Assert.AreEqual(i + 1, ministore.GetNextBlock(i)); } Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(50)); // 51 -> 103 is the next for (int i = 51; i < 103; i++) { Assert.AreEqual(i + 1, ministore.GetNextBlock(i)); } Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(103)); // Then there are 3 one block ones Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(104)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(105)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(106)); // 107 -> 154 is the next for (int i = 107; i < 154; i++) { Assert.AreEqual(i + 1, ministore.GetNextBlock(i)); } Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(154)); // 155 -> 160 is the next for (int i = 155; i < 160; i++) { Assert.AreEqual(i + 1, ministore.GetNextBlock(i)); } Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(160)); // 161 -> 166 is the next for (int i = 161; i < 166; i++) { Assert.AreEqual(i + 1, ministore.GetNextBlock(i)); } Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(166)); // 167 -> 172 is the next for (int i = 167; i < 172; i++) { Assert.AreEqual(i + 1, ministore.GetNextBlock(i)); } Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(172)); // Now some short ones Assert.AreEqual(174, ministore.GetNextBlock(173)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(174)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(175)); Assert.AreEqual(177, ministore.GetNextBlock(176)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(177)); Assert.AreEqual(179, ministore.GetNextBlock(178)); Assert.AreEqual(180, ministore.GetNextBlock(179)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(180)); // 181 onwards is free for (int i = 181; i < fs.GetBigBlockSizeDetails().GetBATEntriesPerBlock(); i++) { Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(i)); } fs.Close(); } fsD.Close(); fsC.Close(); fsB.Close(); fsA.Close(); }
public void TestCreateMiniStoreFirst() { NPOIFSFileSystem fs = new NPOIFSFileSystem(); NPOIFSMiniStore ministore = fs.GetMiniStore(); DocumentInputStream dis; DocumentEntry entry; // 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)); // Ministore has no blocks, so can't iterate until used try { ministore.GetNextBlock(0); } catch (ArgumentOutOfRangeException e) { } // Write a very small new document, will populate the ministore for us byte[] data = new byte[8]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)(i + 42); } fs.Root.CreateDocument("mini", new ByteArrayInputStream(data)); // Should now have a mini-fat and a mini-stream 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)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(3)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(4)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(1)); // Re-fetch the mini store, and add it a second time ministore = fs.GetMiniStore(); fs.Root.CreateDocument("mini2", new ByteArrayInputStream(data)); // Main unchanged, ministore has a second 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)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(3)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(4)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(2)); // Check the data is unchanged and the right length entry = (DocumentEntry)fs.Root.GetEntry("mini"); Assert.AreEqual(data.Length, entry.Size); byte[] rdata = new byte[data.Length]; dis = new DocumentInputStream(entry); IOUtils.ReadFully(dis, rdata); CollectionAssert.AreEqual(data, rdata); dis.Close(); entry = (DocumentEntry)fs.Root.GetEntry("mini2"); Assert.AreEqual(data.Length, entry.Size); rdata = new byte[data.Length]; dis = new DocumentInputStream(entry); IOUtils.ReadFully(dis, rdata); CollectionAssert.AreEqual(data, rdata); dis.Close(); // Done fs.Close(); }
public void TestCreateBlockIfNeeded() { NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi")); NPOIFSMiniStore ministore = fs.GetMiniStore(); // 178 -> 179 -> 180, 181+ is free Assert.AreEqual(179, ministore.GetNextBlock(178)); Assert.AreEqual(180, ministore.GetNextBlock(179)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(180)); for (int i = 181; i < 256; i++) { Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(i)); } // However, the ministore data only covers blocks to 183 for (int i = 0; i <= 183; i++) { ministore.GetBlockAt(i); } try { ministore.GetBlockAt(184); Assert.Fail("No block at 184"); } catch (IndexOutOfRangeException) { } // The ministore itself is made up of 23 big blocks IEnumerator <ByteBuffer> it = new NPOIFSStream(fs, fs.Root.Property.StartBlock).GetBlockIterator(); int count = 0; while (it.MoveNext()) { count++; //it.MoveNext(); } Assert.AreEqual(23, count); // Ask it to get block 184 with creating, it will do ministore.CreateBlockIfNeeded(184); // The ministore should be one big block bigger now it = new NPOIFSStream(fs, fs.Root.Property.StartBlock).GetBlockIterator(); count = 0; while (it.MoveNext()) { count++; //it.MoveNext(); } Assert.AreEqual(24, count); // The mini block block counts now run to 191 for (int i = 0; i <= 191; i++) { ministore.GetBlockAt(i); } try { ministore.GetBlockAt(192); Assert.Fail("No block at 192"); } catch (IndexOutOfRangeException) { } // Now try writing through to 192, check that the SBAT and blocks are there byte[] data = new byte[15 * 64]; NPOIFSStream stream = new NPOIFSStream(ministore, 178); stream.UpdateContents(data); // Check now Assert.AreEqual(179, ministore.GetNextBlock(178)); Assert.AreEqual(180, ministore.GetNextBlock(179)); Assert.AreEqual(181, ministore.GetNextBlock(180)); Assert.AreEqual(182, ministore.GetNextBlock(181)); Assert.AreEqual(183, ministore.GetNextBlock(182)); Assert.AreEqual(184, ministore.GetNextBlock(183)); Assert.AreEqual(185, ministore.GetNextBlock(184)); Assert.AreEqual(186, ministore.GetNextBlock(185)); Assert.AreEqual(187, ministore.GetNextBlock(186)); Assert.AreEqual(188, ministore.GetNextBlock(187)); Assert.AreEqual(189, ministore.GetNextBlock(188)); Assert.AreEqual(190, ministore.GetNextBlock(189)); Assert.AreEqual(191, ministore.GetNextBlock(190)); Assert.AreEqual(192, ministore.GetNextBlock(191)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(192)); for (int i = 193; i < 256; i++) { Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(i)); } fs.Close(); }
public void TestPropertiesAndFatOnRead() { NPOIFSFileSystem fsA, fsB; fsA = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi")); fsB = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi")); foreach (NPOIFSFileSystem fs in new NPOIFSFileSystem[] { fsA, fsB }) { fs.GetBATBlockAndIndex(0); fs.GetBATBlockAndIndex(1); try { fs.GetBATBlockAndIndex(140); Assert.Fail("Should only be one BAT, but a 2nd was found"); } //catch (IndexOutOfRangeException) catch (ArgumentOutOfRangeException) { } Assert.AreEqual(98, fs.GetNextBlock(97)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(98)); NPropertyTable props = fs.PropertyTable; Assert.AreEqual(90, props.StartBlock); Assert.AreEqual(7, props.CountBlocks); RootProperty root = props.Root; Assert.AreEqual("Root Entry", root.Name); Assert.AreEqual(11564, root.Size); Assert.AreEqual(0, root.StartBlock); NStorage.Properties.Property prop; IEnumerator <NStorage.Properties.Property> pi = root.Children; //prop = pi.Current; pi.MoveNext(); prop = pi.Current; Assert.AreEqual("Thumbnail", prop.Name); pi.MoveNext(); prop = pi.Current; Assert.AreEqual("\x0005DocumentSummaryInformation", prop.Name); pi.MoveNext(); prop = pi.Current; Assert.AreEqual("\x0005SummaryInformation", prop.Name); pi.MoveNext(); prop = pi.Current; Assert.AreEqual("Image", prop.Name); pi.MoveNext(); prop = pi.Current; Assert.AreEqual(false, pi.MoveNext()); NPOIFSMiniStore miniStore = fs.GetMiniStore(); miniStore.GetBATBlockAndIndex(0); miniStore.GetBATBlockAndIndex(128); try { miniStore.GetBATBlockAndIndex(256); Assert.Fail("Should only be two SBATs, but a 3rd was found"); } //catch (IndexOutOfRangeException) catch (ArgumentOutOfRangeException) { } for (int i = 0; i < 50; i++) { Assert.AreEqual(i + 1, miniStore.GetNextBlock(i)); } Assert.AreEqual(POIFSConstants.END_OF_CHAIN, miniStore.GetNextBlock(50)); fs.Close(); } fsA = new NPOIFSFileSystem(_inst.GetFile("BlockSize4096.zvi")); fsB = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize4096.zvi")); foreach (NPOIFSFileSystem fs in new NPOIFSFileSystem[] { fsA, fsB }) { fs.GetBATBlockAndIndex(0); fs.GetBATBlockAndIndex(1); try { fs.GetBATBlockAndIndex(1040); Assert.Fail("Should only be one BAT, but a 2nd was found"); } //catch (IndexOutOfRangeException) catch (ArgumentOutOfRangeException) { } Assert.AreEqual(1, fs.GetNextBlock(0)); Assert.AreEqual(2, fs.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2)); NPropertyTable props = fs.PropertyTable; Assert.AreEqual(12, props.StartBlock); Assert.AreEqual(1, props.CountBlocks); RootProperty root = props.Root; Assert.AreEqual("Root Entry", root.Name); Assert.AreEqual(11564, root.Size); Assert.AreEqual(0, root.StartBlock); NStorage.Properties.Property prop; IEnumerator <NStorage.Properties.Property> pi = root.Children; pi.MoveNext(); prop = pi.Current; Assert.AreEqual("Thumbnail", prop.Name); pi.MoveNext(); prop = pi.Current; Assert.AreEqual("\x0005DocumentSummaryInformation", prop.Name); pi.MoveNext(); prop = pi.Current; Assert.AreEqual("\x0005SummaryInformation", prop.Name); pi.MoveNext(); prop = pi.Current; Assert.AreEqual("Image", prop.Name); pi.MoveNext(); prop = pi.Current; Assert.AreEqual("Tags", prop.Name); Assert.AreEqual(false, pi.MoveNext()); NPOIFSMiniStore miniStore = fs.GetMiniStore(); miniStore.GetBATBlockAndIndex(0); miniStore.GetBATBlockAndIndex(128); miniStore.GetBATBlockAndIndex(1023); try { miniStore.GetBATBlockAndIndex(1024); Assert.Fail("Should only be one SBAT, but a 2nd was found"); } //catch(IndexOutOfRangeException) catch (ArgumentOutOfRangeException) { } for (int i = 0; i < 50; i++) { Assert.AreEqual(i + 1, miniStore.GetNextBlock(i)); } Assert.AreEqual(POIFSConstants.END_OF_CHAIN, miniStore.GetNextBlock(50)); fs.Close(); } }
public void TestWriteMiniStreams() { NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi")); NPOIFSMiniStore ministore = fs.GetMiniStore(); NPOIFSStream stream = new NPOIFSStream(ministore, 178); // 178 -> 179 -> 180 -> end Assert.AreEqual(179, ministore.GetNextBlock(178)); Assert.AreEqual(180, ministore.GetNextBlock(179)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(180)); // Try writing 3 full blocks worth byte[] data = new byte[64 * 3]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)i; } stream = new NPOIFSStream(ministore, 178); stream.UpdateContents(data); // Check Assert.AreEqual(179, ministore.GetNextBlock(178)); Assert.AreEqual(180, ministore.GetNextBlock(179)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(180)); stream = new NPOIFSStream(ministore, 178); IEnumerator <ByteBuffer> it = stream.GetBlockIterator(); it.MoveNext(); ByteBuffer b178 = it.Current; it.MoveNext(); ByteBuffer b179 = it.Current; it.MoveNext(); ByteBuffer b180 = it.Current; Assert.AreEqual(false, it.MoveNext()); Assert.AreEqual((byte)0x00, b178.Read()); Assert.AreEqual((byte)0x01, b178.Read()); Assert.AreEqual((byte)0x40, b179.Read()); Assert.AreEqual((byte)0x41, b179.Read()); Assert.AreEqual((byte)0x80, b180.Read()); Assert.AreEqual((byte)0x81, b180.Read()); // Try writing just into 3 blocks worth data = new byte[64 * 2 + 12]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)(i + 4); } stream = new NPOIFSStream(ministore, 178); stream.UpdateContents(data); // Check Assert.AreEqual(179, ministore.GetNextBlock(178)); Assert.AreEqual(180, ministore.GetNextBlock(179)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(180)); stream = new NPOIFSStream(ministore, 178); it = stream.GetBlockIterator(); it.MoveNext(); b178 = it.Current; it.MoveNext(); b179 = it.Current; it.MoveNext(); b180 = it.Current; Assert.AreEqual(false, it.MoveNext()); Assert.AreEqual((byte)0x04, b178.Read()); Assert.AreEqual((byte)0x05, b178.Read()); Assert.AreEqual((byte)0x44, b179.Read()); Assert.AreEqual((byte)0x45, b179.Read()); Assert.AreEqual((byte)0x84, b180.Read()); Assert.AreEqual((byte)0x85, b180.Read()); // Try writing 1, should truncate data = new byte[12]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)(i + 9); } stream = new NPOIFSStream(ministore, 178); stream.UpdateContents(data); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(178)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(179)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(180)); stream = new NPOIFSStream(ministore, 178); it = stream.GetBlockIterator(); it.MoveNext(); b178 = it.Current; Assert.AreEqual(false, it.MoveNext()); Assert.AreEqual((byte)0x09, b178[0]); Assert.AreEqual((byte)0x0a, b178[1]); // Try writing 5, should extend Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(178)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(179)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(180)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(181)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(182)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(183)); data = new byte[64 * 4 + 12]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)(i + 3); } stream = new NPOIFSStream(ministore, 178); stream.UpdateContents(data); Assert.AreEqual(179, ministore.GetNextBlock(178)); Assert.AreEqual(180, ministore.GetNextBlock(179)); Assert.AreEqual(181, ministore.GetNextBlock(180)); Assert.AreEqual(182, ministore.GetNextBlock(181)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(182)); stream = new NPOIFSStream(ministore, 178); it = stream.GetBlockIterator(); it.MoveNext(); b178 = it.Current; it.MoveNext(); b179 = it.Current; it.MoveNext(); b180 = it.Current; it.MoveNext(); ByteBuffer b181 = it.Current; it.MoveNext(); ByteBuffer b182 = it.Current; Assert.AreEqual(false, it.MoveNext()); Assert.AreEqual((byte)0x03, b178[0]); Assert.AreEqual((byte)0x04, b178[1]); Assert.AreEqual((byte)0x43, b179[0]); Assert.AreEqual((byte)0x44, b179[1]); Assert.AreEqual((byte)0x83, b180[0]); Assert.AreEqual((byte)0x84, b180[1]); Assert.AreEqual((byte)0xc3, b181[0]); Assert.AreEqual((byte)0xc4, b181[1]); Assert.AreEqual((byte)0x03, b182[0]); Assert.AreEqual((byte)0x04, b182[1]); // Write lots, so it needs another big block ministore.GetBlockAt(183); try { ministore.GetBlockAt(184); Assert.Fail("Block 184 should be off the end of the list"); } // catch (ArgumentOutOfRangeException e) catch (Exception) { } data = new byte[64 * 6 + 12]; for (int i = 0; i < data.Length; i++) { data[i] = (byte)(i + 1); } stream = new NPOIFSStream(ministore, 178); stream.UpdateContents(data); // Should have added 2 more blocks to the chain Assert.AreEqual(179, ministore.GetNextBlock(178)); Assert.AreEqual(180, ministore.GetNextBlock(179)); Assert.AreEqual(181, ministore.GetNextBlock(180)); Assert.AreEqual(182, ministore.GetNextBlock(181)); Assert.AreEqual(183, ministore.GetNextBlock(182)); Assert.AreEqual(184, ministore.GetNextBlock(183)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, ministore.GetNextBlock(184)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, ministore.GetNextBlock(185)); // Block 184 should exist ministore.GetBlockAt(183); ministore.GetBlockAt(184); ministore.GetBlockAt(185); // Check contents stream = new NPOIFSStream(ministore, 178); it = stream.GetBlockIterator(); it.MoveNext(); b178 = it.Current; it.MoveNext(); b179 = it.Current; it.MoveNext(); b180 = it.Current; it.MoveNext(); b181 = it.Current; it.MoveNext(); b182 = it.Current; it.MoveNext(); ByteBuffer b183 = it.Current; it.MoveNext(); ByteBuffer b184 = it.Current; Assert.AreEqual(false, it.MoveNext()); Assert.AreEqual((byte)0x01, b178[0]); Assert.AreEqual((byte)0x02, b178[1]); Assert.AreEqual((byte)0x41, b179[0]); Assert.AreEqual((byte)0x42, b179[1]); Assert.AreEqual((byte)0x81, b180[0]); Assert.AreEqual((byte)0x82, b180[1]); Assert.AreEqual((byte)0xc1, b181[0]); Assert.AreEqual((byte)0xc2, b181[1]); Assert.AreEqual((byte)0x01, b182[0]); Assert.AreEqual((byte)0x02, b182[1]); Assert.AreEqual((byte)0x41, b183[0]); Assert.AreEqual((byte)0x42, b183[1]); Assert.AreEqual((byte)0x81, b184[0]); Assert.AreEqual((byte)0x82, b184[1]); fs.Close(); }