forked from canonical/mini-iso-tools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.c
172 lines (144 loc) · 4.25 KB
/
tests.c
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
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <string.h>
#include <json-c/json.h>
#include "json.h"
json_object *find_obj_of_biggest_key(json_object *obj);
static void find_largest_NULL(void **state)
{
assert_null(find_obj_of_biggest_key(NULL));
}
static void find_largest_simple(void **state)
{
json_object *root = json_tokener_parse("{'a': 1, 'b': 2, 'c': 3}");
assert_non_null(root);
json_object *largest = find_obj_of_biggest_key(root);
assert_non_null(largest);
assert_int_equal(3, json_object_get_int(largest));
}
static void find_largest_reversed(void **state)
{
json_object *root = json_tokener_parse("{'c': 3, 'b': 2, 'a': 1}");
assert_non_null(root);
json_object *largest = find_obj_of_biggest_key(root);
assert_non_null(largest);
assert_int_equal(3, json_object_get_int(largest));
}
static void read_NULL(void **state)
{
assert_null(read_iso_choices(NULL));
}
static void read_not_exist(void **state)
{
assert_null(read_iso_choices("/not/exist"));
}
static void read_empty_obj(void **state)
{
assert_null(read_iso_choices("test/emtpy-obj.json"));
}
static void read_ubuntu_server(void **state)
{
choices_t *choices = read_iso_choices("test/ubuntu-server.json");
assert_non_null(choices);
assert_int_equal(2, choices->len);
iso_data_t *first = choices->values[0];
assert_string_equal("Ubuntu Server 22.10 (Kinetic Kudu)", first->label);
assert_string_equal(
"https://releases.ubuntu.com/kinetic/ubuntu-22.10-live-server-amd64.iso",
first->url);
assert_string_equal(
"874452797430a94ca240c95d8503035aa145bd03ef7d84f9b23b78f3c5099aed",
first->sha256sum);
assert_int_equal(1642631168, first->size);
iso_data_t *second = choices->values[1];
assert_string_equal("Ubuntu Server 23.04 (Lunar Lobster)", second->label);
assert_string_equal(
"https://cdimage.ubuntu.com/ubuntu-server/daily-live/20230122/lunar-live-server-amd64.iso",
second->url);
assert_string_equal(
"b67e566f6b7ff5d314173a2b55bb413cf4ab2b1b94c59f1ff8b65b862c1d7de7",
second->sha256sum);
assert_int_equal(1762381824, second->size);
}
static void eq_good(void **state)
{
assert_true(eq("a", "a"));
}
static void eq_bad(void **state)
{
assert_false(eq("a", "b"));
}
static void eq_NULL(void **state)
{
assert_null(eq(NULL, "a"));
assert_null(eq("a", NULL));
assert_null(eq(NULL, NULL));
}
static void lt_good(void **state)
{
assert_true(lt("a", "b"));
}
static void lt_bad(void **state)
{
assert_false(lt("a", "a"));
}
static void lt_NULL(void **state)
{
assert_false(lt(NULL, "a"));
assert_false(lt("a", NULL));
assert_false(lt(NULL, NULL));
}
static void get_good(void **state)
{
json_object *root = json_tokener_parse("{'key': 'value'}");
assert_non_null(get(root, "key"));
}
static void get_obj_NULL(void **state)
{
assert_null(get(NULL, "key"));
}
static void get_NULL(void **state)
{
assert_null(get(NULL, NULL));
}
static void get_key_NULL(void **state)
{
json_object *root = json_tokener_parse("{'key': 'value'}");
assert_null(get(root, NULL));
}
static void str_good(void **state)
{
json_object *root = json_tokener_parse("{'key': 'value'}");
assert_string_equal("value", str(get(root, "key")));
}
static void str_NULL(void **state)
{
assert_null(str(NULL));
}
int main(void)
{
const struct CMUnitTest tests[] = {
cmocka_unit_test(find_largest_NULL),
cmocka_unit_test(find_largest_simple),
cmocka_unit_test(find_largest_reversed),
cmocka_unit_test(read_NULL),
cmocka_unit_test(read_not_exist),
cmocka_unit_test(read_empty_obj),
cmocka_unit_test(read_ubuntu_server),
cmocka_unit_test(eq_good),
cmocka_unit_test(eq_bad),
cmocka_unit_test(eq_NULL),
cmocka_unit_test(lt_good),
cmocka_unit_test(lt_bad),
cmocka_unit_test(lt_NULL),
cmocka_unit_test(get_good),
cmocka_unit_test(get_NULL),
cmocka_unit_test(get_obj_NULL),
cmocka_unit_test(get_key_NULL),
cmocka_unit_test(str_good),
cmocka_unit_test(str_NULL),
};
return cmocka_run_group_tests(tests, NULL, NULL);
}