misc-test.c 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 "../misc.h"
  22. int main(int argc,char **argv)
  23. {
  24. const char *base32TestStr = "asdf";
  25. char *fields[16];
  26. char buf[1024];
  27. char buf2[1024];
  28. char buf3[4096];
  29. unsigned int i;
  30. unsigned long tmpl,tmpl2;
  31. unsigned long long tmp64;
  32. srand(time(0));
  33. Anode_base32_5_to_8((const unsigned char *)base32TestStr,buf);
  34. printf("Base32 from test string: %s\n",buf);
  35. Anode_base32_8_to_5("MFZWIZQA",(unsigned char *)buf2);
  36. printf("Test string from Base32 (upper case): %s\n",buf2);
  37. Anode_base32_8_to_5("mfzwizqa",(unsigned char *)buf2);
  38. printf("Test string from Base32 (lower case): %s\n",buf2);
  39. printf("Testing variable length encoding/decoded with pad5 functions...\n");
  40. for(i=0;i<1024;++i) {
  41. tmpl = rand() % (sizeof(buf) - 8);
  42. if (!tmpl)
  43. tmpl = 1;
  44. for(tmpl2=0;tmpl2<tmpl;++tmpl2)
  45. buf[tmpl2] = (buf2[tmpl2] = (char)(rand() >> 3));
  46. if (!Anode_base32_encode_pad5(buf2,tmpl,buf3,sizeof(buf3))) {
  47. printf("Failed (encode failed).\n");
  48. return 1;
  49. }
  50. memset(buf2,0,sizeof(buf2));
  51. if (!Anode_base32_decode_pad5(buf3,buf2,sizeof(buf2))) {
  52. printf("Failed (decode failed).\n");
  53. return 1;
  54. }
  55. if (memcmp(buf,buf2,tmpl)) {
  56. printf("Failed (compare failed).\n");
  57. return 1;
  58. }
  59. }
  60. printf("Anode_htonll(0x0102030405060708) == 0x%.16llx\n",tmp64 = Anode_htonll(0x0102030405060708ULL));
  61. printf("Anode_ntohll(0x%.16llx) == 0x%.16llx\n",tmp64,Anode_ntohll(tmp64));
  62. if (Anode_ntohll(tmp64) != 0x0102030405060708ULL) {
  63. printf("Failed.\n");
  64. return 1;
  65. }
  66. strcpy(buf,"foo bar baz");
  67. Anode_trim(buf);
  68. printf("Testing string trim: 'foo bar baz' -> '%s'\n",buf);
  69. strcpy(buf,"foo bar baz ");
  70. Anode_trim(buf);
  71. printf("Testing string trim: 'foo bar baz ' -> '%s'\n",buf);
  72. strcpy(buf," foo bar baz");
  73. Anode_trim(buf);
  74. printf("Testing string trim: ' foo bar baz' -> '%s'\n",buf);
  75. strcpy(buf," foo bar baz ");
  76. Anode_trim(buf);
  77. printf("Testing string trim: ' foo bar baz ' -> '%s'\n",buf);
  78. strcpy(buf,"");
  79. Anode_trim(buf);
  80. printf("Testing string trim: '' -> '%s'\n",buf);
  81. strcpy(buf," ");
  82. Anode_trim(buf);
  83. printf("Testing string trim: ' ' -> '%s'\n",buf);
  84. printf("Testing string split.\n");
  85. strcpy(buf,"66.246.138.121,5323,0");
  86. i = Anode_split(buf,';',fields,16);
  87. if (i != 1) {
  88. printf("Failed.\n");
  89. return 1;
  90. } else printf("Fields: %s\n",fields[0]);
  91. strcpy(buf,"a;b;c");
  92. i = Anode_split(buf,';',fields,16);
  93. if (i != 3) {
  94. printf("Failed.\n");
  95. return 1;
  96. } else printf("Fields: %s %s %s\n",fields[0],fields[1],fields[2]);
  97. strcpy(buf,";;");
  98. i = Anode_split(buf,';',fields,16);
  99. if (i != 3) {
  100. printf("Failed.\n");
  101. return 1;
  102. } else printf("Fields: %s %s %s\n",fields[0],fields[1],fields[2]);
  103. strcpy(buf,"a;b;");
  104. i = Anode_split(buf,';',fields,16);
  105. if (i != 3) {
  106. printf("Failed.\n");
  107. return 1;
  108. } else printf("Fields: %s %s %s\n",fields[0],fields[1],fields[2]);
  109. strcpy(buf,"a;;c");
  110. i = Anode_split(buf,';',fields,16);
  111. if (i != 3) {
  112. printf("Failed.\n");
  113. return 1;
  114. } else printf("Fields: %s %s %s\n",fields[0],fields[1],fields[2]);
  115. strcpy(buf,";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;");
  116. i = Anode_split(buf,';',fields,16);
  117. if (i != 16) {
  118. printf("Failed.\n");
  119. return 1;
  120. }
  121. strcpy(buf,"");
  122. i = Anode_split(buf,';',fields,16);
  123. if (i != 0) {
  124. printf("Failed.\n");
  125. return 1;
  126. }
  127. printf("Passed.\n");
  128. return 0;
  129. }