Beispiel #1
0
        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();
        }
Beispiel #2
0
        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();
        }
Beispiel #3
0
        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();
            }
        }