Aktualizr
C++ SOTA Client
role.cc
1 #include "uptane/tuf.h"
2 
3 #include <sstream>
4 
5 using Uptane::Role;
6 using Uptane::Version;
7 
8 const std::string Role::ROOT = "root";
9 const std::string Role::SNAPSHOT = "snapshot";
10 const std::string Role::TARGETS = "targets";
11 const std::string Role::TIMESTAMP = "timestamp";
12 
13 Role::Role(const std::string &role_name, const bool delegation) {
14  std::string role_name_lower;
15  std::transform(role_name.begin(), role_name.end(), std::back_inserter(role_name_lower), ::tolower);
16  name_ = role_name_lower;
17  if (delegation) {
18  if (IsReserved(name_)) {
19  throw Uptane::Exception("", "Delegated role name " + role_name + " is reserved.");
20  }
21  role_ = RoleEnum::kDelegation;
22  name_ = role_name;
23  } else if (role_name_lower == ROOT) {
24  role_ = RoleEnum::kRoot;
25  } else if (role_name_lower == SNAPSHOT) {
26  role_ = RoleEnum::kSnapshot;
27  } else if (role_name_lower == TARGETS) {
28  role_ = RoleEnum::kTargets;
29  } else if (role_name_lower == TIMESTAMP) {
30  role_ = RoleEnum::kTimestamp;
31  } else {
32  role_ = RoleEnum::kInvalidRole;
33  name_ = "invalidrole";
34  }
35 }
36 
37 std::string Role::ToString() const { return name_; }
38 
39 std::ostream &Uptane::operator<<(std::ostream &os, const Role &role) {
40  os << role.ToString();
41  return os;
42 }
43 
44 std::string Version::RoleFileName(const Role &role) const {
45  std::stringstream ss;
46  if (version_ != Version::ANY_VERSION) {
47  ss << version_ << ".";
48  }
49  ss << role.ToString() << ".json";
50  return ss.str();
51 }
Uptane::Version
Metadata version numbers.
Definition: tuf.h:120
Uptane::Exception
Definition: exceptions.h:10
Uptane::Role
TUF Roles.
Definition: tuf.h:61