Enclave_test.cpp 10 KB

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