Skip to content

Commit da240b6

Browse files
committed
tests: convert files test to NUTS
1 parent f02ba32 commit da240b6

File tree

4 files changed

+280
-256
lines changed

4 files changed

+280
-256
lines changed

src/platform/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ nng_directory(platform)
1313
add_subdirectory(posix)
1414
add_subdirectory(windows)
1515

16+
nng_test(files_test)
1617
nng_test(ipc_stream_test)
1718
nng_test(platform_test)
1819
nng_test(resolver_test)

src/platform/files_test.c

+279
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,279 @@
1+
//
2+
// Copyright 2024 Staysail Systems, Inc. <info@staysail.tech>
3+
// Copyright 2018 Capitar IT Group BV <info@capitar.com>
4+
//
5+
// This software is supplied under the terms of the MIT License, a
6+
// copy of which should be located in the distribution where this
7+
// file was obtained (LICENSE.txt). A copy of the license may also be
8+
// found online at https://opensource.org/licenses/MIT.
9+
//
10+
11+
#include "core/nng_impl.h"
12+
#include <nuts.h>
13+
14+
#include <stdio.h>
15+
#include <string.h>
16+
17+
#ifdef NNG_PLATFORM_POSIX
18+
#include <sys/stat.h>
19+
#include <unistd.h>
20+
#endif
21+
22+
static void
23+
test_permissions(void)
24+
{
25+
#ifdef NNG_PLATFORM_POSIX
26+
char *temp;
27+
char *file;
28+
void *data;
29+
size_t n;
30+
temp = nni_plat_temp_dir();
31+
NUTS_TRUE(temp != NULL);
32+
file = nni_file_join(temp, "nng_files_perms_test");
33+
if (geteuid() == 0) {
34+
NUTS_SKIP("Cannot test permissions as root");
35+
return;
36+
}
37+
NUTS_PASS(nni_file_put(file, "abc", 4));
38+
chmod(file, 0);
39+
NUTS_FAIL(nni_file_get(file, &data, &n), NNG_EPERM);
40+
NUTS_FAIL(nni_file_put(file, "def", 4), NNG_EPERM);
41+
nni_file_delete(file);
42+
nni_strfree(file);
43+
nni_strfree(temp);
44+
#else
45+
NUTS_SKIP("Not a POSIX platform");
46+
#endif
47+
}
48+
49+
struct walkarg {
50+
int a;
51+
int b;
52+
int c;
53+
int d;
54+
int seen;
55+
};
56+
57+
static int
58+
walker(const char *name, void *arg)
59+
{
60+
struct walkarg *wa = arg;
61+
const char *bn;
62+
63+
bn = nni_file_basename(name);
64+
if (wa != NULL) {
65+
wa->seen++;
66+
if (strcmp(bn, "a") == 0) {
67+
wa->a++;
68+
} else if (strcmp(bn, "b") == 0) {
69+
wa->b++;
70+
} else if (strcmp(bn, "c") == 0) {
71+
wa->c++;
72+
} else if (strcmp(bn, "d") == 0) {
73+
wa->d++;
74+
}
75+
}
76+
if (strcmp(bn, "stop") == 0) {
77+
return (NNI_FILE_WALK_STOP);
78+
}
79+
if (strcmp(bn, "prunechild") == 0) {
80+
return (NNI_FILE_WALK_PRUNE_CHILD);
81+
}
82+
if (strcmp(bn, "prunesib") == 0) {
83+
return (NNI_FILE_WALK_PRUNE_SIB);
84+
}
85+
return (NNI_FILE_WALK_CONTINUE);
86+
}
87+
88+
static void
89+
test_directory_names(void)
90+
{
91+
char *d;
92+
93+
NUTS_TRUE((d = nni_plat_temp_dir()) != NULL);
94+
nni_strfree(d);
95+
96+
NUTS_TRUE((d = nni_file_join("a", "b")) != NULL);
97+
NUTS_TRUE(d[0] == 'a');
98+
NUTS_TRUE(d[2] == 'b');
99+
NUTS_TRUE(d[3] == '\0');
100+
NUTS_TRUE((d[1] == '/') || (d[1] == '\\'));
101+
nni_strfree(d);
102+
}
103+
104+
static void
105+
test_create_file_in_absent_dir(void)
106+
{
107+
int rv;
108+
char *tmp;
109+
char *d1;
110+
char *d2;
111+
NUTS_TRUE((tmp = nni_plat_temp_dir()) != NULL);
112+
NUTS_TRUE((d1 = nni_file_join(tmp, "bogusdir")) != NULL);
113+
NUTS_TRUE((d2 = nni_file_join(d1, "a")) != NULL);
114+
NUTS_TRUE((rv = nni_plat_file_put(d2, "", 0)) == 0);
115+
NUTS_PASS(nni_file_delete(d2));
116+
NUTS_PASS(nni_file_delete(d1));
117+
nni_strfree(d2);
118+
nni_strfree(d1);
119+
nni_strfree(tmp);
120+
}
121+
122+
static void
123+
test_cannot_read_missing_file(void)
124+
{
125+
void *data;
126+
size_t n;
127+
NUTS_FAIL(nni_file_get("/bogus/dir/a", &data, &n), NNG_ENOENT);
128+
}
129+
130+
static void
131+
test_delete_missing_file(void)
132+
{
133+
NUTS_PASS(nni_file_delete("/bogus/dir/a"));
134+
}
135+
136+
static void
137+
test_walk_missing_dir(void)
138+
{
139+
NUTS_FAIL(nni_file_walk("/bogus/dir/a", walker, NULL, 0), NNG_ENOENT);
140+
}
141+
142+
struct scratch {
143+
char *temp;
144+
char *mydir;
145+
char *a;
146+
char *b;
147+
char *c;
148+
char *d;
149+
};
150+
151+
static void
152+
make_scratch(struct scratch *s)
153+
{
154+
s->temp = nni_plat_temp_dir();
155+
NUTS_TRUE(s->temp != NULL);
156+
s->mydir = nni_file_join(s->temp, "nng_files_test");
157+
NUTS_TRUE(s->mydir != NULL);
158+
s->a = nni_file_join(s->mydir, "a");
159+
NUTS_TRUE(s->a != NULL);
160+
s->b = nni_file_join(s->mydir, "b");
161+
NUTS_TRUE(s->b != NULL);
162+
s->c = nni_file_join(s->mydir, "c");
163+
NUTS_TRUE(s->c != NULL);
164+
s->d = nni_file_join(s->c, "d");
165+
NUTS_TRUE(s->d != NULL);
166+
167+
NUTS_PASS(nni_file_put(s->a, "alpha", 6));
168+
NUTS_PASS(nni_file_put(s->b, "bravo", 6));
169+
NUTS_PASS(nni_file_put(s->d, "delta", 6));
170+
}
171+
172+
static void
173+
clean_scratch(struct scratch *s)
174+
{
175+
nni_strfree(s->temp);
176+
nni_file_delete(s->a);
177+
nni_file_delete(s->b);
178+
nni_file_delete(s->d);
179+
nni_file_delete(s->c);
180+
nni_file_delete(s->mydir);
181+
nni_strfree(s->a);
182+
nni_strfree(s->b);
183+
nni_strfree(s->c);
184+
nni_strfree(s->d);
185+
nni_strfree(s->mydir);
186+
}
187+
188+
static void
189+
test_directory_walk(void)
190+
{
191+
struct scratch s = { 0 };
192+
struct walkarg wa = { 0 };
193+
194+
make_scratch(&s);
195+
memset(&wa, 0, sizeof(wa));
196+
NUTS_PASS(nni_file_walk(s.mydir, walker, &wa, 0));
197+
NUTS_TRUE(wa.a == 1);
198+
NUTS_TRUE(wa.b == 1);
199+
NUTS_TRUE(wa.c == 1);
200+
NUTS_TRUE(wa.d == 1);
201+
NUTS_TRUE(wa.seen == 4);
202+
203+
memset(&wa, 0, sizeof(wa));
204+
NUTS_PASS(
205+
nni_file_walk(s.mydir, walker, &wa, NNI_FILE_WALK_FILES_ONLY));
206+
NUTS_TRUE(wa.a == 1);
207+
NUTS_TRUE(wa.b == 1);
208+
NUTS_TRUE(wa.c == 0);
209+
NUTS_TRUE(wa.d == 1);
210+
NUTS_TRUE(wa.seen == 3);
211+
212+
memset(&wa, 0, sizeof(wa));
213+
NUTS_PASS(nni_file_walk(s.mydir, walker, &wa, NNI_FILE_WALK_SHALLOW));
214+
NUTS_TRUE(wa.a == 1);
215+
NUTS_TRUE(wa.b == 1);
216+
NUTS_TRUE(wa.c == 1);
217+
NUTS_TRUE(wa.d == 0);
218+
NUTS_TRUE(wa.seen == 3);
219+
220+
memset(&wa, 0, sizeof(wa));
221+
NUTS_PASS(nni_file_walk(s.mydir, walker, &wa,
222+
NNI_FILE_WALK_SHALLOW | NNI_FILE_WALK_FILES_ONLY));
223+
NUTS_TRUE(wa.a == 1);
224+
NUTS_TRUE(wa.b == 1);
225+
NUTS_TRUE(wa.c == 0);
226+
NUTS_TRUE(wa.d == 0);
227+
NUTS_TRUE(wa.seen == 2);
228+
229+
clean_scratch(&s);
230+
}
231+
232+
static void
233+
test_file_contents(void)
234+
{
235+
void *data;
236+
size_t len;
237+
struct scratch s = { 0 };
238+
239+
make_scratch(&s);
240+
241+
NUTS_PASS(nni_file_get(s.a, &data, &len));
242+
NUTS_TRUE(len == 6);
243+
NUTS_MATCH(data, "alpha");
244+
nni_free(data, len);
245+
clean_scratch(&s);
246+
}
247+
248+
static void
249+
test_empty_files(void)
250+
{
251+
char *temp;
252+
char *empty;
253+
void *data;
254+
size_t n;
255+
temp = nni_plat_temp_dir();
256+
NUTS_TRUE(temp != NULL);
257+
empty = nni_file_join(temp, "nng_files_test1");
258+
NUTS_TRUE(empty != NULL);
259+
NUTS_PASS(nni_file_put(empty, "", 0));
260+
NUTS_PASS(nni_file_get(empty, &data, &n));
261+
nni_free(data, n);
262+
NUTS_TRUE(n == 0);
263+
nni_file_delete(empty);
264+
nni_strfree(empty);
265+
nni_strfree(temp);
266+
}
267+
268+
NUTS_TESTS = {
269+
{ "permissions", test_permissions },
270+
{ "directory names", test_directory_names },
271+
{ "create file absent dir", test_create_file_in_absent_dir },
272+
{ "cannot read missing file", test_cannot_read_missing_file },
273+
{ "delete missing file", test_delete_missing_file },
274+
{ "walk missing dir", test_walk_missing_dir },
275+
{ "walk directory", test_directory_walk },
276+
{ "file contents", test_file_contents },
277+
{ "empty files", test_empty_files },
278+
{ NULL, NULL },
279+
};

tests/CMakeLists.txt

-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,6 @@ else ()
124124
endmacro(add_nng_test3)
125125
endif ()
126126

127-
add_nng_test(files 5)
128127
add_nng_test1(httpclient 60 NNG_SUPP_HTTP)
129128
add_nng_test1(httpserver 30 NNG_SUPP_HTTP)
130129
add_nng_test(wss 30)

0 commit comments

Comments
 (0)