]> git.ipfire.org Git - people/ms/libloc.git/blame - src/test-database.c
Add license attribute to the database
[people/ms/libloc.git] / src / test-database.c
CommitLineData
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>
25
26#include <loc/libloc.h>
9fc7f001 27#include <loc/database.h>
c182393f 28#include <loc/writer.h>
2601e83e 29
64ce6e2f 30const char* VENDOR = "Test Vendor";
2601e83e
MT
31const char* DESCRIPTION =
32 "Lorem ipsum dolor sit amet, consectetur adipiscing elit. "
33 "Proin ultrices pulvinar dolor, et sollicitudin eros ultricies "
34 "vitae. Nam in volutpat libero. Nulla facilisi. Pellentesque "
35 "tempor felis enim. Integer congue nisi in maximus pretium. "
36 "Pellentesque et turpis elementum, luctus mi at, interdum erat. "
37 "Maecenas ut venenatis nunc.";
4bf49d00 38const char* LICENSE = "CC";
2601e83e
MT
39
40int main(int argc, char** argv) {
41 int err;
42
43 struct loc_ctx* ctx;
44 err = loc_new(&ctx);
45 if (err < 0)
46 exit(EXIT_FAILURE);
47
48 // Create a database
c182393f
MT
49 struct loc_writer* writer;
50 err = loc_writer_new(ctx, &writer);
2601e83e
MT
51 if (err < 0)
52 exit(EXIT_FAILURE);
53
54 // Set the vendor
64ce6e2f 55 err = loc_writer_set_vendor(writer, VENDOR);
2601e83e
MT
56 if (err) {
57 fprintf(stderr, "Could not set vendor\n");
58 exit(EXIT_FAILURE);
59 }
60
61 // Retrieve vendor
64ce6e2f
MT
62 const char* vendor = loc_writer_get_vendor(writer);
63 if (vendor) {
64 printf("Vendor is: %s\n", vendor);
2601e83e
MT
65 } else {
66 fprintf(stderr, "Could not retrieve vendor\n");
67 exit(EXIT_FAILURE);
68 }
69
70 // Set a description
c182393f 71 err = loc_writer_set_description(writer, DESCRIPTION);
2601e83e
MT
72 if (err) {
73 fprintf(stderr, "Could not set description\n");
74 exit(EXIT_FAILURE);
75 }
76
77 // Retrieve description
c182393f 78 const char* description = loc_writer_get_description(writer);
2601e83e
MT
79 if (description) {
80 printf("Description is: %s\n", description);
81 } else {
82 fprintf(stderr, "Could not retrieve description\n");
83 exit(EXIT_FAILURE);
84 }
85
4bf49d00
MT
86 // Set a license
87 err = loc_writer_set_license(writer, LICENSE);
88 if (err) {
89 fprintf(stderr, "Could not set license\n");
90 exit(EXIT_FAILURE);
91 }
92
93 // Retrieve license
94 const char* license = loc_writer_get_license(writer);
95 if (license) {
96 printf("License is: %s\n", license);
97 } else {
98 fprintf(stderr, "Could not retrieve license\n");
99 exit(EXIT_FAILURE);
100 }
101
2601e83e
MT
102 FILE* f = fopen("test.db", "w");
103 if (!f) {
104 fprintf(stderr, "Could not open file for writing: %s\n", strerror(errno));
105 exit(EXIT_FAILURE);
106 }
107
c182393f 108 err = loc_writer_write(writer, f);
2601e83e
MT
109 if (err) {
110 fprintf(stderr, "Could not write database: %s\n", strerror(err));
111 exit(EXIT_FAILURE);
112 }
64ce6e2f 113 loc_writer_unref(writer);
2601e83e
MT
114
115 // Close the file
116 fclose(f);
117
2601e83e
MT
118 // And open it again from disk
119 f = fopen("test.db", "r");
120 if (!f) {
121 fprintf(stderr, "Could not open file for reading: %s\n", strerror(errno));
122 exit(EXIT_FAILURE);
123 }
124
c182393f
MT
125 struct loc_database* db;
126 err = loc_database_new(ctx, &db, f);
2601e83e
MT
127 if (err) {
128 fprintf(stderr, "Could not open database: %s\n", strerror(-err));
129 exit(EXIT_FAILURE);
130 }
131
64ce6e2f
MT
132 vendor = loc_database_get_vendor(db);
133 if (!vendor) {
2601e83e
MT
134 fprintf(stderr, "Could not retrieve vendor\n");
135 exit(EXIT_FAILURE);
64ce6e2f
MT
136 } else if (strcmp(vendor, VENDOR) != 0) {
137 fprintf(stderr, "Vendor doesn't match: %s != %s\n", vendor, VENDOR);
2601e83e
MT
138 exit(EXIT_FAILURE);
139 }
140
141 // Close the database
c182393f 142 loc_database_unref(db);
2601e83e
MT
143
144 loc_unref(ctx);
145
146 return EXIT_SUCCESS;
147}