예제 #1
0
        public void Validate()
        {
            // Load the sample file data
            file = new AFPFile(testFilePath, false);

            // Ensure it validates
            Assert.IsTrue(file.EncodeData().Any());

            // Remove the first container information from all fields it contains
            Container c = file.Fields.First(f => f.LowestLevelContainer != null).LowestLevelContainer;

            foreach (DataStructure s in c.Structures)
            {
                s.Containers.Remove(c);
            }

            // it should no longer validate
            Assert.IsFalse(file.EncodeData().Any());

            // Reload data
            file = new AFPFile(testFilePath, false);

            // Surround the file with BPF/EPF tags
            file.AddField(StructuredField.New <BPF>(), 0);
            file.AddField(StructuredField.New <EPF>(), file.Fields.Count);

            // Check it still validates (container info should have been updated)
            Assert.IsTrue(file.EncodeData().Any());

            // Try adding a TLE field to the top of the file
            TLE newTLE = StructuredField.New <TLE>();

            file.AddField(newTLE, 0);

            // Ensure it does not validate
            Assert.IsFalse(file.EncodeData().Any());

            // Delete that field, and add it in a place where it is allowed to be (in this case, after a BPG)
            file.DeleteField(newTLE);
            int newIndex = 0;

            for (int i = 0; i < file.Fields.Count; i++)
            {
                if (file.Fields[i] is BPG)
                {
                    newIndex = i + 1; break;
                }
            }
            file.AddField(newTLE, newIndex);

            Assert.IsTrue(file.EncodeData().Any());
        }
예제 #2
0
        public void DecodeAndParseData_ThenReencode()
        {
            byte[] rawFile = File.ReadAllBytes(testFilePath);

            // Load an AFP file, parse its data, and save it to an in-memory byte stream
            file = new AFPFile(rawFile, true);
            Assert.IsNotNull(file.Fields);
            Assert.AreNotEqual(0, file.Fields.Count);

            // Compare original raw bytes with reencoded byte stream - they must be identical
            byte[] encoded = file.EncodeData();
            Assert.IsTrue(rawFile.SequenceEqual(encoded));
        }