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

mendersoftware / mender / 1038172795

16 Oct 2023 12:17PM UTC coverage: 80.166% (+0.6%) from 79.608%
1038172795

push

gitlab-ci

kacf
test: Add test for verifying hostname when doing TLS handshake.

Ticket: MEN-6788

Signed-off-by: Kristian Amlie <kristian.amlie@northern.tech>

6479 of 8082 relevant lines covered (80.17%)

10736.64 hits per line

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

72.83
/artifact/v3/header/header.cpp
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
#include <artifact/v3/header/header.hpp>
16

17
#include <iomanip>
18
#include <memory>
19
#include <string>
20
#include <system_error>
21
#include <vector>
22
#include <iostream>
23
#include <fstream>
24

25
#include <common/expected.hpp>
26
#include <common/error.hpp>
27
#include <common/io.hpp>
28
#include <common/log.hpp>
29
#include <common/json.hpp>
30
#include <common/common.hpp>
31
#include <common/path.hpp>
32

33
#include <artifact/error.hpp>
34
#include <artifact/lexer.hpp>
35
#include <artifact/tar/tar.hpp>
36

37
#include <artifact/v3/header/token.hpp>
38

39
namespace mender {
40
namespace artifact {
41
namespace v3 {
42
namespace header {
43

44
using namespace std;
45

46
namespace expected = mender::common::expected;
47
namespace io = mender::common::io;
48
namespace error = mender::common::error;
49
namespace log = mender::common::log;
50
namespace json = mender::common::json;
51
namespace path = mender::common::path;
52

53

54
namespace {
55
string IndexString(int index) {
1✔
56
        stringstream index_string {};
2✔
57
        index_string << setw(4) << setfill('0') << index;
1✔
58
        return index_string.str();
1✔
59
}
60
} // namespace
61

62
ExpectedHeader Parse(io::Reader &reader, ParserConfig conf) {
139✔
63
        Header header {};
278✔
64

65
        shared_ptr<tar::Reader> tar_reader {make_shared<tar::Reader>(reader)};
139✔
66

67
        auto lexer = lexer::Lexer<header::token::Token, header::token::Type> {tar_reader};
278✔
68

69
        token::Token tok = lexer.Next();
278✔
70

71
        if (tok.type != token::Type::HeaderInfo) {
139✔
72
                return expected::unexpected(parser_error::MakeError(
1✔
73
                        parser_error::Code::ParseError,
74
                        "Got unexpected token: '" + tok.TypeToString() + "' expected 'header-info'"));
3✔
75
        }
76

77
        auto expected_info = header::info::Parse(*tok.value);
138✔
78

79
        if (!expected_info) {
138✔
80
                return expected::unexpected(parser_error::MakeError(
×
81
                        parser_error::Code::ParseError,
82
                        "Failed to parse the header-info: " + expected_info.error().message));
×
83
        }
84
        header.info = expected_info.value();
138✔
85

86
        tok = lexer.Next();
138✔
87
        vector<ArtifactScript> state_scripts {};
138✔
88
        while (tok.type == token::Type::ArtifactScripts) {
145✔
89
                log::Trace("Parsing state script...");
14✔
90
                const string artifact_script_path =
91
                        path::Join(conf.artifact_scripts_filesystem_path, tok.name);
7✔
92
                errno = 0;
7✔
93
                ofstream myfile(artifact_script_path);
14✔
94
                log::Trace("state script name: " + tok.name);
14✔
95
                if (!myfile.good()) {
7✔
96
                        auto io_errno = errno;
×
97
                        return expected::unexpected(error::Error(
×
98
                                std::generic_category().default_error_condition(io_errno),
×
99
                                "Failed to create a file for writing the Artifact script: " + artifact_script_path
×
100
                                        + " to the filesystem"));
×
101
                }
102
                io::StreamWriter sw {myfile};
14✔
103

104
                auto err = io::Copy(sw, *tok.value);
7✔
105
                if (err != error::NoError) {
7✔
106
                        return expected::unexpected(err);
×
107
                }
108

109
                state_scripts.push_back(artifact_script_path);
7✔
110

111
                tok = lexer.Next();
7✔
112
        }
113

114
        // Write the Artifact script version file
115
        if (state_scripts.size() > 0) {
138✔
116
                const string artifact_script_version_file =
117
                        path::Join(conf.artifact_scripts_filesystem_path, "version");
4✔
118
                errno = 0;
4✔
119
                ofstream myfile(artifact_script_version_file);
8✔
120
                log::Trace("Creating the Artifact script version file: " + artifact_script_version_file);
8✔
121
                if (!myfile.good()) {
4✔
122
                        auto io_errno = errno;
×
123
                        return expected::unexpected(error::Error(
×
124
                                std::generic_category().default_error_condition(io_errno),
×
125
                                "Failed to create the Artifact script version file: "
126
                                        + artifact_script_version_file));
×
127
                }
128
                myfile << to_string(conf.artifact_scripts_version);
8✔
129
                if (!myfile.good()) {
4✔
130
                        auto io_errno = errno;
×
131
                        return expected::unexpected(error::Error(
×
132
                                std::generic_category().default_error_condition(io_errno),
×
133
                                "I/O error writing the Artifact scripts version file"));
×
134
                }
135
        }
136

137

138
        header.artifactScripts = std::move(state_scripts);
138✔
139

140
        vector<SubHeader> subheaders {};
138✔
141

142
        int current_index {0};
143
        while (tok.type != token::Type::EOFToken) {
275✔
144
                log::Trace("Parsing the sub-header ...");
276✔
145

146
                // NOTE: We currently do not support multiple payloads
147
                if (current_index != 0) {
138✔
148
                        return expected::unexpected(parser_error::MakeError(
×
149
                                parser_error::Code::ParseError,
150
                                "Multiple header entries found. Currently only one is supported"));
×
151
                }
152

153
                SubHeader sub_header {};
275✔
154
                if (tok.type != token::Type::ArtifactHeaderTypeInfo) {
138✔
155
                        return expected::unexpected(parser_error::MakeError(
×
156
                                parser_error::Code::ParseError,
157
                                "Unexpected entry: " + tok.TypeToString() + " expected: type-info"));
×
158
                }
159

160
                if (current_index != tok.Index()) {
138✔
161
                        return expected::unexpected(parser_error::MakeError(
1✔
162
                                parser_error::Code::ParseError,
163
                                "Unexpected index order for the type-info: " + tok.name + " expected: headers/"
2✔
164
                                        + IndexString(current_index) + "/type-info"));
5✔
165
                }
166
                auto expected_type_info = type_info::Parse(*tok.value);
137✔
167
                if (!expected_type_info) {
137✔
168
                        return expected::unexpected(expected_type_info.error());
×
169
                }
170
                sub_header.type_info = expected_type_info.value();
137✔
171

172
                // NOTE (workaround): Bug in the Artifact format writer:
173
                // If the type is a RootfsImage, then the payload-type will be empty. This
174
                // is a bug in the mender-artifact tool, which writes the payload. For now,
175
                // just work around it.
176
                if (header.info.payloads[current_index].type == Payload::RootfsImage) {
137✔
177
                        log::Debug(
53✔
178
                                "Setting the type-info in payload nr " + to_string(current_index)
106✔
179
                                + " to rootfs-image");
106✔
180
                        sub_header.type_info.type = "rootfs-image";
53✔
181
                }
182

183
                tok = lexer.Next();
137✔
184

185
                log::Trace("sub-header: looking for meta-data");
274✔
186

187
                // meta-data (optional)
188
                if (tok.type == token::Type::ArtifactHeaderMetaData) {
137✔
189
                        if (current_index != tok.Index()) {
54✔
190
                                return expected::unexpected(parser_error::MakeError(
×
191
                                        parser_error::Code::ParseError,
192
                                        "Unexpected index order for the meta-data: " + tok.name + " expected: headers/"
×
193
                                                + IndexString(current_index) + "/meta-data"));
×
194
                        }
195
                        auto expected_meta_data = meta_data::Parse(*tok.value);
54✔
196
                        if (!expected_meta_data) {
54✔
197
                                return expected::unexpected(expected_meta_data.error());
×
198
                        }
199
                        sub_header.metadata = expected_meta_data.value();
54✔
200
                        tok = lexer.Next();
54✔
201
                }
202
                log::Trace("sub-header: parsed the meta-data");
274✔
203

204
                header.subHeaders.push_back(sub_header);
137✔
205

206
                current_index++;
207
        }
208

209
        return header;
137✔
210
}
211

212
} // namespace header
213
} // namespace v3
214
} // namespace artifact
215
} // namespace mender
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