1/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 2 -*- */ 2/* test-geoip.c 3 * 4 * Copyright (C) 2006 MaxMind LLC 5 * 6 * This library is free software; you can redistribute it and/or 7 * modify it under the terms of the GNU Lesser General Public 8 * License as published by the Free Software Foundation; either 9 * version 2.1 of the License, or (at your option) any later version. 10 * 11 * This library 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 GNU 14 * Lesser General Public License for more details. 15 * 16 * You should have received a copy of the GNU Lesser General Public 17 * License along with this library; if not, write to the Free Software 18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 19 */ 20 21#include "GeoIP.h" 22 23int main() 24{ 25 FILE *f; 26 char ipAddress[30]; 27 char expectedCountry[3]; 28 char expectedCountry3[4]; 29 const char *returnedCountry; 30 GeoIP *gi; 31 int failed = 0; 32 int test_num = 1; 33 34 int i; 35 for (i = 0; i < 2; ++i) { 36 if (0 == i) { 37 /* Read from filesystem, check for updated file */ 38 gi = GeoIP_open(SRCDIR "/data/GeoIP.dat", 39 GEOIP_STANDARD | GEOIP_CHECK_CACHE); 40 } else { 41 /* Read from memory, faster but takes up more memory */ 42 gi = GeoIP_open(SRCDIR "/data/GeoIP.dat", GEOIP_MEMORY_CACHE); 43 } 44 45 if (gi == NULL) { 46 fprintf(stderr, "Error opening database\n"); 47 exit(1); 48 } 49 50 /* make sure GeoIP deals with invalid query gracefully */ 51 returnedCountry = GeoIP_country_code_by_addr(gi, NULL); 52 if (returnedCountry != NULL) { 53 fprintf(stderr, 54 "Invalid Query test failed, got non NULL, expected NULL\n"); 55 failed = 1; 56 } 57 58 returnedCountry = GeoIP_country_code_by_name(gi, NULL); 59 if (returnedCountry != NULL) { 60 fprintf(stderr, 61 "Invalid Query test failed, got non NULL, expected NULL\n"); 62 failed = 1; 63 } 64 65 f = fopen(SRCDIR "/test/country_test.txt", "r"); 66 67 while (fscanf(f, "%s%s%s", ipAddress, expectedCountry, expectedCountry3) 68 != EOF) { 69 returnedCountry = GeoIP_country_code_by_addr(gi, ipAddress); 70 if (returnedCountry == NULL 71 || strcmp(returnedCountry, expectedCountry) != 0) { 72 fprintf(stderr, 73 "Test addr %d for %s failed, got %s, expected %s\n", 74 test_num, ipAddress, returnedCountry, expectedCountry); 75 failed = 1; 76 } 77 returnedCountry = GeoIP_country_code_by_name(gi, ipAddress); 78 if (returnedCountry == NULL 79 || strcmp(returnedCountry, expectedCountry) != 0) { 80 fprintf(stderr, 81 "Test name %d for %s failed, got %s, expected %s\n", 82 test_num, ipAddress, returnedCountry, expectedCountry); 83 failed = 1; 84 } 85 returnedCountry = GeoIP_country_code3_by_addr(gi, ipAddress); 86 if (returnedCountry == NULL 87 || strcmp(returnedCountry, expectedCountry3) != 0) { 88 fprintf(stderr, 89 "Test addr %d for %s failed, got %s, expected %s\n", 90 test_num, ipAddress, returnedCountry, expectedCountry); 91 failed = 1; 92 } 93 returnedCountry = GeoIP_country_code3_by_name(gi, ipAddress); 94 if (returnedCountry == NULL 95 || strcmp(returnedCountry, expectedCountry3) != 0) { 96 fprintf(stderr, 97 "Test name %d for %s failed, got %s, expected %s\n", 98 test_num, ipAddress, returnedCountry, expectedCountry); 99 failed = 1; 100 } 101 test_num++; 102 } 103 fclose(f); 104 105 f = fopen(SRCDIR "/test/country_test2.txt", "r"); 106 while (fscanf(f, "%s%s", ipAddress, expectedCountry) != EOF) { 107 returnedCountry = GeoIP_country_code_by_addr(gi, ipAddress); 108 if (returnedCountry == NULL 109 || strcmp(returnedCountry, expectedCountry) != 0) { 110 fprintf(stderr, "Test addr %d %s failed, got %s, expected %s\n", 111 test_num, ipAddress, returnedCountry, expectedCountry); 112 failed = 1; 113 } 114 test_num++; 115 } 116 fclose(f); 117 118 f = fopen(SRCDIR "/test/country_test_name.txt", "r"); 119 while (fscanf(f, "%s%s", ipAddress, expectedCountry) != EOF) { 120 returnedCountry = GeoIP_country_code_by_name(gi, ipAddress); 121 if (returnedCountry == NULL 122 || strcmp(returnedCountry, expectedCountry) != 0) { 123 fprintf(stderr, "Test addr %d %s failed, got %s, expected %s\n", 124 test_num, ipAddress, returnedCountry, expectedCountry); 125 failed = 1; 126 } 127 test_num++; 128 } 129 130 fclose(f); 131 GeoIP_delete(gi); 132 } 133 return failed; 134} 135