Esempio n. 1
0
        public void Read_EndOfFileAtIsMain_ThrowException()
        {
            byte[] bytes =
            {
                0x61, 0x74, 0x6F, 0x6D, // Magic number.
                0x01, 0x00,             // Version 1.
                0x01,                   // The origin is set.
                0x20, 0x00, 0x00, 0x00, // The origin is set to 0x20.
                0x00, 0x00, 0x00, 0x00,

                // Base part of the atom.
                0x00,      // Procedure type.
                0x01,      // The procedure is defined.
                0x01,      // The procedure is global.
                0x50, 0x00 // The procedure is called 'P'.
            };

            string message = "Unexpected end of object file. Expected 'is main' bool.";

            using (var stream = new MemoryStream(bytes))
            {
                var reader = new AtomReader();
                Assert.That(() => reader.Read(stream),
                            Throws.Exception.TypeOf <InvalidObjectFileException>().With.Message.EqualTo(message));
            }
        }
Esempio n. 2
0
        public void Read_EndOfFileAtNullTerminatedString_ThrowsException()
        {
            byte[] bytes =
            {
                0x61, 0x74, 0x6F, 0x6D, // Magic number.
                0x01, 0x00,             // Version 1.
                0x01,                   // The origin is set.
                0x20, 0x00, 0x00, 0x00, // The origin is set to 0x20.
                0x00, 0x00, 0x00, 0x00,

                // Base part of the atom.
                0x01,      // String type.
                0x01,      // The string is defined.
                0x01,      // The string is global.
                0x53, 0x00 // The string is called 'S'.
            };

            string message = "Unexpected end of object file. Expected null terminated string.";

            using (var stream = new MemoryStream(bytes))
            {
                var reader = new AtomReader();
                Assert.That(() => reader.Read(stream),
                            Throws.Exception.TypeOf <InvalidObjectFileException>().With.Message.EqualTo(message));
            }
        }
Esempio n. 3
0
        public void Read_EndOfFileInDataBlock_ThrowsException()
        {
            byte[] bytes =
            {
                0x61, 0x74, 0x6F, 0x6D, // Magic number.
                0x01, 0x00,             // Version 1.
                0x01,                   // The origin is set.
                0x20, 0x00, 0x00, 0x00, // The origin is set to 0x20.
                0x00, 0x00, 0x00, 0x00,

                // Base part of the atom.
                0x02,                         // Data type.
                0x01,                         // The data is defined.
                0x01,                         // The data is global.
                0x44, 0x61, 0x74, 0x61, 0x00, // The block is called 'Data'.

                // Data part of the atom.
                0x02, 0x00, 0x00, 0x00,         // The block consist of 2 bytes of data.
                0xAA
            };

            string message = "Unexpected end of object file. Expected a data block of 0x02 bytes but 0x01 was read.";

            using (var stream = new MemoryStream(bytes))
            {
                var reader = new AtomReader();
                Assert.That(() => reader.Read(stream),
                            Throws.Exception.TypeOf <InvalidObjectFileException>().With.Message.EqualTo(message));
            }
        }
Esempio n. 4
0
        public void Read_EndOfFileAtReferenceAddressSize_ThrowException()
        {
            byte[] bytes =
            {
                0x61, 0x74, 0x6F, 0x6D, // Magic number.
                0x01, 0x00,             // Version 1.
                0x01,                   // The origin is set.
                0x20, 0x00, 0x00, 0x00, // The origin is set to 0x20.
                0x00, 0x00, 0x00, 0x00,

                // Base part of the atom.
                0x00,       // Procedure type.
                0x01,       // The procedure is defined.
                0x01,       // The procedure is global.
                0x50, 0x00, // The procedure is called 'P'.

                // Procedure part of the atom.
                0x01,                   // This is the main procedure.
                0x01, 0x00, 0x00, 0x00, // The size of the code is 1 byte.
                0xAA,                   // Code block.
                0x01, 0x00,             // One reference.
                0x01, 0x00, 0x00, 0x00, // Refer to atom number 1.
                0x01                    // The address is in little endian.
            };

            string message = "Unexpected end of object file. Expected 'address size' byte.";

            using (var stream = new MemoryStream(bytes))
            {
                var reader = new AtomReader();
                Assert.That(() => reader.Read(stream),
                            Throws.Exception.TypeOf <InvalidObjectFileException>().With.Message.EqualTo(message));
            }
        }
Esempio n. 5
0
        public void Read_ValidHeaderAndProcedureWithNewRefrenceOverlappingAnOldOne_ThrowsException()
        {
            byte[] bytes =
            {
                0x61, 0x74, 0x6F, 0x6D, // Magic number.
                0x01, 0x00,             // Version 1.
                0x01,                   // The origin is set.
                0x20, 0x00, 0x00, 0x00, // The origin is set to 0x20.
                0x00, 0x00, 0x00, 0x00,

                // Base part of the atom.
                0x00,                         // Procedure type.
                0x01,                         // The procedure is defined.
                0x01,                         // The procedure is global.
                0x50, 0x72, 0x6F, 0x63, 0x00, // The procedure is called 'Proc'.

                // Procedure part of the atom.
                0x01,                         // This is the main procedure.
                0x05, 0x00, 0x00, 0x00,       // The size of the code is 5 bytes.
                0x00, 0x00, 0x00, 0x00, 0x00, // The procedure code.
                0x02, 0x00,                   // Number of references.

                // Old Reference.
                0x01, 0x00, 0x00, 0x00, // It is atom number 1 that is being referenced (index based).
                0x01,                   // The address is in little endian.
                0x04,                   // The size of the address is 4 bytes.
                0x01, 0x00, 0x00, 0x00, // The address to relocate.

                // New Reference.
                0x01, 0x00, 0x00, 0x00, // It is atom number 1 that is being referenced (index based).
                0x01,                   // The address is in little endian.
                0x04,                   // The size of the address is 4 bytes.
                0x00, 0x00, 0x00, 0x00, // The address to relocate.

                // Null terminated string.
                0x01,       // Null terminated string type.
                0x01,       // The string is defined.
                0x00,       // The string is not global.
                0x53, 0x00, // The string is called 'S'.

                // String part
                0x54, 0x78, 0x74, 0x00          // The string is 'Txt'.
            };

            string message = "Proc's reference to 'S' has an overlapping address with the reference to 'S' at 0x01.";

            using (var stream = new MemoryStream(bytes))
            {
                var reader = new AtomReader();
                Assert.That(() => reader.Read(stream),
                            Throws.Exception.TypeOf <InvalidObjectFileException>().With.Message.EqualTo(message));
            }
        }
Esempio n. 6
0
        public void Read_ReferenceAddressSizeIsNot2Or4_ThrowException()
        {
            byte[] bytes =
            {
                0x61, 0x74, 0x6F, 0x6D, // Magic number.
                0x01, 0x00,             // Version 1.
                0x01,                   // The origin is set.
                0x20, 0x00, 0x00, 0x00, // The origin is set to 0x20.
                0x00, 0x00, 0x00, 0x00,

                // Base part of the atom.
                0x00,       // Procedure type.
                0x01,       // The procedure is defined.
                0x01,       // The procedure is global.
                0x50, 0x00, // The procedure is called 'P'.

                // Procedure part of the atom.
                0x01,                   // This is the main procedure.
                0x01, 0x00, 0x00, 0x00, // The size of the code is 1 byte.
                0xAA,                   // Code block.
                0x01, 0x00,             // One reference.
                0x01, 0x00, 0x00, 0x00, // Refer to atom number 1.
                0x01,                   // The address is in little endian.
                0x00                    // Invalid address size
            };

            string message = "The address size at 0x21 is invalid. It must be 2 or 4.";

            for (int i = 0; i < byte.MaxValue; i++)
            {
                if (i.IsOneOf(2, 4))
                {
                    continue;
                }

                bytes[bytes.Length - 1] = (byte)i;
                using (var stream = new MemoryStream(bytes))
                {
                    var reader = new AtomReader();
                    Assert.That(() => reader.Read(stream),
                                Throws.Exception.TypeOf <InvalidObjectFileException>().With.Message.EqualTo(message));
                }
            }
        }
Esempio n. 7
0
        public void Read_ValidHeaderAndProcedure_ProcedureRead()
        {
            byte[] bytes =
            {
                0x61, 0x74, 0x6F, 0x6D, // Magic number.
                0x01, 0x00,             // Version 1.
                0x01,                   // The origin is set.
                0x20, 0x00, 0x00, 0x00, // The origin is set to 0x20.
                0x00, 0x00, 0x00, 0x00,

                // Base part of the atom.
                0x00,                         // Procedure type.
                0x01,                         // The procedure is defined.
                0x01,                         // The procedure is global.
                0x50, 0x72, 0x6F, 0x63, 0x00, // The procedure is called 'Proc'.

                // Procedure part of the atom.
                0x01,                   // This is the main procedure.
                0x01, 0x00, 0x00, 0x00, // The size of the code is 1 byte.
                0xAA,                   // The procedure code.
                0x00, 0x00              // Number of references.
            };

            using (var stream = new MemoryStream(bytes))
            {
                var reader = new AtomReader();
                var file   = reader.Read(stream);

                Assert.True(file.IsOriginSet);
                Assert.AreEqual(0x20, file.Origin);

                var procedure = (Procedure)file.Atoms[0];

                Assert.True(procedure.IsDefined);
                Assert.True(procedure.IsGlobal);
                Assert.AreEqual("Proc", procedure.Name);

                Assert.True(procedure.IsMain);
                CollectionAssert.AreEqual(new byte[] { 0xAA }, procedure.Code);
                Assert.AreEqual(0, procedure.References.Count);
            }
        }
Esempio n. 8
0
        public void Read_ValidHeader_NoAtomsRead()
        {
            byte[] bytes =
            {
                0x61, 0x74, 0x6F, 0x6D, // Magic number.
                0x01, 0x00,             // Version 1.
                0x01,                   // The origin is set.
                0x20, 0x00, 0x00, 0x00, // The origin is set to 0x20.
                0x00, 0x00, 0x00, 0x00,
            };

            using (var stream = new MemoryStream(bytes))
            {
                var reader = new AtomReader();
                var file   = reader.Read(stream);

                Assert.AreEqual(0, file.Atoms.Count);
                Assert.True(file.IsOriginSet);
                Assert.AreEqual(0x20, file.Origin);
            }
        }
Esempio n. 9
0
        public void Read_ValidHeaderAndData_DataRead()
        {
            byte[] bytes =
            {
                0x61, 0x74, 0x6F, 0x6D, // Magic number.
                0x01, 0x00,             // Version 1.
                0x01,                   // The origin is set.
                0x20, 0x00, 0x00, 0x00, // The origin is set to 0x20.
                0x00, 0x00, 0x00, 0x00,

                // Base part of the atom.
                0x02,                         // Data type.
                0x01,                         // The data is defined.
                0x01,                         // The data is global.
                0x44, 0x61, 0x74, 0x61, 0x00, // The data is called 'Data'.

                // Data part of the atom.
                0x01, 0x00, 0x00, 0x00,         // The size of the data block is 1 byte.
                0xAA                            // The data block.
            };

            using (var stream = new MemoryStream(bytes))
            {
                var reader = new AtomReader();
                var file   = reader.Read(stream);

                Assert.True(file.IsOriginSet);
                Assert.AreEqual(0x20, file.Origin);

                var data = (Data)file.Atoms[0];

                Assert.True(data.IsDefined);
                Assert.True(data.IsGlobal);
                Assert.AreEqual("Data", data.Name);

                CollectionAssert.AreEqual(new byte[] { 0xAA }, data.Content);
            }
        }
Esempio n. 10
0
        public void Read_ValidHeaderAndNullTerminatedString_StringRead()
        {
            byte[] bytes =
            {
                0x61, 0x74, 0x6F, 0x6D, // Magic number.
                0x01, 0x00,             // Version 1.
                0x01,                   // The origin is set.
                0x20, 0x00, 0x00, 0x00, // The origin is set to 0x20.
                0x00, 0x00, 0x00, 0x00,

                // Base part of the atom.
                0x01,                   // String type.
                0x01,                   // The string is defined.
                0x01,                   // The string is global.
                0x54, 0x78, 0x74, 0x00, // The string is called 'Txt'.

                // String part of the atom.
                0x41, 0x62, 0x63, 0x00          // The string is 'Abc'.
            };

            using (var stream = new MemoryStream(bytes))
            {
                var reader = new AtomReader();
                var file   = reader.Read(stream);

                Assert.True(file.IsOriginSet);
                Assert.AreEqual(0x20, file.Origin);

                var s = (NullTerminatedString)file.Atoms[0];

                Assert.True(s.IsDefined);
                Assert.True(s.IsGlobal);
                Assert.AreEqual("Txt", s.Name);

                CollectionAssert.AreEqual("Abc", s.Content);
            }
        }
Esempio n. 11
0
        public void Read_ValidHeaderAndProcedureWithReference_ProcedureRead()
        {
            byte[] bytes =
            {
                0x61, 0x74, 0x6F, 0x6D, // Magic number.
                0x01, 0x00,             // Version 1.
                0x01,                   // The origin is set.
                0x20, 0x00, 0x00, 0x00, // The origin is set to 0x20.
                0x00, 0x00, 0x00, 0x00,

                // Base part of the atom.
                0x00,                         // Procedure type.
                0x01,                         // The procedure is defined.
                0x01,                         // The procedure is global.
                0x50, 0x72, 0x6F, 0x63, 0x00, // The procedure is called 'Proc'.

                // Procedure part of the atom.
                0x01,                   // This is the main procedure.
                0x04, 0x00, 0x00, 0x00, // The size of the code is 4 bytes.
                0x00, 0x00, 0x00, 0x00, // The procedure code.
                0x01, 0x00,             // Number of references.

                // Reference.
                0x01, 0x00, 0x00, 0x00, // It is atom number 1 that is being referenced (index based).
                0x01,                   // The address is in little endian.
                0x04,                   // The size of the address is 4 bytes.
                0x00, 0x00, 0x00, 0x00, // The address to relocate.

                // Null terminated string.
                0x01,       // Null terminated string type.
                0x01,       // The string is defined.
                0x00,       // The string is not global.
                0x53, 0x00, // The string is called 'S'.

                // String part
                0x54, 0x78, 0x74, 0x00          // The string is 'Txt'.
            };

            using (var stream = new MemoryStream(bytes))
            {
                var reader = new AtomReader();
                var file   = reader.Read(stream);

                Assert.True(file.IsOriginSet);
                Assert.AreEqual(0x20, file.Origin);

                var procedure = (Procedure)file.Atoms[0];
                var s         = (NullTerminatedString)file.Atoms[1];

                Assert.True(procedure.IsDefined);
                Assert.True(procedure.IsGlobal);
                Assert.AreEqual("Proc", procedure.Name);

                Assert.True(procedure.IsMain);
                CollectionAssert.AreEqual(new byte[] { 0x00, 0x00, 0x00, 0x00 }, procedure.Code);

                Assert.AreEqual(1, procedure.References.Count);
                Assert.AreEqual(0, procedure.References[0].Address);
                Assert.AreSame(s, procedure.References[0].Atom);

                Assert.True(s.IsDefined);
                Assert.False(s.IsGlobal);
                Assert.AreEqual("S", s.Name);
                Assert.AreEqual("Txt", s.Content);
            }
        }