• 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

93.75
/artifact/v3/header/meta_data.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

18
#include <common/expected.hpp>
19
#include <common/error.hpp>
20
#include <common/io.hpp>
21
#include <common/json.hpp>
22
#include <common/log.hpp>
23
#include <common/common.hpp>
24

25
#include <artifact/error.hpp>
26
#include <string>
27
#include <iostream>
28

29
namespace mender {
30
namespace artifact {
31
namespace v3 {
32
namespace header {
33
namespace meta_data {
34

35
const string empty_json_error_message =
36
        "[json.exception.parse_error.101] parse error at line 1, column 1: syntax error while parsing value - unexpected end of input; expected '[', '{', or a literal";
37

38
namespace json = mender::common::json;
39
namespace log = mender::common::log;
40

41
ExpectedMetaData VerifyMetaDataJson(const json::Json &json) {
7✔
42
        // 1. Only contains top-level keys
43
        if (!json.IsObject()) {
7✔
44
                return expected::unexpected(parser_error::MakeError(
1✔
45
                        parser_error::Code::ParseError, "The meta-data needs to be a top-level object"));
2✔
46
        }
47
        // 2. Only allowed strings, numbers, and lists of the aforementioned
48
        auto expected_children = json.GetChildren();
12✔
49
        auto children = expected_children.value();
12✔
50
        if (!all_of(children.cbegin(), children.cend(), [](const json::ChildrenMap::value_type &it) {
6✔
51
                        if (it.second.IsArray()) {
12✔
52
                                auto array_size = it.second.GetArraySize().value();
2✔
53
                                for (size_t i = 0; i < array_size; ++i) {
2✔
54
                                        const json::ExpectedJson array_item = it.second.Get(i);
4✔
55
                                        if (!array_item) {
2✔
56
                                                return false;
×
57
                                        }
58

59
                                        return array_item.value().IsString() || array_item.value().IsNumber();
2✔
60
                                }
61
                                return true;
×
62
                        }
63
                        return it.second.IsString() || it.second.IsNumber();
10✔
64
                })) {
65
                auto err = parser_error::MakeError(
66
                        parser_error::Code::ParseError,
67
                        "The meta-data needs to only be strings, ints and arrays of ints and strings");
4✔
68
                return expected::unexpected(err);
4✔
69
        }
70

71
        return json;
4✔
72
}
73

74
ExpectedMetaData Parse(io::Reader &reader) {
62✔
75
        log::Trace("Parsing the header meta-data");
62✔
76
        auto expected_json = json::Load(reader);
124✔
77

78
        if (!expected_json) {
62✔
79
                log::Trace("Received json load error: " + expected_json.error().message);
55✔
80
                bool is_empty_payload_error =
81
                        expected_json.error().message.find(empty_json_error_message) != string::npos;
55✔
82
                if (is_empty_payload_error) {
55✔
83
                        log::Trace("Received an empty Json body. Not treating this as an error");
53✔
84
                        return json::Json();
106✔
85
                }
86
                return expected::unexpected(parser_error::MakeError(
2✔
87
                        parser_error::Code::ParseError,
88
                        "Failed to parse the  meta-data JSON: " + expected_json.error().message));
4✔
89
        }
90

91
        const json::Json meta_data_json = expected_json.value();
14✔
92

93
        return VerifyMetaDataJson(meta_data_json);
7✔
94
}
95

96
} // namespace meta_data
97
} // namespace header
98
} // namespace v3
99
} // namespace artifact
100
} // 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