2
0

dictionary-test.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /* libanode: the Anode C reference implementation
  2. * Copyright (C) 2009 Adam Ierymenko <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <string.h>
  18. #include <stdlib.h>
  19. #include <time.h>
  20. #include <sys/time.h>
  21. #include "../dictionary.h"
  22. static const char *HASH_TESTS[16] = {
  23. "test",
  24. "testt",
  25. "",
  26. "foo",
  27. "fooo",
  28. "1",
  29. "2",
  30. "3",
  31. "4",
  32. "11",
  33. "22",
  34. "33",
  35. "44",
  36. "adklfjklejrer",
  37. "erngnetbekjrq",
  38. "erklerqqqqre"
  39. };
  40. int diterate(void *arg,const char *key,const char *value)
  41. {
  42. printf(" %s: %s\n",key ? key : "(null)",value ? value : "(null)");
  43. return 1;
  44. }
  45. int main(int argc,char **argv)
  46. {
  47. char tmp[1024];
  48. char fuzzparam1[16],fuzzparam2[16],fuzzparam3[16];
  49. struct AnodeDictionary d;
  50. unsigned int i,j,k,cs;
  51. srandom(time(0));
  52. printf("Trying out hash function a little...\n");
  53. for(i=0;i<16;++i)
  54. printf(" %s: %u\n",HASH_TESTS[i],(unsigned int)AnodeDictionary__get_bucket(HASH_TESTS[i]));
  55. for(cs=0;cs<2;++cs) {
  56. printf("\nTesting with case sensitivity = %d\n",cs);
  57. AnodeDictionary_init(&d,cs);
  58. printf("\nTesting dictionary by adding and retrieving some keys...\n");
  59. AnodeDictionary_put(&d,"test1","This is the first test");
  60. AnodeDictionary_put(&d,"test2","This is the second test");
  61. AnodeDictionary_put(&d,"test3","This is the third test (lower case)");
  62. AnodeDictionary_put(&d,"TEST3","This is the third test (UPPER CASE)");
  63. AnodeDictionary_iterate(&d,(void *)0,&diterate);
  64. if (d.size != (cs ? 4 : 3)) {
  65. printf("Failed (size).\n");
  66. return 1;
  67. }
  68. AnodeDictionary_clear(&d);
  69. if (d.size||(AnodeDictionary_get(&d,"test1"))) {
  70. printf("Failed (clear).\n");
  71. return 1;
  72. }
  73. printf("\nTesting read, trial 1: simple key=value with unterminated line\n");
  74. strcpy(tmp,"foo=bar\nbar=baz\ntest1=Happy happy joyjoy!\ntest2=foobarbaz\nlinewithnocr=thisworked");
  75. AnodeDictionary_read(&d,tmp,"\r\n","=","",'\\',0,0);
  76. printf("Results:\n");
  77. AnodeDictionary_iterate(&d,(void *)0,&diterate);
  78. AnodeDictionary_clear(&d);
  79. printf("\nTesting read, trial 2: key=value with escape chars, escaped CRs\n");
  80. strcpy(tmp,"foo=bar\r\nbar==baz\nte\\=st1=\\=Happy happy joyjoy!\ntest2=foobarbaz\\\nfoobarbaz on next line\r\n");
  81. AnodeDictionary_read(&d,tmp,"\r\n","=","",'\\',0,0);
  82. printf("Results:\n");
  83. AnodeDictionary_iterate(&d,(void *)0,&diterate);
  84. AnodeDictionary_clear(&d);
  85. printf("\nTesting read, trial 3: HTTP header-like dictionary\n");
  86. strcpy(tmp,"Host: some.host.net\r\nX-Some-Header: foo bar\r\nX-Some-Other-Header: y0y0y0y0y0\r\n");
  87. AnodeDictionary_read(&d,tmp,"\r\n",": ","",0,0,0);
  88. printf("Results:\n");
  89. AnodeDictionary_iterate(&d,(void *)0,&diterate);
  90. AnodeDictionary_clear(&d);
  91. printf("\nTesting read, trial 4: single line key/value\n");
  92. strcpy(tmp,"Header: one line only");
  93. AnodeDictionary_read(&d,tmp,"\r\n",": ","",0,0,0);
  94. printf("Results:\n");
  95. AnodeDictionary_iterate(&d,(void *)0,&diterate);
  96. AnodeDictionary_clear(&d);
  97. printf("\nFuzzing dictionary reader...\n"); fflush(stdout);
  98. for(i=0;i<200000;++i) {
  99. j = random() % (sizeof(tmp) - 1);
  100. for(k=0;k<j;++k) {
  101. tmp[k] = (char)((unsigned int)random() >> 3);
  102. if (!tmp[k]) tmp[k] = 1;
  103. }
  104. tmp[j] = (char)0;
  105. j = random() % (sizeof(fuzzparam1) - 1);
  106. for(k=0;k<j;++k) {
  107. fuzzparam1[k] = (char)((unsigned int)random() >> 3);
  108. if (!fuzzparam1[k]) fuzzparam1[k] = 1;
  109. }
  110. fuzzparam1[j] = (char)0;
  111. j = random() % (sizeof(fuzzparam2) - 1);
  112. for(k=0;k<j;++k) {
  113. fuzzparam1[k] = (char)((unsigned int)random() >> 3);
  114. if (!fuzzparam2[k]) fuzzparam2[k] = 1;
  115. }
  116. fuzzparam2[j] = (char)0;
  117. j = random() % (sizeof(fuzzparam3) - 1);
  118. for(k=0;k<j;++k) {
  119. fuzzparam3[k] = (char)((unsigned int)random() >> 3);
  120. if (!fuzzparam3[k]) fuzzparam3[k] = 1;
  121. }
  122. fuzzparam3[j] = (char)0;
  123. AnodeDictionary_read(&d,tmp,fuzzparam1,fuzzparam2,fuzzparam3,random() & 3,random() & 1,random() & 1);
  124. AnodeDictionary_clear(&d);
  125. }
  126. AnodeDictionary_destroy(&d);
  127. }
  128. return 0;
  129. }