aboutsummaryrefslogtreecommitdiff
path: root/tests/files.c
diff options
context:
space:
mode:
authorGarrett D'Amore <garrett@damore.org>2018-01-10 16:14:54 -0800
committerGarrett D'Amore <garrett@damore.org>2018-01-11 06:42:19 -0800
commit3dd4cbf8efcfd574e2244798a86edd2f10c9cb45 (patch)
treea5087862282f1ed950cf3daf3c3e45078b9e4216 /tests/files.c
parent282da09430fc39d8f93d78b828d3653e95318255 (diff)
downloadnng-3dd4cbf8efcfd574e2244798a86edd2f10c9cb45.tar.gz
nng-3dd4cbf8efcfd574e2244798a86edd2f10c9cb45.tar.bz2
nng-3dd4cbf8efcfd574e2244798a86edd2f10c9cb45.zip
Refactored file API.
This refactor of the file API provides a simpler and easier to use interface for our needs (and simpler to implement) in both the ZeroTier transport and the HTTP/TLS file accesses. It also removes some restrictions present on the old one, although it is still not suitable for working with large files. (It will work, just be very inefficient as the entire file must be loaded into memory.)
Diffstat (limited to 'tests/files.c')
-rw-r--r--tests/files.c207
1 files changed, 133 insertions, 74 deletions
diff --git a/tests/files.c b/tests/files.c
index 6e9e5564..3b21ac65 100644
--- a/tests/files.c
+++ b/tests/files.c
@@ -1,6 +1,6 @@
//
-// Copyright 2017 Garrett D'Amore <garrett@damore.org>
-// Copyright 2017 Capitar IT Group BV <info@capitar.com>
+// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2018 Capitar IT Group BV <info@capitar.com>
//
// This software is supplied under the terms of the MIT License, a
// copy of which should be located in the distribution where this
@@ -32,24 +32,63 @@ test_permissions(void)
size_t n;
temp = nni_plat_temp_dir();
So(temp != NULL);
- file = nni_plat_join_dir(temp, "nng_files_perms_test");
+ file = nni_file_join(temp, "nng_files_perms_test");
if (geteuid() == 0) {
ConveySkip("Cannot test permissions as root");
}
- So(nni_plat_file_put(file, "abc", 4) == 0);
+ So(nni_file_put(file, "abc", 4) == 0);
Reset({
- nni_plat_file_delete(file);
+ nni_file_delete(file);
nni_strfree(file);
nni_strfree(temp);
});
chmod(file, 0);
- So((rv = nni_plat_file_get(file, &data, &n)) != 0);
+ So((rv = nni_file_get(file, &data, &n)) != 0);
So(rv == NNG_EPERM);
- So(nni_plat_file_put(file, "def", 4) == NNG_EPERM);
+ So(nni_file_put(file, "def", 4) == NNG_EPERM);
});
#endif
}
+struct walkarg {
+ int a;
+ int b;
+ int c;
+ int d;
+ int seen;
+};
+
+static int
+walker(const char *name, void *arg)
+{
+ struct walkarg *wa = arg;
+ const char * bn;
+
+ bn = nni_file_basename(name);
+ if (wa != NULL) {
+ wa->seen++;
+ if (strcmp(bn, "a") == 0) {
+ wa->a++;
+ } else if (strcmp(bn, "b") == 0) {
+ wa->b++;
+ } else if (strcmp(bn, "c") == 0) {
+ wa->c++;
+ } else if (strcmp(bn, "d") == 0) {
+ wa->d++;
+ }
+ }
+ if (strcmp(bn, "stop") == 0) {
+ return (NNI_FILE_WALK_STOP);
+ }
+ if (strcmp(bn, "prunechild") == 0) {
+ return (NNI_FILE_WALK_PRUNE_CHILD);
+ }
+ if (strcmp(bn, "prunesib") == 0) {
+ return (NNI_FILE_WALK_PRUNE_SIB);
+ }
+ return (NNI_FILE_WALK_CONTINUE);
+}
+
TestMain("Platform File Support", {
Convey("Directory names work", {
char *d;
@@ -60,7 +99,7 @@ TestMain("Platform File Support", {
So((d = nni_plat_home_dir()) != NULL);
nni_strfree(d);
- So((d = nni_plat_join_dir("a", "b")) != NULL);
+ So((d = nni_file_join("a", "b")) != NULL);
So(d[0] == 'a');
So(d[2] == 'b');
So(d[3] == '\0');
@@ -68,45 +107,40 @@ TestMain("Platform File Support", {
nni_strfree(d);
});
- Convey("Cannot create file in non-extant directory", {
- int rv;
- So((rv = nni_plat_file_put("/bogus/dir/a", "", 0)) != 0);
- So(rv == NNG_ENOENT);
+ Convey("Can create file in non-existent directory", {
+ int rv;
+ char *tmp;
+ char *d1;
+ char *d2;
+ So((tmp = nni_plat_temp_dir()) != NULL);
+ So((d1 = nni_file_join(tmp, "bogusdir")) != NULL);
+ So((d2 = nni_file_join(d1, "a")) != NULL);
+ So((rv = nni_plat_file_put(d2, "", 0)) == 0);
+ So(nni_file_delete(d2) == 0);
+ So(nni_file_delete(d1) == 0);
+ nni_strfree(d2);
+ nni_strfree(d1);
+ nni_strfree(tmp);
});
Convey("Cannot read missing file", {
int rv;
void * data;
size_t n;
- So((rv = nni_plat_file_get("/bogus/dir/a", &data, &n)) != 0);
- So(rv == NNG_ENOENT);
- });
- Convey("Cannot delete missing file", {
- int rv;
- So((rv = nni_plat_file_delete("/bogus/dir/a")) != 0);
- So(rv == NNG_ENOENT);
- });
- Convey("Cannot open missing directory", {
- int rv;
- void *dir;
- So((rv = nni_plat_dir_open(
- &dir, "/bogus/nng_does_not_exist")) != 0);
+ So((rv = nni_file_get("/bogus/dir/a", &data, &n)) != 0);
So(rv == NNG_ENOENT);
});
- Convey("Cannot create directory in non-existing subdir", {
- int rv;
- So((rv = nni_plat_dir_create(
- "/bogus/nng_does_not_exist/subdir")) != 0);
+
+ Convey("Delete of missing file passes",
+ { So(nni_file_delete("/bogus/dir/a") == 0); });
+
+ Convey("Walk of missing directory fails", {
+ int rv = nni_file_walk("/bogus/dir/a", walker, NULL, 0);
So(rv == NNG_ENOENT);
});
+
Convey("Remove missing directory works",
- { So(nni_plat_dir_remove("/bogus/nng_does_not_exist") == 0); });
+ { So(nni_file_delete("/bogus/nng_does_not_exist") == 0); });
- Convey("Create existing directory works", {
- char *tmp;
- tmp = nni_plat_temp_dir();
- So(nni_plat_dir_create(tmp) == 0);
- nni_strfree(tmp);
- });
Convey("We can create a pair of files", {
char *temp;
@@ -114,62 +148,86 @@ TestMain("Platform File Support", {
char *a;
char *b;
char *c;
+ char *d;
temp = nni_plat_temp_dir();
So(temp != NULL);
- mydir = nni_plat_join_dir(temp, "nng_files_test");
+ mydir = nni_file_join(temp, "nng_files_test");
So(mydir != NULL);
- a = nni_plat_join_dir(mydir, "a");
+ a = nni_file_join(mydir, "a");
So(a != NULL);
- b = nni_plat_join_dir(mydir, "b");
+ b = nni_file_join(mydir, "b");
So(b != NULL);
- c = nni_plat_join_dir(mydir, "c");
+ c = nni_file_join(mydir, "c");
So(c != NULL);
+ d = nni_file_join(c, "d");
+ So(d != NULL);
- So(nni_plat_dir_create(mydir) == 0);
- So(nni_plat_file_put(a, "alpha", 6) == 0);
- So(nni_plat_file_put(b, "bravo", 6) == 0);
+ So(nni_file_put(a, "alpha", 6) == 0);
+ So(nni_file_put(b, "bravo", 6) == 0);
+ So(nni_file_put(d, "delta", 6) == 0);
Reset({
nni_strfree(temp);
- nni_plat_file_delete(a);
- nni_plat_file_delete(b);
+ nni_file_delete(a);
+ nni_file_delete(b);
+ nni_file_delete(d);
+ nni_file_delete(c);
+ nni_file_delete(mydir);
nni_strfree(a);
nni_strfree(b);
nni_strfree(c);
- nni_plat_dir_remove(mydir);
+ nni_strfree(d);
nni_strfree(mydir);
});
- Convey("Directory list works", {
- int seen_a = 0;
- int seen_b = 0;
- int seen_what = 0;
- int rv;
- void * dirh;
- const char *name;
-
- So(nni_plat_dir_open(&dirh, mydir) == 0);
- while ((rv = nni_plat_dir_next(dirh, &name)) == 0) {
- if (strcmp(name, "a") == 0) {
- seen_a++;
- } else if (strcmp(name, "b") == 0) {
- seen_b++;
- } else {
- seen_what++;
- }
- }
- So(rv == NNG_ENOENT);
- So(seen_a == 1);
- So(seen_b == 1);
- So(seen_what == 0);
- nni_plat_dir_close(dirh);
+ Convey("Directory walk works", {
+ struct walkarg wa = { 0 };
+ int rv;
+
+ rv = nni_file_walk(mydir, walker, &wa, 0);
+ So(rv == 0);
+ So(wa.a == 1);
+ So(wa.b == 1);
+ So(wa.c == 1);
+ So(wa.d == 1);
+ So(wa.seen == 4);
+
+ memset(&wa, 0, sizeof(wa));
+ rv = nni_file_walk(
+ mydir, walker, &wa, NNI_FILE_WALK_FILES_ONLY);
+ So(rv == 0);
+ So(wa.a == 1);
+ So(wa.b == 1);
+ So(wa.c == 0);
+ So(wa.d == 1);
+ So(wa.seen == 3);
+
+ memset(&wa, 0, sizeof(wa));
+ rv = nni_file_walk(
+ mydir, walker, &wa, NNI_FILE_WALK_SHALLOW);
+ So(rv == 0);
+ So(wa.a == 1);
+ So(wa.b == 1);
+ So(wa.c == 1);
+ So(wa.d == 0);
+ So(wa.seen == 3);
+
+ memset(&wa, 0, sizeof(wa));
+ rv = nni_file_walk(mydir, walker, &wa,
+ NNI_FILE_WALK_SHALLOW | NNI_FILE_WALK_FILES_ONLY);
+ So(rv == 0);
+ So(wa.a == 1);
+ So(wa.b == 1);
+ So(wa.c == 0);
+ So(wa.d == 0);
+ So(wa.seen == 2);
});
Convey("Contents work", {
void * data;
size_t len;
- So(nni_plat_file_get(a, &data, &len) == 0);
+ So(nni_file_get(a, &data, &len) == 0);
So(len == 6);
So(strcmp(data, "alpha") == 0);
nni_free(data, len);
@@ -183,15 +241,16 @@ TestMain("Platform File Support", {
size_t n;
temp = nni_plat_temp_dir();
So(temp != NULL);
- empty = nni_plat_join_dir(temp, "nng_files_test1");
+ empty = nni_file_join(temp, "nng_files_test1");
So(empty != NULL);
- So(nni_plat_file_put(empty, "", 0) == 0);
+ So(nni_file_put(empty, "", 0) == 0);
Reset({
- nni_plat_file_delete(empty);
+ nni_file_delete(empty);
nni_strfree(empty);
nni_strfree(temp);
});
- So(nni_plat_file_get(empty, &data, &n) == 0);
+ So(nni_file_get(empty, &data, &n) == 0);
+ nni_free(data, n);
So(n == 0);
});