]>
Commit | Line | Data |
---|---|---|
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> | |
37317e4f | 25 | #include <syslog.h> |
2601e83e MT |
26 | |
27 | #include <loc/libloc.h> | |
9fc7f001 | 28 | #include <loc/database.h> |
c182393f | 29 | #include <loc/writer.h> |
2601e83e | 30 | |
64ce6e2f | 31 | const char* VENDOR = "Test Vendor"; |
2601e83e MT |
32 | const char* DESCRIPTION = |
33 | "Lorem ipsum dolor sit amet, consectetur adipiscing elit. " | |
34 | "Proin ultrices pulvinar dolor, et sollicitudin eros ultricies " | |
35 | "vitae. Nam in volutpat libero. Nulla facilisi. Pellentesque " | |
36 | "tempor felis enim. Integer congue nisi in maximus pretium. " | |
37 | "Pellentesque et turpis elementum, luctus mi at, interdum erat. " | |
38 | "Maecenas ut venenatis nunc."; | |
4bf49d00 | 39 | const char* LICENSE = "CC"; |
2601e83e | 40 | |
fccd8798 MT |
41 | static int attempt_to_open(struct loc_ctx* ctx, char* path) { |
42 | FILE* f = fopen(path, "r"); | |
43 | if (!f) | |
44 | return -1; | |
45 | ||
46 | struct loc_database* db; | |
47 | int r = loc_database_new(ctx, &db, f); | |
48 | ||
49 | if (r == 0) { | |
50 | fprintf(stderr, "Opening %s was unexpectedly successful\n", path); | |
51 | loc_database_unref(db); | |
52 | ||
53 | r = 1; | |
54 | } | |
55 | ||
56 | // Close the file again | |
57 | fclose(f); | |
58 | ||
59 | return r; | |
60 | } | |
61 | ||
2601e83e MT |
62 | int main(int argc, char** argv) { |
63 | int err; | |
64 | ||
65 | struct loc_ctx* ctx; | |
66 | err = loc_new(&ctx); | |
67 | if (err < 0) | |
68 | exit(EXIT_FAILURE); | |
69 | ||
37317e4f MT |
70 | // Enable debug logging |
71 | loc_set_log_priority(ctx, LOG_DEBUG); | |
72 | ||
fccd8798 MT |
73 | // Try opening an empty file |
74 | err = attempt_to_open(ctx, "/dev/null"); | |
75 | if (err == 0) | |
76 | exit(EXIT_FAILURE); | |
77 | ||
78 | // Try opening a file with all zeroes | |
79 | err = attempt_to_open(ctx, "/dev/zero"); | |
80 | if (err == 0) | |
81 | exit(EXIT_FAILURE); | |
82 | ||
83 | // Try opening a file with random data | |
84 | err = attempt_to_open(ctx, "/dev/urandom"); | |
85 | if (err == 0) | |
86 | exit(EXIT_FAILURE); | |
87 | ||
2601e83e | 88 | // Create a database |
c182393f | 89 | struct loc_writer* writer; |
5ce881d4 | 90 | err = loc_writer_new(ctx, &writer, NULL, NULL); |
2601e83e MT |
91 | if (err < 0) |
92 | exit(EXIT_FAILURE); | |
93 | ||
94 | // Set the vendor | |
64ce6e2f | 95 | err = loc_writer_set_vendor(writer, VENDOR); |
2601e83e MT |
96 | if (err) { |
97 | fprintf(stderr, "Could not set vendor\n"); | |
98 | exit(EXIT_FAILURE); | |
99 | } | |
100 | ||
101 | // Retrieve vendor | |
64ce6e2f MT |
102 | const char* vendor = loc_writer_get_vendor(writer); |
103 | if (vendor) { | |
104 | printf("Vendor is: %s\n", vendor); | |
2601e83e MT |
105 | } else { |
106 | fprintf(stderr, "Could not retrieve vendor\n"); | |
107 | exit(EXIT_FAILURE); | |
108 | } | |
109 | ||
110 | // Set a description | |
c182393f | 111 | err = loc_writer_set_description(writer, DESCRIPTION); |
2601e83e MT |
112 | if (err) { |
113 | fprintf(stderr, "Could not set description\n"); | |
114 | exit(EXIT_FAILURE); | |
115 | } | |
116 | ||
117 | // Retrieve description | |
c182393f | 118 | const char* description = loc_writer_get_description(writer); |
2601e83e MT |
119 | if (description) { |
120 | printf("Description is: %s\n", description); | |
121 | } else { | |
122 | fprintf(stderr, "Could not retrieve description\n"); | |
123 | exit(EXIT_FAILURE); | |
124 | } | |
125 | ||
4bf49d00 MT |
126 | // Set a license |
127 | err = loc_writer_set_license(writer, LICENSE); | |
128 | if (err) { | |
129 | fprintf(stderr, "Could not set license\n"); | |
130 | exit(EXIT_FAILURE); | |
131 | } | |
132 | ||
133 | // Retrieve license | |
134 | const char* license = loc_writer_get_license(writer); | |
135 | if (license) { | |
136 | printf("License is: %s\n", license); | |
137 | } else { | |
138 | fprintf(stderr, "Could not retrieve license\n"); | |
139 | exit(EXIT_FAILURE); | |
140 | } | |
141 | ||
6254dca6 | 142 | FILE* f = tmpfile(); |
2601e83e MT |
143 | if (!f) { |
144 | fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno)); | |
145 | exit(EXIT_FAILURE); | |
146 | } | |
147 | ||
22c7b98b | 148 | err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET); |
2601e83e MT |
149 | if (err) { |
150 | fprintf(stderr, "Could not write database: %s\n", strerror(err)); | |
151 | exit(EXIT_FAILURE); | |
152 | } | |
64ce6e2f | 153 | loc_writer_unref(writer); |
2601e83e | 154 | |
2601e83e | 155 | // And open it again from disk |
c182393f MT |
156 | struct loc_database* db; |
157 | err = loc_database_new(ctx, &db, f); | |
2601e83e MT |
158 | if (err) { |
159 | fprintf(stderr, "Could not open database: %s\n", strerror(-err)); | |
160 | exit(EXIT_FAILURE); | |
161 | } | |
162 | ||
726f9984 | 163 | // Try reading something from the database |
64ce6e2f MT |
164 | vendor = loc_database_get_vendor(db); |
165 | if (!vendor) { | |
2601e83e MT |
166 | fprintf(stderr, "Could not retrieve vendor\n"); |
167 | exit(EXIT_FAILURE); | |
64ce6e2f MT |
168 | } else if (strcmp(vendor, VENDOR) != 0) { |
169 | fprintf(stderr, "Vendor doesn't match: %s != %s\n", vendor, VENDOR); | |
2601e83e MT |
170 | exit(EXIT_FAILURE); |
171 | } | |
172 | ||
173 | // Close the database | |
c182393f | 174 | loc_database_unref(db); |
2601e83e | 175 | loc_unref(ctx); |
6254dca6 | 176 | fclose(f); |
2601e83e MT |
177 | |
178 | return EXIT_SUCCESS; | |
179 | } |