]> git.ipfire.org Git - people/ms/libloc.git/blob - src/test-as.c
database: Save time when the database was created
[people/ms/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 "database.h"
23
24 #define TEST_AS_COUNT 100
25
26 int main(int argc, char** argv) {
27 int err;
28
29 struct loc_ctx* ctx;
30 err = loc_new(&ctx);
31 if (err < 0)
32 exit(EXIT_FAILURE);
33
34 // Create a database
35 struct loc_database* db;
36 err = loc_database_new(ctx, &db, 1024);
37 if (err < 0)
38 exit(EXIT_FAILURE);
39
40 char name[256];
41 for (unsigned int i = 1; i <= TEST_AS_COUNT; i++) {
42 struct loc_as* as = loc_database_add_as(db, i);
43
44 sprintf(name, "Test AS%u", i);
45 loc_as_set_name(as, name);
46
47 loc_as_unref(as);
48 }
49
50 FILE* f = fopen("test.db", "w");
51 if (!f) {
52 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
53 exit(EXIT_FAILURE);
54 }
55
56 err = loc_database_write(db, f);
57 if (err) {
58 fprintf(stderr, "Could not write database: %s\n", strerror(-err));
59 exit(EXIT_FAILURE);
60 }
61 fclose(f);
62
63 loc_database_unref(db);
64
65 // And open it again from disk
66 f = fopen("test.db", "r");
67 if (!f) {
68 fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno));
69 exit(EXIT_FAILURE);
70 }
71
72 err = loc_database_open(ctx, &db, f);
73 if (err) {
74 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
75 exit(EXIT_FAILURE);
76 }
77
78 size_t as_count = loc_database_count_as(db);
79 if (as_count != TEST_AS_COUNT) {
80 fprintf(stderr, "Could not read all ASes\n");
81 exit(EXIT_FAILURE);
82 }
83
84 loc_database_unref(db);
85 loc_unref(ctx);
86
87 return EXIT_SUCCESS;
88 }