-
Notifications
You must be signed in to change notification settings - Fork 0
/
encode_decode_unittest.cc
117 lines (90 loc) · 3.78 KB
/
encode_decode_unittest.cc
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
// Copyright (c) 2011 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stddef.h>
#include <memory>
#include "courgette/base_test_unittest.h"
#include "courgette/courgette.h"
#include "courgette/courgette_flow.h"
#include "courgette/streams.h"
namespace courgette {
class EncodeDecodeTest : public BaseTest {
public:
void TestAssembleToStreamDisassemble(const std::string& file,
size_t expected_encoded_length) const;
};
void EncodeDecodeTest::TestAssembleToStreamDisassemble(
const std::string& file,
size_t expected_encoded_length) const {
const uint8_t* original_data = reinterpret_cast<const uint8_t*>(file.data());
size_t original_length = file.length();
CourgetteFlow flow;
// Convert executable to encoded assembly.
RegionBuffer original_buffer(Region(original_data, original_length));
flow.ReadDisassemblerFromBuffer(flow.ONLY, original_buffer);
EXPECT_EQ(C_OK, flow.status());
EXPECT_TRUE(nullptr != flow.data(flow.ONLY)->disassembler.get());
flow.CreateAssemblyProgramFromDisassembler(flow.ONLY, false);
EXPECT_EQ(C_OK, flow.status());
EXPECT_TRUE(nullptr != flow.data(flow.ONLY)->program.get());
flow.CreateEncodedProgramFromDisassemblerAndAssemblyProgram(flow.ONLY);
EXPECT_EQ(C_OK, flow.status());
EXPECT_TRUE(nullptr != flow.data(flow.ONLY)->encoded.get());
flow.DestroyAssemblyProgram(flow.ONLY);
EXPECT_EQ(C_OK, flow.status());
EXPECT_TRUE(nullptr == flow.data(flow.ONLY)->program.get());
flow.DestroyDisassembler(flow.ONLY);
EXPECT_EQ(C_OK, flow.status());
EXPECT_TRUE(nullptr == flow.data(flow.ONLY)->disassembler.get());
flow.WriteSinkStreamSetFromEncodedProgram(flow.ONLY);
EXPECT_EQ(C_OK, flow.status());
flow.DestroyEncodedProgram(flow.ONLY);
EXPECT_EQ(C_OK, flow.status());
EXPECT_TRUE(nullptr == flow.data(flow.ONLY)->encoded.get());
SinkStream sink;
flow.WriteSinkStreamFromSinkStreamSet(flow.ONLY, &sink);
EXPECT_EQ(C_OK, flow.status());
const void* encoded_data = sink.Buffer();
size_t encoded_length = sink.Length();
EXPECT_EQ(expected_encoded_length, encoded_length);
// Convert encoded assembly back to executable.
RegionBuffer encoded_buffer(Region(encoded_data, encoded_length));
flow.ReadSourceStreamSetFromBuffer(flow.ONLY, encoded_buffer);
EXPECT_EQ(C_OK, flow.status());
flow.ReadEncodedProgramFromSourceStreamSet(flow.ONLY);
EXPECT_EQ(C_OK, flow.status());
EXPECT_TRUE(nullptr != flow.data(flow.ONLY)->encoded.get());
SinkStream executable;
flow.WriteExecutableFromEncodedProgram(flow.ONLY, &executable);
EXPECT_EQ(C_OK, flow.status());
flow.DestroyEncodedProgram(flow.ONLY);
EXPECT_EQ(C_OK, flow.status());
EXPECT_TRUE(nullptr == flow.data(flow.ONLY)->encoded.get());
EXPECT_TRUE(flow.ok());
EXPECT_FALSE(flow.failed());
const void* executable_data = executable.Buffer();
size_t executable_length = executable.Length();
EXPECT_EQ(original_length, executable_length);
EXPECT_EQ(0, memcmp(original_data, executable_data, original_length));
}
TEST_F(EncodeDecodeTest, PE) {
std::string file = FileContents("setup1.exe");
TestAssembleToStreamDisassemble(file, 972845);
}
TEST_F(EncodeDecodeTest, PE64) {
std::string file = FileContents("chrome64_1.exe");
TestAssembleToStreamDisassemble(file, 810090);
}
TEST_F(EncodeDecodeTest, Elf_Small) {
std::string file = FileContents("elf-32-1");
TestAssembleToStreamDisassemble(file, 136201);
}
TEST_F(EncodeDecodeTest, Elf_HighBSS) {
std::string file = FileContents("elf-32-high-bss");
TestAssembleToStreamDisassemble(file, 7308);
}
TEST_F(EncodeDecodeTest, Elf_Arm) {
std::string file = FileContents("elf-armv7");
TestAssembleToStreamDisassemble(file, 8531);
}
} // namespace courgette