1 /* 2 This file is part of libmicrospdy 3 Copyright Copyright (C) 2012 Andrey Uzunov 4 5 This program is free software: you can redistribute it and/or modify 6 it under the terms of the GNU General Public License as published by 7 the Free Software Foundation, either version 3 of the License, or 8 (at your option) any later version. 9 10 This program is distributed in the hope that it will be useful, 11 but WITHOUT ANY WARRANTY; without even the implied warranty of 12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 GNU General Public License for more details. 14 15 You should have received a copy of the GNU General Public License 16 along with this program. If not, see <http://www.gnu.org/licenses/>. 17 */ 18 19 /** 20 * @file struct_namevalue.c 21 * @brief tests all the API functions for handling struct SPDY_NameValue 22 * @author Andrey Uzunov 23 */ 24 25 #include "platform.h" 26 #include "microspdy.h" 27 #include "common.h" 28 #include "../microspdy/structures.h" 29 #include "../microspdy/alstructures.h" 30 31 char *pairs[] = {"one","1","two","2","three","3","four","4","five","5"}; 32 char *pairs_with_dups[] = {"one","1","two","2","one","11","two","22","three","3","two","222","two","2222","four","","five","5"};//82 33 char *pairs_with_empty[] = {"name","","name","value"}; 34 char *pairs_different[] = {"30","thirty","40","fouthy"}; 35 int size; 36 int size2; 37 int brake_at = 3; 38 bool flag; 39 40 41 int 42 iterate_cb (void *cls, const char *name, const char * const * value, int num_values) 43 { 44 int *c = (int*)cls; 45 46 if(*c < 0 || *c > size) 47 exit(11); 48 49 if(strcmp(name,pairs[*c]) != 0) 50 { 51 FAIL_TEST("name is wrong\n"); 52 } 53 54 if(1 != num_values) 55 { 56 FAIL_TEST("num_values is wrong\n"); 57 } 58 59 if(strcmp(value[0],pairs[(*c)+1]) != 0) 60 { 61 FAIL_TEST("value is wrong\n"); 62 } 63 64 (*c)+=2; 65 66 return SPDY_YES; 67 } 68 69 int 70 iterate_brake_cb (void *cls, const char *name, const char * const *value, int num_values) 71 { 72 (void)name; 73 (void)value; 74 (void)num_values; 75 76 int *c = (int*)cls; 77 78 if(*c < 0 || *c >= brake_at) 79 { 80 FAIL_TEST("iteration was not interrupted\n"); 81 } 82 83 (*c)++; 84 85 if(*c == brake_at) return SPDY_NO; 86 87 return SPDY_YES; 88 } 89 90 int 91 main() 92 { 93 SPDY_init(); 94 95 const char *const*value; 96 const char *const*value2; 97 int i; 98 int j; 99 int cls = 0; 100 int ret; 101 int ret2; 102 void *ob1; 103 void *ob2; 104 void *ob3; 105 void *stream; 106 char data[] = "anything"; 107 struct SPDY_NameValue *container; 108 struct SPDY_NameValue *container2; 109 struct SPDY_NameValue *container3; 110 struct SPDY_NameValue *container_arr[2]; 111 112 size = sizeof(pairs)/sizeof(pairs[0]); 113 114 if(NULL == (container = SPDY_name_value_create ())) 115 { 116 FAIL_TEST("SPDY_name_value_create failed\n"); 117 } 118 119 if(NULL != SPDY_name_value_lookup (container, "anything", &ret)) 120 { 121 FAIL_TEST("SPDY_name_value_lookup failed\n"); 122 } 123 124 if(SPDY_name_value_iterate (container, NULL, NULL) != 0) 125 { 126 FAIL_TEST("SPDY_name_value_iterate failed\n"); 127 } 128 129 for(i=0;i<size; i+=2) 130 { 131 if(SPDY_YES != SPDY_name_value_add(container,pairs[i],pairs[i+1])) 132 { 133 FAIL_TEST("SPDY_name_value_add failed\n"); 134 } 135 136 if(SPDY_name_value_iterate (container, NULL, NULL) != ((i / 2) + 1)) 137 { 138 FAIL_TEST("SPDY_name_value_iterate failed\n"); 139 } 140 } 141 142 if(NULL != SPDY_name_value_lookup (container, "anything", &ret)) 143 { 144 FAIL_TEST("SPDY_name_value_lookup failed\n"); 145 } 146 147 for(i=size - 2; i >= 0; i-=2) 148 { 149 value = SPDY_name_value_lookup(container,pairs[i], &ret); 150 if(NULL == value || 1 !=ret || strcmp(value[0], pairs[i+1]) != 0) 151 { 152 printf("%p; %i; %i\n", value, ret, 153 (NULL == value) ? -1 : strcmp(value[0], pairs[i+1])); 154 FAIL_TEST("SPDY_name_value_lookup failed\n"); 155 } 156 } 157 158 SPDY_name_value_iterate (container, &iterate_cb, &cls); 159 160 cls = 0; 161 if(SPDY_name_value_iterate (container, &iterate_brake_cb, &cls) != brake_at) 162 { 163 FAIL_TEST("SPDY_name_value_iterate with brake failed\n"); 164 } 165 166 SPDY_name_value_destroy(container); 167 168 //check everything with NULL values 169 for(i=0; i<7; ++i) 170 { 171 printf("%i ",i); 172 ob1 = (i & 4) ? data : NULL; 173 ob2 = (i & 2) ? data : NULL; 174 ob3 = (i & 1) ? data : NULL; 175 if(SPDY_INPUT_ERROR != SPDY_name_value_add(ob1,ob2,ob3)) 176 { 177 FAIL_TEST("SPDY_name_value_add with NULLs failed\n"); 178 } 179 } 180 printf("\n"); 181 fflush(stdout); 182 183 if(SPDY_INPUT_ERROR != SPDY_name_value_iterate(NULL,NULL,NULL)) 184 { 185 FAIL_TEST("SPDY_name_value_iterate with NULLs failed\n"); 186 } 187 188 for(i=0; i<7; ++i) 189 { 190 printf("%i ",i); 191 ob1 = (i & 4) ? data : NULL; 192 ob2 = (i & 2) ? data : NULL; 193 ob3 = (i & 1) ? data : NULL; 194 if(NULL != SPDY_name_value_lookup(ob1,ob2,ob3)) 195 { 196 FAIL_TEST("SPDY_name_value_lookup with NULLs failed\n"); 197 } 198 } 199 printf("\n"); 200 SPDY_name_value_destroy(NULL); 201 202 if(NULL == (container = SPDY_name_value_create ())) 203 { 204 FAIL_TEST("SPDY_name_value_create failed\n"); 205 } 206 207 size = sizeof(pairs_with_dups)/sizeof(pairs_with_dups[0]); 208 209 for(i=0;i<size; i+=2) 210 { 211 if(SPDY_YES != SPDY_name_value_add(container,pairs_with_dups[i],pairs_with_dups[i+1])) 212 { 213 FAIL_TEST("SPDY_name_value_add failed\n"); 214 } 215 } 216 if(SPDY_name_value_iterate (container, NULL, NULL) != atoi(pairs_with_dups[size - 1])) 217 { 218 FAIL_TEST("SPDY_name_value_iterate failed\n"); 219 } 220 for(i=size - 2; i >= 0; i-=2) 221 { 222 value = SPDY_name_value_lookup(container,pairs_with_dups[i], &ret); 223 if(NULL == value) 224 { 225 FAIL_TEST("SPDY_name_value_lookup failed\n"); 226 } 227 flag = false; 228 for(j=0; j<ret; ++j) 229 if(0 == strcmp(pairs_with_dups[i + 1], value[j])) 230 { 231 if(flag) 232 FAIL_TEST("SPDY_name_value_lookup failed\n"); 233 flag=true; 234 } 235 236 if(!flag) 237 FAIL_TEST("SPDY_name_value_lookup failed\n"); 238 } 239 if(SPDY_NO != SPDY_name_value_add(container,pairs_with_dups[0],pairs_with_dups[1])) 240 FAIL_TEST("SPDY_name_value_add failed\n"); 241 242 SPDY_name_value_destroy(container); 243 244 if(NULL == (container = SPDY_name_value_create ())) 245 { 246 FAIL_TEST("SPDY_name_value_create failed\n"); 247 } 248 249 size = sizeof(pairs_with_empty)/sizeof(pairs_with_empty[0]); 250 251 for(i=0;i<size; i+=2) 252 { 253 if(SPDY_YES != SPDY_name_value_add(container,pairs_with_empty[i],pairs_with_empty[i+1])) 254 { 255 FAIL_TEST("SPDY_name_value_add failed\n"); 256 } 257 value = SPDY_name_value_lookup(container,pairs_with_empty[i], &ret); 258 if(NULL == value || 1 != ret) 259 { 260 printf("%p; %i\n", value, ret); 261 FAIL_TEST("SPDY_name_value_lookup failed\n"); 262 } 263 } 264 265 ret = SPDY_name_value_iterate(container, NULL, NULL); 266 if(SPDY_INPUT_ERROR != SPDY_name_value_add(container, "capitalLeter","anything") 267 || SPDY_name_value_iterate(container, NULL, NULL) != ret) 268 { 269 FAIL_TEST("SPDY_name_value_add failed\n"); 270 } 271 272 SPDY_name_value_destroy(container); 273 274 if(NULL == (container = SPDY_name_value_create ())) 275 { 276 FAIL_TEST("SPDY_name_value_create failed\n"); 277 } 278 279 size = sizeof(pairs_with_dups)/sizeof(pairs_with_dups[0]); 280 281 for(i=0;i<size; i+=2) 282 { 283 if(SPDY_YES != SPDY_name_value_add(container,pairs_with_dups[i],pairs_with_dups[i+1])) 284 { 285 FAIL_TEST("SPDY_name_value_add failed\n"); 286 } 287 } 288 289 if(NULL == (container2 = SPDY_name_value_create ())) 290 { 291 FAIL_TEST("SPDY_name_value_create failed\n"); 292 } 293 294 size2 = sizeof(pairs_different)/sizeof(pairs_different[0]); 295 296 for(i=0;i<size2; i+=2) 297 { 298 if(SPDY_YES != SPDY_name_value_add(container2,pairs_different[i],pairs_different[i+1])) 299 { 300 FAIL_TEST("SPDY_name_value_add failed\n"); 301 } 302 } 303 304 container_arr[0] = container; 305 container_arr[1] = container2; 306 if(0 > (ret = SPDYF_name_value_to_stream(container_arr, 2, &stream)) || NULL == stream) 307 FAIL_TEST("SPDYF_name_value_to_stream failed\n"); 308 ret = SPDYF_name_value_from_stream(stream, ret, &container3); 309 if(SPDY_YES != ret) 310 FAIL_TEST("SPDYF_name_value_from_stream failed\n"); 311 312 if(SPDY_name_value_iterate(container3, NULL, NULL) 313 != (SPDY_name_value_iterate(container, NULL, NULL) + SPDY_name_value_iterate(container2, NULL, NULL))) 314 FAIL_TEST("SPDYF_name_value_from_stream failed\n"); 315 316 for(i=size - 2; i >= 0; i-=2) 317 { 318 value = SPDY_name_value_lookup(container,pairs_with_dups[i], &ret); 319 if(NULL == value) 320 FAIL_TEST("SPDY_name_value_lookup failed\n"); 321 value2 = SPDY_name_value_lookup(container3,pairs_with_dups[i], &ret2); 322 if(NULL == value2) 323 FAIL_TEST("SPDY_name_value_lookup failed\n"); 324 325 for(j=0; j<ret; ++j) 326 if(0 != strcmp(value2[j], value[j])) 327 FAIL_TEST("SPDY_name_value_lookup failed\n"); 328 } 329 for(i=size2 - 2; i >= 0; i-=2) 330 { 331 value = SPDY_name_value_lookup(container2,pairs_different[i], &ret); 332 if(NULL == value) 333 FAIL_TEST("SPDY_name_value_lookup failed\n"); 334 value2 = SPDY_name_value_lookup(container3,pairs_different[i], &ret2); 335 if(NULL == value2) 336 FAIL_TEST("SPDY_name_value_lookup failed\n"); 337 338 for(j=0; j<ret; ++j) 339 if(0 != strcmp(value2[j], value[j])) 340 FAIL_TEST("SPDY_name_value_lookup failed\n"); 341 } 342 343 SPDY_deinit(); 344 345 return 0; 346 } 347