예제 #1
0
        public void SeveralFilesInSeveralSegments()
        {
            var firstFileBytes  = TestData.GetElfFileBytesFromBuildId(TestData.expectedId);
            var secondFileBytes = TestData.GetElfFileBytesFromBuildId(TestData.anotherExpectedId);
            var data            = firstFileBytes.Concat(secondFileBytes).ToArray();

            var firstFileSection  = new FileSection("", 0, (ulong)firstFileBytes.Length);
            var secondFileSection = new FileSection("", (ulong)firstFileBytes.Length,
                                                    (ulong)data.Length);

            var reader = new BinaryReader(new MemoryStream(data));

            var firstSegmentSize = firstFileBytes.Length - 1;
            var firstSegment     = new ProgramHeader(ProgramHeader.Type.LoadableSegment, 0, 0,
                                                     (ulong)firstFileBytes.Length - 1);

            var secondSegment = new ProgramHeader(ProgramHeader.Type.LoadableSegment,
                                                  (ulong)firstSegmentSize,
                                                  (ulong)firstSegmentSize,
                                                  (ulong)secondFileBytes.Length + 1);

            var firstModule = ReadModule(data, new[] { secondSegment, firstSegment },
                                         firstFileSection);

            Assert.AreEqual(TestData.expectedId, firstModule.Id);

            var secondModule = ReadModule(data, new[] { firstSegment, secondSegment },
                                          secondFileSection);

            Assert.AreEqual(TestData.anotherExpectedId, secondModule.Id);
        }
예제 #2
0
        public void FileStartBeforeSegment()
        {
            var data    = TestData.GetElfFileBytesFromBuildId(TestData.expectedId);
            var segment = new ProgramHeader(ProgramHeader.Type.LoadableSegment, 0, 1,
                                            (ulong)data.Length);

            var module = ReadModule(data, new[] { segment },
                                    new FileSection("", 0, (ulong)data.Length));

            Assert.AreEqual(BuildId.Empty, module.Id);
        }
예제 #3
0
        public void BuildIdEndsOutsideSegment()
        {
            var data    = TestData.GetElfFileBytesFromBuildId(TestData.expectedId);
            var segment = new ProgramHeader(
                ProgramHeader.Type.LoadableSegment, 0, 0, (ulong)data.Length - 1);

            var module = ReadModule(data, new[] { segment },
                                    new FileSection("", 0, (ulong)data.Length));

            Assert.AreNotEqual(TestData.expectedId, module.Id);
        }
예제 #4
0
        public void FileAtTheMiddleOfSegment()
        {
            var buildIdFileData = TestData.GetElfFileBytesFromBuildId(TestData.expectedId);
            var buildIdLength   = buildIdFileData.Length;
            var data            = new byte[buildIdLength].Concat(buildIdFileData).ToArray();

            var segment = new ProgramHeader(ProgramHeader.Type.LoadableSegment, 0, 0,
                                            (ulong)data.Length);

            var module = ReadModule(data, new[] { segment },
                                    new FileSection("", (ulong)buildIdLength, (ulong)buildIdLength * 2));

            Assert.AreEqual(TestData.expectedId, module.Id);
        }
예제 #5
0
        // This test is checking a dump with the following structure:
        // Start position    Section description                Section size in bytes
        //              0    Dump elf header                      64
        //             64    Program headers                      --
        //             64      File  index note program header    56
        //            120      File1 header                       56
        //            176      File2 header                       56
        //            232    File1 contents                       156
        //            388    File index note                      104
        //            484    File2 contents                       156
        TestDumpFile CreateValidDumpFile()
        {
            ushort programHeadersCount = 3;

            byte[] elfHeaderBytes = TestData.GetElfBytes(ElfHeader.Size, ProgramHeader.Size,
                                                         programHeadersCount, false);

            byte[] file1Bytes  = TestData.GetElfFileBytesFromBuildId(TestData.expectedId);
            ulong  file1Offset =
                (ulong)elfHeaderBytes.Length + programHeadersCount * (ulong)ProgramHeader.Size;
            ulong file1Size    = (ulong)file1Bytes.Length;
            ulong file1Address = 1000;

            byte[] file2Bytes   = TestData.GetElfFileBytesFromBuildId(TestData.anotherExpectedId);
            ulong  file2Offset  = 484;
            ulong  file2Size    = (ulong)file2Bytes.Length;
            ulong  file2Address = 2000;

            ulong fileIndexOffset = file1Offset + file1Size;

            byte[] fileIndexBytes = TestData.GetNtFileSectionsBytes(
                new[] { file1Address, file2Address },
                new[] { file1Address + file1Size, file2Address + file2Size }, new[] { 0ul, 0ul },
                new[] { expectedFileName, anotherExpectedFileName });
            byte[] fileIndexNoteBytes = TestData.GetNoteSectionBytes(
                NoteSection.CoreName, NoteSection.NtFileType, fileIndexBytes);

            byte[] fileIndexHeaderBytes = TestData.GetProgramHeaderBytes(
                ProgramHeader.Type.NoteSegment, fileIndexOffset, 0ul /* address */,
                (ulong)(fileIndexNoteBytes.Length));
            byte[] file1HeaderBytes = TestData.GetProgramHeaderBytes(
                ProgramHeader.Type.LoadableSegment, file1Offset, file1Address, file1Size);
            byte[] file2HeaderBytes = TestData.GetProgramHeaderBytes(
                ProgramHeader.Type.LoadableSegment, file2Offset, file2Address, file2Size);
            byte[] headers = elfHeaderBytes.Concat(fileIndexHeaderBytes)
                             .Concat(file1HeaderBytes)
                             .Concat(file2HeaderBytes)
                             .ToArray();

            Assert.That(file2Offset,
                        Is.EqualTo(fileIndexOffset + (ulong)fileIndexNoteBytes.Length));

            return(new TestDumpFile {
                NoteNtFileEnd = file2Offset,
                Bytes = headers.Concat(file1Bytes)
                        .Concat(fileIndexNoteBytes)
                        .Concat(file2Bytes)
                        .ToArray(),
            });
        }
예제 #6
0
        public void FileInSeveralSegments(int firstSegmentSize)
        {
            var data = TestData.GetElfFileBytesFromBuildId(TestData.expectedId);
            var secondSegmentSize = data.Length - firstSegmentSize;
            var firstSegment      = new ProgramHeader(
                ProgramHeader.Type.LoadableSegment, 0, 0, (ulong)firstSegmentSize);

            var secondSegment = new ProgramHeader(ProgramHeader.Type.LoadableSegment,
                                                  (ulong)firstSegmentSize,
                                                  (ulong)firstSegmentSize,
                                                  (ulong)secondSegmentSize);

            var module = ReadModule(data, new[] { secondSegment, firstSegment },
                                    new FileSection("", 0, (ulong)data.Length));

            Assert.AreEqual(TestData.expectedId, module.Id);
        }
예제 #7
0
        public void CorrectModules()
        {
            ushort programHeadersCount = 4;
            var    elfHeaderBytes      = TestData.GetElfBytes(ElfHeader.Size, ProgramHeader.Size,
                                                              programHeadersCount, false);

            ulong firstFileOffset = ElfHeader.Size +
                                    (ulong)programHeadersCount * ProgramHeader.Size;

            var firstFileBytes  = TestData.GetElfFileBytesFromBuildId(TestData.expectedId);
            var secondFileBytes = TestData.GetElfFileBytesFromBuildId(TestData.anotherExpectedId);

            var firstFileHeaderBytes = TestData.GetProgramHeaderBytes(
                ProgramHeader.Type.LoadableSegment, firstFileOffset, firstFileOffset,
                (ulong)firstFileBytes.Length);

            var firstFileIndexOffset = firstFileOffset + (ulong)firstFileBytes.Length;
            var fileIndexBytes       = TestData.GetNtFileSectionBytes(
                firstFileOffset, firstFileIndexOffset, 0ul, expectedFileName);

            var firstFileIndexNoteBytes = TestData.GetNoteSectionBytes(
                NoteSection.CoreName, NoteSection.NtFileType, fileIndexBytes);

            var firstFileIndexHeaderBytes = TestData.GetProgramHeaderBytes(
                ProgramHeader.Type.NoteSegment, firstFileIndexOffset, firstFileIndexOffset,
                (ulong)firstFileBytes.Length);

            ulong secondFileIndexOffset = firstFileIndexOffset +
                                          (ulong)firstFileIndexNoteBytes.Length;
            ulong secondFileOffset  = secondFileIndexOffset + (ulong)firstFileIndexNoteBytes.Length;
            ulong secondFileDataEnd = secondFileOffset + (ulong)secondFileBytes.Length;

            var secondIndexBytes = TestData.GetNtFileSectionBytes(
                secondFileOffset, secondFileDataEnd, 0ul, anotherExpectedFileName);

            var secondFileIndexHeaderBytes = TestData.GetProgramHeaderBytes(
                ProgramHeader.Type.NoteSegment, secondFileIndexOffset, secondFileIndexOffset,
                (ulong)secondIndexBytes.Length);

            var secondFileIndexNoteBytes = TestData.GetNoteSectionBytes(
                NoteSection.CoreName, NoteSection.NtFileType, secondIndexBytes);

            var secondFileHeaderBytes = TestData.GetProgramHeaderBytes(
                ProgramHeader.Type.LoadableSegment, secondFileOffset, secondFileOffset,
                (ulong)secondFileBytes.Length);

            var dumpBytes = elfHeaderBytes.Concat(firstFileHeaderBytes)
                            .Concat(firstFileIndexHeaderBytes).Concat(secondFileIndexHeaderBytes)
                            .Concat(secondFileHeaderBytes).Concat(firstFileBytes)
                            .Concat(firstFileIndexNoteBytes).Concat(secondFileIndexNoteBytes)
                            .Concat(secondFileBytes).ToArray();

            _fileSystem.AddFile(dumpPath, new MockFileData(dumpBytes));

            DumpReadResult dump = _provider.GetModules(dumpPath);

            Assert.AreEqual(2, dump.Modules.Count());

            var firstModule = dump.Modules.First();

            Assert.AreEqual(expectedFileName, firstModule.Path);
            Assert.AreEqual(TestData.expectedId, firstModule.Id);

            var lastModule = dump.Modules.Last();

            Assert.AreEqual(anotherExpectedFileName, lastModule.Path);
            Assert.AreEqual(TestData.anotherExpectedId, lastModule.Id);
        }