Enclave_test.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418
  1. /*
  2. * Copyright (C) 2019 Intel Corporation. All rights reserved.
  3. * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. */
  5. #include <stdio.h>
  6. #include <string.h>
  7. #include "Enclave_t.h"
  8. #include "wasm_export.h"
  9. #include "bh_platform.h"
  10. void
  11. ecall_iwasm_test()
  12. {
  13. ocall_print(" Prepare to invoke sgx_open ... ==>\n\n");
  14. /* creat test.txt firstly */
  15. char file[] = "test.txt";
  16. char file_hd_ln[] = "test_hd_ln.txt";
  17. char file_sf_ln[] = "test_sf_ln.txt";
  18. char rlt_dir_path[] = "./tmp";
  19. char rlt_dir_path_new[] = "./tmp_new";
  20. char *str0 = (char *)"good luck, ";
  21. char *str1 = (char *)"have fun!";
  22. char buf0[4], buf1[4];
  23. struct iovec iov_w[2];
  24. struct iovec iov_r[2];
  25. char buf[2048];
  26. ssize_t total_size;
  27. ssize_t s_ret;
  28. int fd;
  29. int dird;
  30. int file_buf;
  31. int ret;
  32. long ret_l;
  33. DIR *dirp;
  34. struct dirent *p_dirent;
  35. struct stat *statbuf;
  36. struct pollfd fds[1];
  37. char *res;
  38. /** getopt value **/
  39. int argc = 3;
  40. char n_1[] = "./main";
  41. char n_2[] = "-a";
  42. char n_3[] = "test.txt";
  43. char *argv[3];
  44. argv[0] = n_1;
  45. argv[1] = n_2;
  46. argv[2] = n_3;
  47. /* time clock */
  48. struct timespec ts;
  49. struct timespec t_res;
  50. struct timespec times[2];
  51. struct stat statbuf_2;
  52. times[0] = statbuf_2.st_atim;
  53. times[1] = statbuf_2.st_mtim;
  54. struct timespec rqtp;
  55. struct timespec rmtp;
  56. rqtp.tv_sec = 0;
  57. rqtp.tv_nsec = 0;
  58. /** mkdirat **/
  59. /* mkdir tmp in current directory for test
  60. * if the ./tmp directory has exits, mkdirat will fail
  61. */
  62. ret = mkdirat(AT_FDCWD, rlt_dir_path, 0755);
  63. if (ret == 0) {
  64. ocall_print("\tOperation mkdirat success.\n");
  65. }
  66. /* flags:
  67. 0100: - O_CREAT
  68. 02 : - O_RDWR */
  69. /** 1. open **/
  70. fd = open(file, O_RDWR);
  71. if (fd != -1) {
  72. ocall_print("\tOperation open test_open.txt success.\n");
  73. }
  74. /** read **/
  75. total_size = read(fd, buf, 2048);
  76. if (total_size != -1) {
  77. ocall_print("\tOperation read test_open.txt success.\n");
  78. ocall_print("\t\tthe details of the file: ");
  79. ocall_print(buf);
  80. ocall_print("\n");
  81. }
  82. /** lseek **/
  83. ret = lseek(fd, 1, SEEK_CUR);
  84. if (ret != -1) {
  85. ocall_print("\tOperation lseek success.\n");
  86. }
  87. /** ftruncate **/
  88. ret = ftruncate(fd, 0);
  89. if (ret == 0) {
  90. ocall_print("\tOperation ftruncate success.\n");
  91. }
  92. /** fdatasync **/
  93. ret = fdatasync(fd);
  94. if (ret == 0) {
  95. ocall_print("\tOperation fdatasync success.\n");
  96. }
  97. /** isatty **/
  98. ret = isatty(fd);
  99. if (ret == 0) {
  100. ocall_print("\tOperation fisatty success.\n");
  101. }
  102. /** fsync **/
  103. ret = fsync(fd);
  104. if (ret == 0) {
  105. ocall_print("\tOperation fsync success.\n");
  106. }
  107. /** 1. close **/
  108. ret = close(fd);
  109. if (ret != -1) {
  110. ocall_print("\tOperation close success.\n");
  111. }
  112. /*----------------------------------------------------------------*/
  113. /**-- DIR --**/
  114. /** fdopendir **/
  115. /* 2. open */
  116. dird = open(rlt_dir_path, O_RDONLY);
  117. dirp = fdopendir(dird);
  118. if (dirp != NULL) {
  119. ocall_print("\tOperation fdopendir success.\n");
  120. }
  121. /** readdir **/
  122. p_dirent = readdir(dirp);
  123. if (p_dirent != NULL) {
  124. ocall_print("\tOperation readdir success.\t");
  125. ocall_print(p_dirent->d_name);
  126. ocall_print("\n");
  127. }
  128. /** rewinddir **/
  129. rewinddir(dirp);
  130. /** seekdir **/
  131. seekdir(dirp, 1);
  132. /** telldir **/
  133. ret_l = telldir(dirp);
  134. if (ret_l != -1) {
  135. ocall_print("\tOperation telldir success. \n");
  136. }
  137. /** closedir **/
  138. ret = closedir(dirp);
  139. if (ret == 0) {
  140. ocall_print("\tOperation closedir success. \n");
  141. }
  142. /* 2. close */
  143. close(dird);
  144. /*----------------------------------------------------------------*/
  145. /** fstat **/
  146. /** 3. open file firstly **/
  147. fd = open(file, O_RDWR);
  148. statbuf = (stat *)malloc(sizeof(stat));
  149. ret = fstat(fd, statbuf);
  150. if (ret == 0) {
  151. ocall_print("\tOperation fstat success. \n");
  152. }
  153. free(statbuf);
  154. /* 3. close */
  155. close(fd);
  156. /*----------------------------------------------------------------*/
  157. /** fstatat **/
  158. /* 4. open */
  159. dird = open(rlt_dir_path, O_RDONLY);
  160. ret = fstatat(AT_FDCWD, rlt_dir_path, statbuf, 0);
  161. if (ret == 0) {
  162. ocall_print("\tOperation fstatat success. \n");
  163. }
  164. /** renameat **/
  165. ret = renameat(AT_FDCWD, rlt_dir_path, AT_FDCWD, rlt_dir_path_new);
  166. if (ret == 0) {
  167. ocall_print("\tOperation renameat ./tmp to "
  168. "./tmp_new success. \n");
  169. }
  170. renameat(AT_FDCWD, rlt_dir_path_new, AT_FDCWD, rlt_dir_path);
  171. /** link **/
  172. ret = link(file, file_hd_ln);
  173. if (ret == 0) {
  174. ocall_print("\tOperation link success. \n");
  175. }
  176. /** unlinkat **/
  177. ret = unlinkat(AT_FDCWD, file_hd_ln, 0);
  178. if (ret == 0) {
  179. ocall_print("\tOperation unlinkat success. \n");
  180. }
  181. /** linkat **/
  182. ret = linkat(AT_FDCWD, file, AT_FDCWD, file_hd_ln, 0);
  183. if (ret == 0) {
  184. ocall_print("\tOperation linkat success. \n");
  185. }
  186. /* delete hard link file */
  187. unlinkat(AT_FDCWD, file_hd_ln, 0);
  188. /** symlinkat **/
  189. ret = symlinkat(file, AT_FDCWD, file_sf_ln);
  190. if (ret == 0) {
  191. ocall_print("\tOperation symlinkat from test.txt "
  192. "to text_sf_ln.txt success. \n");
  193. }
  194. /** readlinkat **/
  195. total_size = readlinkat(AT_FDCWD, file_sf_ln, buf, sizeof(buf));
  196. if (total_size != -1) {
  197. ocall_print("\tOperation readlinkat success. \n");
  198. ocall_print("\t\t the link details of the file is: ");
  199. ocall_print(buf);
  200. ocall_print("\n");
  201. }
  202. /* delete soft link file */
  203. unlinkat(AT_FDCWD, file_sf_ln, 0);
  204. /* 4. close */
  205. close(dird);
  206. /*----------------------------------------------------------------*/
  207. /* 5. open */
  208. fd = open(file, O_RDWR);
  209. /** ioctl **/
  210. ret = ioctl(fd, FIONREAD, &file_buf);
  211. if (ret == 0) {
  212. ocall_print("\tOperation ioctl success. \n");
  213. }
  214. /** fcntl(fd, cmd) **/
  215. ret = fcntl(fd, F_GETFD);
  216. if (ret != 0 || ret != -1) {
  217. ocall_print("\tOperation fcntl_1 success. \n");
  218. }
  219. /** fcntl(fd, cmd, long) **/
  220. ret = fcntl(fd, F_SETFD, ret);
  221. if (ret != 0 || ret != -1) {
  222. ocall_print("\tOperation fcntl_2 success. \n");
  223. }
  224. /* 5. close */
  225. close(fd);
  226. /*----------------------------------------------------------------*/
  227. /** posix_fallocate **/
  228. /* 6. open */
  229. fd = open(file, O_RDWR);
  230. ret = posix_fallocate(fd, 1, 1);
  231. if (ret != 0 || ret != -1) {
  232. ocall_print("\tOperation posix_fallocate success. \n");
  233. }
  234. /* 6. close */
  235. close(fd);
  236. /** poll **/
  237. ret = poll(fds, 1, 10);
  238. if (ret != 0 || ret != -1) {
  239. ocall_print("\tOperation poll success. \n");
  240. }
  241. /** realpath **/
  242. res = realpath(file, res);
  243. if (res) {
  244. ocall_print("\tOperation realpath success. \n");
  245. ocall_print("\t\t the absolute path of the file is: ");
  246. ocall_print(res);
  247. ocall_print("\n");
  248. }
  249. /** getrandom **/
  250. total_size = getrandom(buf, 1024, 0);
  251. if (ret != -1) {
  252. ocall_print("\tOperation getrandom success. \n");
  253. }
  254. /** writev **/
  255. /* 7. open */
  256. fd = open(file, O_RDWR);
  257. iov_w[0].iov_base = str0;
  258. iov_w[0].iov_len = strlen(str0);
  259. iov_w[1].iov_base = str1;
  260. iov_w[1].iov_len = strlen(str1);
  261. s_ret = writev(fd, iov_w, 2);
  262. if (s_ret != -1) {
  263. ocall_print("\tOperation writev success. \n");
  264. }
  265. /** readv **/
  266. iov_r[0].iov_base = buf0;
  267. iov_r[0].iov_len = sizeof(buf0) - 1;
  268. iov_r[1].iov_base = buf1;
  269. iov_r[1].iov_len = sizeof(buf1) - 1;
  270. s_ret = readv(fd, iov_r, 2);
  271. if (s_ret != -1) {
  272. ocall_print("\tOperation readv success. \n");
  273. ocall_print("\t\t");
  274. ocall_print(buf0);
  275. ocall_print(buf1);
  276. ocall_print("\n");
  277. }
  278. iov_r[0].iov_base = buf0;
  279. iov_r[0].iov_len = sizeof(buf0) - 1;
  280. iov_r[1].iov_base = buf1;
  281. iov_r[1].iov_len = sizeof(buf1) - 1;
  282. s_ret = preadv(fd, iov_r, 2, 2);
  283. if (s_ret != -1) {
  284. ocall_print("\tOperation readv success. \n");
  285. ocall_print("\t\t");
  286. ocall_print(buf0);
  287. ocall_print(buf1);
  288. ocall_print("\n");
  289. }
  290. /* 7. close */
  291. close(fd);
  292. /** getopt **/
  293. while ((ret = getopt(argc, argv, "f:abc"))
  294. != -1) { // get option from the getopt() method
  295. switch (ret) {
  296. // For option i, r, l, print that these are options
  297. case 'a':
  298. case 'b':
  299. case 'c':
  300. ocall_print("\tGiven Option operation success. \n");
  301. break;
  302. case 'f': // here f is used for some file name
  303. ocall_print("\tGiven File operation success.\n");
  304. break;
  305. case '?': // used for some unknown options
  306. ocall_print("\tunknown option trigger success.\n");
  307. break;
  308. }
  309. }
  310. /** sched_yield **/
  311. ret = sched_yield();
  312. if (ret == 0) {
  313. ocall_print("\tOperation sched_yield success. \n");
  314. }
  315. /** clock_gettime **/
  316. ret = clock_gettime(CLOCK_REALTIME, &ts);
  317. if (ret == 0) {
  318. ocall_print("\tOperation clock_gettime success. \n");
  319. }
  320. /** clock_getres **/
  321. ret = clock_getres(CLOCK_REALTIME, &t_res);
  322. if (ret == 0) {
  323. ocall_print("\tOperation clock_getres success. \n");
  324. }
  325. /** futimens **/
  326. /* 8. open */
  327. fd = open(file, O_RDWR);
  328. ret = futimens(fd, NULL);
  329. if (ret == 0) {
  330. ocall_print("\tOperation futimens NULL success. \n");
  331. }
  332. ret = futimens(fd, times);
  333. if (ret == 0) {
  334. ocall_print("\tOperation futimens times[2] success. \n");
  335. }
  336. /* 8. close */
  337. close(fd);
  338. /** utimensat **/
  339. /* 9. open */
  340. dird = open(rlt_dir_path, O_RDONLY);
  341. ret = utimensat(AT_FDCWD, file, NULL, AT_SYMLINK_NOFOLLOW);
  342. if (ret == 0) {
  343. ocall_print("\tOperation utimensat NULL success. \n");
  344. }
  345. ret = utimensat(AT_FDCWD, file, times, AT_SYMLINK_NOFOLLOW);
  346. if (ret == 0) {
  347. ocall_print("\tOperation utimensat times[2] success. \n");
  348. }
  349. /* 9. close */
  350. close(fd);
  351. /** clock_nanosleep **/
  352. ret = clock_nanosleep(CLOCK_REALTIME, 0, &rqtp, NULL);
  353. if (ret == 0) {
  354. ocall_print("\tOperation clock_nanosleep NULL success. \n");
  355. }
  356. ret = clock_nanosleep(CLOCK_REALTIME, 0, &rqtp, &rmtp);
  357. if (ret == 0) {
  358. ocall_print("\tOperation clock_nanosleep 2 success. \n");
  359. }
  360. ocall_print("\n<== ... End test\n");
  361. }