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

mendersoftware / mender-mcu / 1570959687

03 Dec 2024 01:28PM UTC coverage: 25.126% (-0.4%) from 25.487%
1570959687

push

gitlab-ci

danielskinstad
test: unit tests for mender-artifact

Ticket: MEN-7683

Signed-off-by: Daniel Skinstad Drabitzius <daniel.drabitzius@northern.tech>

697 of 2774 relevant lines covered (25.13%)

6.13 hits per line

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

0.0
/core/src/mender-artifact-download.c
1
/**
2
 * @file      mender-artifact-download.c
3
 * @brief     Mender artifact download implementation
4
 *
5
 * Copyright joelguittet and mender-mcu-client contributors
6
 * Copyright Northern.tech AS
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *     http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20

21
#include "mender-api.h"
22
#include "mender-artifact.h"
23
#include "mender-artifact-download.h"
24
#include "mender-artifact-download-data.h"
25
#include "mender-error-counters.h"
26
#include "mender-http.h"
27
#include "mender-log.h"
28

29
static mender_err_t mender_download_artifact_callback(mender_http_client_event_t       event,
30
                                                      void                            *data,
31
                                                      size_t                           data_length,
32
                                                      mender_artifact_download_data_t *dl_data);
33

34
mender_err_t
35
mender_download_artifact(const char *uri, mender_deployment_data_t *deployment_data, mender_update_module_t **update_module) {
×
36
    assert(NULL != uri);
×
37
    assert(NULL != deployment_data);
×
38
    assert(NULL != update_module);
×
39

40
    mender_err_t ret;
41
    int          status = 0;
×
42

43
    mender_artifact_download_data_t dl_data = {
×
44
        .deployment                 = deployment_data,
45
        .artifact_download_callback = &mender_download_artifact_callback,
46
        .ret                        = MENDER_OK,
47
    };
48

49
    /* Perform HTTP request */
50
    if (MENDER_OK != (ret = mender_http_artifact_download(uri, &dl_data, &status))) {
×
51
        mender_log_error("Unable to perform HTTP request");
×
52
        mender_err_count_net_inc();
×
53
        return ret;
×
54
    }
55

56
    /* Treatment depending of the status */
57
    if (200 == status) {
×
58
        /* Nothing to do */
59
        *update_module = dl_data.update_module;
×
60
        return MENDER_OK;
×
61
    } else {
62
        mender_api_print_response_error(NULL, status);
×
63
        return MENDER_FAIL;
×
64
    }
65
}
66

67
static mender_err_t
68
mender_download_artifact_callback(mender_http_client_event_t event, void *data, size_t data_length, mender_artifact_download_data_t *dl_data) {
×
69
    mender_err_t ret = MENDER_OK;
×
70

71
    mender_artifact_ctx_t *mender_artifact_ctx = NULL;
×
72

73
    /* Treatment depending of the event */
74
    switch (event) {
×
75
        case MENDER_HTTP_EVENT_CONNECTED:
×
76
            /* Create new artifact context */
77
            if (NULL == (mender_artifact_ctx = mender_artifact_create_ctx(2 * MENDER_ARTIFACT_STREAM_BLOCK_SIZE + mender_http_recv_buf_length))) {
×
78
                mender_log_error("Unable to create artifact context");
×
79
                return MENDER_FAIL;
×
80
            }
81
            break;
×
82
        case MENDER_HTTP_EVENT_DATA_RECEIVED:
×
83
            /* Check input data */
84
            if ((NULL == data) || (0 == data_length)) {
×
85
                mender_log_error("Invalid data received");
×
86
                return MENDER_FAIL;
×
87
            }
88

89
            /* Check artifact context */
90
            if (MENDER_OK != mender_artifact_get_ctx(&mender_artifact_ctx)) {
×
91
                mender_log_error("Unable to get artifact context");
×
92
                return MENDER_FAIL;
×
93
            }
94
            assert(NULL != mender_artifact_ctx);
×
95

96
            /* Parse input data */
97
            if (MENDER_OK != (ret = mender_artifact_process_data(mender_artifact_ctx, data, data_length, dl_data))) {
×
98
                mender_log_error("Unable to process data");
×
99
                return ret;
×
100
            }
101
            break;
×
102
        case MENDER_HTTP_EVENT_DISCONNECTED:
×
103
            break;
×
104
        case MENDER_HTTP_EVENT_ERROR:
×
105
            /* Downloading the artifact fails */
106
            mender_log_error("An error occurred");
×
107
            return MENDER_FAIL;
×
108
        default:
×
109
            /* Should not occur */
110
            return MENDER_FAIL;
×
111
    }
112

113
    return ret;
×
114
}
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