]>
git.ipfire.org Git - people/ms/libloc.git/blob - src/test-address.c
2 libloc - A library to determine the location of someone on the Internet
4 Copyright (C) 2022 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 <libloc/libloc.h>
22 #include <libloc/address.h>
23 #include <libloc/private.h>
25 static int perform_tests(struct loc_ctx
* ctx
, const int family
) {
26 struct in6_addr address
= IN6ADDR_ANY_INIT
;
31 loc_address_reset(&address
, family
);
33 if (!loc_address_all_zeroes(&address
)) {
34 fprintf(stderr
, "IP address isn't all zeroes\n");
38 if (loc_address_all_ones(&address
)) {
39 fprintf(stderr
, "IP address isn't all ones\n");
53 // Convert this to a string a few times
54 for (unsigned int i
= 0; i
< 100; i
++) {
55 s
= loc_address_str(&address
);
57 printf("Iteration %d: %s\n", i
, s
);
59 if (strcmp(s
, e
) != 0) {
60 fprintf(stderr
, "IP address was formatted in an invalid format: %s\n", s
);
65 // Increment the IP address
66 loc_address_increment(&address
);
78 s
= loc_address_str(&address
);
80 printf("Incremented IP address to %s\n", s
);
82 if (strcmp(s
, e
) != 0) {
83 printf("IP address has been incremented incorrectly: %s\n", s
);
87 if (loc_address_all_zeroes(&address
)) {
88 printf("IP address shouldn't be all zeroes any more\n");
92 if (loc_address_all_ones(&address
)) {
93 printf("IP address shouldn't be all ones any more\n");
97 // Decrement the IP address
98 loc_address_decrement(&address
);
100 s
= loc_address_str(&address
);
102 printf("Incremented IP address to %s\n", s
);
104 if (!loc_address_all_zeroes(&address
)) {
105 printf("IP address hasn't been decremented correctly: %s\n",
106 loc_address_str(&address
));
113 int main(int argc
, char** argv
) {
114 struct loc_ctx
* ctx
= NULL
;
115 int r
= EXIT_FAILURE
;
117 int err
= loc_new(&ctx
);
121 // Enable debug logging
122 loc_set_log_priority(ctx
, LOG_DEBUG
);
124 // Perform all tests for IPv6
125 r
= perform_tests(ctx
, AF_INET6
);
129 // Perform all tests for IPv4
130 r
= perform_tests(ctx
, AF_INET
);