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

mendersoftware / mender / 1542824816

07 Nov 2024 04:43PM UTC coverage: 79.85% (+0.08%) from 79.772%
1542824816

push

gitlab-ci

web-flow
Merge pull request #1692 from lluiscampos/4.0.x-bugfixes-manifest

4.0.x: Bugfixes for Artifact manifest

26 of 28 new or added lines in 4 files covered. (92.86%)

1 existing line in 1 file now uncovered.

7149 of 8953 relevant lines covered (79.85%)

12223.2 hits per line

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

64.1
/src/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
#include <artifact/sha/sha.hpp>
26

27

28
namespace mender {
29
namespace artifact {
30
namespace v3 {
31
namespace version {
32

33
namespace io = mender::common::io;
34
namespace expected = mender::common::expected;
35
namespace error = mender::common::error;
36
namespace json = mender::common::json;
37
namespace sha = mender::sha;
38

39
const int supported_parser_version {3};
40
const string supported_parser_format {"mender"};
41

42
const ErrorCategoryClass ErrorCategory {};
43

44
const char *ErrorCategoryClass::name() const noexcept {
×
45
        return "VersionParserErrorCategory";
×
46
}
47

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

63
error::Error MakeError(ErrorCode code, const string &msg) {
×
64
        return error::Error(error_condition(code, ErrorCategory), msg);
4✔
65
}
66

67

68
ExpectedVersion Parse(io::Reader &reader) {
151✔
69
        Version v {};
302✔
70

71
        sha::Reader sha_reader {reader};
302✔
72

73
        auto expected_json = json::Load(sha_reader);
151✔
74
        if (!expected_json) {
151✔
75
                return expected::unexpected(MakeError(
2✔
76
                        ParseError,
77
                        "Failed to parse the version header JSON: " + expected_json.error().message));
6✔
78
        }
79

80
        const json::Json version_json = expected_json.value();
149✔
81

82
        auto version = version_json.Get("version").and_then(json::ToInt64);
298✔
83

84
        if (!version) {
149✔
85
                return expected::unexpected(MakeError(VersionError, version.error().message));
×
86
        }
87

88
        if (version.value() != supported_parser_version) {
149✔
89
                return expected::unexpected(MakeError(
1✔
90
                        FormatError,
91
                        "Only version " + std::to_string(supported_parser_version)
2✔
92
                                + " is supported, received version " + std::to_string(version.value())));
5✔
93
        }
94
        v.version = supported_parser_version;
148✔
95

96
        auto format = version_json.Get("format").and_then(json::ToString);
296✔
97

98
        if (!format) {
148✔
99
                return expected::unexpected(MakeError(FormatError, format.error().message));
×
100
        }
101

102
        if (format != supported_parser_format) {
148✔
103
                return expected::unexpected(MakeError(
1✔
104
                        FormatError,
105
                        "The client only understands the 'mender' Artifact type. Got format: "
106
                                + format.value()));
3✔
107
        }
108
        v.format = supported_parser_format;
109

110
        auto expected_sha = sha_reader.ShaSum();
147✔
111
        if (!expected_sha) {
147✔
NEW
112
                expected::unexpected(
×
NEW
113
                        MakeError(FormatError, "Invalid ShaSum: " + expected_sha.error().message));
×
114
        }
115
        v.shasum = expected_sha.value();
147✔
116

117
        return v;
147✔
118
}
119

120
} // namespace version
121
} // namespace v3
122
} // namespace artifact
123
} // 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