]> git.ipfire.org Git - location/libloc.git/blob - src/test-as.c
test: Generate random temporary files
[location/libloc.git] / src / test-as.c
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 <errno.h>
18 #include <stdlib.h>
19 #include <string.h>
20
21 #include <loc/libloc.h>
22 #include <loc/database.h>
23 #include <loc/writer.h>
24
25 #define TEST_AS_COUNT 5000
26
27 int main(int argc, char** argv) {
28 int err;
29
30 struct loc_ctx* ctx;
31 err = loc_new(&ctx);
32 if (err < 0)
33 exit(EXIT_FAILURE);
34
35 // Create a database
36 struct loc_writer* writer;
37 err = loc_writer_new(ctx, &writer, NULL, NULL);
38 if (err < 0)
39 exit(EXIT_FAILURE);
40
41 char name[256];
42 for (unsigned int i = 1; i <= TEST_AS_COUNT; i++) {
43 struct loc_as* as;
44 loc_writer_add_as(writer, &as, i);
45
46 sprintf(name, "Test AS%u", i);
47 loc_as_set_name(as, name);
48
49 loc_as_unref(as);
50 }
51
52 FILE* f = tmpfile();
53 if (!f) {
54 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
55 exit(EXIT_FAILURE);
56 }
57
58 err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET);
59 if (err) {
60 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
61 exit(EXIT_FAILURE);
62 }
63
64 loc_writer_unref(writer);
65
66 // And open it again from disk
67 struct loc_database* db;
68 err = loc_database_new(ctx, &db, f);
69 if (err) {
70 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
71 exit(EXIT_FAILURE);
72 }
73
74 size_t as_count = loc_database_count_as(db);
75 if (as_count != TEST_AS_COUNT) {
76 fprintf(stderr, "Could not read all ASes\n");
77 exit(EXIT_FAILURE);
78 }
79
80 struct loc_as* as;
81 for (unsigned int i = 1; i <= 10; i++) {
82 err = loc_database_get_as(db, &as, i);
83 if (err) {
84 fprintf(stderr, "Could not find AS%d\n", i);
85 exit(EXIT_FAILURE);
86 }
87
88 loc_as_unref(as);
89 }
90
91 // Enumerator
92
93 struct loc_database_enumerator* enumerator;
94 err = loc_database_enumerator_new(&enumerator, db, LOC_DB_ENUMERATE_ASES);
95 if (err) {
96 fprintf(stderr, "Could not create a database enumerator\n");
97 exit(EXIT_FAILURE);
98 }
99
100 loc_database_enumerator_set_string(enumerator, "10");
101
102 err = loc_database_enumerator_next_as(enumerator, &as);
103 if (err) {
104 fprintf(stderr, "Could not enumerate next AS\n");
105 exit(EXIT_FAILURE);
106 }
107
108 while (as) {
109 printf("Found AS%d: %s\n", loc_as_get_number(as), loc_as_get_name(as));
110
111 err = loc_database_enumerator_next_as(enumerator, &as);
112 if (err) {
113 fprintf(stderr, "Could not enumerate next AS\n");
114 exit(EXIT_FAILURE);
115 }
116 }
117
118 loc_database_enumerator_unref(enumerator);
119 loc_database_unref(db);
120 loc_unref(ctx);
121 fclose(f);
122
123 return EXIT_SUCCESS;
124 }