Consolidates app id comparison
[chromium-blink-merge.git] / courgette / encoded_program_unittest.cc
bloba2ce9b67d93e51f9d516f8737060349a6c0c45a3
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "courgette/encoded_program.h"
7 #include "base/macros.h"
8 #include "base/memory/scoped_ptr.h"
9 #include "courgette/disassembler.h"
10 #include "courgette/streams.h"
11 #include "testing/gtest/include/gtest/gtest.h"
13 namespace {
15 using courgette::EncodedProgram;
17 struct AddressSpec {
18 int32 index;
19 courgette::RVA rva;
22 // Creates a simple new program with given addresses. The orders of elements
23 // in |abs32_specs| and |rel32_specs| are important.
24 scoped_ptr<EncodedProgram> CreateTestProgram(AddressSpec* abs32_specs,
25 size_t num_abs32_specs,
26 AddressSpec* rel32_specs,
27 size_t num_rel32_specs) {
28 scoped_ptr<EncodedProgram> program(new EncodedProgram());
30 uint32 base = 0x00900000;
31 program->set_image_base(base);
33 for (size_t i = 0; i < num_abs32_specs; ++i) {
34 EXPECT_TRUE(program->DefineAbs32Label(abs32_specs[i].index,
35 abs32_specs[i].rva));
37 for (size_t i = 0; i < num_rel32_specs; ++i) {
38 EXPECT_TRUE(program->DefineRel32Label(rel32_specs[i].index,
39 rel32_specs[i].rva));
41 program->EndLabels();
43 EXPECT_TRUE(program->AddOrigin(0)); // Start at base.
44 for (size_t i = 0; i < num_abs32_specs; ++i)
45 EXPECT_TRUE(program->AddAbs32(abs32_specs[i].index));
46 for (size_t i = 0; i < num_rel32_specs; ++i)
47 EXPECT_TRUE(program->AddRel32(rel32_specs[i].index));
48 return program;
51 bool CompareSink(const uint8 expected[],
52 size_t num_expected,
53 courgette::SinkStream* ss) {
54 size_t n = ss->Length();
55 if (num_expected != n)
56 return false;
57 const uint8* buffer = ss->Buffer();
58 return memcmp(&expected[0], buffer, n) == 0;
61 } // namespace
63 // Create a simple program with a few addresses and references and
64 // check that the bits produced are as expected.
65 TEST(EncodedProgramTest, Test) {
66 // ABS32 index 7 == base + 4.
67 AddressSpec abs32_specs[] = {{7, 4}};
68 // REL32 index 5 == base + 0.
69 AddressSpec rel32_specs[] = {{5, 0}};
70 scoped_ptr<EncodedProgram> program(
71 CreateTestProgram(abs32_specs, arraysize(abs32_specs),
72 rel32_specs, arraysize(rel32_specs)));
74 // Serialize and deserialize.
76 courgette::SinkStreamSet sinks;
77 EXPECT_TRUE(program->WriteTo(&sinks));
78 program.reset();
80 courgette::SinkStream sink;
81 bool can_collect = sinks.CopyTo(&sink);
82 EXPECT_TRUE(can_collect);
84 const void* buffer = sink.Buffer();
85 size_t length = sink.Length();
87 courgette::SourceStreamSet sources;
88 bool can_get_source_streams = sources.Init(buffer, length);
89 EXPECT_TRUE(can_get_source_streams);
91 scoped_ptr<EncodedProgram> encoded2(new EncodedProgram());
92 bool can_read = encoded2->ReadFrom(&sources);
93 EXPECT_TRUE(can_read);
95 // Finally, try to assemble.
96 courgette::SinkStream assembled;
97 bool can_assemble = encoded2->AssembleTo(&assembled);
98 EXPECT_TRUE(can_assemble);
99 encoded2.reset();
101 const uint8 golden[] = {
102 0x04, 0x00, 0x90, 0x00, // ABS32 to base + 4
103 0xF8, 0xFF, 0xFF, 0xFF // REL32 from next line to base + 2
105 EXPECT_TRUE(CompareSink(golden, arraysize(golden), &assembled));
108 // A larger test with multiple addresses. We encode the program and check the
109 // contents of the address streams.
110 TEST(EncodedProgramTest, TestWriteAddress) {
111 // Absolute addresses by index: [_, _, _, 2, _, 23, _, 11].
112 AddressSpec abs32_specs[] = {{7, 11}, {3, 2}, {5, 23}};
113 // Relative addresses by index: [16, 7, _, 32].
114 AddressSpec rel32_specs[] = {{0, 16}, {3, 32}, {1, 7}};
115 scoped_ptr<EncodedProgram> program(
116 CreateTestProgram(abs32_specs, arraysize(abs32_specs),
117 rel32_specs, arraysize(rel32_specs)));
119 courgette::SinkStreamSet sinks;
120 EXPECT_TRUE(program->WriteTo(&sinks));
121 program.reset();
123 // Check addresses in sinks.
124 const uint8 golden_abs32_indexes[] = {
125 0x03, 0x07, 0x03, 0x05 // 3 indexes: [7, 3, 5].
127 EXPECT_TRUE(CompareSink(golden_abs32_indexes,
128 arraysize(golden_abs32_indexes),
129 sinks.stream(courgette::kStreamAbs32Indexes)));
131 const uint8 golden_rel32_indexes[] = {
132 0x03, 0x00, 0x03, 0x01 // 3 indexes: [0, 3, 1].
134 EXPECT_TRUE(CompareSink(golden_rel32_indexes,
135 arraysize(golden_rel32_indexes),
136 sinks.stream(courgette::kStreamRel32Indexes)));
138 // Addresses: [_, _, _, 2, _, 23, _, 11].
139 // Padded: [0, 0, 0, 2, 2, 23, 23, 11].
140 // Delta: [0, 0, 0, 2, 0, 21, 0, -12].
141 // Hex: [0, 0, 0, 0x02, 0, 0x15, 0, 0xFFFFFFF4].
142 // Complement neg: [0, 0, 0, 0x02, 0, 0x15, 0, (0x0B)].
143 // Varint32 Signed: [0, 0, 0, 0x04, 0, 0x2A, 0, 0x17].
144 const uint8 golden_abs32_addresses[] = {
145 0x08, // 8 address deltas.
146 0x00, 0x00, 0x00, 0x04, 0x00, 0x2A, 0x00, 0x17,
148 EXPECT_TRUE(CompareSink(golden_abs32_addresses,
149 arraysize(golden_abs32_addresses),
150 sinks.stream(courgette::kStreamAbs32Addresses)));
152 // Addresses: [16, 7, _, 32].
153 // Padded: [16, 7, 7, 32].
154 // Delta: [16, -9, 0, 25].
155 // Hex: [0x10, 0xFFFFFFF7, 0, 0x19].
156 // Complement Neg: [0x10, (0x08), 0, 0x19].
157 // Varint32 Signed: [0x20, 0x11, 0, 0x32].
158 const uint8 golden_rel32_addresses[] = {
159 0x04, // 4 address deltas.
160 0x20, 0x11, 0x00, 0x32,
162 EXPECT_TRUE(CompareSink(golden_rel32_addresses,
163 arraysize(golden_rel32_addresses),
164 sinks.stream(courgette::kStreamRel32Addresses)));