libtld 1.2.0
|
00001 /* TLD library -- Test the TLD library by including the tld.c file. 00002 * Copyright (C) 2011 Made to Order Software Corp. 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation; either version 2 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program; if not, write to the Free Software 00016 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00017 */ 00018 00019 /* We want to directly test the library functions, including all the private 00020 * functions so we include the source directly. 00021 */ 00022 #include "tld.c" 00023 #include "tld_data.c" 00024 #include <stdlib.h> 00025 #include <string.h> 00026 00027 int err_count = 0; 00028 00029 void test_compare() 00030 { 00031 struct data 00032 { 00033 const char *a; 00034 const char *b; 00035 int n; 00036 int r; 00037 }; 00038 struct data d[] = { 00039 { "uj", "uk", 2, -1 }, 00040 { "uk", "uk", 2, 0 }, 00041 { "ul", "uk", 2, 1 }, 00042 00043 { "uj", "ukmore", 2, -1 }, 00044 { "uk", "ukstuff", 2, 0 }, 00045 { "ul", "ukhere", 2, 1 }, 00046 00047 { "uk1", "ukmore", 2, 1 }, 00048 { "uk2", "ukstuff", 2, 1 }, 00049 { "uk3", "ukhere", 2, 1 }, 00050 00051 { "uk1", "uk.", 3, 1 }, 00052 { "uk2", "uk.", 3, 1 }, 00053 { "uk3", "uk.", 3, 1 }, 00054 00055 { "uk1", ".uk", 3, 1 }, 00056 { "uk2", ".uk", 3, 1 }, 00057 { "uk3", ".uk", 3, 1 }, 00058 00059 { "uk", "uk1", 3, -1 }, 00060 { "uk", "uk22", 4, -1 }, 00061 { "uk", "uk333", 5, -1 }, 00062 00063 { "uk1", "uk", 2, 1 }, 00064 { "uk22", "uk", 2, 1 }, 00065 { "uk333", "uk", 2, 1 }, 00066 }; 00067 int i, r, max; 00068 00069 max = sizeof(d) / sizeof(d[0]); 00070 for(i = 0; i < max; ++i) 00071 { 00072 r = cmp(d[i].a, d[i].b, d[i].n); 00073 if(r != d[i].r) { 00074 fprintf(stderr, "error: cmp() failed with \"%s\" / \"%s\", expected %d and got %d\n", 00075 d[i].a, d[i].b, d[i].r, r); 00076 ++err_count; 00077 } 00078 } 00079 } 00080 00081 void test_search() 00082 { 00083 struct search_info 00084 { 00085 int f_start; 00086 int f_end; 00087 const char * f_tld; 00088 int f_length; 00089 int f_result; 00090 }; 00091 struct search_info d[] = { 00092 /* 00093 * This table is very annoying since each time the data changes 00094 * it gets out of sync. On the other hand that's the best way 00095 * to make sure our tests work like in the real world. 00096 */ 00097 00098 /* get the .uk offset */ 00099 { 4944, 5300, "uk", 2, 5276 }, 00100 00101 /* get each offset of the .uk 2nd level domain */ 00102 { 4759, 4784, "ac", 2, 4759 }, 00103 { 4759, 4784, "bl", 2, 4760 }, 00104 { 4759, 4784, "british-library", 15, 4761 }, 00105 { 4759, 4784, "co", 2, 4762 }, 00106 { 4759, 4784, "gov", 3, 4763 }, 00107 { 4759, 4784, "govt", 4, 4764 }, 00108 { 4759, 4784, "icnet", 5, 4765 }, 00109 { 4759, 4784, "jet", 3, 4766 }, 00110 { 4759, 4784, "lea", 3, 4767 }, 00111 { 4759, 4784, "ltd", 3, 4768 }, 00112 { 4759, 4784, "me", 2, 4769 }, 00113 { 4759, 4784, "mil", 3, 4770 }, 00114 { 4759, 4784, "mod", 3, 4771 }, 00115 { 4759, 4784, "national-library-scotland", 25, 4772 }, 00116 { 4759, 4784, "nel", 3, 4773 }, 00117 { 4759, 4784, "net", 3, 4774 }, 00118 { 4759, 4784, "nhs", 3, 4775 }, 00119 { 4759, 4784, "nic", 3, 4776 }, 00120 { 4759, 4784, "nls", 3, 4777 }, 00121 { 4759, 4784, "org", 3, 4778 }, 00122 { 4759, 4784, "orgn", 4, 4779 }, 00123 { 4759, 4784, "parliament", 10, 4780 }, 00124 { 4759, 4784, "plc", 3, 4781 }, 00125 { 4759, 4784, "police", 6, 4782 }, 00126 { 4759, 4784, "sch", 3, 4783 }, 00127 00128 /* test with a few invalid TLDs for .uk */ 00129 { 4759, 4784, "com", 3, -1 }, 00130 { 4759, 4784, "aca", 3, -1 }, 00131 { 4759, 4784, "aac", 3, -1 }, 00132 { 4759, 4784, "ca", 2, -1 }, 00133 { 4759, 4784, "cn", 2, -1 }, 00134 { 4759, 4784, "cp", 2, -1 }, 00135 { 4759, 4784, "cz", 2, -1 }, 00136 00137 /* get the .vu offset */ 00138 { 4944, 5300, "vu", 2, 5286 }, 00139 00140 /* get the .gov.vu offset */ 00141 { 4889, 4890, "gov", 3, 4889 }, 00142 00143 /* test with a few .vu 2nd level domains that do not exist */ 00144 { 4889, 4890, "edu", 3, -1 }, 00145 { 4889, 4890, "net", 3, -1 }, 00146 00147 /* verify ordering of mari, mari-el, and marine */ 00148 { 4299, 4435, "mari", 4, 4361 }, 00149 { 4299, 4435, "mari-el", 7, 4362 }, 00150 { 4299, 4435, "marine", 6, 4363 }, 00151 }; 00152 int i, r, max; 00153 00154 max = sizeof(d) / sizeof(d[0]); 00155 for(i = 0; i < max; ++i) 00156 { 00157 r = search(d[i].f_start, d[i].f_end, d[i].f_tld, d[i].f_length); 00158 if(r != d[i].f_result) 00159 { 00160 fprintf(stderr, "error: test_search() failed with \"%s\", expected %d and got %d\n", 00161 d[i].f_tld, d[i].f_result, r); 00162 ++err_count; 00163 } 00164 } 00165 } 00166 00167 00168 void test_search_array(int start, int end) 00169 { 00170 int i, r; 00171 00172 /* now test all from the arrays */ 00173 for(i = start; i < end; ++i) 00174 { 00175 /*printf("{%d..%d} i = %d, [%s]\n", start, end, i, tld_descriptions[i].f_tld);*/ 00176 r = search(start, end, tld_descriptions[i].f_tld, strlen(tld_descriptions[i].f_tld)); 00177 if(r != i) 00178 { 00179 fprintf(stderr, "error: test_search_array() failed with \"%s\", expected %d and got %d\n", 00180 tld_descriptions[i].f_tld, i, r); 00181 ++err_count; 00182 } 00183 if(tld_descriptions[i].f_start_offset != -1) 00184 { 00185 test_search_array(tld_descriptions[i].f_start_offset, 00186 tld_descriptions[i].f_end_offset); 00187 } 00188 } 00189 } 00190 00191 void test_search_all() 00192 { 00193 test_search_array(tld_start_offset, tld_end_offset); 00194 } 00195 00196 00197 int main(int argc, char *argv[]) 00198 { 00199 fprintf(stderr, "testing tld version %s\n", tld_version()); 00200 00201 /* call all the tests, one by one 00202 * failures are "recorded" in the err_count global variable 00203 * and the process stops with an error message and exit(1) 00204 * if err_count is not zero. 00205 */ 00206 test_compare(); 00207 test_search(); 00208 test_search_all(); 00209 00210 if(err_count) 00211 { 00212 fprintf(stderr, "%d error%s occured.\n", 00213 err_count, err_count != 1 ? "s" : ""); 00214 } 00215 exit(err_count ? 1 : 0); 00216 } 00217 00218 /* vim: ts=4 sw=4 00219 */
This document is part of the libtld Project.
Copyright by Made to Order Software Corp.