Skip to content

Commit 7d771f6

Browse files
committed
feat(tests): add ubus tests for umlua auth
1 parent a283a8f commit 7d771f6

File tree

2 files changed

+113
-72
lines changed

2 files changed

+113
-72
lines changed

src/services/sysagent/plugins/openwrt/plg_sysagent_openwrt.c

Lines changed: 24 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <umdaemon.h>
1818
#include <time.h>
1919
#include <json_object.h>
20+
#include <json_tokener.h>
2021
#include <spscq.h>
2122
#include <unistd.h>
2223
#include <errno.h>
@@ -54,14 +55,15 @@ static const struct blobmsg_policy list_signals_policy[] = {};
5455
enum {
5556
RUN_SIGNAL_ID,
5657
RUN_SIGNAL_ARGS,
58+
RUN_SIGNAL_AUTH,
5759
__RUN_SIGNAL_MAX
5860
};
5961

6062
// run_signal policiy
6163
static const struct blobmsg_policy run_signal_policy[] = {
6264
[RUN_SIGNAL_ID] = { .name = "id", .type = BLOBMSG_TYPE_STRING },
63-
[RUN_SIGNAL_ARGS] = { .name = "args", .type = BLOBMSG_TYPE_STRING }
64-
65+
[RUN_SIGNAL_ARGS] = { .name = "args", .type = BLOBMSG_TYPE_STRING },
66+
[RUN_SIGNAL_AUTH] = { .name = "auth", .type = BLOBMSG_TYPE_STRING }
6567
};
6668

6769
// match umsignal callback
@@ -101,14 +103,26 @@ run_signal(struct ubus_context *ctx,
101103

102104
if (tb[RUN_SIGNAL_ID] != NULL) {
103105
// id and args
104-
char *id = strdup(blobmsg_get_string(tb[RUN_SIGNAL_ID]));
105-
char *args = NULL;
106+
char *id = blobmsg_get_string(tb[RUN_SIGNAL_ID]);
107+
char *args = "";
108+
char *auth = "";
109+
int uflags = 0;
106110
// check args
107111
if (tb[RUN_SIGNAL_ARGS] != NULL) {
108-
args = strdup(blobmsg_get_string(tb[RUN_SIGNAL_ARGS]));
109-
110-
} else {
111-
args = strdup("");
112+
args = blobmsg_get_string(tb[RUN_SIGNAL_ARGS]);
113+
}
114+
// check auth
115+
if (tb[RUN_SIGNAL_AUTH] != NULL) {
116+
auth = blobmsg_get_string(tb[RUN_SIGNAL_AUTH]);
117+
// check auth (already checked for errors)
118+
if (auth != NULL) {
119+
json_object *j = json_tokener_parse(auth);
120+
if (j != NULL) {
121+
json_object *j_usr = json_object_object_get(j, "flags");
122+
uflags = json_object_get_int(j_usr);
123+
json_object_put(j);
124+
}
125+
}
112126
}
113127

114128
// output buffer
@@ -119,15 +133,15 @@ run_signal(struct ubus_context *ctx,
119133
umplg_data_std_t e_d = { .items = NULL };
120134
umplg_data_std_items_t items = { .table = NULL };
121135
umplg_data_std_item_t item = { .name = "", .value = args };
122-
umplg_data_std_item_t auth_item = { .name = "", .value = "" };
136+
umplg_data_std_item_t auth_item = { .name = "", .value = auth };
123137
// init std data
124138
umplg_stdd_init(&e_d);
125139
umplg_stdd_item_add(&items, &item);
126140
umplg_stdd_item_add(&items, &auth_item);
127141
umplg_stdd_items_add(&e_d, &items);
128142

129143
// run signal (set)
130-
int r = umplg_proc_signal(umplgm, id, &e_d, &buff, &b_sz, 0, NULL);
144+
int r = umplg_proc_signal(umplgm, id, &e_d, &buff, &b_sz, uflags, NULL);
131145

132146
switch (r) {
133147
case UMPLG_RES_SUCCESS:
@@ -151,8 +165,6 @@ run_signal(struct ubus_context *ctx,
151165
HASH_CLEAR(hh, items.table);
152166
umplg_stdd_free(&e_d);
153167
free(buff);
154-
free(id);
155-
free(args);
156168

157169
// missing args
158170
} else {

test/check_openwrt.c

Lines changed: 89 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -127,61 +127,7 @@ run_dtor(void **state)
127127
}
128128

129129
static void
130-
list_signals_cb(struct ubus_request *req, int type, struct blob_attr *msg)
131-
{
132-
// ubus output
133-
char *str = blobmsg_format_json(msg, true);
134-
assert_non_null(str);
135-
136-
// add result
137-
char **out_str = (char **)req->priv;
138-
*out_str = str;
139-
}
140-
141-
static void
142-
run_signal_w_static_output_cb(struct ubus_request *req,
143-
int type,
144-
struct blob_attr *msg)
145-
{
146-
// ubus output
147-
char *str = blobmsg_format_json(msg, true);
148-
assert_non_null(str);
149-
150-
// add result
151-
char **out_str = (char **)req->priv;
152-
*out_str = str;
153-
}
154-
155-
static void
156-
run_missing_signal_cb(struct ubus_request *req, int type, struct blob_attr *msg)
157-
{
158-
// ubus output
159-
char *str = blobmsg_format_json(msg, true);
160-
assert_non_null(str);
161-
162-
// add result
163-
char **out_str = (char **)req->priv;
164-
*out_str = str;
165-
}
166-
167-
static void
168-
run_signal_w_args_return_named_arg_cb(struct ubus_request *req,
169-
int type,
170-
struct blob_attr *msg)
171-
{
172-
// ubus output
173-
char *str = blobmsg_format_json(msg, true);
174-
assert_non_null(str);
175-
176-
// add result
177-
char **out_str = (char **)req->priv;
178-
*out_str = str;
179-
}
180-
181-
static void
182-
run_signal_w_id_missing_cb(struct ubus_request *req,
183-
int type,
184-
struct blob_attr *msg)
130+
ubus_cb(struct ubus_request *req, int type, struct blob_attr *msg)
185131
{
186132
// ubus output
187133
char *str = blobmsg_format_json(msg, true);
@@ -209,7 +155,7 @@ call_list_signals(void **state)
209155
id,
210156
"list_signals",
211157
b.head,
212-
list_signals_cb,
158+
ubus_cb,
213159
&out_str,
214160
2000);
215161
assert_int_equal(r, 0);
@@ -258,7 +204,7 @@ call_run_signal_w_static_output(void **state)
258204
id,
259205
"run_signal",
260206
b.head,
261-
run_signal_w_static_output_cb,
207+
ubus_cb,
262208
&out_str,
263209
2000);
264210
assert_int_equal(r, 0);
@@ -289,7 +235,7 @@ call_run_missing_signal(void **state)
289235
id,
290236
"run_signal",
291237
b.head,
292-
run_missing_signal_cb,
238+
ubus_cb,
293239
&out_str,
294240
2000);
295241
assert_int_equal(r, 0);
@@ -323,7 +269,7 @@ call_run_signal_w_args_return_named_arg(void **state)
323269
id,
324270
"run_signal",
325271
b.head,
326-
run_signal_w_args_return_named_arg_cb,
272+
ubus_cb,
327273
&out_str,
328274
2000);
329275
assert_int_equal(r, 0);
@@ -353,7 +299,7 @@ call_run_signal_w_id_missing(void **state)
353299
id,
354300
"run_signal",
355301
b.head,
356-
run_signal_w_id_missing_cb,
302+
ubus_cb,
357303
&out_str,
358304
2000);
359305
assert_int_equal(r, 0);
@@ -366,6 +312,87 @@ call_run_signal_w_id_missing(void **state)
366312
free(out_str);
367313
}
368314

315+
static void
316+
signal_match_cb(umplg_sh_t *shd, void *args)
317+
{
318+
shd->min_auth_lvl = shd->min_auth_lvl ? 0 : 1;
319+
}
320+
321+
static void
322+
call_run_signal_w_sufficient_authentication_level(void **state)
323+
{
324+
uint32_t id;
325+
326+
// look for umink object
327+
int r = ubus_lookup_id(ctx, "umink", &id);
328+
assert_int_equal(r, 0);
329+
330+
blob_buf_init(&b, 0);
331+
blobmsg_add_u32(&b, "id", id);
332+
blobmsg_add_json_from_string(
333+
&b,
334+
"{\"id\": \"TEST_EVENT_01\", \"auth\": \"{\\\"flags\\\": 0}\"}");
335+
char *out_str = NULL;
336+
337+
r = ubus_invoke(ctx,
338+
id,
339+
"run_signal",
340+
b.head,
341+
ubus_cb,
342+
&out_str,
343+
2000);
344+
assert_int_equal(r, 0);
345+
assert_non_null(out_str);
346+
347+
// get result from ubus call
348+
assert_string_equal(out_str, "{\"result\":\"test_data\"}");
349+
350+
// free
351+
free(out_str);
352+
}
353+
354+
static void
355+
call_run_signal_w_insufficient_authentication_level(void **state)
356+
{
357+
// get pm
358+
test_t *data = *state;
359+
assert_non_null(data);
360+
umplg_mngr_t *m = data->m;
361+
362+
uint32_t id;
363+
364+
// set min user role level for signal to 1 (admin)
365+
umplg_match_signal(m, "TEST_EVENT_01", &signal_match_cb, NULL);
366+
367+
// look for umink object
368+
int r = ubus_lookup_id(ctx, "umink", &id);
369+
assert_int_equal(r, 0);
370+
371+
blob_buf_init(&b, 0);
372+
blobmsg_add_u32(&b, "id", id);
373+
blobmsg_add_json_from_string(
374+
&b,
375+
"{\"id\": \"TEST_EVENT_01\", \"auth\": \"{\\\"flags\\\": 0}\"}");
376+
char *out_str = NULL;
377+
378+
r = ubus_invoke(ctx,
379+
id,
380+
"run_signal",
381+
b.head,
382+
ubus_cb,
383+
&out_str,
384+
2000);
385+
assert_int_equal(r, 0);
386+
assert_non_null(out_str);
387+
388+
// get result from ubus call
389+
assert_string_equal(out_str, "{\"result\":\"authentication error\"}");
390+
391+
// free
392+
free(out_str);
393+
}
394+
395+
369396
int
370397
main(int argc, char **argv)
371398
{
@@ -377,6 +404,8 @@ main(int argc, char **argv)
377404
cmocka_unit_test(call_run_missing_signal),
378405
cmocka_unit_test(call_run_signal_w_id_missing),
379406
cmocka_unit_test(call_run_signal_w_args_return_named_arg),
407+
cmocka_unit_test(call_run_signal_w_sufficient_authentication_level),
408+
cmocka_unit_test(call_run_signal_w_insufficient_authentication_level),
380409
};
381410

382411
int r = cmocka_run_group_tests(tests, run_init, run_dtor);

0 commit comments

Comments
 (0)