test-textfile.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. *
  4. * BlueZ - Bluetooth protocol stack for Linux
  5. *
  6. * Copyright (C) 2004-2010 Marcel Holtmann <marcel@holtmann.org>
  7. *
  8. *
  9. */
  10. #ifdef HAVE_CONFIG_H
  11. #include <config.h>
  12. #endif
  13. #define _GNU_SOURCE
  14. #include <stdio.h>
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include <stdlib.h>
  18. #include <string.h>
  19. #include <glib.h>
  20. #include "src/textfile.h"
  21. #include "src/shared/tester.h"
  22. static const char test_pathname[] = "/tmp/textfile";
  23. static void util_create_empty(void)
  24. {
  25. int fd;
  26. fd = creat(test_pathname, 0644);
  27. if (fd < 0)
  28. return;
  29. if (ftruncate(fd, 0) < 0)
  30. goto done;
  31. done:
  32. close(fd);
  33. }
  34. static void util_create_pagesize(void)
  35. {
  36. char value[512];
  37. unsigned int i;
  38. int fd, size;
  39. size = getpagesize();
  40. if (size < 0)
  41. return;
  42. fd = creat(test_pathname, 0644);
  43. if (fd < 0)
  44. return;
  45. if (ftruncate(fd, 0) < 0)
  46. goto done;
  47. memset(value, 0, sizeof(value));
  48. for (i = 0; i < (size / sizeof(value)); i++) {
  49. if (write(fd, value, sizeof(value)) < 0)
  50. break;
  51. }
  52. done:
  53. close(fd);
  54. }
  55. static void test_pagesize(const void *data)
  56. {
  57. char key[18], *str;
  58. int size;
  59. size = getpagesize();
  60. g_assert(size >= 4096);
  61. tester_debug("System uses a page size of %d bytes\n", size);
  62. util_create_pagesize();
  63. sprintf(key, "11:11:11:11:11:11");
  64. str = textfile_get(test_pathname, key);
  65. tester_debug("%s\n", str);
  66. g_assert(str == NULL);
  67. tester_test_passed();
  68. }
  69. static void test_delete(const void *data)
  70. {
  71. char key[18], value[512], *str;
  72. util_create_empty();
  73. sprintf(key, "00:00:00:00:00:00");
  74. g_assert(textfile_del(test_pathname, key) == 0);
  75. memset(value, 0, sizeof(value));
  76. g_assert(textfile_put(test_pathname, key, value) == 0);
  77. str = textfile_get(test_pathname, key);
  78. g_assert(str != NULL);
  79. tester_debug("%s\n", str);
  80. g_free(str);
  81. tester_test_passed();
  82. }
  83. static void test_overwrite(const void *data)
  84. {
  85. char key[18], value[512], *str;
  86. util_create_empty();
  87. sprintf(key, "00:00:00:00:00:00");
  88. memset(value, 0, sizeof(value));
  89. g_assert(textfile_put(test_pathname, key, value) == 0);
  90. snprintf(value, sizeof(value), "Test");
  91. g_assert(textfile_put(test_pathname, key, value) == 0);
  92. g_assert(textfile_put(test_pathname, key, value) == 0);
  93. g_assert(textfile_put(test_pathname, key, value) == 0);
  94. g_assert(textfile_del(test_pathname, key) == 0);
  95. str = textfile_get(test_pathname, key);
  96. tester_debug("%s\n", str);
  97. g_assert(str == NULL);
  98. tester_test_passed();
  99. }
  100. static void check_entry(char *key, char *value, void *data)
  101. {
  102. unsigned int max = GPOINTER_TO_UINT(data);
  103. unsigned int len;
  104. len = strtol(key + 16, NULL, 16);
  105. if (len == 1)
  106. len = max;
  107. if (g_test_verbose())
  108. g_print("%s %s\n", key, value);
  109. g_assert(strlen(value) == len);
  110. }
  111. static void test_multiple(const void *data)
  112. {
  113. char key[18], value[512], *str;
  114. unsigned int i, j, max = 10;
  115. util_create_empty();
  116. for (i = 1; i < max + 1; i++) {
  117. sprintf(key, "00:00:00:00:00:%02X", i);
  118. memset(value, 0, sizeof(value));
  119. for (j = 0; j < i; j++)
  120. value[j] = 'x';
  121. g_assert(textfile_put(test_pathname, key, value) == 0);
  122. str = textfile_get(test_pathname, key);
  123. tester_debug("%s %s\n", key, str);
  124. g_assert(str != NULL);
  125. g_assert(strcmp(str, value) == 0);
  126. free(str);
  127. }
  128. sprintf(key, "00:00:00:00:00:%02X", max);
  129. memset(value, 0, sizeof(value));
  130. for (j = 0; j < max; j++)
  131. value[j] = 'y';
  132. g_assert(textfile_put(test_pathname, key, value) == 0);
  133. str = textfile_get(test_pathname, key);
  134. tester_debug("%s %s\n", key, str);
  135. g_assert(str != NULL);
  136. g_assert(strcmp(str, value) == 0);
  137. free(str);
  138. sprintf(key, "00:00:00:00:00:%02X", 1);
  139. memset(value, 0, sizeof(value));
  140. for (j = 0; j < max; j++)
  141. value[j] = 'z';
  142. g_assert(textfile_put(test_pathname, key, value) == 0);
  143. str = textfile_get(test_pathname, key);
  144. tester_debug("%s %s\n", key, str);
  145. g_assert(str != NULL);
  146. g_assert(strcmp(str, value) == 0);
  147. free(str);
  148. for (i = 1; i < max + 1; i++) {
  149. sprintf(key, "00:00:00:00:00:%02X", i);
  150. str = textfile_get(test_pathname, key);
  151. tester_debug("%s %s\n", key, str);
  152. g_assert(str != NULL);
  153. if (i == 1)
  154. g_assert(strlen(str) == max);
  155. else
  156. g_assert(strlen(str) == i);
  157. g_free(str);
  158. }
  159. sprintf(key, "00:00:00:00:00:%02X", 2);
  160. g_assert(textfile_del(test_pathname, key) == 0);
  161. sprintf(key, "00:00:00:00:00:%02X", max - 3);
  162. g_assert(textfile_del(test_pathname, key) == 0);
  163. textfile_foreach(test_pathname, check_entry, GUINT_TO_POINTER(max));
  164. sprintf(key, "00:00:00:00:00:%02X", 1);
  165. g_assert(textfile_del(test_pathname, key) == 0);
  166. sprintf(key, "00:00:00:00:00:%02X", max);
  167. g_assert(textfile_del(test_pathname, key) == 0);
  168. sprintf(key, "00:00:00:00:00:%02X", max + 1);
  169. g_assert(textfile_del(test_pathname, key) == 0);
  170. textfile_foreach(test_pathname, check_entry, GUINT_TO_POINTER(max));
  171. tester_test_passed();
  172. }
  173. int main(int argc, char *argv[])
  174. {
  175. tester_init(&argc, &argv);
  176. tester_add("/textfile/pagesize", NULL, NULL, test_pagesize, NULL);
  177. tester_add("/textfile/delete", NULL, NULL, test_delete, NULL);
  178. tester_add("/textfile/overwrite", NULL, NULL, test_overwrite, NULL);
  179. tester_add("/textfile/multiple", NULL, NULL, test_multiple, NULL);
  180. return tester_run();
  181. }