GCC Code Coverage Report


Directory: src/
File: src/protocol.c
Date: 2023-08-28 07:33:56
Exec Total Coverage
Lines: 78 114 68.4%
Branches: 22 44 50.0%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2023 Egor Tensin <Egor.Tensin@gmail.com>
3 * This file is part of the "cimple" project.
4 * For details, see https://github.com/egor-tensin/cimple.
5 * Distributed under the MIT License.
6 */
7
8 #include "protocol.h"
9 #include "base64.h"
10 #include "compiler.h"
11 #include "const.h"
12 #include "json_rpc.h"
13 #include "process.h"
14 #include "run_queue.h"
15
16 #include <stddef.h>
17 #include <stdint.h>
18 #include <stdlib.h>
19
20 static const char *const run_key_id = "id";
21 static const char *const run_key_url = "url";
22 static const char *const run_key_rev = "rev";
23
24 9180 int run_request_create(struct jsonrpc_request **request, const struct run *run)
25 {
26 9180 int ret = 0;
27
28 9180 ret = jsonrpc_request_create(request, 1, CMD_RUN, NULL);
29
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
30 return ret;
31 9180 ret = jsonrpc_request_set_param_string(*request, run_key_url, run_get_url(run));
32
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
33 goto free_request;
34 9180 ret = jsonrpc_request_set_param_string(*request, run_key_rev, run_get_rev(run));
35
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
36 goto free_request;
37
38 9180 return ret;
39
40 free_request:
41 jsonrpc_request_destroy(*request);
42
43 return ret;
44 }
45
46 9180 int run_request_parse(const struct jsonrpc_request *request, struct run **run)
47 {
48 9180 int ret = 0;
49
50 9180 const char *url = NULL;
51 9180 ret = jsonrpc_request_get_param_string(request, run_key_url, &url);
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
53 return ret;
54 9180 const char *rev = NULL;
55 9180 ret = jsonrpc_request_get_param_string(request, run_key_rev, &rev);
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
57 return ret;
58
59 9180 return run_create(run, 0, url, rev);
60 }
61
62 9234 int new_worker_request_create(struct jsonrpc_request **request)
63 {
64 9234 return jsonrpc_notification_create(request, CMD_NEW_WORKER, NULL);
65 }
66
67 int new_worker_request_parse(UNUSED const struct jsonrpc_request *request)
68 {
69 return 0;
70 }
71
72 9180 int start_request_create(struct jsonrpc_request **request, const struct run *run)
73 {
74 9180 int ret = 0;
75
76 9180 ret = jsonrpc_notification_create(request, CMD_START, NULL);
77
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
78 return ret;
79 9180 ret = jsonrpc_request_set_param_int(*request, run_key_id, run_get_id(run));
80
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
81 goto free_request;
82 9180 ret = jsonrpc_request_set_param_string(*request, run_key_url, run_get_url(run));
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
84 goto free_request;
85 9180 ret = jsonrpc_request_set_param_string(*request, run_key_rev, run_get_rev(run));
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
87 goto free_request;
88
89 9180 return ret;
90
91 free_request:
92 jsonrpc_request_destroy(*request);
93
94 return ret;
95 }
96
97 9180 int start_request_parse(const struct jsonrpc_request *request, struct run **run)
98 {
99 9180 int ret = 0;
100
101 9180 int64_t id = 0;
102 9180 ret = jsonrpc_request_get_param_int(request, run_key_id, &id);
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
104 return ret;
105 9180 const char *url = NULL;
106 9180 ret = jsonrpc_request_get_param_string(request, run_key_url, &url);
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
108 return ret;
109 9180 const char *rev = NULL;
110 9180 ret = jsonrpc_request_get_param_string(request, run_key_rev, &rev);
111
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
112 return ret;
113
114 9180 return run_create(run, (int)id, url, rev);
115 }
116
117 static const char *const finished_key_run_id = "run_id";
118 static const char *const finished_key_ec = "exit_code";
119 static const char *const finished_key_data = "output";
120
121 9180 int finished_request_create(struct jsonrpc_request **request, int run_id,
122 const struct proc_output *output)
123 {
124 9180 int ret = 0;
125
126 9180 ret = jsonrpc_notification_create(request, CMD_FINISHED, NULL);
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
128 return ret;
129 9180 ret = jsonrpc_request_set_param_int(*request, finished_key_run_id, run_id);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
131 goto free_request;
132 9180 ret = jsonrpc_request_set_param_int(*request, finished_key_ec, output->ec);
133
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
134 goto free_request;
135
136 9180 char *b64data = NULL;
137 9180 ret = base64_encode(output->data, output->data_size, &b64data);
138
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
139 goto free_request;
140
141 9180 ret = jsonrpc_request_set_param_string(*request, finished_key_data, b64data);
142 9180 free(b64data);
143
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
144 goto free_request;
145
146 9180 return ret;
147
148 free_request:
149 jsonrpc_request_destroy(*request);
150
151 return ret;
152 }
153
154 9180 int finished_request_parse(const struct jsonrpc_request *request, int *_run_id,
155 struct proc_output **_output)
156 {
157 9180 int ret = 0;
158
159 9180 struct proc_output *output = NULL;
160 9180 ret = proc_output_create(&output);
161
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
162 return ret;
163
164 9180 int64_t run_id = 0;
165 9180 ret = jsonrpc_request_get_param_int(request, finished_key_run_id, &run_id);
166
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
167 goto free_output;
168
169 9180 int64_t ec = -1;
170 9180 ret = jsonrpc_request_get_param_int(request, finished_key_ec, &ec);
171
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
172 goto free_output;
173 9180 output->ec = (int)ec;
174
175 9180 const char *b64data = NULL;
176 9180 ret = jsonrpc_request_get_param_string(request, finished_key_data, &b64data);
177
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
178 goto free_output;
179
180 9180 ret = base64_decode(b64data, &output->data, &output->data_size);
181
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 9180 times.
9180 if (ret < 0)
182 goto free_output;
183
184 9180 *_run_id = (int)run_id;
185 9180 *_output = output;
186 9180 return ret;
187
188 free_output:
189 proc_output_destroy(output);
190
191 return ret;
192 }
193