abuf.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Copyright 2021 Google LLC
  4. * Written by Simon Glass <sjg@chromium.org>
  5. */
  6. #include <common.h>
  7. #include <abuf.h>
  8. #include <mapmem.h>
  9. #include <test/lib.h>
  10. #include <test/test.h>
  11. #include <test/ut.h>
  12. static char test_data[] = "1234";
  13. #define TEST_DATA_LEN sizeof(test_data)
  14. /* Test abuf_set() */
  15. static int lib_test_abuf_set(struct unit_test_state *uts)
  16. {
  17. struct abuf buf;
  18. ulong start;
  19. start = ut_check_free();
  20. abuf_init(&buf);
  21. abuf_set(&buf, test_data, TEST_DATA_LEN);
  22. ut_asserteq_ptr(test_data, buf.data);
  23. ut_asserteq(TEST_DATA_LEN, buf.size);
  24. ut_asserteq(false, buf.alloced);
  25. /* Force it to allocate */
  26. ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN + 1));
  27. ut_assertnonnull(buf.data);
  28. ut_asserteq(TEST_DATA_LEN + 1, buf.size);
  29. ut_asserteq(true, buf.alloced);
  30. /* Now set it again, to force it to free */
  31. abuf_set(&buf, test_data, TEST_DATA_LEN);
  32. ut_asserteq_ptr(test_data, buf.data);
  33. ut_asserteq(TEST_DATA_LEN, buf.size);
  34. ut_asserteq(false, buf.alloced);
  35. /* Check for memory leaks */
  36. ut_assertok(ut_check_delta(start));
  37. return 0;
  38. }
  39. LIB_TEST(lib_test_abuf_set, 0);
  40. /* Test abuf_map_sysmem() */
  41. static int lib_test_abuf_map_sysmem(struct unit_test_state *uts)
  42. {
  43. struct abuf buf;
  44. ulong addr;
  45. abuf_init(&buf);
  46. addr = 0x100;
  47. abuf_map_sysmem(&buf, addr, TEST_DATA_LEN);
  48. ut_asserteq_ptr(map_sysmem(0x100, 0), buf.data);
  49. ut_asserteq(TEST_DATA_LEN, buf.size);
  50. ut_asserteq(false, buf.alloced);
  51. return 0;
  52. }
  53. LIB_TEST(lib_test_abuf_map_sysmem, 0);
  54. /* Test abuf_realloc() */
  55. static int lib_test_abuf_realloc(struct unit_test_state *uts)
  56. {
  57. struct abuf buf;
  58. ulong start;
  59. void *ptr;
  60. /*
  61. * TODO: crashes on sandbox sometimes due to an apparent bug in
  62. * realloc().
  63. */
  64. return 0;
  65. start = ut_check_free();
  66. abuf_init(&buf);
  67. /* Allocate an empty buffer */
  68. ut_asserteq(true, abuf_realloc(&buf, 0));
  69. ut_assertnull(buf.data);
  70. ut_asserteq(0, buf.size);
  71. ut_asserteq(false, buf.alloced);
  72. /* Allocate a non-empty abuf */
  73. ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
  74. ut_assertnonnull(buf.data);
  75. ut_asserteq(TEST_DATA_LEN, buf.size);
  76. ut_asserteq(true, buf.alloced);
  77. ptr = buf.data;
  78. /*
  79. * Make it smaller; the pointer should remain the same. Note this relies
  80. * on knowledge of how U-Boot's realloc() works
  81. */
  82. ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN - 1));
  83. ut_asserteq(TEST_DATA_LEN - 1, buf.size);
  84. ut_asserteq(true, buf.alloced);
  85. ut_asserteq_ptr(ptr, buf.data);
  86. /*
  87. * Make it larger, forcing reallocation. Note this relies on knowledge
  88. * of how U-Boot's realloc() works
  89. */
  90. ut_asserteq(true, abuf_realloc(&buf, 0x1000));
  91. ut_assert(buf.data != ptr);
  92. ut_asserteq(0x1000, buf.size);
  93. ut_asserteq(true, buf.alloced);
  94. /* Free it */
  95. ut_asserteq(true, abuf_realloc(&buf, 0));
  96. ut_assertnull(buf.data);
  97. ut_asserteq(0, buf.size);
  98. ut_asserteq(false, buf.alloced);
  99. /* Check for memory leaks */
  100. ut_assertok(ut_check_delta(start));
  101. return 0;
  102. }
  103. LIB_TEST(lib_test_abuf_realloc, 0);
  104. /* Test abuf_realloc() on an non-allocated buffer of zero size */
  105. static int lib_test_abuf_realloc_size(struct unit_test_state *uts)
  106. {
  107. struct abuf buf;
  108. ulong start;
  109. start = ut_check_free();
  110. abuf_init(&buf);
  111. /* Allocate some space */
  112. ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
  113. ut_assertnonnull(buf.data);
  114. ut_asserteq(TEST_DATA_LEN, buf.size);
  115. ut_asserteq(true, buf.alloced);
  116. /* Free it */
  117. ut_asserteq(true, abuf_realloc(&buf, 0));
  118. ut_assertnull(buf.data);
  119. ut_asserteq(0, buf.size);
  120. ut_asserteq(false, buf.alloced);
  121. /* Check for memory leaks */
  122. ut_assertok(ut_check_delta(start));
  123. return 0;
  124. }
  125. LIB_TEST(lib_test_abuf_realloc_size, 0);
  126. /* Test handling of buffers that are too large */
  127. static int lib_test_abuf_large(struct unit_test_state *uts)
  128. {
  129. struct abuf buf;
  130. ulong start;
  131. size_t size;
  132. int delta;
  133. void *ptr;
  134. /*
  135. * This crashes at present due to trying to allocate more memory than
  136. * available, which breaks something on sandbox.
  137. */
  138. return 0;
  139. start = ut_check_free();
  140. /* Try an impossible size */
  141. abuf_init(&buf);
  142. ut_asserteq(false, abuf_realloc(&buf, CONFIG_SYS_MALLOC_LEN));
  143. ut_assertnull(buf.data);
  144. ut_asserteq(0, buf.size);
  145. ut_asserteq(false, buf.alloced);
  146. abuf_uninit(&buf);
  147. ut_assertnull(buf.data);
  148. ut_asserteq(0, buf.size);
  149. ut_asserteq(false, buf.alloced);
  150. /* Start with a normal size then try to increase it, to check realloc */
  151. ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
  152. ut_assertnonnull(buf.data);
  153. ut_asserteq(TEST_DATA_LEN, buf.size);
  154. ut_asserteq(true, buf.alloced);
  155. ptr = buf.data;
  156. delta = ut_check_delta(start);
  157. ut_assert(delta > 0);
  158. /* try to increase it */
  159. ut_asserteq(false, abuf_realloc(&buf, CONFIG_SYS_MALLOC_LEN));
  160. ut_asserteq_ptr(ptr, buf.data);
  161. ut_asserteq(TEST_DATA_LEN, buf.size);
  162. ut_asserteq(true, buf.alloced);
  163. ut_asserteq(delta, ut_check_delta(start));
  164. /* Check for memory leaks */
  165. abuf_uninit(&buf);
  166. ut_assertok(ut_check_delta(start));
  167. /* Start with a huge unallocated buf and try to move it */
  168. abuf_init(&buf);
  169. abuf_map_sysmem(&buf, 0, CONFIG_SYS_MALLOC_LEN);
  170. ut_asserteq(CONFIG_SYS_MALLOC_LEN, buf.size);
  171. ut_asserteq(false, buf.alloced);
  172. ut_assertnull(abuf_uninit_move(&buf, &size));
  173. /* Check for memory leaks */
  174. abuf_uninit(&buf);
  175. ut_assertok(ut_check_delta(start));
  176. return 0;
  177. }
  178. LIB_TEST(lib_test_abuf_large, 0);
  179. /* Test abuf_uninit_move() */
  180. static int lib_test_abuf_uninit_move(struct unit_test_state *uts)
  181. {
  182. void *ptr, *orig_ptr;
  183. struct abuf buf;
  184. size_t size;
  185. ulong start;
  186. int delta;
  187. start = ut_check_free();
  188. /*
  189. * TODO: crashes on sandbox sometimes due to an apparent bug in
  190. * realloc().
  191. */
  192. return 0;
  193. /* Move an empty buffer */
  194. abuf_init(&buf);
  195. ut_assertnull(abuf_uninit_move(&buf, &size));
  196. ut_asserteq(0, size);
  197. ut_assertnull(abuf_uninit_move(&buf, NULL));
  198. /* Move an unallocated buffer */
  199. abuf_set(&buf, test_data, TEST_DATA_LEN);
  200. ut_assertok(ut_check_delta(start));
  201. ptr = abuf_uninit_move(&buf, &size);
  202. ut_asserteq(TEST_DATA_LEN, size);
  203. ut_asserteq_str(ptr, test_data);
  204. ut_assertnonnull(ptr);
  205. ut_assertnull(buf.data);
  206. ut_asserteq(0, buf.size);
  207. ut_asserteq(false, buf.alloced);
  208. /* Check that freeing it frees the only allocation */
  209. delta = ut_check_delta(start);
  210. ut_assert(delta > 0);
  211. free(ptr);
  212. ut_assertok(ut_check_delta(start));
  213. /* Move an allocated buffer */
  214. ut_asserteq(true, abuf_realloc(&buf, TEST_DATA_LEN));
  215. orig_ptr = buf.data;
  216. strcpy(orig_ptr, test_data);
  217. delta = ut_check_delta(start);
  218. ut_assert(delta > 0);
  219. ptr = abuf_uninit_move(&buf, &size);
  220. ut_asserteq(TEST_DATA_LEN, size);
  221. ut_assertnonnull(ptr);
  222. ut_asserteq_ptr(ptr, orig_ptr);
  223. ut_asserteq_str(ptr, test_data);
  224. ut_assertnull(buf.data);
  225. ut_asserteq(0, buf.size);
  226. ut_asserteq(false, buf.alloced);
  227. /* Check there was no new allocation */
  228. ut_asserteq(delta, ut_check_delta(start));
  229. /* Check that freeing it frees the only allocation */
  230. free(ptr);
  231. ut_assertok(ut_check_delta(start));
  232. /* Move an unallocated buffer, without the size */
  233. abuf_set(&buf, test_data, TEST_DATA_LEN);
  234. ut_assertok(ut_check_delta(start));
  235. ptr = abuf_uninit_move(&buf, NULL);
  236. ut_asserteq_str(ptr, test_data);
  237. return 0;
  238. }
  239. LIB_TEST(lib_test_abuf_uninit_move, 0);
  240. /* Test abuf_uninit() */
  241. static int lib_test_abuf_uninit(struct unit_test_state *uts)
  242. {
  243. struct abuf buf;
  244. /* Nothing in the buffer */
  245. abuf_init(&buf);
  246. abuf_uninit(&buf);
  247. ut_assertnull(buf.data);
  248. ut_asserteq(0, buf.size);
  249. ut_asserteq(false, buf.alloced);
  250. /* Not allocated */
  251. abuf_set(&buf, test_data, TEST_DATA_LEN);
  252. abuf_uninit(&buf);
  253. ut_assertnull(buf.data);
  254. ut_asserteq(0, buf.size);
  255. ut_asserteq(false, buf.alloced);
  256. return 0;
  257. }
  258. LIB_TEST(lib_test_abuf_uninit, 0);
  259. /* Test abuf_init_set() */
  260. static int lib_test_abuf_init_set(struct unit_test_state *uts)
  261. {
  262. struct abuf buf;
  263. abuf_init_set(&buf, test_data, TEST_DATA_LEN);
  264. ut_asserteq_ptr(test_data, buf.data);
  265. ut_asserteq(TEST_DATA_LEN, buf.size);
  266. ut_asserteq(false, buf.alloced);
  267. return 0;
  268. }
  269. LIB_TEST(lib_test_abuf_init_set, 0);
  270. /* Test abuf_init_move() */
  271. static int lib_test_abuf_init_move(struct unit_test_state *uts)
  272. {
  273. struct abuf buf;
  274. void *ptr;
  275. /*
  276. * TODO: crashes on sandbox sometimes due to an apparent bug in
  277. * realloc().
  278. */
  279. return 0;
  280. ptr = strdup(test_data);
  281. ut_assertnonnull(ptr);
  282. free(ptr);
  283. abuf_init_move(&buf, ptr, TEST_DATA_LEN);
  284. ut_asserteq_ptr(ptr, abuf_data(&buf));
  285. ut_asserteq(TEST_DATA_LEN, abuf_size(&buf));
  286. ut_asserteq(true, buf.alloced);
  287. return 0;
  288. }
  289. LIB_TEST(lib_test_abuf_init_move, 0);
  290. /* Test abuf_init() */
  291. static int lib_test_abuf_init(struct unit_test_state *uts)
  292. {
  293. struct abuf buf;
  294. buf.data = &buf;
  295. buf.size = 123;
  296. buf.alloced = true;
  297. abuf_init(&buf);
  298. ut_assertnull(buf.data);
  299. ut_asserteq(0, buf.size);
  300. ut_asserteq(false, buf.alloced);
  301. return 0;
  302. }
  303. LIB_TEST(lib_test_abuf_init, 0);