Aktualizr
C++ SOTA Client
metafake.h
1 #ifndef METAFAKE_H_
2 #define METAFAKE_H_
3 
4 #include <fstream>
5 #include <string>
6 #include <vector>
7 
8 #include <boost/filesystem.hpp>
9 
10 #include "logging/logging.h"
11 #include "uptane_repo.h"
12 #include "utilities/utils.h"
13 
14 class MetaFake {
15  public:
16  MetaFake(const boost::filesystem::path &meta_dir_in)
17  : meta_dir(meta_dir_in), work_dir(meta_dir / "fake_meta"), repo(work_dir, "2021-07-04T16:33:27Z", "id0") {
18  repo.generateRepo(KeyType::kED25519);
19  backup();
20  create_image();
21  create_testData();
22  }
23 
24  private:
25  void create_testData(void) {
26  boost::filesystem::path file_name;
27  std::string hwid;
28  Delegation delegation;
29 
30  // add image for "has update" metadata
31  file_name = "dummy_firmware.txt";
32  repo.addImage(work_dir / file_name, file_name, "dummy", "", delegation);
33 
34  file_name = "primary_firmware.txt";
35  hwid = "primary_hw";
36  repo.addImage(work_dir / file_name, file_name, hwid, "", delegation);
37  repo.addTarget(file_name.string(), hwid, "CA:FE:A6:D2:84:9D", "");
38 
39  file_name = "secondary_firmware.txt";
40  hwid = "secondary_hw";
41  repo.addImage(work_dir / file_name, file_name, hwid, "", delegation);
42  repo.addTarget(file_name.string(), hwid, "secondary_ecu_serial", "");
43 
44  repo.signTargets();
45  rename("_hasupdates");
46 
47  // add image for "no update" metadata
48  restore();
49 
50  file_name = "dummy_firmware.txt";
51  repo.addImage(work_dir / file_name, file_name, "dummy", "", delegation);
52 
53  repo.signTargets();
54  rename("_noupdates");
55 
56  // add image for "multi secondary ecu" metadata
57  restore();
58 
59  file_name = "dummy_firmware.txt";
60  repo.addImage(work_dir / file_name, file_name, "dummy", "", delegation);
61 
62  file_name = "secondary_firmware.txt";
63  hwid = "sec_hw1";
64  repo.addImage(work_dir / file_name, file_name, hwid, "", delegation);
65  repo.addTarget(file_name.string(), hwid, "sec_serial1", "");
66 
67  file_name = "secondary_firmware2.txt";
68  hwid = "sec_hw2";
69  repo.addImage(work_dir / file_name, file_name, hwid, "", delegation);
70  repo.addTarget(file_name.string(), hwid, "sec_serial2", "");
71 
72  repo.signTargets();
73  rename("_multisec");
74 
75  // copy metadata to work_dir
76  Utils::copyDir(work_dir / ImageRepo::dir, meta_dir / "repo");
77  Utils::copyDir(work_dir / DirectorRepo::dir, meta_dir / "director");
78  if (!boost::filesystem::exists(meta_dir / "campaigner") &&
79  boost::filesystem::is_directory("tests/test_data/campaigner")) {
80  Utils::copyDir("tests/test_data/campaigner", meta_dir / "campaigner");
81  }
82  }
83 
84  void create_image(void) {
85  std::string content = "Just to increment timestamp, ignore it\n";
86  Utils::writeFile(work_dir / "dummy_firmware.txt", content);
87  content = "This is a dummy firmware file (should never be downloaded)\n";
88  Utils::writeFile(work_dir / "primary_firmware.txt", content);
89  content = "This is content";
90  Utils::writeFile(work_dir / "secondary_firmware.txt", content);
91  content = "This is more content\n";
92  Utils::writeFile(work_dir / "secondary_firmware2.txt", content);
93  }
94 
95  void backup(void) {
96  backup_files.push_back(work_dir / DirectorRepo::dir / "targets.json");
97  backup_content.push_back(Utils::readFile(backup_files[0], false));
98 
99  backup_files.push_back(work_dir / ImageRepo::dir / "snapshot.json");
100  backup_content.push_back(Utils::readFile(backup_files[1], false));
101 
102  backup_files.push_back(work_dir / ImageRepo::dir / "targets.json");
103  backup_content.push_back(Utils::readFile(backup_files[2], false));
104 
105  backup_files.push_back(work_dir / ImageRepo::dir / "timestamp.json");
106  backup_content.push_back(Utils::readFile(backup_files[3], false));
107  }
108 
109  void restore(void) {
110  for (unsigned int i = 0; i < backup_files.size(); i++) {
111  Utils::writeFile(backup_files[i], backup_content[i]);
112  }
113  }
114 
115  void rename(const std::string &appendix) {
116  for (unsigned int i = 0; i < backup_files.size(); i++) {
117  boost::filesystem::rename(backup_files[i],
118  (backup_files[i].parent_path() / backup_files[i].stem()).string() + appendix + ".json");
119  }
120  }
121 
122  private:
123  boost::filesystem::path meta_dir;
124  boost::filesystem::path work_dir;
125  UptaneRepo repo;
126  std::vector<boost::filesystem::path> backup_files;
127  std::vector<std::string> backup_content;
128 };
129 
130 #endif // METAFAKE_H
131 
132 // vim: set tabstop=2 shiftwidth=2 expandtab:
UptaneRepo
Definition: uptane_repo.h:7
Delegation
Definition: repo.h:19
MetaFake
Definition: metafake.h:14