mkvol_bad.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. /*
  2. * Copyright (c) International Business Machines Corp., 2006
  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 2 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
  12. * the 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, write to the Free Software
  16. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17. *
  18. * Author: Artem B. Bityutskiy
  19. *
  20. * Test UBI volume creation and deletion ioctl()s with bad input and in case of
  21. * incorrect usage.
  22. */
  23. #include <string.h>
  24. #include <errno.h>
  25. #include <stdio.h>
  26. #include "libubi.h"
  27. #define PROGRAM_NAME "mkvol_bad"
  28. #include "common.h"
  29. #include "helpers.h"
  30. static libubi_t libubi;
  31. static struct ubi_dev_info dev_info;
  32. const char *node;
  33. /**
  34. * test_mkvol - test that UBI mkvol ioctl rejects bad input parameters.
  35. *
  36. * This function returns %0 if the test passed and %-1 if not.
  37. */
  38. static int test_mkvol(void)
  39. {
  40. int ret, i;
  41. struct ubi_mkvol_request req;
  42. const char *name = PROGRAM_NAME ":test_mkvol()";
  43. req.alignment = 1;
  44. req.bytes = dev_info.avail_bytes;
  45. req.vol_type = UBI_DYNAMIC_VOLUME;
  46. req.name = name;
  47. req.flags = 0;
  48. /* Bad volume ID */
  49. req.vol_id = -2;
  50. ret = ubi_mkvol(libubi, node, &req);
  51. if (check_failed(ret, EINVAL, "ubi_mkvol", "vol_id = %d", req.vol_id))
  52. return -1;
  53. req.vol_id = dev_info.max_vol_count;
  54. ret = ubi_mkvol(libubi, node, &req);
  55. if (check_failed(ret, EINVAL, "ubi_mkvol", "vol_id = %d", req.vol_id))
  56. return -1;
  57. /* Bad alignment */
  58. req.vol_id = 0;
  59. req.alignment = 0;
  60. ret = ubi_mkvol(libubi, node, &req);
  61. if (check_failed(ret, EINVAL, "ubi_mkvol", "alignment = %d",
  62. req.alignment))
  63. return -1;
  64. req.alignment = -1;
  65. ret = ubi_mkvol(libubi, node, &req);
  66. if (check_failed(ret, EINVAL, "ubi_mkvol", "alignment = %d",
  67. req.alignment))
  68. return -1;
  69. req.alignment = dev_info.leb_size + 1;
  70. ret = ubi_mkvol(libubi, node, &req);
  71. if (check_failed(ret, EINVAL, "ubi_mkvol", "alignment = %d",
  72. req.alignment))
  73. return -1;
  74. if (dev_info.min_io_size > 1) {
  75. req.alignment = dev_info.min_io_size + 1;
  76. ret = ubi_mkvol(libubi, node, &req);
  77. if (check_failed(ret, EINVAL, "ubi_mkvol", "alignment = %d",
  78. req.alignment))
  79. return -1;
  80. }
  81. /* Bad bytes */
  82. req.alignment = 1;
  83. req.bytes = -1;
  84. ret = ubi_mkvol(libubi, node, &req);
  85. if (check_failed(ret, EINVAL, "ubi_mkvol", "bytes = %lld", req.bytes))
  86. return -1;
  87. req.bytes = 0;
  88. ret = ubi_mkvol(libubi, node, &req);
  89. if (check_failed(ret, EINVAL, "ubi_mkvol", "bytes = %lld", req.bytes))
  90. return -1;
  91. req.bytes = dev_info.avail_bytes + 1;
  92. ret = ubi_mkvol(libubi, node, &req);
  93. if (check_failed(ret, ENOSPC, "ubi_mkvol", "bytes = %lld", req.bytes))
  94. return -1;
  95. req.alignment = dev_info.leb_size - dev_info.min_io_size;
  96. req.bytes = (long long)(dev_info.leb_size - dev_info.leb_size % req.alignment) *
  97. dev_info.avail_lebs + 1;
  98. ret = ubi_mkvol(libubi, node, &req);
  99. if (check_failed(ret, ENOSPC, "ubi_mkvol", "bytes = %lld", req.bytes))
  100. return -1;
  101. /* Bad vol_type */
  102. req.alignment = 1;
  103. req.bytes = dev_info.leb_size;
  104. req.vol_type = UBI_DYNAMIC_VOLUME + UBI_STATIC_VOLUME;
  105. ret = ubi_mkvol(libubi, node, &req);
  106. if (check_failed(ret, EINVAL, "ubi_mkvol", "vol_type = %d",
  107. req.vol_type))
  108. return -1;
  109. req.vol_type = UBI_DYNAMIC_VOLUME;
  110. /* Too long name */
  111. {
  112. char name[UBI_VOL_NAME_MAX + 5];
  113. memset(name, 'x', UBI_VOL_NAME_MAX + 1);
  114. name[UBI_VOL_NAME_MAX + 1] = '\0';
  115. req.name = name;
  116. ret = ubi_mkvol(libubi, node, &req);
  117. if (check_failed(ret, EINVAL, "ubi_mkvol", "name_len = %d",
  118. UBI_VOL_NAME_MAX + 1))
  119. return -1;
  120. }
  121. /* Try to create 2 volumes with the same ID and name */
  122. req.name = name;
  123. req.vol_id = 0;
  124. if (ubi_mkvol(libubi, node, &req)) {
  125. failed("ubi_mkvol");
  126. return -1;
  127. }
  128. ret = ubi_mkvol(libubi, node, &req);
  129. if (check_failed(ret, EEXIST, "ubi_mkvol",
  130. "volume with ID 0 created twice"))
  131. return -1;
  132. req.vol_id = 1;
  133. ret = ubi_mkvol(libubi, node, &req);
  134. if (check_failed(ret, EEXIST, "ubi_mkvol",
  135. "volume with name \"%s\" created twice", name))
  136. return -1;
  137. if (ubi_rmvol(libubi, node, 0)) {
  138. failed("ubi_rmvol");
  139. return -1;
  140. }
  141. /* Try to use too much space */
  142. req.vol_id = 0;
  143. req.bytes = dev_info.avail_bytes;
  144. if (ubi_mkvol(libubi, node, &req)) {
  145. failed("ubi_mkvol");
  146. return -1;
  147. }
  148. req.bytes = 1;
  149. req.vol_id = 1;
  150. ret = ubi_mkvol(libubi, node, &req);
  151. if (check_failed(ret, EEXIST, "ubi_mkvol",
  152. "created volume of maximum size %lld, but still "
  153. "can create more volumes", dev_info.avail_bytes))
  154. return -1;
  155. if (ubi_rmvol(libubi, node, 0)) {
  156. failed("ubi_rmvol");
  157. return -1;
  158. }
  159. /* Try to create too many volumes */
  160. for (i = 0; i < dev_info.max_vol_count; i++) {
  161. char nm[strlen(name) + 50];
  162. req.vol_id = UBI_VOL_NUM_AUTO;
  163. req.alignment = 1;
  164. req.bytes = 1;
  165. req.vol_type = UBI_STATIC_VOLUME;
  166. sprintf(nm, "%s:%d", name, i);
  167. req.name = nm;
  168. if (ubi_mkvol(libubi, node, &req)) {
  169. /*
  170. * Note, because of gluebi we may be unable to create
  171. * dev_info.max_vol_count devices (MTD restrictions).
  172. */
  173. if (errno == ENFILE || errno == ENOSPC)
  174. break;
  175. failed("ubi_mkvol");
  176. errorm("vol_id %d", i);
  177. goto remove;
  178. }
  179. }
  180. for (i = 0; i < dev_info.max_vol_count + 1; i++)
  181. ubi_rmvol(libubi, node, i);
  182. return 0;
  183. remove:
  184. for (i = 0; i < dev_info.max_vol_count + 1; i++)
  185. ubi_rmvol(libubi, node, i);
  186. return -1;
  187. }
  188. /**
  189. * test_rmvol - test that UBI rmvol ioctl rejects bad input parameters.
  190. *
  191. * This function returns %0 if the test passed and %-1 if not.
  192. */
  193. static int test_rmvol(void)
  194. {
  195. int ret;
  196. struct ubi_mkvol_request req;
  197. const char *name = PROGRAM_NAME ":test_rmvol()";
  198. /* Bad vol_id */
  199. ret = ubi_rmvol(libubi, node, -1);
  200. if (check_failed(ret, EINVAL, "ubi_rmvol", "vol_id = -1"))
  201. return -1;
  202. ret = ubi_rmvol(libubi, node, dev_info.max_vol_count);
  203. if (check_failed(ret, EINVAL, "ubi_rmvol", "vol_id = %d",
  204. dev_info.max_vol_count))
  205. return -1;
  206. /* Try to remove non-existing volume */
  207. ret = ubi_rmvol(libubi, node, 0);
  208. if (check_failed(ret, ENODEV, "ubi_rmvol",
  209. "removed non-existing volume 0"))
  210. return -1;
  211. /* Try to remove volume twice */
  212. req.vol_id = UBI_VOL_NUM_AUTO;
  213. req.alignment = 1;
  214. req.bytes = dev_info.avail_bytes;
  215. req.vol_type = UBI_DYNAMIC_VOLUME;
  216. req.name = name;
  217. req.flags = 0;
  218. if (ubi_mkvol(libubi, node, &req)) {
  219. failed("ubi_mkvol");
  220. return -1;
  221. }
  222. if (ubi_rmvol(libubi, node, req.vol_id)) {
  223. failed("ubi_rmvol");
  224. return -1;
  225. }
  226. ret = ubi_rmvol(libubi, node, req.vol_id);
  227. if (check_failed(ret, ENODEV, "ubi_rmvol", "volume %d removed twice",
  228. req.vol_id))
  229. return -1;
  230. return 0;
  231. }
  232. int main(int argc, char * const argv[])
  233. {
  234. if (initial_check(argc, argv))
  235. return 1;
  236. node = argv[1];
  237. libubi = libubi_open();
  238. if (libubi == NULL) {
  239. failed("libubi_open");
  240. return 1;
  241. }
  242. if (ubi_get_dev_info(libubi, node, &dev_info)) {
  243. failed("ubi_get_dev_info");
  244. goto close;
  245. }
  246. if (test_mkvol())
  247. goto close;
  248. if (test_rmvol())
  249. goto close;
  250. libubi_close(libubi);
  251. return 0;
  252. close:
  253. libubi_close(libubi);
  254. return 1;
  255. }