Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <libubus.h>
#include <libubox/blobmsg_json.h>
#include <libubox/blobmsg.h>
#include <json-validator.h>
#include <json-c/json.h>
#include <json-editor.h>
#include <json-c/json_tokener.h>
#include <easy/easy.h>
#include <qos.h>
#include <qosmngr.h>
struct test_ctx {
struct blob_buf bb;
struct ubus_object qos;
FILE *fp;
};
/* declare qosmngr functions */
int wl_radio_status(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int wl_radio_get_param(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int sta_disconnect(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int nbr_transition(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int nbr_request(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int nbr_add(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int wps_start(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int wps_stop(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int vsie_add(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *ureq, const char *method,
struct blob_attr *msg);
int vsie_del(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *ureq, const char *method,
struct blob_attr *msg);
int nbr_del(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int sta_monitor(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int wl_scan(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int wl_autochannel(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int wps_set_ap_pin(struct ubus_context *ctx, struct ubus_object *obj,
struct ubus_request_data *req, const char *method,
struct blob_attr *msg);
int qosmngr_event_main(const char *evmap_file);
/* overload ubus_send_reply to prevent segfault*/
int ubus_send_reply(struct ubus_context *ctx, struct ubus_request_data *req,
struct blob_attr *msg)
{
return 0;
}
/* TODO: how to fix events without fopen and fclose every poll? */
struct json_object *poll_test_log(FILE *fp, const char *prefix)
{
char line[256] = {0};
char msg[256] = {0};
struct json_object *obj = NULL;
//if (!fp) {
fp = fopen("/tmp/test.log", "r");
if (!fp)
return NULL;
//}
while (fgets(line, 256, fp)) {
char *ptr, *s;
char appended[32];
snprintf(appended, sizeof(appended), "%s:", prefix);
ptr = strstr(line, appended);
if (!ptr)
continue;
s = multi_tok(ptr, appended);
s = multi_tok(NULL, appended);
strncpy(msg, s, sizeof(msg));
}
if (strlen(msg))
obj = json_tokener_parse(msg);
fclose(fp);
return obj;
}
static int group_setup(void **state)
{
struct test_ctx *ctx = calloc(1, sizeof(struct test_ctx));
if (!ctx)
return -1;
remove("/tmp/test.log");
ctx->qos.name = "qos";
memset(&ctx->bb, 0, sizeof(struct blob_buf));
*state = ctx;
return 0;
}
static void test_api_get_stats(void **state)
{
struct test_ctx *ctx = (struct test_ctx *) *state;
struct blob_buf *bb = &ctx->bb;
struct ubus_object *obj = &ctx->qos;
struct json_object *jobj, *tmp;
blobmsg_add_string(bb, "ifname", "test");
tmp = json_object_get_by_string(jobj, "iface");
assert_string_equal(json_object_get_string(tmp), "test");
json_object_put(jobj);
return;
}
static int group_teardown(void **state)
{
struct test_ctx *ctx = (struct test_ctx *) *state;
blob_buf_free(&ctx->bb);
free(ctx);
remove("/tmp/test.log");
/* TODO: fix event poll file */
//if (ctx->fp)
//fclose(ctx->fp);
return 0;
}
static int setup(void **state)
{
struct test_ctx *ctx = (struct test_ctx *) *state;
blob_buf_init(&ctx->bb, 0);
return 0;
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test_setup(test_api_get_stats, setup),
};
return cmocka_run_group_tests(tests, group_setup, group_teardown);
}