PageRenderTime 53ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/uClinux-dist/lib/sysfsutils/test/test.c

https://bitbucket.org/__wp__/mb-linux-msli
C | 329 lines | 259 code | 38 blank | 32 comment | 44 complexity | c3de92f37db6d4aae658ac0c8b54556e MD5 | raw file
Possible License(s): AGPL-3.0, GPL-2.0, LGPL-2.0, MPL-2.0, ISC, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, 0BSD, CC-BY-SA-3.0, GPL-3.0, LGPL-3.0, AGPL-1.0, Unlicense
  1. /*
  2. * test.c
  3. *
  4. * Main program for the libsysfs testsuite
  5. *
  6. * Copyright (C) IBM Corp. 2004-2005
  7. *
  8. * This program is free software; you can redistribute it and/or modify it
  9. * under the terms of the GNU General Public License as published by the
  10. * Free Software Foundation version 2 of the License.
  11. *
  12. * This program is distributed in the hope that it will be useful, but
  13. * WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 675 Mass Ave, Cambridge, MA 02139, USA.
  20. *
  21. */
  22. /**
  23. * this doesn't do much, just loops throug to call each function.
  24. */
  25. #include "test-defs.h"
  26. #include <errno.h>
  27. /*************************************************/
  28. char *function_name[] = {
  29. "sysfs_get_mnt_path",
  30. "sysfs_remove_trailing_slash",
  31. "sysfs_get_name_from_path",
  32. "sysfs_path_is_dir",
  33. "sysfs_path_is_link",
  34. "sysfs_path_is_file",
  35. "sysfs_get_link",
  36. "sysfs_close_list",
  37. "sysfs_open_directory_list",
  38. "sysfs_close_attribute",
  39. "sysfs_open_attribute",
  40. "sysfs_read_attribute",
  41. "sysfs_write_attribute",
  42. "sysfs_close_driver",
  43. "sysfs_open_driver",
  44. "sysfs_open_driver_path",
  45. "sysfs_get_driver_attr",
  46. "sysfs_get_driver_attributes",
  47. "sysfs_get_driver_devices",
  48. // "sysfs_get_driver_module",
  49. "sysfs_close_device",
  50. "sysfs_open_device",
  51. "sysfs_get_device_parent",
  52. "sysfs_open_device_path",
  53. "sysfs_get_device_attr",
  54. // "sysfs_get_device_bus",
  55. "sysfs_get_device_attributes",
  56. "sysfs_close_bus",
  57. "sysfs_open_bus",
  58. "sysfs_get_bus_device",
  59. "sysfs_get_bus_driver",
  60. "sysfs_get_bus_drivers",
  61. "sysfs_get_bus_devices",
  62. "sysfs_close_class_device",
  63. "sysfs_open_class_device_path",
  64. "sysfs_open_class_device",
  65. "sysfs_get_classdev_device",
  66. "sysfs_get_classdev_parent",
  67. "sysfs_close_class",
  68. "sysfs_open_class",
  69. "sysfs_get_class_devices",
  70. "sysfs_get_class_device",
  71. "sysfs_get_classdev_attributes",
  72. "sysfs_get_classdev_attr",
  73. "sysfs_close_module",
  74. "sysfs_open_module_path",
  75. "sysfs_open_module",
  76. "sysfs_get_module_attr",
  77. "sysfs_get_module_attributes",
  78. "sysfs_get_module_parms",
  79. "sysfs_get_module_sections",
  80. "sysfs_get_module_parm",
  81. "sysfs_get_module_section",
  82. };
  83. int (*func_table[])(int) = {
  84. test_sysfs_get_mnt_path,
  85. test_sysfs_remove_trailing_slash,
  86. test_sysfs_get_name_from_path,
  87. test_sysfs_path_is_dir,
  88. test_sysfs_path_is_link,
  89. test_sysfs_path_is_file,
  90. test_sysfs_get_link,
  91. test_sysfs_close_list,
  92. test_sysfs_open_directory_list,
  93. test_sysfs_close_attribute,
  94. test_sysfs_open_attribute,
  95. test_sysfs_read_attribute,
  96. test_sysfs_write_attribute,
  97. test_sysfs_close_driver,
  98. test_sysfs_open_driver,
  99. test_sysfs_open_driver_path,
  100. test_sysfs_get_driver_attr,
  101. test_sysfs_get_driver_attributes,
  102. test_sysfs_get_driver_devices,
  103. // test_sysfs_get_driver_module,
  104. test_sysfs_close_device,
  105. test_sysfs_open_device,
  106. test_sysfs_get_device_parent,
  107. test_sysfs_open_device_path,
  108. test_sysfs_get_device_attr,
  109. // test_sysfs_get_device_bus,
  110. test_sysfs_get_device_attributes,
  111. test_sysfs_close_bus,
  112. test_sysfs_open_bus,
  113. test_sysfs_get_bus_device,
  114. test_sysfs_get_bus_driver,
  115. test_sysfs_get_bus_drivers,
  116. test_sysfs_get_bus_devices,
  117. test_sysfs_close_class_device,
  118. test_sysfs_open_class_device_path,
  119. test_sysfs_open_class_device,
  120. test_sysfs_get_classdev_device,
  121. test_sysfs_get_classdev_parent,
  122. test_sysfs_close_class,
  123. test_sysfs_open_class,
  124. test_sysfs_get_class_devices,
  125. test_sysfs_get_class_device,
  126. test_sysfs_get_classdev_attributes,
  127. test_sysfs_get_classdev_attr,
  128. test_sysfs_close_module,
  129. test_sysfs_open_module_path,
  130. test_sysfs_open_module,
  131. test_sysfs_get_module_attr,
  132. test_sysfs_get_module_attributes,
  133. test_sysfs_get_module_parms,
  134. test_sysfs_get_module_sections,
  135. test_sysfs_get_module_parm,
  136. test_sysfs_get_module_section,
  137. };
  138. char *dir_paths[] = {
  139. val_dir_path,
  140. val_root_dev_path,
  141. val_class_dev_path,
  142. val_block_class_dev_path,
  143. val_drv_path,
  144. val_drv1_path,
  145. NULL
  146. };
  147. char *file_paths[] = {
  148. val_file_path,
  149. val_write_attr_path,
  150. NULL
  151. };
  152. char *link_paths[] = {
  153. val_link_path,
  154. NULL
  155. };
  156. static int path_is_dir(const char *path)
  157. {
  158. struct stat astats;
  159. if ((lstat(path, &astats)) != 0)
  160. goto direrr;
  161. if (S_ISDIR(astats.st_mode))
  162. return 0;
  163. direrr:
  164. fprintf(stdout, "Config error: %s not a directory\n", path);
  165. return 1;
  166. }
  167. static int path_is_file(const char *path)
  168. {
  169. struct stat astats;
  170. if ((lstat(path, &astats)) != 0)
  171. goto fileerr;
  172. if (S_ISREG(astats.st_mode))
  173. return 0;
  174. fileerr:
  175. fprintf(stdout, "Config error: %s not a file\n", path);
  176. return 1;
  177. }
  178. static int path_is_link(const char *path)
  179. {
  180. struct stat astats;
  181. if ((lstat(path, &astats)) != 0)
  182. goto linkerr;
  183. if (S_ISLNK(astats.st_mode))
  184. return 0;
  185. linkerr:
  186. fprintf(stdout, "Config error: %s not a link\n", path);
  187. return 1;
  188. }
  189. /*
  190. * Check validity of the test.h file entries
  191. */
  192. static int check_header(void)
  193. {
  194. char *var_path = NULL;
  195. char path1[SYSFS_PATH_MAX];
  196. int i = 0;
  197. for (i = 0; dir_paths[i] != NULL; i++) {
  198. var_path = dir_paths[i];
  199. if (path_is_dir(var_path) != 0)
  200. return 1;
  201. }
  202. for (i = 0; file_paths[i] != NULL; i++) {
  203. var_path = file_paths[i];
  204. if (path_is_file(var_path) != 0)
  205. return 1;
  206. }
  207. for (i = 0; link_paths[i] != NULL; i++) {
  208. var_path = link_paths[i];
  209. if (path_is_link(var_path) != 0)
  210. return 1;
  211. }
  212. memset(path1, 0, SYSFS_PATH_MAX);
  213. strcpy(path1, val_root_dev_path);
  214. strcat(path1, "/");
  215. strcat(path1, val_subdir_name);
  216. if (path_is_dir(path1) != 0)
  217. return 1;
  218. memset(path1, 0, SYSFS_PATH_MAX);
  219. strcpy(path1, val_drv_path);
  220. strcat(path1, "/");
  221. strcat(path1, val_drv_dev_name);
  222. if (path_is_link(path1) != 0)
  223. return 1;
  224. memset(path1, 0, SYSFS_PATH_MAX);
  225. strcpy(path1, val_dir_path);
  226. strcat(path1, "/devices");
  227. strcat(path1, "/");
  228. strcat(path1, val_subdir_link_name);
  229. if (path_is_link(path1) != 0)
  230. return 1;
  231. memset(path1, 0, SYSFS_PATH_MAX);
  232. strcpy(path1, val_class_dev_path);
  233. strcat(path1, "/");
  234. strcat(path1, val_class_dev_attr);
  235. if (path_is_file(path1) != 0)
  236. return 1;
  237. memset(path1, 0, SYSFS_PATH_MAX);
  238. strcpy(path1, val_dev_path);
  239. strcat(path1, "/");
  240. strcat(path1, val_dev_attr);
  241. if (path_is_file(path1) != 0)
  242. return 1;
  243. memset(path1, 0, SYSFS_PATH_MAX);
  244. strcpy(path1, val_drv_path);
  245. strcat(path1, "/");
  246. strcat(path1, val_drv_attr_name);
  247. if (path_is_file(path1) != 0)
  248. return 1;
  249. return 0;
  250. }
  251. static void usage(void)
  252. {
  253. fprintf(stdout, "testlibsysfs <no-of-times> [log-file]\n");
  254. fprintf(stdout, "\t eg: testlibsysfs 3 /home/user/test.log\n");
  255. }
  256. int main(int argc, char *argv[])
  257. {
  258. char std_out[] = "/dev/stdout";
  259. char *file_name = NULL;
  260. int i = 0, j = 0, k = 0, num = 1;
  261. if (argc == 3) {
  262. file_name = argv[2];
  263. } else {
  264. file_name = std_out;
  265. }
  266. my_stdout = fopen(file_name,"w");
  267. if (argc < 2) {
  268. usage();
  269. return 0;
  270. } else
  271. num = strtol(argv[1], NULL, 0);
  272. if (check_header() != 0)
  273. return 1;
  274. dbg_print("\nTest running %d times\n", num);
  275. for (k = 0; k < num ; k++) {
  276. dbg_print("\nThis is the %d test run\n", k+1);
  277. for (i = 0; i < FUNC_TABLE_SIZE; i++) {
  278. dbg_print("\n**************************************************************\n");
  279. dbg_print("TESTING: %s, function no: %d\n\n",
  280. function_name[i], i);
  281. for (j = 0; ; j++) {
  282. fflush(my_stdout);
  283. if (func_table[i](j) == -1)
  284. break;
  285. }
  286. dbg_print("**************************************************************\n");
  287. }
  288. }
  289. fclose(my_stdout);
  290. return 0;
  291. }