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

mendersoftware / mender / 1022567176

02 Oct 2023 07:50AM UTC coverage: 80.127% (+2.5%) from 77.645%
1022567176

push

gitlab-ci

kacf
chore: Centralize selection of `std::filesystem` library.

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

6447 of 8046 relevant lines covered (80.13%)

9912.21 hits per line

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

61.29
/artifact/v3/version/version.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/version/version.hpp>
16

17
#include <string>
18
#include <vector>
19

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

25

26
namespace mender {
27
namespace artifact {
28
namespace v3 {
29
namespace version {
30

31
namespace io = mender::common::io;
32
namespace expected = mender::common::expected;
33
namespace error = mender::common::error;
34
namespace json = mender::common::json;
35

36
const int supported_parser_version {3};
37
const string supported_parser_format {"mender"};
38

39
const ErrorCategoryClass ErrorCategory {};
40

41
const char *ErrorCategoryClass::name() const noexcept {
×
42
        return "VersionParserErrorCategory";
×
43
}
44

45
string ErrorCategoryClass::message(int code) const {
×
46
        switch (code) {
×
47
        case NoError:
48
                return "Success";
×
49
        case ParseError:
50
                return "Parse error";
×
51
        case VersionError:
52
                return "Wrong Artifact version";
×
53
        case FormatError:
54
                return "Wrong Artifact format";
×
55
        default:
56
                return "Unknown";
×
57
        }
58
}
59

60
error::Error MakeError(ErrorCode code, const string &msg) {
×
61
        return error::Error(error_condition(code, ErrorCategory), msg);
4✔
62
}
63

64

65
ExpectedVersion Parse(io::Reader &reader) {
138✔
66
        auto expected_json = json::Load(reader);
138✔
67
        if (!expected_json) {
138✔
68
                return expected::unexpected(MakeError(
2✔
69
                        ParseError,
70
                        "Failed to parse the version header JSON: " + expected_json.error().message));
6✔
71
        }
72

73
        const json::Json version_json = expected_json.value();
136✔
74

75
        auto version =
76
                version_json.Get("version").and_then([](const json::Json &j) { return j.GetInt(); });
408✔
77

78
        if (!version) {
136✔
79
                return expected::unexpected(MakeError(VersionError, version.error().message));
×
80
        }
81

82
        if (version.value() != supported_parser_version) {
136✔
83
                return expected::unexpected(MakeError(
1✔
84
                        FormatError,
85
                        "Only version " + std::to_string(supported_parser_version)
2✔
86
                                + " is supported, received version " + std::to_string(version.value())));
5✔
87
        }
88

89
        auto format =
90
                version_json.Get("format").and_then([](const json::Json &j) { return j.GetString(); });
405✔
91

92
        if (!format) {
135✔
93
                return expected::unexpected(MakeError(FormatError, format.error().message));
×
94
        }
95

96
        if (format != supported_parser_format) {
135✔
97
                return expected::unexpected(MakeError(
1✔
98
                        FormatError,
99
                        "The client only understands the 'mender' Artifact type. Got format: "
100
                                + format.value()));
3✔
101
        }
102

103
        return Version {.version = supported_parser_version, .format = supported_parser_format};
268✔
104
}
105

106
} // namespace version
107
} // namespace v3
108
} // namespace artifact
109
} // 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