]> git.ipfire.org Git - location/libloc.git/blame - src/test-database.c
systemd: Rename location-downloader to location-update
[location/libloc.git] / src / test-database.c
CommitLineData
2601e83e
MT
1/*
2 libloc - A library to determine the location of someone on the Internet
3
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15*/
16
17#include <stdio.h>
18#include <stddef.h>
19#include <stdlib.h>
20#include <string.h>
21#include <fcntl.h>
22#include <ctype.h>
23#include <errno.h>
24#include <unistd.h>
25
26#include <loc/libloc.h>
9fc7f001 27#include <loc/database.h>
c182393f 28#include <loc/writer.h>
2601e83e 29
64ce6e2f 30const char* VENDOR = "Test Vendor";
2601e83e
MT
31const char* DESCRIPTION =
32 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
33 "Proin ultrices pulvinar dolor, et sollicitudin eros ultricies "
34 "vitae. Nam in volutpat libero. Nulla facilisi. Pellentesque "
35 "tempor felis enim. Integer congue nisi in maximus pretium. "
36 "Pellentesque et turpis elementum, luctus mi at, interdum erat. "
37 "Maecenas ut venenatis nunc.";
4bf49d00 38const char* LICENSE = "CC";
2601e83e 39
fccd8798
MT
40static int attempt_to_open(struct loc_ctx* ctx, char* path) {
41 FILE* f = fopen(path, "r");
42 if (!f)
43 return -1;
44
45 struct loc_database* db;
46 int r = loc_database_new(ctx, &db, f);
47
48 if (r == 0) {
49 fprintf(stderr, "Opening %s was unexpectedly successful\n", path);
50 loc_database_unref(db);
51
52 r = 1;
53 }
54
55 // Close the file again
56 fclose(f);
57
58 return r;
59}
60
2601e83e
MT
61int main(int argc, char** argv) {
62 int err;
63
64 struct loc_ctx* ctx;
65 err = loc_new(&ctx);
66 if (err < 0)
67 exit(EXIT_FAILURE);
68
fccd8798
MT
69 // Try opening an empty file
70 err = attempt_to_open(ctx, "/dev/null");
71 if (err == 0)
72 exit(EXIT_FAILURE);
73
74 // Try opening a file with all zeroes
75 err = attempt_to_open(ctx, "/dev/zero");
76 if (err == 0)
77 exit(EXIT_FAILURE);
78
79 // Try opening a file with random data
80 err = attempt_to_open(ctx, "/dev/urandom");
81 if (err == 0)
82 exit(EXIT_FAILURE);
83
2601e83e 84 // Create a database
c182393f 85 struct loc_writer* writer;
5ce881d4 86 err = loc_writer_new(ctx, &writer, NULL, NULL);
2601e83e
MT
87 if (err < 0)
88 exit(EXIT_FAILURE);
89
90 // Set the vendor
64ce6e2f 91 err = loc_writer_set_vendor(writer, VENDOR);
2601e83e
MT
92 if (err) {
93 fprintf(stderr, "Could not set vendor\n");
94 exit(EXIT_FAILURE);
95 }
96
97 // Retrieve vendor
64ce6e2f
MT
98 const char* vendor = loc_writer_get_vendor(writer);
99 if (vendor) {
100 printf("Vendor is: %s\n", vendor);
2601e83e
MT
101 } else {
102 fprintf(stderr, "Could not retrieve vendor\n");
103 exit(EXIT_FAILURE);
104 }
105
106 // Set a description
c182393f 107 err = loc_writer_set_description(writer, DESCRIPTION);
2601e83e
MT
108 if (err) {
109 fprintf(stderr, "Could not set description\n");
110 exit(EXIT_FAILURE);
111 }
112
113 // Retrieve description
c182393f 114 const char* description = loc_writer_get_description(writer);
2601e83e
MT
115 if (description) {
116 printf("Description is: %s\n", description);
117 } else {
118 fprintf(stderr, "Could not retrieve description\n");
119 exit(EXIT_FAILURE);
120 }
121
4bf49d00
MT
122 // Set a license
123 err = loc_writer_set_license(writer, LICENSE);
124 if (err) {
125 fprintf(stderr, "Could not set license\n");
126 exit(EXIT_FAILURE);
127 }
128
129 // Retrieve license
130 const char* license = loc_writer_get_license(writer);
131 if (license) {
132 printf("License is: %s\n", license);
133 } else {
134 fprintf(stderr, "Could not retrieve license\n");
135 exit(EXIT_FAILURE);
136 }
137
6254dca6 138 FILE* f = tmpfile();
2601e83e
MT
139 if (!f) {
140 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
141 exit(EXIT_FAILURE);
142 }
143
22c7b98b 144 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
2601e83e
MT
145 if (err) {
146 fprintf(stderr, "Could not write database: %s\n", strerror(err));
147 exit(EXIT_FAILURE);
148 }
64ce6e2f 149 loc_writer_unref(writer);
2601e83e 150
2601e83e 151 // And open it again from disk
c182393f
MT
152 struct loc_database* db;
153 err = loc_database_new(ctx, &db, f);
2601e83e
MT
154 if (err) {
155 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
156 exit(EXIT_FAILURE);
157 }
158
726f9984 159 // Try reading something from the database
64ce6e2f
MT
160 vendor = loc_database_get_vendor(db);
161 if (!vendor) {
2601e83e
MT
162 fprintf(stderr, "Could not retrieve vendor\n");
163 exit(EXIT_FAILURE);
64ce6e2f
MT
164 } else if (strcmp(vendor, VENDOR) != 0) {
165 fprintf(stderr, "Vendor doesn't match: %s != %s\n", vendor, VENDOR);
2601e83e
MT
166 exit(EXIT_FAILURE);
167 }
168
169 // Close the database
c182393f 170 loc_database_unref(db);
2601e83e 171 loc_unref(ctx);
6254dca6 172 fclose(f);
2601e83e
MT
173
174 return EXIT_SUCCESS;
175}