summaryrefslogtreecommitdiffstats
path: root/test/test-geoip.c
blob: 909e63cd787eba0f9c6758fc5c6c0ff4ed9205c6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */
/* test-geoip.c
 *
 * Copyright (C) 2016 MaxMind, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include "GeoIP.h"

int main(void) {
    FILE *f;
    char *db_info;
    char ipAddress[30];
    char expectedCountry[3];
    char expectedCountry3[4];
    const char *returnedCountry;
    GeoIP *gi;
    int failed = 0;
    int test_num = 1;

    int i;
    for (i = 0; i < 2; ++i) {
        if (0 == i) {
            /* Read from filesystem, check for updated file */
            gi = GeoIP_open(SRCDIR "/data/GeoIP.dat",
                            GEOIP_STANDARD | GEOIP_CHECK_CACHE);
        } else {
            /* Read from memory, faster but takes up more memory */
            gi = GeoIP_open(SRCDIR "/data/GeoIP.dat", GEOIP_MEMORY_CACHE);
        }

        if (gi == NULL) {
            fprintf(stderr, "Error opening database\n");
            exit(1);
        }

        db_info = GeoIP_database_info(gi);
        if (!db_info || strcmp(db_info,
                               "GEO-106 20160621 Build 1 Copyright (c) 2016 "
                               "MaxMind Inc All Rights Reserved")) {
            fprintf(stderr, "Error reading database info (got %s).\n", db_info);
            free(db_info);
            failed = 1;
        }

        /* make sure GeoIP deals with invalid query gracefully */
        returnedCountry = GeoIP_country_code_by_addr(gi, NULL);
        if (returnedCountry != NULL) {
            fprintf(stderr,
                    "Invalid Query test failed, got non NULL, expected NULL\n");
            failed = 1;
        }

        returnedCountry = GeoIP_country_code_by_name(gi, NULL);
        if (returnedCountry != NULL) {
            fprintf(stderr,
                    "Invalid Query test failed, got non NULL, expected NULL\n");
            failed = 1;
        }

        f = fopen(SRCDIR "/test/country_test.txt", "r");

        while (
            fscanf(f, "%s%s%s", ipAddress, expectedCountry, expectedCountry3) !=
            EOF) {
            returnedCountry = GeoIP_country_code_by_addr(gi, ipAddress);
            if (returnedCountry == NULL ||
                strcmp(returnedCountry, expectedCountry) != 0) {
                fprintf(stderr,
                        "Test addr %d for %s failed, got %s, expected %s\n",
                        test_num,
                        ipAddress,
                        returnedCountry,
                        expectedCountry);
                failed = 1;
            }
            returnedCountry = GeoIP_country_code_by_name(gi, ipAddress);
            if (returnedCountry == NULL ||
                strcmp(returnedCountry, expectedCountry) != 0) {
                fprintf(stderr,
                        "Test name %d for %s failed, got %s, expected %s\n",
                        test_num,
                        ipAddress,
                        returnedCountry,
                        expectedCountry);
                failed = 1;
            }
            returnedCountry = GeoIP_country_code3_by_addr(gi, ipAddress);
            if (returnedCountry == NULL ||
                strcmp(returnedCountry, expectedCountry3) != 0) {
                fprintf(stderr,
                        "Test addr %d for %s failed, got %s, expected %s\n",
                        test_num,
                        ipAddress,
                        returnedCountry,
                        expectedCountry);
                failed = 1;
            }
            returnedCountry = GeoIP_country_code3_by_name(gi, ipAddress);
            if (returnedCountry == NULL ||
                strcmp(returnedCountry, expectedCountry3) != 0) {
                fprintf(stderr,
                        "Test name %d for %s failed, got %s, expected %s\n",
                        test_num,
                        ipAddress,
                        returnedCountry,
                        expectedCountry);
                failed = 1;
            }
            test_num++;
        }
        fclose(f);

        f = fopen(SRCDIR "/test/country_test2.txt", "r");
        while (fscanf(f, "%s%s", ipAddress, expectedCountry) != EOF) {
            returnedCountry = GeoIP_country_code_by_addr(gi, ipAddress);
            if (returnedCountry == NULL ||
                strcmp(returnedCountry, expectedCountry) != 0) {
                fprintf(stderr,
                        "Test addr %d %s failed, got %s, expected %s\n",
                        test_num,
                        ipAddress,
                        returnedCountry,
                        expectedCountry);
                failed = 1;
            }
            test_num++;
        }
        fclose(f);
        GeoIP_delete(gi);
    }
    return failed;
}