25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

197 lines
4.3 KiB

  1. /* Copyright (c) 2014, Google Inc.
  2. *
  3. * Permission to use, copy, modify, and/or distribute this software for any
  4. * purpose with or without fee is hereby granted, provided that the above
  5. * copyright notice and this permission notice appear in all copies.
  6. *
  7. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  8. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  9. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  10. * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  11. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
  12. * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
  13. * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */
  14. #if !defined(_POSIX_C_SOURCE)
  15. #define _POSIX_C_SOURCE 201410L
  16. #endif
  17. #include <openssl/crypto.h>
  18. #include <openssl/lhash.h>
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. struct dummy_lhash_node {
  23. char *s;
  24. struct dummy_lhash_node *next;
  25. };
  26. struct dummy_lhash {
  27. struct dummy_lhash_node *head;
  28. };
  29. static void dummy_lh_free(struct dummy_lhash *lh) {
  30. struct dummy_lhash_node *cur, *next;
  31. for (cur = lh->head; cur != NULL; cur = next) {
  32. next = cur->next;
  33. free(cur->s);
  34. free(cur);
  35. }
  36. }
  37. static size_t dummy_lh_num_items(const struct dummy_lhash *lh) {
  38. size_t count = 0;
  39. struct dummy_lhash_node *cur;
  40. for (cur = lh->head; cur != NULL; cur = cur->next) {
  41. count++;
  42. }
  43. return count;
  44. }
  45. static char *dummy_lh_retrieve(struct dummy_lhash *lh, const char *s) {
  46. struct dummy_lhash_node *cur;
  47. for (cur = lh->head; cur != NULL; cur = cur->next) {
  48. if (strcmp(cur->s, s) == 0) {
  49. return cur->s;
  50. }
  51. }
  52. return NULL;
  53. }
  54. static int dummy_lh_insert(struct dummy_lhash *lh, char **old_data, char *s) {
  55. struct dummy_lhash_node *node, *cur;
  56. for (cur = lh->head; cur != NULL; cur = cur->next) {
  57. if (strcmp(cur->s, s) == 0) {
  58. *old_data = cur->s;
  59. cur->s = s;
  60. return 1;
  61. }
  62. }
  63. node = malloc(sizeof(struct dummy_lhash_node));
  64. *old_data = NULL;
  65. node->s = s;
  66. node->next = lh->head;
  67. lh->head = node;
  68. return 1;
  69. }
  70. static char *dummy_lh_delete(struct dummy_lhash *lh, const void *s) {
  71. struct dummy_lhash_node *cur, **next_ptr;
  72. char *ret;
  73. next_ptr = &lh->head;
  74. for (cur = lh->head; cur != NULL; cur = cur->next) {
  75. if (strcmp(cur->s, s) == 0) {
  76. ret = cur->s;
  77. *next_ptr = cur->next;
  78. free(cur);
  79. return ret;
  80. }
  81. next_ptr = &cur->next;
  82. }
  83. return NULL;
  84. }
  85. static char *rand_string(void) {
  86. unsigned len = 1 + (rand() % 3);
  87. char *ret = malloc(len + 1);
  88. unsigned i;
  89. for (i = 0; i < len; i++) {
  90. ret[i] = '0' + (rand() & 7);
  91. }
  92. ret[i] = 0;
  93. return ret;
  94. }
  95. int main(int argc, char **argv) {
  96. _LHASH *lh;
  97. struct dummy_lhash dummy_lh = {NULL};
  98. unsigned i;
  99. CRYPTO_library_init();
  100. lh = lh_new(NULL, NULL);
  101. for (i = 0; i < 100000; i++) {
  102. unsigned action;
  103. char *s, *s1, *s2;
  104. if (dummy_lh_num_items(&dummy_lh) != lh_num_items(lh)) {
  105. fprintf(stderr, "Length mismatch\n");
  106. return 1;
  107. }
  108. action = rand() % 3;
  109. switch (action) {
  110. case 0:
  111. s = rand_string();
  112. s1 = (char *)lh_retrieve(lh, s);
  113. s2 = dummy_lh_retrieve(&dummy_lh, s);
  114. if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) {
  115. fprintf(stderr, "lh_retrieve failure\n");
  116. abort();
  117. }
  118. free(s);
  119. break;
  120. case 1:
  121. s = rand_string();
  122. lh_insert(lh, (void **)&s1, s);
  123. dummy_lh_insert(&dummy_lh, &s2, strdup(s));
  124. if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) {
  125. fprintf(stderr, "lh_insert failure\n");
  126. abort();
  127. }
  128. if (s1) {
  129. free(s1);
  130. }
  131. if (s2) {
  132. free(s2);
  133. }
  134. break;
  135. case 2:
  136. s = rand_string();
  137. s1 = lh_delete(lh, s);
  138. s2 = dummy_lh_delete(&dummy_lh, s);
  139. if (s1 != NULL && (s2 == NULL || strcmp(s1, s2) != 0)) {
  140. fprintf(stderr, "lh_insert failure\n");
  141. abort();
  142. }
  143. if (s1) {
  144. free(s1);
  145. }
  146. if (s2) {
  147. free(s2);
  148. }
  149. free(s);
  150. break;
  151. default:
  152. abort();
  153. }
  154. }
  155. lh_doall(lh, free);
  156. lh_free(lh);
  157. dummy_lh_free(&dummy_lh);
  158. printf("PASS\n");
  159. return 0;
  160. }