-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtest_fingerprint.cpp
More file actions
80 lines (68 loc) · 3.02 KB
/
test_fingerprint.cpp
File metadata and controls
80 lines (68 loc) · 3.02 KB
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
#include <gtest/gtest.h>
import std;
import mcpp.toolchain.detect;
import mcpp.toolchain.fingerprint;
using namespace mcpp::toolchain;
namespace {
FingerprintInputs baseline() {
FingerprintInputs in;
in.toolchain.compiler = CompilerId::GCC;
in.toolchain.version = "16.1.0";
in.toolchain.binaryPath = "/usr/bin/g++";
in.toolchain.driverIdent = "g++ (xim-x-gcc 16.1.0) 16.1.0";
in.toolchain.targetTriple = "x86_64-linux-gnu";
in.toolchain.stdlibId = "libstdc++";
in.toolchain.stdlibVersion = "16.1.0";
in.cppStandard = "c++23";
in.compileFlags = "-O2";
in.dependencyLockHash = "deadbeefcafebabe";
in.stdBmiHash = "0123456789abcdef";
return in;
}
} // namespace
TEST(Fingerprint, DeterministicForSameInputs) {
auto a = compute_fingerprint(baseline());
auto b = compute_fingerprint(baseline());
EXPECT_EQ(a.hex, b.hex);
}
TEST(Fingerprint, ProducesSixteenHexChars) {
auto fp = compute_fingerprint(baseline());
EXPECT_EQ(fp.hex.size(), 16u);
for (char c : fp.hex) {
EXPECT_TRUE((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'));
}
}
#define EXPECT_DIFFERENT(field_mutation) do { \
auto base = baseline(); \
auto bfp = compute_fingerprint(base); \
auto in = baseline(); \
field_mutation; \
auto fp = compute_fingerprint(in); \
EXPECT_NE(bfp.hex, fp.hex) \
<< "Mutation '" #field_mutation "' did NOT change fingerprint"; \
} while (0)
TEST(Fingerprint, AllTenFieldsAffectHash) {
EXPECT_DIFFERENT(in.toolchain.compiler = CompilerId::Clang);
EXPECT_DIFFERENT(in.toolchain.version = "16.0.0");
EXPECT_DIFFERENT(in.toolchain.driverIdent = "g++ (xim-x-gcc 15.1.0) 15.1.0");
EXPECT_DIFFERENT(in.toolchain.targetTriple = "aarch64-linux-gnu");
EXPECT_DIFFERENT(in.toolchain.stdlibId = "libc++");
EXPECT_DIFFERENT(in.cppStandard = "c++26");
EXPECT_DIFFERENT(in.compileFlags = "-O3");
// mcpp version is hardcoded inside compute_fingerprint, can't mutate from here.
EXPECT_DIFFERENT(in.dependencyLockHash = "");
EXPECT_DIFFERENT(in.stdBmiHash = "ffffffffffffffff");
}
TEST(Fingerprint, StableAcrossBinaryPathsWhenDriverIdentMatches) {
auto a = baseline();
auto b = baseline();
a.toolchain.binaryPath = "/home/speak/.mcpp/registry/data/xpkgs/xim-x-gcc/16.1.0/bin/g++";
b.toolchain.binaryPath = "/home/speak/.xlings/data/xpkgs/xim-x-mcpp/0.0.14/registry/data/xpkgs/xim-x-gcc/16.1.0/bin/g++";
EXPECT_EQ(compute_fingerprint(a).hex, compute_fingerprint(b).hex);
}
TEST(Fingerprint, HashStringMatchesHashFile) {
auto h1 = hash_string("hello");
auto h2 = hash_string("hello");
EXPECT_EQ(h1, h2);
EXPECT_NE(hash_string("hello"), hash_string("hellp"));
}