]>
Commit | Line | Data |
---|---|---|
a5db3e49 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 <errno.h> | |
18 | #include <stdlib.h> | |
19 | #include <string.h> | |
20 | ||
21 | #include <loc/libloc.h> | |
9fc7f001 | 22 | #include <loc/database.h> |
c182393f | 23 | #include <loc/writer.h> |
a5db3e49 | 24 | |
8f3e2a06 | 25 | #define TEST_AS_COUNT 5000 |
a5db3e49 MT |
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 | |
c182393f | 36 | struct loc_writer* writer; |
5ce881d4 | 37 | err = loc_writer_new(ctx, &writer, NULL, NULL); |
a5db3e49 MT |
38 | if (err < 0) |
39 | exit(EXIT_FAILURE); | |
40 | ||
41 | char name[256]; | |
42 | for (unsigned int i = 1; i <= TEST_AS_COUNT; i++) { | |
c182393f | 43 | struct loc_as* as; |
0e974d4b | 44 | loc_writer_add_as(writer, &as, i); |
a5db3e49 MT |
45 | |
46 | sprintf(name, "Test AS%u", i); | |
47 | loc_as_set_name(as, name); | |
48 | ||
49 | loc_as_unref(as); | |
50 | } | |
51 | ||
726f9984 | 52 | FILE* f = fopen("test.db", "w+"); |
a5db3e49 MT |
53 | if (!f) { |
54 | fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno)); | |
55 | exit(EXIT_FAILURE); | |
56 | } | |
57 | ||
22c7b98b | 58 | err = loc_writer_write(writer, f, LOC_DATABASE_VERSION_UNSET); |
a5db3e49 MT |
59 | if (err) { |
60 | fprintf(stderr, "Could not write database: %s\n", strerror(-err)); | |
61 | exit(EXIT_FAILURE); | |
62 | } | |
63 | fclose(f); | |
64 | ||
c182393f | 65 | loc_writer_unref(writer); |
a5db3e49 MT |
66 | |
67 | // And open it again from disk | |
68 | f = fopen("test.db", "r"); | |
69 | if (!f) { | |
70 | fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno)); | |
71 | exit(EXIT_FAILURE); | |
72 | } | |
73 | ||
c182393f MT |
74 | struct loc_database* db; |
75 | err = loc_database_new(ctx, &db, f); | |
a5db3e49 MT |
76 | if (err) { |
77 | fprintf(stderr, "Could not open database: %s\n", strerror(-err)); | |
78 | exit(EXIT_FAILURE); | |
79 | } | |
80 | ||
81 | size_t as_count = loc_database_count_as(db); | |
82 | if (as_count != TEST_AS_COUNT) { | |
83 | fprintf(stderr, "Could not read all ASes\n"); | |
84 | exit(EXIT_FAILURE); | |
85 | } | |
86 | ||
c182393f | 87 | struct loc_as* as; |
8f3e2a06 MT |
88 | for (unsigned int i = 1; i <= 10; i++) { |
89 | err = loc_database_get_as(db, &as, i); | |
90 | if (err) { | |
91 | fprintf(stderr, "Could not find AS%d\n", i); | |
92 | exit(EXIT_FAILURE); | |
93 | } | |
94 | ||
95 | loc_as_unref(as); | |
c182393f MT |
96 | } |
97 | ||
d3d8ede6 MT |
98 | // Enumerator |
99 | ||
100 | struct loc_database_enumerator* enumerator; | |
ccc7ab4e | 101 | err = loc_database_enumerator_new(&enumerator, db, LOC_DB_ENUMERATE_ASES); |
d3d8ede6 MT |
102 | if (err) { |
103 | fprintf(stderr, "Could not create a database enumerator\n"); | |
104 | exit(EXIT_FAILURE); | |
105 | } | |
106 | ||
107 | loc_database_enumerator_set_string(enumerator, "10"); | |
108 | ||
8b6197b8 MT |
109 | err = loc_database_enumerator_next_as(enumerator, &as); |
110 | if (err) { | |
111 | fprintf(stderr, "Could not enumerate next AS\n"); | |
112 | exit(EXIT_FAILURE); | |
113 | } | |
114 | ||
d3d8ede6 MT |
115 | while (as) { |
116 | printf("Found AS%d: %s\n", loc_as_get_number(as), loc_as_get_name(as)); | |
117 | ||
8b6197b8 MT |
118 | err = loc_database_enumerator_next_as(enumerator, &as); |
119 | if (err) { | |
120 | fprintf(stderr, "Could not enumerate next AS\n"); | |
121 | exit(EXIT_FAILURE); | |
122 | } | |
d3d8ede6 MT |
123 | } |
124 | ||
125 | loc_database_enumerator_unref(enumerator); | |
a5db3e49 MT |
126 | loc_database_unref(db); |
127 | loc_unref(ctx); | |
128 | ||
129 | return EXIT_SUCCESS; | |
130 | } |