1 #include <gtest/gtest.h>
8 #include "logging/logging.h"
9 #include "uptane/exceptions.h"
10 #include "uptane/tuf.h"
11 #include "utilities/utils.h"
14 TEST(Root, RootValidates) {
15 Json::Value initial_root = Utils::parseJSONFile(
"tests/tuf/sample1/root.json");
16 LOG_INFO <<
"Root is:" << initial_root;
19 Uptane::Root root(Uptane::RepositoryType::Director(), initial_root, root1);
21 EXPECT_NO_THROW(
Uptane::Root(Uptane::RepositoryType::Director(), initial_root, root));
25 TEST(Root, RootJsonNoKeys) {
27 Json::Value initial_root = Utils::parseJSONFile(
"tests/tuf/sample1/root.json");
28 initial_root[
"signed"].removeMember(
"keys");
33 TEST(Root, RootJsonNoRoles) {
35 Json::Value initial_root = Utils::parseJSONFile(
"tests/tuf/sample1/root.json");
36 initial_root[
"signed"].removeMember(
"roles");
43 TEST(Root, RootJsonRsassaPssSha256) {
45 Json::Value initial_root = Utils::parseJSONFile(
"tests/tuf/rsassa-pss-sha256/root.json");
46 LOG_INFO <<
"Root is:" << initial_root;
48 Uptane::Root root(Uptane::RepositoryType::Director(), initial_root, root1);
49 EXPECT_NO_THROW(
Uptane::Root(Uptane::RepositoryType::Director(), initial_root, root));
53 TEST(Role, ValidateRoles) {
55 EXPECT_EQ(root.ToInt(), 0);
56 EXPECT_EQ(root.ToString(),
"root");
57 EXPECT_EQ(root.IsDelegation(),
false);
60 EXPECT_EQ(snapshot.ToInt(), 1);
61 EXPECT_EQ(snapshot.ToString(),
"snapshot");
62 EXPECT_EQ(snapshot.IsDelegation(),
false);
65 EXPECT_EQ(targets.ToInt(), 2);
66 EXPECT_EQ(targets.ToString(),
"targets");
67 EXPECT_EQ(targets.IsDelegation(),
false);
70 EXPECT_EQ(timestamp.ToInt(), 3);
71 EXPECT_EQ(timestamp.ToString(),
"timestamp");
72 EXPECT_EQ(timestamp.IsDelegation(),
false);
76 TEST(Role, ValidDelegationName) {
77 Uptane::Role delegated = Uptane::Role::Delegation(
"whatever");
78 EXPECT_EQ(delegated.ToString(),
"whatever");
79 EXPECT_EQ(delegated.IsDelegation(),
true);
83 TEST(Role, InvalidDelegationName) {
90 Json::Value generateTarget(
const std::string& hash,
const int length) {
93 hashes[
"sha256"] = hash;
94 target[
"hashes"] = hashes;
95 target[
"length"] = length;
99 Json::Value generateDirectorTarget(
const std::string& hash,
const int length,
const Uptane::EcuMap& ecu_map) {
100 Json::Value target = generateTarget(hash, length);
103 for (
auto it = ecu_map.cbegin(); it != ecu_map.cend(); ++it) {
105 current[
"hardwareId"] = it->second.ToString();
106 ecus[it->first.ToString()] = current;
108 custom[
"ecuIdentifiers"] = ecus;
109 target[
"custom"] = custom;
113 Json::Value generateImageTarget(
const std::string& hash,
const int length,
114 const std::vector<Uptane::HardwareIdentifier>& hardwareIds) {
115 Json::Value target = generateTarget(hash, length);
118 for (Json::Value::ArrayIndex i = 0; i < hardwareIds.size(); ++i) {
119 hwids[i] = hardwareIds[i].ToString();
121 custom[
"hardwareIds"] = hwids;
122 target[
"custom"] = custom;
127 TEST(Target, Match) {
129 std::vector<Uptane::HardwareIdentifier> hardwareIds;
130 Uptane::EcuMap ecu_map;
131 hardwareIds.emplace_back(hwid);
133 Uptane::Target target1(
"abc", generateDirectorTarget(
"hash_good", 739, ecu_map));
134 Uptane::Target target2(
"abc", generateImageTarget(
"hash_good", 739, hardwareIds));
135 EXPECT_TRUE(target1.MatchTarget(target2));
136 EXPECT_TRUE(target2.MatchTarget(target1));
140 TEST(Target, MatchDirector) {
143 Uptane::EcuMap ecu_map;
146 Uptane::Target target1(
"abc", generateDirectorTarget(
"hash_good", 739, ecu_map));
147 Uptane::Target target2(
"abc", generateDirectorTarget(
"hash_good", 739, ecu_map));
148 EXPECT_TRUE(target1.MatchTarget(target2));
149 EXPECT_TRUE(target2.MatchTarget(target1));
153 TEST(Target, MatchImages) {
156 std::vector<Uptane::HardwareIdentifier> hardwareIds;
157 hardwareIds.emplace_back(hwid);
158 hardwareIds.emplace_back(hwid2);
159 Uptane::Target target1(
"abc", generateImageTarget(
"hash_good", 739, hardwareIds));
160 Uptane::Target target2(
"abc", generateImageTarget(
"hash_good", 739, hardwareIds));
161 EXPECT_TRUE(target1.MatchTarget(target2));
162 EXPECT_TRUE(target2.MatchTarget(target1));
166 TEST(Target, MatchExtraHwId) {
168 std::vector<Uptane::HardwareIdentifier> hardwareIds;
169 Uptane::EcuMap ecu_map;
170 hardwareIds.emplace_back(hwid);
172 Uptane::Target target1(
"abc", generateDirectorTarget(
"hash_good", 739, ecu_map));
174 Uptane::Target target2(
"abc", generateImageTarget(
"hash_good", 739, hardwareIds));
175 EXPECT_TRUE(target1.MatchTarget(target2));
176 EXPECT_TRUE(target2.MatchTarget(target1));
180 TEST(Target, MatchTwo) {
183 std::vector<Uptane::HardwareIdentifier> hardwareIds;
184 Uptane::EcuMap ecu_map;
185 hardwareIds.emplace_back(hwid);
186 hardwareIds.emplace_back(hwid2);
189 Uptane::Target target1(
"abc", generateDirectorTarget(
"hash_good", 739, ecu_map));
190 Uptane::Target target2(
"abc", generateImageTarget(
"hash_good", 739, hardwareIds));
191 EXPECT_TRUE(target1.MatchTarget(target2));
192 EXPECT_TRUE(target2.MatchTarget(target1));
196 TEST(Target, MultipleHwIdMismatch) {
198 std::vector<Uptane::HardwareIdentifier> hardwareIds;
199 hardwareIds.emplace_back(hwid);
201 Uptane::Target target1(
"abc", generateImageTarget(
"hash_good", 739, hardwareIds));
203 Uptane::Target target2(
"abc", generateImageTarget(
"hash_good", 739, hardwareIds));
204 EXPECT_FALSE(target1.MatchTarget(target2));
205 EXPECT_FALSE(target2.MatchTarget(target1));
209 TEST(Target, MissingHwId) {
211 std::vector<Uptane::HardwareIdentifier> hardwareIds;
212 Uptane::EcuMap ecu_map;
213 hardwareIds.emplace_back(hwid);
215 Uptane::Target target1(
"abc", generateDirectorTarget(
"hash_good", 739, ecu_map));
217 Uptane::Target target2(
"abc", generateImageTarget(
"hash_good", 739, hardwareIds));
218 EXPECT_FALSE(target1.MatchTarget(target2));
219 EXPECT_FALSE(target2.MatchTarget(target1));
223 TEST(Target, FilenameMismatch) {
225 std::vector<Uptane::HardwareIdentifier> hardwareIds;
226 Uptane::EcuMap ecu_map;
227 hardwareIds.emplace_back(hwid);
229 Uptane::Target target1(
"abc", generateDirectorTarget(
"hash_good", 739, ecu_map));
230 Uptane::Target target2(
"xyz", generateImageTarget(
"hash_good", 739, hardwareIds));
231 EXPECT_FALSE(target1.MatchTarget(target2));
232 EXPECT_FALSE(target2.MatchTarget(target1));
236 TEST(Target, LengthMismatch) {
238 std::vector<Uptane::HardwareIdentifier> hardwareIds;
239 Uptane::EcuMap ecu_map;
240 hardwareIds.emplace_back(hwid);
242 Uptane::Target target1(
"abc", generateDirectorTarget(
"hash_good", 739, ecu_map));
243 Uptane::Target target2(
"abc", generateImageTarget(
"hash_good", 1, hardwareIds));
244 EXPECT_FALSE(target1.MatchTarget(target2));
245 EXPECT_FALSE(target2.MatchTarget(target1));
249 TEST(Target, HardwareIdMismatch) {
251 std::vector<Uptane::HardwareIdentifier> hardwareIds;
252 Uptane::EcuMap ecu_map;
253 hardwareIds.emplace_back(hwid);
255 Uptane::Target target1(
"abc", generateDirectorTarget(
"hash_good", 739, ecu_map));
257 Uptane::Target target2(
"abc", generateImageTarget(
"hash_good", 739, hardwareIds));
258 EXPECT_FALSE(target1.MatchTarget(target2));
259 EXPECT_FALSE(target2.MatchTarget(target1));
263 TEST(Target, HashMismatch) {
265 std::vector<Uptane::HardwareIdentifier> hardwareIds;
266 Uptane::EcuMap ecu_map;
267 hardwareIds.emplace_back(hwid);
269 Uptane::Target target1(
"abc", generateDirectorTarget(
"hash_good", 739, ecu_map));
270 Uptane::Target target2(
"abc", generateImageTarget(
"hash_bad", 739, hardwareIds));
271 EXPECT_FALSE(target1.MatchTarget(target2));
272 EXPECT_FALSE(target2.MatchTarget(target1));
276 int main(
int argc,
char** argv) {
277 ::testing::InitGoogleTest(&argc, argv);
278 logger_set_threshold(boost::log::trivial::trace);
279 return RUN_ALL_TESTS();