public NPropertyTable(HeaderBlock headerBlock, NPOIFSFileSystem fileSystem) :base(headerBlock, BuildProperties( (new NPOIFSStream(fileSystem, headerBlock.PropertyStart)).GetEnumerator(),headerBlock.BigBlockSize) ) { _bigBigBlockSize = headerBlock.BigBlockSize; }
/** * Returns test files with 512 byte and 4k block sizes, loaded * both from InputStreams and Files */ protected NPOIFSFileSystem[] get512and4kFileAndInput() { 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")); return new NPOIFSFileSystem[] { fsA, fsB, fsC, fsD }; }
/** * Constructor for a new Document * * @param name the name of the POIFSDocument * @param stream the InputStream we read data from */ public NPOIFSDocument(String name, NPOIFSFileSystem filesystem, Stream stream) { this._filesystem = filesystem; // sotre it int length = Store(stream); // Build the property for it this._property = new DocumentProperty(name, length); _property.StartBlock = _stream.GetStartBlock(); }
public NPOIFSMiniStore(NPOIFSFileSystem filesystem, RootProperty root, List<BATBlock> sbats, HeaderBlock header) { this._filesystem = filesystem; this._sbat_blocks = sbats; this._header = header; this._root = root; this._mini_stream = new NPOIFSStream(filesystem, root.StartBlock); }
public NPOIFSMiniStore(NPOIFSFileSystem filesystem, RootProperty root, List <BATBlock> sbats, HeaderBlock header) { this._filesystem = filesystem; this._sbat_blocks = sbats; this._header = header; this._root = root; this._mini_stream = new NPOIFSStream(filesystem, root.StartBlock); }
/** * Constructor for an existing Document */ public NPOIFSDocument(DocumentProperty property, NPOIFSFileSystem filesystem) { this._property = property; this._filesystem = filesystem; if (property.Size < POIFSConstants.BIG_BLOCK_MINIMUM_DOCUMENT_SIZE) { _stream = new NPOIFSStream(_filesystem.GetMiniStore(), property.StartBlock); _block_size = _filesystem.GetMiniStore().GetBlockStoreBlockSize(); } else { _stream = new NPOIFSStream(_filesystem, property.StartBlock); _block_size = _filesystem.GetBlockStoreBlockSize(); } }
private DirectoryNode(DirectoryProperty property, DirectoryNode parent, POIFSFileSystem oFileSystem, NPOIFSFileSystem nFileSystem) : base(property, parent) { this._oFilesSystem = oFileSystem; this._nFilesSystem = nFileSystem; if (parent == null) { _path = new POIFSDocumentPath(); } else { _path = new POIFSDocumentPath(parent._path, new string[] { property.Name }); } _byname = new Dictionary <string, Entry>(); _entries = new List <Entry>(); IEnumerator <Property> iter = property.Children; while (iter.MoveNext()) { Property child = iter.Current; Entry childNode = null; if (child.IsDirectory) { DirectoryProperty childDir = (DirectoryProperty)child; if (_oFilesSystem != null) { childNode = new DirectoryNode(childDir, _oFilesSystem, this); } else { childNode = new DirectoryNode(childDir, _nFilesSystem, this); } } else { childNode = new DocumentNode((DocumentProperty)child, this); } _entries.Add(childNode); _byname.Add(childNode.Name, childNode); } }
protected static void assertBATCount(NPOIFSFileSystem fs, int expectedBAT, int expectedXBAT) { int foundBAT = 0; int foundXBAT = 0; int sz = (int)(fs.Size / fs.GetBigBlockSize()); for (int i = 0; i < sz; i++) { if (fs.GetNextBlock(i) == POIFSConstants.FAT_SECTOR_BLOCK) { foundBAT++; } if (fs.GetNextBlock(i) == POIFSConstants.DIFAT_SECTOR_BLOCK) { foundXBAT++; } } Assert.AreEqual(expectedBAT, foundBAT, "Wrong number of BATs"); Assert.AreEqual(expectedXBAT, foundXBAT, "Wrong number of XBATs with " + expectedBAT + " BATs"); }
protected DirectoryNode[] openSamples(Stream[] inps, bool oldFails) { NPOIFSFileSystem nfs = new NPOIFSFileSystem(inps[0]); if (openedFSs == null) openedFSs = new List<NPOIFSFileSystem>(); openedFSs.Add(nfs); POIFSFileSystem ofs = null; try { ofs = new POIFSFileSystem(inps[1]); if (oldFails) Assert.Fail("POIFSFileSystem should have failed but didn't"); } catch (Exception e) { if (!oldFails) throw e; } if (ofs == null) return new DirectoryNode[] { nfs.Root }; return new DirectoryNode[] { ofs.Root, nfs.Root }; }
public void TestReadShortStream() { NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi")); // 97 -> 98 -> end NPOIFSStream stream = new NPOIFSStream(fs, 97); IEnumerator<ByteBuffer> i = stream.GetBlockIterator(); Assert.AreEqual(true, i.MoveNext()); // i.MoveNext(); ByteBuffer b97 = i.Current; Assert.AreEqual(true, i.MoveNext()); //i.MoveNext(); ByteBuffer b98 = i.Current; Assert.AreEqual(false, i.MoveNext()); // Check the contents of the 1st block Assert.AreEqual((byte)0x01, b97[0]); Assert.AreEqual((byte)0x00, b97[1]); Assert.AreEqual((byte)0x00, b97[2]); Assert.AreEqual((byte)0x00, b97[3]); Assert.AreEqual((byte)0x02, b97[4]); Assert.AreEqual((byte)0x00, b97[5]); Assert.AreEqual((byte)0x00, b97[6]); Assert.AreEqual((byte)0x00, b97[7]); // Check the contents of the 2nd block Assert.AreEqual((byte)0x81, b98[0]); Assert.AreEqual((byte)0x00, b98[1]); Assert.AreEqual((byte)0x00, b98[2]); Assert.AreEqual((byte)0x00, b98[3]); Assert.AreEqual((byte)0x82, b98[4]); Assert.AreEqual((byte)0x00, b98[5]); Assert.AreEqual((byte)0x00, b98[6]); Assert.AreEqual((byte)0x00, b98[7]); fs.Close(); }
public void TestReadTinyStream() { NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi")); // 98 is actually the last block in a two block stream... NPOIFSStream stream = new NPOIFSStream(fs, 98); IEnumerator<ByteBuffer> i = stream.GetBlockIterator(); Assert.AreEqual(true, i.MoveNext()); ByteBuffer b = i.Current; Assert.AreEqual(false, i.MoveNext()); // Check the contents Assert.AreEqual((byte)0x81, b[0]); Assert.AreEqual((byte)0x00, b[1]); Assert.AreEqual((byte)0x00, b[2]); Assert.AreEqual((byte)0x00, b[3]); Assert.AreEqual((byte)0x82, b[4]); Assert.AreEqual((byte)0x00, b[5]); Assert.AreEqual((byte)0x00, b[6]); Assert.AreEqual((byte)0x00, b[7]); fs.Close(); }
public void TestFiles() { FileStream[] files = { // bug 51891 POIDataSamples.GetPOIFSInstance().GetFile("multimedia.doc"), // tika bug 1072 POIDataSamples.GetPOIFSInstance().GetFile("20-Force-on-a-current-S00.doc"), // other files Containing ole10native records ... POIDataSamples.GetDocumentInstance().GetFile("Bug53380_3.doc"), POIDataSamples.GetDocumentInstance().GetFile("Bug47731.doc") }; foreach (FileStream f in files) { NPOIFSFileSystem fs = new NPOIFSFileSystem(f,null, true, true); List<Entry> entries = new List<Entry>(); FindOle10(entries, fs.Root, "/", ""); foreach (Entry e in entries) { MemoryStream bosExp = new MemoryStream(); Stream is1 = ((DirectoryNode)e.Parent).CreateDocumentInputStream(e); IOUtils.Copy(is1, bosExp); is1.Close(); Ole10Native ole = Ole10Native.CreateFromEmbeddedOleObject((DirectoryNode)e.Parent); MemoryStream bosAct = new MemoryStream(); ole.WriteOut(bosAct); //assertThat(bosExp.ToByteArray(), EqualTo(bosAct.ToByteArray())); Assert.IsTrue(Arrays.Equals(bosExp.ToArray(), bosAct.ToArray())); } fs.Close(); } }
/// <summary> /// Create a DirectoryNode. This method Is not public by design; it /// Is intended strictly for the internal use of this package /// </summary> /// <param name="property">the DirectoryProperty for this DirectoryEntry</param> /// <param name="nFileSystem">the POIFSFileSystem we belong to</param> /// <param name="parent">the parent of this entry</param> public DirectoryNode(DirectoryProperty property, NPOIFSFileSystem nFileSystem, DirectoryNode parent) : this(property, parent, (POIFSFileSystem)null, nFileSystem) { }
public void TestGetFreeBlockWithNoneSpare() { NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi")); int free; Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99)); assertBATCount(fs, 1, 0); for (int i = 100; i < 128; i++) Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(i)); Assert.AreEqual(true, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors); for (int i = 100; i < 128; i++) fs.SetNextBlock(i, POIFSConstants.END_OF_CHAIN); Assert.AreEqual(false, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors); try { Assert.AreEqual(false, fs.GetBATBlockAndIndex(128).Block.HasFreeSectors); Assert.Fail("Should only be one BAT"); } //catch (IndexOutOfRangeException) catch (ArgumentOutOfRangeException) { } assertBATCount(fs, 1, 0); // Now ask for a free one, will need to extend the file Assert.AreEqual(129, fs.GetFreeBlock()); Assert.AreEqual(false, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors); Assert.AreEqual(true, fs.GetBATBlockAndIndex(128).Block.HasFreeSectors); Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(128)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(129)); // We now have 2 BATs, but no XBATs assertBATCount(fs, 2, 0); // Fill up to hold 109 BAT blocks for (int i = 0; i < 109; i++) { fs.GetFreeBlock(); int startOffset = i * 128; while (fs.GetBATBlockAndIndex(startOffset).Block.HasFreeSectors) { free = fs.GetFreeBlock(); fs.SetNextBlock(free, POIFSConstants.END_OF_CHAIN); } } Assert.AreEqual(false, fs.GetBATBlockAndIndex(109 * 128 - 1).Block.HasFreeSectors); try { Assert.AreEqual(false, fs.GetBATBlockAndIndex(109 * 128).Block.HasFreeSectors); Assert.Fail("Should only be 109 BATs"); } // catch (IndexOutOfRangeException) catch (ArgumentOutOfRangeException) { } // We now have 109 BATs, but no XBATs assertBATCount(fs, 109, 0); // Ask for it to be written out, and check the header HeaderBlock header = WriteOutAndReadHeader(fs); Assert.AreEqual(109, header.BATCount); Assert.AreEqual(0, header.XBATCount); free = fs.GetFreeBlock(); Assert.AreEqual(false, fs.GetBATBlockAndIndex(109 * 128 - 1).Block.HasFreeSectors); Assert.AreEqual(true, fs.GetBATBlockAndIndex(110 * 128 - 1).Block.HasFreeSectors); try { Assert.AreEqual(false, fs.GetBATBlockAndIndex(110 * 128).Block.HasFreeSectors); Assert.Fail("Should only be 110 BATs"); } //catch (IndexOutOfRangeException) catch (ArgumentOutOfRangeException) { } assertBATCount(fs, 110, 1); header = WriteOutAndReadHeader(fs); Assert.AreEqual(110, header.BATCount); Assert.AreEqual(1, header.XBATCount); for (int i = 109; i < 109 + 127; i++) { fs.GetFreeBlock(); int startOffset = i * 128; while (fs.GetBATBlockAndIndex(startOffset).Block.HasFreeSectors) { free = fs.GetFreeBlock(); fs.SetNextBlock(free, POIFSConstants.END_OF_CHAIN); } assertBATCount(fs, i + 1, 1); } // Should now have 109+127 = 236 BATs Assert.AreEqual(false, fs.GetBATBlockAndIndex(236 * 128 - 1).Block.HasFreeSectors); try { Assert.AreEqual(false, fs.GetBATBlockAndIndex(236 * 128).Block.HasFreeSectors); Assert.Fail("Should only be 236 BATs"); } catch (ArgumentOutOfRangeException) { } assertBATCount(fs, 236, 1); // Ask for another, will get our 2nd XBAT free = fs.GetFreeBlock(); Assert.AreEqual(false, fs.GetBATBlockAndIndex(236 * 128 - 1).Block.HasFreeSectors); Assert.AreEqual(true, fs.GetBATBlockAndIndex(237 * 128 - 1).Block.HasFreeSectors); try { Assert.AreEqual(false, fs.GetBATBlockAndIndex(237 * 128).Block.HasFreeSectors); Assert.Fail("Should only be 237 BATs"); } // catch (IndexOutOfRangeException) { } catch (ArgumentOutOfRangeException) { } // Check the counts now assertBATCount(fs, 237, 2); // Check the header header = WriteOutAndReadHeader(fs); // Now, write it out, and read it back in again fully fs = WriteOutAndReadBack(fs); // Check that it is seen correctly assertBATCount(fs, 237, 2); Assert.AreEqual(false, fs.GetBATBlockAndIndex(236 * 128 - 1).Block.HasFreeSectors); Assert.AreEqual(true, fs.GetBATBlockAndIndex(237 * 128 - 1).Block.HasFreeSectors); try { Assert.AreEqual(false, fs.GetBATBlockAndIndex(237 * 128).Block.HasFreeSectors); Assert.Fail("Should only be 237 BATs"); } catch (ArgumentOutOfRangeException) { } fs.Close(); }
public void TestGetFreeBlockWithSpare() { NPOIFSFileSystem fs = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi")); Assert.AreEqual(true, fs.GetBATBlockAndIndex(0).Block.HasFreeSectors); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(100)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(101)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(102)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(103)); Assert.AreEqual(100, fs.GetFreeBlock()); Assert.AreEqual(100, fs.GetFreeBlock()); fs.SetNextBlock(100, POIFSConstants.END_OF_CHAIN); Assert.AreEqual(101, fs.GetFreeBlock()); fs.Close(); }
public TestDocumentInputStream() { int blocks = (_workbook_size + 511) / 512; _workbook_data = new byte[512 * blocks]; Arrays.Fill(_workbook_data, unchecked((byte)-1)); for (int j = 0; j < _workbook_size; j++) { _workbook_data[j] = (byte)(j * j); } // Create the Old POIFS Version RawDataBlock[] rawBlocks = new RawDataBlock[blocks]; MemoryStream stream = new MemoryStream(_workbook_data); for (int j = 0; j < blocks; j++) { rawBlocks[j] = new RawDataBlock(stream); } POIFSDocument document = new POIFSDocument("Workbook", rawBlocks, _workbook_size); _workbook_o = new DocumentNode( document.DocumentProperty, new DirectoryNode( new DirectoryProperty("Root Entry"), (POIFSFileSystem)null, null)); // Now create the NPOIFS Version byte[] _workbook_data_only = new byte[_workbook_size]; Array.Copy(_workbook_data, 0, _workbook_data_only, 0, _workbook_size); NPOIFSFileSystem npoifs = new NPOIFSFileSystem(); // Make it easy when debugging to see what isn't the doc byte[] minus1 = new byte[512]; Arrays.Fill(minus1, unchecked((byte)-1)); npoifs.GetBlockAt(-1).Write(minus1); npoifs.GetBlockAt(0).Write(minus1); npoifs.GetBlockAt(1).Write(minus1); // Create the NPOIFS document _workbook_n = (DocumentNode)npoifs.CreateDocument( new MemoryStream(_workbook_data_only), "Workbook" ); }
public void TestGetBlock() { // 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 }) { // Mini stream should be at big block zero Assert.AreEqual(0, fs.PropertyTable.Root.StartBlock); // Grab the ministore NPOIFSMiniStore ministore = fs.GetMiniStore(); ByteBuffer b; // Runs from the start of the data section in 64 byte chungs b = ministore.GetBlockAt(0); Assert.AreEqual((byte)0x9e, b[0]); Assert.AreEqual((byte)0x75, b[1]); Assert.AreEqual((byte)0x97, b[2]); Assert.AreEqual((byte)0xf6, b[3]); Assert.AreEqual((byte)0xff, b[4]); Assert.AreEqual((byte)0x21, b[5]); Assert.AreEqual((byte)0xd2, b[6]); Assert.AreEqual((byte)0x11, b[7]); // And the next block b = ministore.GetBlockAt(1); Assert.AreEqual((byte)0x00, b[0]); Assert.AreEqual((byte)0x00, b[1]); Assert.AreEqual((byte)0x03, b[2]); Assert.AreEqual((byte)0x00, b[3]); Assert.AreEqual((byte)0x12, b[4]); Assert.AreEqual((byte)0x02, b[5]); Assert.AreEqual((byte)0x00, b[6]); Assert.AreEqual((byte)0x00, b[7]); // Check the last data block b = ministore.GetBlockAt(180); Assert.AreEqual((byte)0x30, b[0]); Assert.AreEqual((byte)0x00, b[1]); Assert.AreEqual((byte)0x00, b[2]); Assert.AreEqual((byte)0x00, b[3]); Assert.AreEqual((byte)0x00, b[4]); Assert.AreEqual((byte)0x00, b[5]); Assert.AreEqual((byte)0x00, b[6]); Assert.AreEqual((byte)0x80, b[7]); // And the rest until the end of the big block is zeros for (int i = 181; i < 184; i++) { b = ministore.GetBlockAt(i); Assert.AreEqual((byte)0, b[0]); Assert.AreEqual((byte)0, b[1]); Assert.AreEqual((byte)0, b[2]); Assert.AreEqual((byte)0, b[3]); Assert.AreEqual((byte)0, b[4]); Assert.AreEqual((byte)0, b[5]); Assert.AreEqual((byte)0, b[6]); Assert.AreEqual((byte)0, b[7]); } fs.Close(); } }
public static HeaderBlock WriteOutAndReadHeader(NPOIFSFileSystem fs) { MemoryStream baos = new MemoryStream(); fs.WriteFileSystem(baos); HeaderBlock header = new HeaderBlock(new MemoryStream(baos.ToArray())); return header; }
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(); } }
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 TestReadMultipleTreeLevels() { POIDataSamples _samples = POIDataSamples.GetPublisherInstance(); FileStream sample = _samples.GetFile("Sample.pub"); DocumentInputStream stream; NPOIFSFileSystem npoifs = new NPOIFSFileSystem(sample); try { sample = _samples.GetFile("Sample.pub"); POIFSFileSystem opoifs = new POIFSFileSystem(sample); // Ensure we have what we expect on the root Assert.AreEqual(npoifs, npoifs.Root.NFileSystem); Assert.AreEqual(null, npoifs.Root.FileSystem); Assert.AreEqual(opoifs, opoifs.Root.FileSystem); Assert.AreEqual(null, opoifs.Root.NFileSystem); // Check inside foreach (DirectoryNode root in new DirectoryNode[] { opoifs.Root, npoifs.Root }) { // Top Level Entry top = root.GetEntry("Contents"); Assert.AreEqual(true, top.IsDocumentEntry); stream = root.CreateDocumentInputStream(top); stream.Read(); // One Level Down DirectoryNode escher = (DirectoryNode)root.GetEntry("Escher"); Entry one = escher.GetEntry("EscherStm"); Assert.AreEqual(true, one.IsDocumentEntry); stream = escher.CreateDocumentInputStream(one); stream.Read(); // Two Levels Down DirectoryNode quill = (DirectoryNode)root.GetEntry("Quill"); DirectoryNode quillSub = (DirectoryNode)quill.GetEntry("QuillSub"); Entry two = quillSub.GetEntry("CONTENTS"); Assert.AreEqual(true, two.IsDocumentEntry); stream = quillSub.CreateDocumentInputStream(two); stream.Read(); } } finally { npoifs.Close(); } }
public void CreateWriteRead() { NPOIFSFileSystem fs = new NPOIFSFileSystem(); DocumentEntry miniDoc; DocumentEntry normDoc; // Initially has a BAT but not SBAT Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(2)); // Check that the SBAT is empty Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.Root.Property.StartBlock); // Check that no properties table has been written yet Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.PropertyTable.StartBlock); // Write and read it fs = WriteOutAndReadBack(fs); // Property table entries have been added to the blocks Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(3)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.Root.Property.StartBlock); Assert.AreEqual(2, fs.PropertyTable.StartBlock); // Put everything within a new directory DirectoryEntry testDir = fs.CreateDirectory("Test Directory"); // Add a new Normal Stream (Normal Streams minimum 4096 bytes) byte[] main4096 = new byte[4096]; main4096[0] = unchecked((byte)-10); main4096[4095] = unchecked((byte)-11); testDir.CreateDocument("Normal4096", new MemoryStream(main4096)); Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2)); Assert.AreEqual(4, fs.GetNextBlock(3)); Assert.AreEqual(5, fs.GetNextBlock(4)); Assert.AreEqual(6, fs.GetNextBlock(5)); Assert.AreEqual(7, fs.GetNextBlock(6)); Assert.AreEqual(8, fs.GetNextBlock(7)); Assert.AreEqual(9, fs.GetNextBlock(8)); Assert.AreEqual(10, fs.GetNextBlock(9)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(10)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(11)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.Root.Property.StartBlock); // Add a bigger Normal Stream byte[] main5124 = new byte[5124]; main5124[0] = unchecked((byte)-22); main5124[5123] = unchecked((byte)-33); testDir.CreateDocument("Normal5124", new MemoryStream(main5124)); Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2)); Assert.AreEqual(4, fs.GetNextBlock(3)); Assert.AreEqual(5, fs.GetNextBlock(4)); Assert.AreEqual(6, fs.GetNextBlock(5)); Assert.AreEqual(7, fs.GetNextBlock(6)); Assert.AreEqual(8, fs.GetNextBlock(7)); Assert.AreEqual(9, fs.GetNextBlock(8)); Assert.AreEqual(10, fs.GetNextBlock(9)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(10)); Assert.AreEqual(12, fs.GetNextBlock(11)); Assert.AreEqual(13, fs.GetNextBlock(12)); Assert.AreEqual(14, fs.GetNextBlock(13)); Assert.AreEqual(15, fs.GetNextBlock(14)); Assert.AreEqual(16, fs.GetNextBlock(15)); Assert.AreEqual(17, fs.GetNextBlock(16)); Assert.AreEqual(18, fs.GetNextBlock(17)); Assert.AreEqual(19, fs.GetNextBlock(18)); Assert.AreEqual(20, fs.GetNextBlock(19)); Assert.AreEqual(21, fs.GetNextBlock(20)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(21)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(22)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.Root.Property.StartBlock); // Now Add a mini stream byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 }; testDir.CreateDocument("Mini", new MemoryStream(mini)); // Mini stream will Get one block for fat + one block for data Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2)); Assert.AreEqual(4, fs.GetNextBlock(3)); Assert.AreEqual(5, fs.GetNextBlock(4)); Assert.AreEqual(6, fs.GetNextBlock(5)); Assert.AreEqual(7, fs.GetNextBlock(6)); Assert.AreEqual(8, fs.GetNextBlock(7)); Assert.AreEqual(9, fs.GetNextBlock(8)); Assert.AreEqual(10, fs.GetNextBlock(9)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(10)); Assert.AreEqual(12, fs.GetNextBlock(11)); Assert.AreEqual(13, fs.GetNextBlock(12)); Assert.AreEqual(14, fs.GetNextBlock(13)); Assert.AreEqual(15, fs.GetNextBlock(14)); Assert.AreEqual(16, fs.GetNextBlock(15)); Assert.AreEqual(17, fs.GetNextBlock(16)); Assert.AreEqual(18, fs.GetNextBlock(17)); Assert.AreEqual(19, fs.GetNextBlock(18)); Assert.AreEqual(20, fs.GetNextBlock(19)); Assert.AreEqual(21, fs.GetNextBlock(20)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(21)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(22)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(23)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(24)); // Check the mini stream location was set // (22 is mini fat, 23 is first mini stream block) Assert.AreEqual(23, fs.Root.Property.StartBlock); // Write and read back fs = WriteOutAndReadBack(fs); HeaderBlock header = WriteOutAndReadHeader(fs); // Check the header has the right points in it Assert.AreEqual(1, header.BATCount); Assert.AreEqual(0, header.BATArray[0]); Assert.AreEqual(2, header.PropertyStart); Assert.AreEqual(1, header.SBATCount); Assert.AreEqual(22, header.SBATStart); Assert.AreEqual(23, fs.PropertyTable.Root.StartBlock); // Block use should be almost the same, except the properties // stream will have grown out to cover 2 blocks // Check the block use is all unChanged // Check it's all unChanged Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1)); Assert.AreEqual(24, fs.GetNextBlock(2)); // Properties now extends over 2 blocks Assert.AreEqual(4, fs.GetNextBlock(3)); Assert.AreEqual(5, fs.GetNextBlock(4)); Assert.AreEqual(6, fs.GetNextBlock(5)); Assert.AreEqual(7, fs.GetNextBlock(6)); Assert.AreEqual(8, fs.GetNextBlock(7)); Assert.AreEqual(9, fs.GetNextBlock(8)); Assert.AreEqual(10, fs.GetNextBlock(9)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(10));// End of normal4096 Assert.AreEqual(12, fs.GetNextBlock(11)); Assert.AreEqual(13, fs.GetNextBlock(12)); Assert.AreEqual(14, fs.GetNextBlock(13)); Assert.AreEqual(15, fs.GetNextBlock(14)); Assert.AreEqual(16, fs.GetNextBlock(15)); Assert.AreEqual(17, fs.GetNextBlock(16)); Assert.AreEqual(18, fs.GetNextBlock(17)); Assert.AreEqual(19, fs.GetNextBlock(18)); Assert.AreEqual(20, fs.GetNextBlock(19)); Assert.AreEqual(21, fs.GetNextBlock(20)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(21)); // End of normal5124 Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(22)); // Mini Stream FAT Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(23)); // Mini Stream data Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(24)); // Properties #2 Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(25)); // Check some data Assert.AreEqual(1, fs.Root.EntryCount); testDir = (DirectoryEntry)fs.Root.GetEntry("Test Directory"); Assert.AreEqual(3, testDir.EntryCount); miniDoc = (DocumentEntry)testDir.GetEntry("Mini"); assertContentsMatches(mini, miniDoc); normDoc = (DocumentEntry)testDir.GetEntry("Normal4096"); assertContentsMatches(main4096, normDoc); normDoc = (DocumentEntry)testDir.GetEntry("Normal5124"); assertContentsMatches(main5124, normDoc); // Delete a couple of streams miniDoc.Delete(); normDoc.Delete(); // Check - will have un-used sectors now fs = WriteOutAndReadBack(fs); Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2)); // Props back in 1 block Assert.AreEqual(4, fs.GetNextBlock(3)); Assert.AreEqual(5, fs.GetNextBlock(4)); Assert.AreEqual(6, fs.GetNextBlock(5)); Assert.AreEqual(7, fs.GetNextBlock(6)); Assert.AreEqual(8, fs.GetNextBlock(7)); Assert.AreEqual(9, fs.GetNextBlock(8)); Assert.AreEqual(10, fs.GetNextBlock(9)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(10)); // End of normal4096 Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(11)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(12)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(13)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(14)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(15)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(16)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(17)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(18)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(19)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(20)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(21)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(22)); // Mini Stream FAT Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(23)); // Mini Stream data Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(24)); // Properties gone Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(25)); // All done 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 AddBeforeWrite() { NPOIFSFileSystem fs = new NPOIFSFileSystem(); DocumentEntry miniDoc; DocumentEntry normDoc; HeaderBlock hdr; // Initially has BAT + Properties but nothing else Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(2)); hdr = WriteOutAndReadHeader(fs); // No mini stream, and no xbats // Will have fat then properties stream Assert.AreEqual(1, hdr.BATCount); Assert.AreEqual(0, hdr.BATArray[0]); Assert.AreEqual(2, hdr.PropertyStart); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, hdr.SBATStart); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, hdr.XBATIndex); Assert.AreEqual(POIFSConstants.SMALLER_BIG_BLOCK_SIZE * 4, fs.Size); // Get a clean filesystem to start with fs = new NPOIFSFileSystem(); // Put our test files in a non-standard place DirectoryEntry parentDir = fs.CreateDirectory("Parent Directory"); DirectoryEntry testDir = parentDir.CreateDirectory("Test Directory"); // Add to the mini stream byte[] mini = new byte[] { 42, 0, 1, 2, 3, 4, 42 }; testDir.CreateDocument("Mini", new MemoryStream(mini)); // Add to the main stream byte[] main4096 = new byte[4096]; main4096[0] = unchecked((byte)-10); main4096[4095] = unchecked((byte)-11); testDir.CreateDocument("Normal4096", new MemoryStream(main4096)); // Check the mini stream was Added, then the main stream Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(3)); Assert.AreEqual(5, fs.GetNextBlock(4)); Assert.AreEqual(6, fs.GetNextBlock(5)); Assert.AreEqual(7, fs.GetNextBlock(6)); Assert.AreEqual(8, fs.GetNextBlock(7)); Assert.AreEqual(9, fs.GetNextBlock(8)); Assert.AreEqual(10, fs.GetNextBlock(9)); Assert.AreEqual(11, fs.GetNextBlock(10)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(11)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(12)); Assert.AreEqual(POIFSConstants.SMALLER_BIG_BLOCK_SIZE * 13, fs.Size); // Check that we can read the right data pre-write miniDoc = (DocumentEntry)testDir.GetEntry("Mini"); assertContentsMatches(mini, miniDoc); normDoc = (DocumentEntry)testDir.GetEntry("Normal4096"); assertContentsMatches(main4096, normDoc); // Write, Read, check hdr = WriteOutAndReadHeader(fs); fs = WriteOutAndReadBack(fs); // Check the header details - will have the sbat near the start, // then the properties at the end Assert.AreEqual(1, hdr.BATCount); Assert.AreEqual(0, hdr.BATArray[0]); Assert.AreEqual(2, hdr.SBATStart); Assert.AreEqual(12, hdr.PropertyStart); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, hdr.XBATIndex); // Check the block allocation is unChanged, other than // the properties stream going in at the end Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(0)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(3)); Assert.AreEqual(5, fs.GetNextBlock(4)); Assert.AreEqual(6, fs.GetNextBlock(5)); Assert.AreEqual(7, fs.GetNextBlock(6)); Assert.AreEqual(8, fs.GetNextBlock(7)); Assert.AreEqual(9, fs.GetNextBlock(8)); Assert.AreEqual(10, fs.GetNextBlock(9)); Assert.AreEqual(11, fs.GetNextBlock(10)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(11)); Assert.AreEqual(13, fs.GetNextBlock(12)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(13)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(14)); Assert.AreEqual(POIFSConstants.SMALLER_BIG_BLOCK_SIZE * 15, fs.Size); // Check the data DirectoryEntry fsRoot = fs.Root; Assert.AreEqual(1, fsRoot.EntryCount); parentDir = (DirectoryEntry)fsRoot.GetEntry("Parent Directory"); Assert.AreEqual(1, parentDir.EntryCount); testDir = (DirectoryEntry)parentDir.GetEntry("Test Directory"); Assert.AreEqual(2, testDir.EntryCount); miniDoc = (DocumentEntry)testDir.GetEntry("Mini"); assertContentsMatches(mini, miniDoc); normDoc = (DocumentEntry)testDir.GetEntry("Normal4096"); assertContentsMatches(main4096, normDoc); byte[] mini2 = new byte[] { unchecked((byte)-42), 0, unchecked((byte)-1), unchecked((byte)-2), unchecked((byte)-3), unchecked((byte)-4), unchecked((byte)-42) }; testDir.CreateDocument("Mini2", new MemoryStream(mini2)); // Add to the main stream byte[] main4106 = new byte[4106]; main4106[0] = 41; main4106[4105] = 42; testDir.CreateDocument("Normal4106", new MemoryStream(main4106)); // Recheck the data in all 4 streams fs = WriteOutAndReadBack(fs); fsRoot = fs.Root; Assert.AreEqual(1, fsRoot.EntryCount); parentDir = (DirectoryEntry)fsRoot.GetEntry("Parent Directory"); Assert.AreEqual(1, parentDir.EntryCount); testDir = (DirectoryEntry)parentDir.GetEntry("Test Directory"); Assert.AreEqual(4, testDir.EntryCount); miniDoc = (DocumentEntry)testDir.GetEntry("Mini"); assertContentsMatches(mini, miniDoc); miniDoc = (DocumentEntry)testDir.GetEntry("Mini2"); assertContentsMatches(mini2, miniDoc); normDoc = (DocumentEntry)testDir.GetEntry("Normal4106"); assertContentsMatches(main4106, normDoc); }
public void TestNextBlock() { NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi")); NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi")); foreach (NPOIFSFileSystem fs in new NPOIFSFileSystem[] { fsA, fsB }) { for (int i = 0; i < 21; i++) Assert.AreEqual(i + 1, fs.GetNextBlock(i)); // 21 jumps to 89, then ends Assert.AreEqual(89, fs.GetNextBlock(21)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(89)); for (int i = 22; i < 88; i++) Assert.AreEqual(i + 1, fs.GetNextBlock(i)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(88)); for (int i = 90; i < 96; i++) { Assert.AreEqual(i + 1, fs.GetNextBlock(i)); } Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(96)); Assert.AreEqual(98, fs.GetNextBlock(97)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(98)); Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, fs.GetNextBlock(99)); //Leon i = 100 for (int i = 100; i < fs.GetBigBlockSizeDetails().GetBATEntriesPerBlock(); i++) Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, fs.GetNextBlock(i)); fs.Close(); } fsA = new NPOIFSFileSystem(_inst.GetFile("BlockSize4096.zvi")); fsB = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize4096.zvi")); foreach (NPOIFSFileSystem fs in new NPOIFSFileSystem[] { fsA, fsB }) { Assert.AreEqual(1, fs.GetNextBlock(0)); Assert.AreEqual(2, fs.GetNextBlock(1)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(2)); for (int i = 4; i < 11; i++) Assert.AreEqual(i + 1, fs.GetNextBlock(i)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, fs.GetNextBlock(11)); fs.Close(); } }
public static NPOIFSFileSystem WriteOutAndReadBack(NPOIFSFileSystem original) { MemoryStream baos = new MemoryStream(); original.WriteFileSystem(baos); original.Close(); return new NPOIFSFileSystem(new ByteArrayInputStream(baos.ToArray())); }
public void TestGetBlock() { NPOIFSFileSystem fsA = new NPOIFSFileSystem(_inst.GetFile("BlockSize512.zvi")); NPOIFSFileSystem fsB = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize512.zvi")); foreach (NPOIFSFileSystem fs in new NPOIFSFileSystem[] { fsA, fsB }) { ByteBuffer b; b = fs.GetBlockAt(0); Assert.AreEqual((byte)0x9e, b.Read()); Assert.AreEqual((byte)0x75, b.Read()); Assert.AreEqual((byte)0x97, b.Read()); Assert.AreEqual((byte)0xf6, b.Read()); b = fs.GetBlockAt(1); Assert.AreEqual((byte)0x86, b.Read()); Assert.AreEqual((byte)0x09, b.Read()); Assert.AreEqual((byte)0x22, b.Read()); Assert.AreEqual((byte)0xfb, b.Read()); b = fs.GetBlockAt(99); Assert.AreEqual((byte)0x01, b.Read()); Assert.AreEqual((byte)0x00, b.Read()); Assert.AreEqual((byte)0x00, b.Read()); Assert.AreEqual((byte)0x00, b.Read()); Assert.AreEqual((byte)0x02, b.Read()); Assert.AreEqual((byte)0x00, b.Read()); Assert.AreEqual((byte)0x00, b.Read()); Assert.AreEqual((byte)0x00, b.Read()); fs.Close(); } fsA = new NPOIFSFileSystem(_inst.GetFile("BlockSize4096.zvi")); fsB = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize4096.zvi")); foreach (NPOIFSFileSystem fs in new NPOIFSFileSystem[] { fsA, fsB }) { ByteBuffer b; b = fs.GetBlockAt(0); Assert.AreEqual((byte)0x9e, b.Read()); Assert.AreEqual((byte)0x75, b.Read()); Assert.AreEqual((byte)0x97, b.Read()); Assert.AreEqual((byte)0xf6, b.Read()); b = fs.GetBlockAt(1); Assert.AreEqual((byte)0x00, b.Read()); Assert.AreEqual((byte)0x00, b.Read()); Assert.AreEqual((byte)0x03, b.Read()); Assert.AreEqual((byte)0x00, b.Read()); b = fs.GetBlockAt(14); Assert.AreEqual((byte)0x01, b.Read()); Assert.AreEqual((byte)0x00, b.Read()); Assert.AreEqual((byte)0x00, b.Read()); Assert.AreEqual((byte)0x00, b.Read()); Assert.AreEqual((byte)0x02, b.Read()); Assert.AreEqual((byte)0x00, b.Read()); Assert.AreEqual((byte)0x00, b.Read()); Assert.AreEqual((byte)0x00, b.Read()); fs.Close(); } }
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 TestReadWriteNewStream() { NPOIFSFileSystem fs = new NPOIFSFileSystem(); NPOIFSStream stream = new NPOIFSStream(fs); // Check our filesystem has a BAT and the Properties Assert.AreEqual(2, fs.GetFreeBlock()); BATBlock bat = fs.GetBATBlockAndIndex(0).Block; Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, bat.GetValueAt(0)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(1)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(2)); // Check the stream as-is Assert.AreEqual(POIFSConstants.END_OF_CHAIN, stream.GetStartBlock()); try { stream.GetBlockIterator(); Assert.Fail("Shouldn't be able to get an iterator before writing"); } catch (Exception) { } // Write in two blocks byte[] data = new byte[512 + 20]; for (int i = 0; i < 512; i++) { data[i] = (byte)(i % 256); } for (int i = 512; i < data.Length; i++) { data[i] = (byte)(i % 256 + 100); } stream.UpdateContents(data); // Check now Assert.AreEqual(4, fs.GetFreeBlock()); bat = fs.GetBATBlockAndIndex(0).Block; Assert.AreEqual(POIFSConstants.FAT_SECTOR_BLOCK, bat.GetValueAt(0)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(1)); Assert.AreEqual(3, bat.GetValueAt(2)); Assert.AreEqual(POIFSConstants.END_OF_CHAIN, bat.GetValueAt(3)); Assert.AreEqual(POIFSConstants.UNUSED_BLOCK, bat.GetValueAt(4)); IEnumerator<ByteBuffer> it = stream.GetBlockIterator(); Assert.AreEqual(true, it.MoveNext()); ByteBuffer b = it.Current; byte[] read = new byte[512]; //b.get(read); // Array.Copy(b, 0, read, 0, b.Length); b.Read(read); for (int i = 0; i < read.Length; i++) { //Assert.AreEqual("Wrong value at " + i, data[i], read[i]); Assert.AreEqual(data[i], read[i], "Wrong value at " + i); } Assert.AreEqual(true, it.MoveNext()); b = it.Current; read = new byte[512]; //b.get(read); //Array.Copy(b, 0, read, 0, b.Length); b.Read(read); for (int i = 0; i < 20; i++) { Assert.AreEqual(data[i + 512], read[i]); } for (int i = 20; i < read.Length; i++) { Assert.AreEqual(0, read[i]); } Assert.AreEqual(false, it.MoveNext()); 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 NPOIFSReadCopyWritePOIFSRead() { FileStream testFile = POIDataSamples.GetSpreadSheetInstance().GetFile("Simple.xls"); NPOIFSFileSystem src = new NPOIFSFileSystem(testFile); byte[] wbDataExp = IOUtils.ToByteArray(src.CreateDocumentInputStream("Workbook")); NPOIFSFileSystem nfs = new NPOIFSFileSystem(); EntryUtils.CopyNodes(src.Root, nfs.Root); src.Close(); MemoryStream bos = new MemoryStream(); nfs.WriteFileSystem(bos); nfs.Close(); POIFSFileSystem pfs = new POIFSFileSystem(new MemoryStream(bos.ToArray())); byte[] wbDataAct = IOUtils.ToByteArray(pfs.CreateDocumentInputStream("Workbook")); Assert.That(wbDataExp, new EqualConstraint(wbDataAct)); }
public void TestAreDocumentsIdentical() { POIFSFileSystem fs = new POIFSFileSystem(); DirectoryEntry dirA = fs.CreateDirectory("DirA"); DirectoryEntry dirB = fs.CreateDirectory("DirB"); DocumentEntry entryA1 = dirA.CreateDocument("Entry1", new ByteArrayInputStream(dataSmallA)); DocumentEntry entryA1b = dirA.CreateDocument("Entry1b", new ByteArrayInputStream(dataSmallA)); DocumentEntry entryA2 = dirA.CreateDocument("Entry2", new ByteArrayInputStream(dataSmallB)); DocumentEntry entryB1 = dirB.CreateDocument("Entry1", new ByteArrayInputStream(dataSmallA)); // Names must match Assert.AreEqual(false, entryA1.Name.Equals(entryA1b.Name)); Assert.AreEqual(false, EntryUtils.AreDocumentsIdentical(entryA1, entryA1b)); // Contents must match Assert.AreEqual(false, EntryUtils.AreDocumentsIdentical(entryA1, entryA2)); // Parents don't matter if contents + names are the same Assert.AreEqual(false, entryA1.Parent.Equals(entryB1.Parent)); Assert.AreEqual(true, EntryUtils.AreDocumentsIdentical(entryA1, entryB1)); // Can work with NPOIFS + POIFS //ByteArrayOutputStream tmpO = new ByteArrayOutputStream(); MemoryStream tmpO = new MemoryStream(); fs.WriteFileSystem(tmpO); ByteArrayInputStream tmpI = new ByteArrayInputStream(tmpO.ToArray()); NPOIFSFileSystem nfs = new NPOIFSFileSystem(tmpI); DirectoryEntry dN1 = (DirectoryEntry)nfs.Root.GetEntry("DirA"); DirectoryEntry dN2 = (DirectoryEntry)nfs.Root.GetEntry("DirB"); DocumentEntry eNA1 = (DocumentEntry)dN1.GetEntry(entryA1.Name); DocumentEntry eNA2 = (DocumentEntry)dN1.GetEntry(entryA2.Name); DocumentEntry eNB1 = (DocumentEntry)dN2.GetEntry(entryB1.Name); Assert.AreEqual(false, EntryUtils.AreDocumentsIdentical(eNA1, eNA2)); Assert.AreEqual(true, EntryUtils.AreDocumentsIdentical(eNA1, eNB1)); Assert.AreEqual(false, EntryUtils.AreDocumentsIdentical(eNA1, entryA1b)); Assert.AreEqual(false, EntryUtils.AreDocumentsIdentical(eNA1, entryA2)); Assert.AreEqual(true, EntryUtils.AreDocumentsIdentical(eNA1, entryA1)); Assert.AreEqual(true, EntryUtils.AreDocumentsIdentical(eNA1, entryB1)); }
public void TestBasicOpen() { 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 }) { Assert.AreEqual(512, fs.GetBigBlockSize()); } fsA.Close(); fsB.Close(); fsA = new NPOIFSFileSystem(_inst.GetFile("BlockSize4096.zvi")); fsB = new NPOIFSFileSystem(_inst.OpenResourceAsStream("BlockSize4096.zvi")); foreach (NPOIFSFileSystem fs in new NPOIFSFileSystem[] { fsA, fsB }) { Assert.AreEqual(4096, fs.GetBigBlockSize()); } fsA.Close(); fsB.Close(); }