• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

mendersoftware / mender / 1022753986

02 Oct 2023 10:37AM UTC coverage: 78.168% (-2.0%) from 80.127%
1022753986

push

gitlab-ci

oleorhagen
feat: Run the authentication loop once upon bootstrap

Ticket: MEN-6658
Changelog: None

Signed-off-by: Ole Petter <ole.orhagen@northern.tech>

32 of 32 new or added lines in 1 file covered. (100.0%)

6996 of 8950 relevant lines covered (78.17%)

10353.4 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.57
/artifact/v3/header/token.hpp
1
// Copyright 2023 Northern.tech AS
2
//
3
//    Licensed under the Apache License, Version 2.0 (the "License");
4
//    you may not use this file except in compliance with the License.
5
//    You may obtain a copy of the License at
6
//
7
//        http://www.apache.org/licenses/LICENSE-2.0
8
//
9
//    Unless required by applicable law or agreed to in writing, software
10
//    distributed under the License is distributed on an "AS IS" BASIS,
11
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
//    See the License for the specific language governing permissions and
13
//    limitations under the License.
14

15
#ifndef MENDER_ARTIFACT_V3_HEADER_TOKEN_HPP
16
#define MENDER_ARTIFACT_V3_HEADER_TOKEN_HPP
17

18

19
#include <unordered_map>
20
#include <string>
21
#include <regex>
22
#include <memory>
23

24
#include <common/log.hpp>
25
#include <common/expected.hpp>
26

27
const std::regex artifact_script_regexp {
28
        "scripts/Artifact(Install|Reboot|Rollback|RollbackReboot|Failure)_(Enter|Leave)_[0-9][0-9](_\\S+)?",
29
        std::regex_constants::ECMAScript};
30

31
const int artifact_headers_index_position {8};
32
const int artifact_headers_index_length {4};
33

34
namespace mender {
35
namespace artifact {
36
namespace v3 {
37
namespace header {
38
namespace token {
39

40
using namespace std;
41

42
namespace tar = mender::tar;
43

44
namespace log = mender::common::log;
45
namespace error = mender::common::error;
46
namespace expected = mender::common::expected;
47

48
enum class Type {
49
        Uninitialized = 0,
50
        EOFToken,
51
        Unrecognized,
52
        HeaderInfo,
53
        ArtifactScripts,
54
        ArtifactHeaderTypeInfo,
55
        ArtifactHeaderMetaData,
56
};
57

58
unordered_map<const Type, string> type_map {
59
        {Type::Uninitialized, "Uninitialized"},
60
        {Type::EOFToken, "EOF"},
61
        {Type::Unrecognized, "Unrecognized"},
62
        {Type::HeaderInfo, "header-info"},
63
        {Type::ArtifactScripts, "artifact-scripts"},
64
        {Type::ArtifactHeaderTypeInfo, "type-info"},
65
        {Type::ArtifactHeaderMetaData, "header-meta-data"},
66
};
67

68
class Token {
69
public:
70
        Type type;
71
        string name {};
72
        shared_ptr<tar::Entry> value;
73

74
        Token() :
139✔
75
                type {Type::Uninitialized} {
139✔
76
        }
139✔
77
        explicit Token(Type t) :
137✔
78
                type {t} {
137✔
79
        }
137✔
80
        Token(const string &type_name, tar::Entry &entry) :
338✔
81
                type {StringToType(type_name)},
338✔
82
                name {StringToName(type_name)},
83
                value {make_shared<tar::Entry>(entry)} {
338✔
84
        }
338✔
85

86
        const string TypeToString() const {
1✔
87
                return type_map.at(type);
1✔
88
        }
89

90
        const int Index() const {
192✔
91
                switch (type) {
192✔
92
                case Type::ArtifactHeaderTypeInfo:
192✔
93
                        // fallthrough
94
                case Type::ArtifactHeaderMetaData:
95
                        return stoi(
192✔
96
                                this->name.substr(artifact_headers_index_position, artifact_headers_index_length));
384✔
97
                        break;
98
                default:
×
99
                        return -1;
×
100
                }
101
        }
102

103
private:
104
        Type StringToType(const string &type_name) {
338✔
105
                log::Trace("StringToType: " + type_name);
338✔
106
                if (type_name == "header-info") {
338✔
107
                        return Type::HeaderInfo;
138✔
108
                }
109
                if (std::regex_match(type_name, artifact_script_regexp)) {
200✔
110
                        return Type::ArtifactScripts;
7✔
111
                }
112
                if (std::regex_match(type_name, regex("headers/[0-9]{4}/type-info"))) {
193✔
113
                        return Type::ArtifactHeaderTypeInfo;
139✔
114
                }
115
                if (regex_match(type_name, regex("headers/[0-9]{4}/meta-data"))) {
54✔
116
                        return Type::ArtifactHeaderMetaData;
54✔
117
                }
118
                log::Error("Unrecognized token: " + type_name);
×
119
                return Type::Unrecognized;
×
120
        }
121

122
        string StringToName(const string &type_name) {
338✔
123
                if (std::regex_match(type_name, artifact_script_regexp)) {
338✔
124
                        return type_name.substr(7 + 1); // Strip the scripts + / prefix
7✔
125
                }
126
                return type_name;
331✔
127
        }
128
};
129

130
} // namespace token
131
} // namespace header
132
} // namespace v3
133
} // namespace artifact
134
} // namespace mender
135

136
#endif // MENDER_ARTIFACT_V3_HEADER_TOKEN_HPP
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc