]> git.ipfire.org Git - people/ms/libloc.git/commitdiff
test-database: Check opening files with no or random data
authorMichael Tremer <michael.tremer@ipfire.org>
Sat, 16 May 2020 10:59:26 +0000 (10:59 +0000)
committerMichael Tremer <michael.tremer@ipfire.org>
Sat, 16 May 2020 10:59:26 +0000 (10:59 +0000)
Signed-off-by: Michael Tremer <michael.tremer@ipfire.org>
src/test-database.c

index 506a8bb7b9f9b36bc57264362af8a2b07855144a..dfba4c258c55c9c2409770b5ecdd8662fb47d2cf 100644 (file)
@@ -37,6 +37,27 @@ const char* DESCRIPTION =
        "Maecenas ut venenatis nunc.";
 const char* LICENSE = "CC";
 
+static int attempt_to_open(struct loc_ctx* ctx, char* path) {
+       FILE* f = fopen(path, "r");
+       if (!f)
+               return -1;
+
+       struct loc_database* db;
+       int r = loc_database_new(ctx, &db, f);
+
+       if (r == 0) {
+               fprintf(stderr, "Opening %s was unexpectedly successful\n", path);
+               loc_database_unref(db);
+
+               r = 1;
+       }
+
+       // Close the file again
+       fclose(f);
+
+       return r;
+}
+
 int main(int argc, char** argv) {
        int err;
 
@@ -45,6 +66,21 @@ int main(int argc, char** argv) {
        if (err < 0)
                exit(EXIT_FAILURE);
 
+       // Try opening an empty file
+       err = attempt_to_open(ctx, "/dev/null");
+       if (err == 0)
+               exit(EXIT_FAILURE);
+
+       // Try opening a file with all zeroes
+       err = attempt_to_open(ctx, "/dev/zero");
+       if (err == 0)
+               exit(EXIT_FAILURE);
+
+       // Try opening a file with random data
+       err = attempt_to_open(ctx, "/dev/urandom");
+       if (err == 0)
+               exit(EXIT_FAILURE);
+
        // Create a database
        struct loc_writer* writer;
        err = loc_writer_new(ctx, &writer, NULL);