]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/test-as.c
2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2017 IPFire Development Team <info@ipfire.org>
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.
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.
21 #include <loc/libloc.h>
22 #include <loc/database.h>
23 #include <loc/writer.h>
25 #define TEST_AS_COUNT 5000
27 int main(int argc
, char** argv
) {
36 struct loc_writer
* writer
;
37 err
= loc_writer_new(ctx
, &writer
);
42 for (unsigned int i
= 1; i
<= TEST_AS_COUNT
; i
++) {
44 loc_writer_add_as(writer
, &as
, i
);
46 sprintf(name
, "Test AS%u", i
);
47 loc_as_set_name(as
, name
);
52 FILE* f
= fopen("test.db", "w");
54 fprintf(stderr
, "Could not open file for writing: %s\n", strerror(errno
));
58 err
= loc_writer_write(writer
, f
);
60 fprintf(stderr
, "Could not write database: %s\n", strerror(-err
));
65 loc_writer_unref(writer
);
67 // And open it again from disk
68 f
= fopen("test.db", "r");
70 fprintf(stderr
, "Could not open file for reading: %s\n", strerror(errno
));
74 struct loc_database
* db
;
75 err
= loc_database_new(ctx
, &db
, f
);
77 fprintf(stderr
, "Could not open database: %s\n", strerror(-err
));
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");
88 for (unsigned int i
= 1; i
<= 10; i
++) {
89 err
= loc_database_get_as(db
, &as
, i
);
91 fprintf(stderr
, "Could not find AS%d\n", i
);
100 struct loc_database_enumerator
* enumerator
;
101 err
= loc_database_enumerator_new(&enumerator
, db
);
103 fprintf(stderr
, "Could not create a database enumerator\n");
107 loc_database_enumerator_set_string(enumerator
, "10");
109 as
= loc_database_enumerator_next_as(enumerator
);
111 printf("Found AS%d: %s\n", loc_as_get_number(as
), loc_as_get_name(as
));
113 as
= loc_database_enumerator_next_as(enumerator
);
116 loc_database_enumerator_unref(enumerator
);
117 loc_database_unref(db
);