-
Notifications
You must be signed in to change notification settings - Fork 148
/
instruction_test.cc
103 lines (91 loc) · 4.27 KB
/
instruction_test.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
// Copyright 2011-2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include "third_party/zynamics/bindiff/instruction.h"
#include "gtest/gtest.h"
namespace security::bindiff {
namespace {
TEST(InstructionTest, Instruction) {
Instruction::Cache cache;
Instruction instruction(&cache, 0xbaadf00dbaadf00d, "mov", 1234);
EXPECT_STREQ("mov", instruction.GetMnemonic(&cache).c_str());
EXPECT_EQ(1234, instruction.GetPrime());
EXPECT_EQ(0xbaadf00dbaadf00d, instruction.GetAddress());
}
TEST(InstructionTest, Instructions) {
Instruction::Cache cache;
Instructions instructions;
instructions.emplace_back(&cache, 0x1000000010000000, "one", 1234);
instructions.emplace_back(&cache, 0x1000000010000001, "two", 1235);
instructions.emplace_back(&cache, 0x1000000010000005, "three", 1236);
Instructions instructions2;
instructions2.emplace_back(&cache, 0x1000000010012000, "one", 1232);
instructions2.emplace_back(&cache, 0x1000000010012302, "one", 1234);
instructions2.emplace_back(&cache, 0x1000000010033300, "one", 1237);
instructions2.emplace_back(&cache, 0x1000000010112334, "two", 1235);
instructions2.emplace_back(&cache, 0x1000000010234205, "three", 1236);
instructions2.emplace_back(&cache, 0x1000000010234206, "three", 1237);
instructions2.emplace_back(&cache, 0x1000000010234207, "three", 1236);
InstructionMatches matches;
ComputeLcs(instructions.begin(), instructions.end(), instructions2.begin(),
instructions2.end(), matches);
EXPECT_EQ(3, matches.size());
EXPECT_EQ(0x1000000010000000, matches[0].first->GetAddress());
EXPECT_EQ(0x1000000010012302, matches[0].second->GetAddress());
EXPECT_EQ(0x1000000010000001, matches[1].first->GetAddress());
EXPECT_EQ(0x1000000010112334, matches[1].second->GetAddress());
EXPECT_EQ(0x1000000010000005, matches[2].first->GetAddress());
EXPECT_EQ(0x1000000010234207, matches[2].second->GetAddress());
EXPECT_EQ(1236, matches[2].second->GetPrime());
}
TEST(InstructionTest, LcsEmpty) {
Instructions instructions1;
Instructions instructions2;
InstructionMatches matches;
ComputeLcs(instructions1.begin(), instructions1.end(), instructions2.begin(),
instructions2.end(), matches);
EXPECT_EQ(0, matches.size());
}
TEST(InstructionTest, LcsEmptyOneSided) {
Instructions instructions1;
Instruction::Cache cache;
instructions1.emplace_back(&cache, 0x1000000010000000, "one", 1234);
instructions1.emplace_back(&cache, 0x1000000010000001, "two", 1235);
instructions1.emplace_back(&cache, 0x1000000010000005, "three", 1236);
Instructions instructions2;
InstructionMatches matches;
ComputeLcs(instructions1.begin(), instructions1.end(), instructions2.begin(),
instructions2.end(), matches);
EXPECT_EQ(0, matches.size());
}
TEST(InstructionTest, CommonPrefix) {
Instructions instructions1;
Instruction::Cache cache;
instructions1.emplace_back(&cache, 0x1000000010000000, "one", 1234);
instructions1.emplace_back(&cache, 0x1000000010000001, "two", 1235);
instructions1.emplace_back(&cache, 0x1000000010000005, "three", 1236);
Instructions instructions2;
instructions2.emplace_back(&cache, 0x1000000010000000, "one", 1234);
instructions2.emplace_back(&cache, 0x1000000010000001, "two", 1235);
instructions2.emplace_back(&cache, 0x1000000010000005, "unmatched", 7777);
InstructionMatches matches;
ComputeLcs(instructions1.begin(), instructions1.end(), instructions2.begin(),
instructions2.end(), matches);
EXPECT_EQ(2, matches.size());
EXPECT_EQ(0x1000000010000000, matches[0].first->GetAddress());
EXPECT_EQ(0x1000000010000000, matches[0].second->GetAddress());
EXPECT_EQ(0x1000000010000001, matches[1].first->GetAddress());
EXPECT_EQ(0x1000000010000001, matches[1].second->GetAddress());
}
} // namespace
} // namespace security::bindiff