Aktualizr
C++ SOTA Client
All Classes Namespaces Files Functions Variables Enumerations Enumerator Pages
tuf_test.cc
1 #include <gtest/gtest.h>
2 
3 #include <map>
4 #include <vector>
5 
6 #include <json/json.h>
7 
8 #include "logging/logging.h"
9 #include "uptane/exceptions.h"
10 #include "uptane/tuf.h"
11 #include "utilities/utils.h"
12 
13 /* Validate Root metadata. */
14 TEST(Root, RootValidates) {
15  Json::Value initial_root = Utils::parseJSONFile("tests/tuf/sample1/root.json");
16  LOG_INFO << "Root is:" << initial_root;
17 
18  Uptane::Root root1(Uptane::Root::Policy::kAcceptAll);
19  Uptane::Root root(Uptane::RepositoryType::Director(), initial_root, root1);
20 
21  EXPECT_NO_THROW(Uptane::Root(Uptane::RepositoryType::Director(), initial_root, root));
22 }
23 
24 /* Throw an exception if Root metadata is unsigned. */
25 TEST(Root, RootJsonNoKeys) {
26  Uptane::Root root1(Uptane::Root::Policy::kAcceptAll);
27  Json::Value initial_root = Utils::parseJSONFile("tests/tuf/sample1/root.json");
28  initial_root["signed"].removeMember("keys");
29  EXPECT_THROW(Uptane::Root(Uptane::RepositoryType::Director(), initial_root, root1), Uptane::InvalidMetadata);
30 }
31 
32 /* Throw an exception if Root metadata has no roles. */
33 TEST(Root, RootJsonNoRoles) {
34  Uptane::Root root1(Uptane::Root::Policy::kAcceptAll);
35  Json::Value initial_root = Utils::parseJSONFile("tests/tuf/sample1/root.json");
36  initial_root["signed"].removeMember("roles");
37  EXPECT_THROW(Uptane::Root(Uptane::RepositoryType::Director(), initial_root, root1), Uptane::InvalidMetadata);
38 }
39 
40 /**
41  * Check that a root.json that uses "method": "rsassa-pss-sha256" validates correctly
42  */
43 TEST(Root, RootJsonRsassaPssSha256) {
44  Uptane::Root root1(Uptane::Root::Policy::kAcceptAll);
45  Json::Value initial_root = Utils::parseJSONFile("tests/tuf/rsassa-pss-sha256/root.json");
46  LOG_INFO << "Root is:" << initial_root;
47 
48  Uptane::Root root(Uptane::RepositoryType::Director(), initial_root, root1);
49  EXPECT_NO_THROW(Uptane::Root(Uptane::RepositoryType::Director(), initial_root, root));
50 }
51 
52 /* Validate TUF roles. */
53 TEST(Role, ValidateRoles) {
54  Uptane::Role root = Uptane::Role::Root();
55  EXPECT_EQ(root.ToInt(), 0);
56  EXPECT_EQ(root.ToString(), "root");
57  EXPECT_EQ(root.IsDelegation(), false);
58 
59  Uptane::Role snapshot = Uptane::Role::Snapshot();
60  EXPECT_EQ(snapshot.ToInt(), 1);
61  EXPECT_EQ(snapshot.ToString(), "snapshot");
62  EXPECT_EQ(snapshot.IsDelegation(), false);
63 
64  Uptane::Role targets = Uptane::Role::Targets();
65  EXPECT_EQ(targets.ToInt(), 2);
66  EXPECT_EQ(targets.ToString(), "targets");
67  EXPECT_EQ(targets.IsDelegation(), false);
68 
69  Uptane::Role timestamp = Uptane::Role::Timestamp();
70  EXPECT_EQ(timestamp.ToInt(), 3);
71  EXPECT_EQ(timestamp.ToString(), "timestamp");
72  EXPECT_EQ(timestamp.IsDelegation(), false);
73 }
74 
75 /* Delegated roles have custom names. */
76 TEST(Role, ValidDelegationName) {
77  Uptane::Role delegated = Uptane::Role::Delegation("whatever");
78  EXPECT_EQ(delegated.ToString(), "whatever");
79  EXPECT_EQ(delegated.IsDelegation(), true);
80 }
81 
82 /* Reject delegated role names that are identical to reserved role names. */
83 TEST(Role, InvalidDelegationName) {
84  EXPECT_THROW(Uptane::Role::Delegation("root"), Uptane::Exception);
85  EXPECT_THROW(Uptane::Role::Delegation("snapshot"), Uptane::Exception);
86  EXPECT_THROW(Uptane::Role::Delegation("targets"), Uptane::Exception);
87  EXPECT_THROW(Uptane::Role::Delegation("timestamp"), Uptane::Exception);
88 }
89 
90 Json::Value generateTarget(const std::string& hash, const int length) {
91  Json::Value target;
92  Json::Value hashes;
93  hashes["sha256"] = hash;
94  target["hashes"] = hashes;
95  target["length"] = length;
96  return target;
97 }
98 
99 Json::Value generateDirectorTarget(const std::string& hash, const int length, const Uptane::EcuMap& ecu_map) {
100  Json::Value target = generateTarget(hash, length);
101  Json::Value custom;
102  Json::Value ecus;
103  for (auto it = ecu_map.cbegin(); it != ecu_map.cend(); ++it) {
104  Json::Value current;
105  current["hardwareId"] = it->second.ToString();
106  ecus[it->first.ToString()] = current;
107  }
108  custom["ecuIdentifiers"] = ecus;
109  target["custom"] = custom;
110  return target;
111 }
112 
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);
116  Json::Value custom;
117  Json::Value hwids;
118  for (Json::Value::ArrayIndex i = 0; i < hardwareIds.size(); ++i) {
119  hwids[i] = hardwareIds[i].ToString();
120  }
121  custom["hardwareIds"] = hwids;
122  target["custom"] = custom;
123  return target;
124 }
125 
126 /* Equivalent metadata generated by both repos should match. */
127 TEST(Target, Match) {
128  Uptane::HardwareIdentifier hwid("fake-test");
129  std::vector<Uptane::HardwareIdentifier> hardwareIds;
130  Uptane::EcuMap ecu_map;
131  hardwareIds.emplace_back(hwid);
132  ecu_map.insert({Uptane::EcuSerial("serial"), 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));
137 }
138 
139 /* Two Target objects created by the Director should match. */
140 TEST(Target, MatchDirector) {
141  Uptane::HardwareIdentifier hwid("first-test");
142  Uptane::HardwareIdentifier hwid2("second-test");
143  Uptane::EcuMap ecu_map;
144  ecu_map.insert({Uptane::EcuSerial("serial"), hwid});
145  ecu_map.insert({Uptane::EcuSerial("serial2"), hwid2});
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));
150 }
151 
152 /* Two Target objects created by the Image repo should match. */
153 TEST(Target, MatchImages) {
154  Uptane::HardwareIdentifier hwid("first-test");
155  Uptane::HardwareIdentifier hwid2("second-test");
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));
163 }
164 
165 /* Extra hardware IDs in the Image Target metadata should still match. */
166 TEST(Target, MatchExtraHwId) {
167  Uptane::HardwareIdentifier hwid("fake-test");
168  std::vector<Uptane::HardwareIdentifier> hardwareIds;
169  Uptane::EcuMap ecu_map;
170  hardwareIds.emplace_back(hwid);
171  ecu_map.insert({Uptane::EcuSerial("serial"), hwid});
172  Uptane::Target target1("abc", generateDirectorTarget("hash_good", 739, ecu_map));
173  hardwareIds.emplace_back(Uptane::HardwareIdentifier("extra"));
174  Uptane::Target target2("abc", generateImageTarget("hash_good", 739, hardwareIds));
175  EXPECT_TRUE(target1.MatchTarget(target2));
176  EXPECT_TRUE(target2.MatchTarget(target1));
177 }
178 
179 /* Multiple ECUs should still match. */
180 TEST(Target, MatchTwo) {
181  Uptane::HardwareIdentifier hwid("first-test");
182  Uptane::HardwareIdentifier hwid2("second-test");
183  std::vector<Uptane::HardwareIdentifier> hardwareIds;
184  Uptane::EcuMap ecu_map;
185  hardwareIds.emplace_back(hwid);
186  hardwareIds.emplace_back(hwid2);
187  ecu_map.insert({Uptane::EcuSerial("serial"), hwid});
188  ecu_map.insert({Uptane::EcuSerial("serial2"), 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));
193 }
194 
195 /* Reject inconsistent sets of multiple hardware IDs. */
196 TEST(Target, MultipleHwIdMismatch) {
197  Uptane::HardwareIdentifier hwid("fake-test");
198  std::vector<Uptane::HardwareIdentifier> hardwareIds;
199  hardwareIds.emplace_back(hwid);
200  hardwareIds.emplace_back(Uptane::HardwareIdentifier("extra"));
201  Uptane::Target target1("abc", generateImageTarget("hash_good", 739, hardwareIds));
202  hardwareIds.emplace_back(Uptane::HardwareIdentifier("extra2"));
203  Uptane::Target target2("abc", generateImageTarget("hash_good", 739, hardwareIds));
204  EXPECT_FALSE(target1.MatchTarget(target2));
205  EXPECT_FALSE(target2.MatchTarget(target1));
206 }
207 
208 /* Reject a missing hardware ID. */
209 TEST(Target, MissingHwId) {
210  Uptane::HardwareIdentifier hwid("fake-test");
211  std::vector<Uptane::HardwareIdentifier> hardwareIds;
212  Uptane::EcuMap ecu_map;
213  hardwareIds.emplace_back(hwid);
214  ecu_map.insert({Uptane::EcuSerial("serial"), hwid});
215  Uptane::Target target1("abc", generateDirectorTarget("hash_good", 739, ecu_map));
216  hardwareIds.clear();
217  Uptane::Target target2("abc", generateImageTarget("hash_good", 739, hardwareIds));
218  EXPECT_FALSE(target1.MatchTarget(target2));
219  EXPECT_FALSE(target2.MatchTarget(target1));
220 }
221 
222 /* Reject mismatched filenames. */
223 TEST(Target, FilenameMismatch) {
224  Uptane::HardwareIdentifier hwid("fake-test");
225  std::vector<Uptane::HardwareIdentifier> hardwareIds;
226  Uptane::EcuMap ecu_map;
227  hardwareIds.emplace_back(hwid);
228  ecu_map.insert({Uptane::EcuSerial("serial"), 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));
233 }
234 
235 /* Reject mismatched lengths. */
236 TEST(Target, LengthMismatch) {
237  Uptane::HardwareIdentifier hwid("fake-test");
238  std::vector<Uptane::HardwareIdentifier> hardwareIds;
239  Uptane::EcuMap ecu_map;
240  hardwareIds.emplace_back(hwid);
241  ecu_map.insert({Uptane::EcuSerial("serial"), 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));
246 }
247 
248 /* Reject mismatched hardware IDs. */
249 TEST(Target, HardwareIdMismatch) {
250  Uptane::HardwareIdentifier hwid("fake-test");
251  std::vector<Uptane::HardwareIdentifier> hardwareIds;
252  Uptane::EcuMap ecu_map;
253  hardwareIds.emplace_back(hwid);
254  ecu_map.insert({Uptane::EcuSerial("serial"), hwid});
255  Uptane::Target target1("abc", generateDirectorTarget("hash_good", 739, ecu_map));
256  hardwareIds[0] = Uptane::HardwareIdentifier("alt-test");
257  Uptane::Target target2("abc", generateImageTarget("hash_good", 739, hardwareIds));
258  EXPECT_FALSE(target1.MatchTarget(target2));
259  EXPECT_FALSE(target2.MatchTarget(target1));
260 }
261 
262 /* Reject mismatched hashes. */
263 TEST(Target, HashMismatch) {
264  Uptane::HardwareIdentifier hwid("fake-test");
265  std::vector<Uptane::HardwareIdentifier> hardwareIds;
266  Uptane::EcuMap ecu_map;
267  hardwareIds.emplace_back(hwid);
268  ecu_map.insert({Uptane::EcuSerial("serial"), 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));
273 }
274 
275 #ifndef __NO_MAIN__
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();
280 }
281 #endif
Uptane::InvalidMetadata
Definition: exceptions.h:81
Uptane::HardwareIdentifier
Definition: types.h:315
Uptane::EcuSerial
Definition: types.h:346
Uptane::Exception
Definition: exceptions.h:10
Uptane::Role
TUF Roles.
Definition: tuf.h:61
Uptane::Target
Definition: types.h:379
Uptane::Root
Definition: tuf.h:216