forked from google/bindiff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log_writer_test.cc
129 lines (113 loc) · 4.79 KB
/
log_writer_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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// 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/log_writer.h"
#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "third_party/absl/strings/ascii.h"
#include "third_party/absl/strings/str_cat.h"
#include "third_party/absl/strings/str_replace.h"
#include "third_party/absl/strings/string_view.h"
#include "third_party/zynamics/bindiff/match/call_graph.h"
#include "third_party/zynamics/bindiff/match/flow_graph.h"
#include "third_party/zynamics/bindiff/statistics.h"
#include "third_party/zynamics/bindiff/test_util.h"
#include "third_party/zynamics/binexport/testing.h"
#include "third_party/zynamics/binexport/util/filesystem.h"
#include "third_party/zynamics/binexport/util/status_matchers.h"
namespace security::bindiff {
namespace {
using binexport::GetTestFileContents;
using binexport::GetTestTempPath;
using ::not_absl::IsOk;
using ::testing::IsTrue;
#ifdef GTEST_USES_POSIX_RE
using ::testing::AllOfArray;
using ::testing::ContainsRegex;
#endif
::testing::Environment* const g_bindiff_env =
::testing::AddGlobalTestEnvironment(new BinDiffEnvironment());
class LogWriterTest : public BinDiffTest {
private:
void SetUp() override { SetUpBasicFunctionMatch(); }
};
#ifdef GTEST_USES_POSIX_RE
// Converts a regular expression using character classes into a pure POSIX
// one. This is needed because macOS and Linux support different extensions
// to POSIX regexes.
std::string CanonicalizeRegex(absl::string_view s) {
return absl::StrReplaceAll(s, {{R"(\d)", R"([0-9])"}, {R"(\s)", R"([ \t])"}});
}
std::string RegexEscape(absl::string_view s) {
return CanonicalizeRegex(
absl::StrReplaceAll(s, {{R"(()", R"(\()"}, {R"())", R"(\))"}}));
}
#endif
TEST_F(LogWriterTest, Empty) {
const std::string log = GetTestTempPath("empty.log");
ResultsLogWriter writer(log);
EXPECT_THAT(writer.Write(primary_->call_graph, secondary_->call_graph,
primary_->flow_graphs, secondary_->flow_graphs,
fixed_points_),
IsOk());
ASSERT_THAT(FileExists(log), IsTrue());
const std::string log_output =
absl::AsciiStrToLower(GetTestFileContents(log));
#ifdef GTEST_USES_POSIX_RE
constexpr absl::string_view kPaddedValueRegex = R"((\s|\.)*:?\s+\d+(\.\d+)?)";
// Log should mention the call graph MD indices and overall
// similarity/confidence. Auto-deduce template argument, as regex matchers are
// in internal namespace.
std::vector regexes = {
ContainsRegex(CanonicalizeRegex(
absl::StrCat(R"(call graph\s*1\s+md index)", kPaddedValueRegex))),
ContainsRegex(CanonicalizeRegex(
absl::StrCat(R"(call graph\s*2\s+md index)", kPaddedValueRegex))),
ContainsRegex(
CanonicalizeRegex(absl::StrCat(R"(similarity)", kPaddedValueRegex))),
ContainsRegex(
CanonicalizeRegex(absl::StrCat(R"(confidence)", kPaddedValueRegex))),
};
EXPECT_THAT(log_output, AllOfArray(regexes));
// All of the statistics usually shown in the UI should be present.
regexes.clear();
for (int i = 0; i < Counts::ui_entry_size(); ++i) {
regexes.emplace_back(ContainsRegex(CanonicalizeRegex(
absl::StrCat(RegexEscape(absl::AsciiStrToLower(
Counts::GetDisplayName(static_cast<Counts::Kind>(i)))),
kPaddedValueRegex))));
}
EXPECT_THAT(log_output, AllOfArray(regexes));
// Same for confidence values.
regexes.clear();
for (const auto* step : GetDefaultMatchingSteps()) {
regexes.emplace_back(ContainsRegex(CanonicalizeRegex(absl::StrCat(
RegexEscape(absl::AsciiStrToLower(step->name())), kPaddedValueRegex))));
}
for (const auto* step : GetDefaultMatchingStepsBasicBlock()) {
regexes.emplace_back(ContainsRegex(CanonicalizeRegex(absl::StrCat(
RegexEscape(absl::AsciiStrToLower(step->name())), kPaddedValueRegex))));
}
EXPECT_THAT(log_output, AllOfArray(regexes));
// Verify matches and that everything was matched
EXPECT_THAT(
log_output,
AllOf(
ContainsRegex(CanonicalizeRegex(R"(matched\s+1\s+of\s+1\s*/\s*1)")),
ContainsRegex(CanonicalizeRegex(R"(unmatched primary\s+(0|\(0\)))")),
ContainsRegex(
CanonicalizeRegex(R"(unmatched secondary\s+(0|\(0\)))"))));
#endif
}
} // namespace
} // namespace security::bindiff