/
testmain.c
280 lines (229 loc) · 7.54 KB
/
testmain.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include "user.h"
#include "sqlite3.h"
#include "CuTest.h"
struct sqlite3 *db = 0;
void setup() {
if (sqlite3_open("", &db) != SQLITE_OK) {
goto setuperror;
}
if (sqlite3_exec(db, "CREATE TABLE user (\n"
" uid integer not null primary key,\n"
" login text not null,\n"
" pass text not null,\n"
" fullname text not null default '',\n"
" email text not null default '',\n"
" unique(login),\n"
" unique(email)\n"
")", NULL, NULL, NULL) != SQLITE_OK) {
goto setuperror;
}
if (sqlite3_exec(db, "CREATE TABLE groups (\n"
" gid integer not null primary key,\n"
" name text not null,\n"
" unique(name)\n"
")", NULL, NULL, NULL) != SQLITE_OK) {
goto setuperror;
}
if (sqlite3_exec(db, "CREATE TABLE usergroup (\n"
" uid integer not null,\n"
" gid integer not null,\n"
" animal text not null default '',\n"
" primary key (uid, gid, animal)\n"
")", NULL, NULL, NULL) != SQLITE_OK) {
goto setuperror;
}
if (sqlite3_exec(db, "INSERT INTO user (uid, login, pass, fullname, email)\n"
"VALUES (1, 'bob', '$1$2$abc', 'Bob Smith', 'bob@email.org')",
NULL, NULL, NULL) != SQLITE_OK) {
goto setuperror;
}
if (sqlite3_exec(db, "INSERT INTO user (uid, login, pass, fullname, email)\n"
"VALUES (2, 'tom', '$1$3$def', 'Tom Jones', 'tom@smith.us')",
NULL, NULL, NULL) != SQLITE_OK) {
goto setuperror;
}
if (sqlite3_exec(db, "INSERT INTO groups (gid, name)\n"
"VALUES (1, 'admin')",
NULL, NULL, NULL) != SQLITE_OK) {
goto setuperror;
}
if (sqlite3_exec(db, "INSERT INTO groups (gid, name)\n"
"VALUES (2, 'user')",
NULL, NULL, NULL) != SQLITE_OK) {
goto setuperror;
}
if (sqlite3_exec(db, "INSERT INTO usergroup (uid, gid, animal)\n"
"VALUES (1, 2, 'first')",
NULL, NULL, NULL) != SQLITE_OK) {
goto setuperror;
}
if (sqlite3_exec(db, "INSERT INTO usergroup (uid, gid, animal)\n"
"VALUES (1, 1, 'first')",
NULL, NULL, NULL) != SQLITE_OK) {
goto setuperror;
}
if (sqlite3_exec(db, "INSERT INTO usergroup (uid, gid, animal)\n"
"VALUES (2, 2, 'first')",
NULL, NULL, NULL) != SQLITE_OK) {
goto setuperror;
}
if (sqlite3_exec(db, "INSERT INTO usergroup (uid, gid, animal)\n"
"VALUES (1, 2, 'second')",
NULL, NULL, NULL) != SQLITE_OK) {
goto setuperror;
}
return;
setuperror:
fprintf(stderr, "setup: %s\n", sqlite3_errmsg(db));
exit(1);
}
void teardown() {
if (db != NULL) {
sqlite3_close(db);
db = NULL;
}
}
void test_user_get_uid_retrieves_targetted_user(CuTest *tc)
{
int uid;
setup();
uid = user_get_uid("bob");
CuAssertIntEquals(tc, 1, uid);
teardown();
}
void test_user_get_uid_does_not_retrieve_invalid_user(CuTest *tc)
{
int uid;
setup();
uid = user_get_uid("noone");
CuAssertIntEquals(tc, -1, uid);
teardown();
}
void test_user_get_group_gid_retrieves_desired_group(CuTest *tc)
{
int gid;
setup();
gid = user_get_group_gid("admin", "first");
CuAssertIntEquals(tc, 1, gid);
teardown();
}
void test_user_get_group_gid_does_not_retrieve_invalid_group(CuTest *tc)
{
int gid;
setup();
gid = user_get_group_gid("nosuchgroup", "first");
CuAssertIntEquals(tc, -1, gid);
teardown();
}
void test_user_is_in_group_true_when_user_in_group(CuTest *tc)
{
int is_in_group;
setup();
is_in_group = user_is_in_group("bob", "user", "second");
CuAssertIntEquals(tc, 1, is_in_group);
teardown();
}
void test_user_is_in_group_false_when_user_not_in_group(CuTest *tc)
{
int is_in_group;
setup();
is_in_group = user_is_in_group("tom", "user", "second");
CuAssertIntEquals(tc, 0, is_in_group);
teardown();
}
void test_user_group_add_succeeds_for_valid_user_group(CuTest *tc)
{
int is_in_group;
setup();
user_set_group("bob", "admin", "second");
is_in_group = user_is_in_group("bob", "admin", "second");
CuAssertIntEquals(tc, 1, is_in_group);
teardown();
}
void test_user_group_add_fails_for_invalid_user(CuTest *tc)
{
int is_in_group;
setup();
user_set_group("frank", "admin", "second");
is_in_group = user_is_in_group("frank", "admin", "second");
CuAssertIntEquals(tc, 0, is_in_group);
teardown();
}
void test_user_groups_retrieves_users_groups(CuTest *tc)
{
char **groups;
setup();
groups = user_get_groups("bob", "first");
CuAssertPtrNotNull(tc, groups);
CuAssertStrEquals(tc, "admin", groups[0]);
CuAssertStrEquals(tc, "user", groups[1]);
/* This assertion ensures that groups come from a single
* animal.
*/
CuAssertPtrEquals(tc, NULL, groups[2]);
teardown();
}
void test_users_in_animal_is_not_null(CuTest *tc)
{
char **users;
setup();
users = users_in_animal("first");
CuAssertPtrNotNull(tc, users);
teardown();
}
void test_user_in_animal_returns_two_users_for_first_animal(CuTest *tc)
{
char **users;
setup();
users = users_in_animal("first");
CuAssertPtrNotNull(tc, users[0]);
CuAssertPtrNotNull(tc, users[1]);
CuAssertPtrEquals(tc, NULL, users[2]);
teardown();
}
void test_user_get_record_returns_a_record(CuTest *tc)
{
struct user_record *rec;
setup();
rec = user_get_record("tom");
CuAssertPtrNotNull(tc, rec);
teardown();
}
void test_user_get_record_returns_requested_user(CuTest *tc)
{
struct user_record *rec;
setup();
rec = user_get_record("tom");
CuAssertIntEquals(tc, 2, rec->uid);
CuAssertStrEquals(tc, "tom", rec->login);
CuAssertStrEquals(tc, "$1$3$def", rec->password);
CuAssertStrEquals(tc, "Tom Jones", rec->name);
CuAssertStrEquals(tc, "tom@smith.us", rec->email);
teardown();
}
int main(int argc, char **argv)
{
CuString *output = CuStringNew();
CuSuite *suite = CuSuiteNew();
SUITE_ADD_TEST(suite, test_user_get_uid_retrieves_targetted_user);
SUITE_ADD_TEST(suite, test_user_get_uid_does_not_retrieve_invalid_user);
SUITE_ADD_TEST(suite, test_user_get_group_gid_retrieves_desired_group);
SUITE_ADD_TEST(suite, test_user_get_group_gid_does_not_retrieve_invalid_group);
SUITE_ADD_TEST(suite, test_user_is_in_group_true_when_user_in_group);
SUITE_ADD_TEST(suite, test_user_is_in_group_false_when_user_not_in_group);
SUITE_ADD_TEST(suite, test_user_group_add_succeeds_for_valid_user_group);
SUITE_ADD_TEST(suite, test_user_group_add_fails_for_invalid_user);
SUITE_ADD_TEST(suite, test_user_groups_retrieves_users_groups);
SUITE_ADD_TEST(suite, test_users_in_animal_is_not_null);
SUITE_ADD_TEST(suite, test_user_in_animal_returns_two_users_for_first_animal);
SUITE_ADD_TEST(suite, test_user_get_record_returns_a_record);
SUITE_ADD_TEST(suite, test_user_get_record_returns_requested_user);
CuSuiteRun(suite);
CuSuiteSummary(suite, output);
CuSuiteDetails(suite, output);
printf("%s\n", output->buffer);
return suite->failCount;
}