libubi_test.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. #include <stdarg.h>
  2. #include <setjmp.h>
  3. #include <stddef.h>
  4. #include <stdint.h>
  5. #include <unistd.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <sys/ioctl.h>
  12. #include <cmocka.h>
  13. #include "libubi.h"
  14. #include "test_lib.h"
  15. static void test_libubi_open(void **state)
  16. {
  17. libubi_t lib = NULL;
  18. expect_open(SYSFS_ROOT "/class/ubi", O_RDONLY, 4);
  19. expect_close(4,0);
  20. expect_open(SYSFS_ROOT "/class/ubi/version", O_RDONLY, 0);
  21. expect_read_real(50,0);
  22. expect_close(3,1);
  23. lib = libubi_open();
  24. assert_non_null(lib);
  25. libubi_close(lib);
  26. (void) state;
  27. }
  28. static void test_ubi_vol_block_create(void **state)
  29. {
  30. int mock_fd = 1;
  31. expect_ioctl_short(UBI_IOCVOLCRBLK, 0);
  32. int r = ubi_vol_block_create(mock_fd);
  33. assert_int_equal(r, 0);
  34. (void) state;
  35. }
  36. static void test_ubi_vol_block_remove(void **state)
  37. {
  38. int mock_fd = 1;
  39. expect_ioctl_short(UBI_IOCVOLRMBLK, 0);
  40. int r = ubi_vol_block_remove(mock_fd);
  41. assert_int_equal(r, 0);
  42. (void) state;
  43. }
  44. static void test_ubi_leb_unmap(void **state)
  45. {
  46. int mock_fd = 1;
  47. int lnum = 12;
  48. expect_ioctl(UBI_IOCEBUNMAP, 0, &lnum);
  49. int r = ubi_leb_unmap(mock_fd, lnum);
  50. assert_int_equal(r, 0);
  51. (void) state;
  52. }
  53. static void test_ubi_is_mapped(void **state)
  54. {
  55. int mock_fd = 1;
  56. int lnum = 1;
  57. expect_ioctl(UBI_IOCEBISMAP, 0, &lnum);
  58. int r = ubi_is_mapped(mock_fd, lnum);
  59. assert_int_equal(r, 0);
  60. (void) state;
  61. }
  62. static void test_ubi_update_start(void **state)
  63. {
  64. int mock_fd = 1;
  65. long long bytes = 0x1234;
  66. expect_ioctl(UBI_IOCVOLUP, 0, &bytes);
  67. int r = ubi_update_start(NULL, mock_fd, bytes);
  68. assert_int_equal(r, 0);
  69. (void) state;
  70. }
  71. static libubi_t mock_libubi_open()
  72. {
  73. expect_open(SYSFS_ROOT "/class/ubi", O_RDONLY, 4);
  74. expect_close(4,0);
  75. expect_open(SYSFS_ROOT "/class/ubi/version", O_RDONLY, 0);
  76. expect_read_real(50,0);
  77. expect_close(3,1);
  78. libubi_t lib = libubi_open();
  79. assert_non_null(lib);
  80. return lib;
  81. }
  82. static void test_ubi_dev_present(void **state)
  83. {
  84. libubi_t lib = mock_libubi_open();
  85. int r = ubi_dev_present(lib, 0);
  86. assert_int_equal(r, 1);
  87. libubi_close(lib);
  88. (void) state;
  89. }
  90. static void test_ubi_rsvol(void **state)
  91. {
  92. const char *node = "/foo";
  93. int vol_id = 0;
  94. long long bytes = 0xadadaf;
  95. struct ubi_rsvol_req req;
  96. memset(&req, 0, sizeof(req));
  97. req.bytes = bytes;
  98. req.vol_id = vol_id;
  99. expect_open(node, O_RDONLY, 4);
  100. expect_ioctl(UBI_IOCRSVOL, 0, &req);
  101. expect_close(4, 0);
  102. int r = ubi_rsvol(NULL, node, vol_id, bytes);
  103. assert_int_equal(r, 0);
  104. (void) state;
  105. }
  106. static void test_ubi_rnvols(void **state)
  107. {
  108. libubi_t lib = mock_libubi_open();
  109. const char *node = "/foo";
  110. struct ubi_rnvol_req req;
  111. memset(&req, 0xaf, sizeof(req));
  112. expect_open(node, O_RDONLY, 4);
  113. expect_ioctl(UBI_IOCRNVOL, 0, &req);
  114. expect_close(4, 0);
  115. int r = ubi_rnvols(lib, node, &req);
  116. assert_int_equal(r, 0);
  117. libubi_close(lib);
  118. (void) state;
  119. }
  120. static void test_ubi_rmvol(void **state)
  121. {
  122. libubi_t lib = mock_libubi_open();
  123. const char *node = "/foo";
  124. int vol_id = 12;
  125. expect_open(node, O_RDONLY, 4);
  126. expect_ioctl(UBI_IOCRMVOL, 0, &vol_id);
  127. expect_close(4, 0);
  128. int r = ubi_rmvol(lib, node, vol_id);
  129. assert_int_equal(r, 0);
  130. libubi_close(lib);
  131. (void) state;
  132. }
  133. static void test_ubi_leb_change_start(void **state)
  134. {
  135. libubi_t lib = mock_libubi_open();
  136. int mock_fd = 1;
  137. int lnum = 12;
  138. int bytes = 48;
  139. struct ubi_leb_change_req req;
  140. memset(&req, 0, sizeof(req));
  141. req.lnum = lnum;
  142. req.bytes = bytes;
  143. req.dtype = 3;
  144. expect_ioctl(UBI_IOCEBCH, 0, &req);
  145. int r = ubi_leb_change_start(lib, mock_fd, lnum, bytes);
  146. assert_int_equal(r, 0);
  147. libubi_close(lib);
  148. (void) state;
  149. }
  150. static void test_ubi_get_info(void **state)
  151. {
  152. libubi_t lib = mock_libubi_open();
  153. struct ubi_info info;
  154. expect_open(SYSFS_ROOT "/class/misc/ubi_ctrl/dev", O_RDONLY, 0);
  155. expect_read_real(50,0);
  156. expect_read(1,0);
  157. expect_close(3,1);
  158. expect_open(SYSFS_ROOT "/class/ubi/version", O_RDONLY, 0);
  159. expect_read_real(50,0);
  160. expect_close(3,1);
  161. int r = ubi_get_info(lib, &info);
  162. assert_int_equal(r, 0);
  163. assert_int_equal(info.dev_count, 1);
  164. libubi_close(lib);
  165. (void) state;
  166. }
  167. static void test_ubi_mkvol(void **state)
  168. {
  169. libubi_t lib = mock_libubi_open();
  170. const char *node = "/foo";
  171. const char *vol_name = "testvol";
  172. int vol_id = 12;
  173. struct ubi_mkvol_request req;
  174. struct ubi_mkvol_req rr;
  175. memset(&rr, 0, sizeof(rr));
  176. memset(&req, 0, sizeof(req));
  177. req.vol_id = vol_id;
  178. req.name = vol_name;
  179. rr.vol_id = vol_id;
  180. rr.name_len = strlen(vol_name);
  181. strncpy(rr.name, vol_name, UBI_MAX_VOLUME_NAME + 1);
  182. expect_open(node, O_RDONLY, 3);
  183. expect_ioctl(UBI_IOCMKVOL, 0, &rr);
  184. expect_close(3,0);
  185. int r = ubi_mkvol(lib, node, &req);
  186. assert_int_equal(r, 0);
  187. assert_int_equal(req.vol_id, vol_id);
  188. libubi_close(lib);
  189. (void) state;
  190. }
  191. void test_ubi_remove_dev(void **state)
  192. {
  193. const char *node = "/foo";
  194. libubi_t lib = mock_libubi_open();
  195. int ubi_dev = 0xAA;
  196. expect_open(node, O_RDONLY, 4);
  197. expect_ioctl(UBI_IOCDET, 0, &ubi_dev);
  198. expect_close(4,0);
  199. int r = ubi_remove_dev(lib, node, ubi_dev);
  200. assert_int_equal(r, 0);
  201. libubi_close(lib);
  202. (void) state;
  203. }
  204. void test_ubi_attach(void **state)
  205. {
  206. const char *node = "/foo";
  207. struct ubi_attach_request req;
  208. struct ubi_attach_req rr;
  209. memset(&req, 0, sizeof(req));
  210. memset(&rr, 0, sizeof(rr));
  211. libubi_t lib = mock_libubi_open();
  212. req.dev_num = 1;
  213. req.mtd_num = 1;
  214. rr.ubi_num = 1;
  215. rr.mtd_num = 1;
  216. expect_open(node, O_RDONLY, 4);
  217. expect_ioctl(UBI_IOCATT, 0, &rr);
  218. expect_close(4,0);
  219. int r = ubi_attach(lib, node, &req);
  220. assert_int_equal(r, 0);
  221. libubi_close(lib);
  222. (void) state;
  223. }
  224. void test_ubi_set_property(void **state)
  225. {
  226. int mock_fd = 1;
  227. uint8_t prop = 0xad;
  228. uint64_t val = 0xaabbccdd;
  229. struct ubi_set_vol_prop_req req;
  230. memset(&req, 0, sizeof(req));
  231. req.property = prop;
  232. req.value = val;
  233. expect_ioctl(UBI_IOCSETVOLPROP, 0, &req);
  234. int r = ubi_set_property(mock_fd, prop, val);
  235. assert_int_equal(r,0);
  236. (void)state;
  237. }
  238. /* functions to test
  239. * ubi_get_vol_info
  240. * ubi_get_vol_info1
  241. * ubi_get_vol_info1_nm
  242. * ubi_get_dev_info1
  243. * ubi_get_dev_info
  244. * ubi_probe_node
  245. * ubi_detach_mtd
  246. * ubi_detach
  247. * mtd_num2ubi_dev
  248. */
  249. int main(void)
  250. {
  251. const struct CMUnitTest tests[] = {
  252. cmocka_unit_test(test_libubi_open),
  253. cmocka_unit_test(test_ubi_vol_block_create),
  254. cmocka_unit_test(test_ubi_vol_block_remove),
  255. cmocka_unit_test(test_ubi_update_start),
  256. cmocka_unit_test(test_ubi_dev_present),
  257. cmocka_unit_test(test_ubi_rsvol),
  258. cmocka_unit_test(test_ubi_rmvol),
  259. cmocka_unit_test(test_ubi_rnvols),
  260. cmocka_unit_test(test_ubi_leb_change_start),
  261. cmocka_unit_test(test_ubi_get_info),
  262. cmocka_unit_test(test_ubi_mkvol),
  263. cmocka_unit_test(test_ubi_leb_unmap),
  264. cmocka_unit_test(test_ubi_is_mapped),
  265. cmocka_unit_test(test_ubi_remove_dev),
  266. cmocka_unit_test(test_ubi_attach),
  267. cmocka_unit_test(test_ubi_set_property),
  268. };
  269. return cmocka_run_group_tests(tests, NULL, NULL);
  270. }