File.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. #include "pal.h"
  2. #include "pal_debug.h"
  3. #include "api.h"
  4. #define NUM_TO_HEX(num) \
  5. ((num) >= 10 ? 'a' + ((num) - 10) : '0' + (num))
  6. static __attribute__((noinline))
  7. void print_hex (char * fmt, const void * data, int len)
  8. {
  9. char * buf = __alloca(len * 2 + 1);
  10. buf[len * 2] = '\0';
  11. for (int i = 0; i < len; i++) {
  12. unsigned char b = ((unsigned char *)data)[i];
  13. buf[i * 2] = NUM_TO_HEX(b >> 4);
  14. buf[i * 2 + 1] = NUM_TO_HEX(b & 0xf);
  15. }
  16. pal_printf(fmt, buf);
  17. }
  18. int main (int argc, char ** argv, char ** envp)
  19. {
  20. char buffer1[40], buffer2[40], buffer3[40];
  21. int ret;
  22. /* test regular file opening */
  23. PAL_HANDLE file1 = DkStreamOpen("file:File",
  24. PAL_ACCESS_RDWR, 0, 0, 0);
  25. if (file1) {
  26. pal_printf("File Open Test 1 OK\n");
  27. /* test file read */
  28. ret = DkStreamRead(file1, 0, 40, buffer1, NULL, 0);
  29. if (ret > 0) {
  30. buffer1[ret] = 0;
  31. print_hex("Read Test 1 (0th - 40th): %s\n", buffer1, 40);
  32. }
  33. ret = DkStreamRead(file1, 0, 40, buffer1, NULL, 0);
  34. if (ret > 0) {
  35. buffer1[ret] = 0;
  36. print_hex("Read Test 2 (0th - 40th): %s\n", buffer1, 40);
  37. }
  38. ret = DkStreamRead(file1, 200, 40, buffer2, NULL, 0);
  39. if (ret > 0) {
  40. buffer2[ret] = 0;
  41. print_hex("Read Test 3 (200th - 240th): %s\n", buffer2, 40);
  42. }
  43. /* test file attribute query */
  44. PAL_STREAM_ATTR attr1;
  45. if (DkStreamAttributesQueryByHandle(file1, &attr1))
  46. pal_printf("Query by Handle: type = %d, size = %ld\n",
  47. attr1.handle_type, attr1.pending_size);
  48. /* test file map */
  49. void * mem1 = (void *) DkStreamMap(file1, NULL,
  50. PAL_PROT_READ | PAL_PROT_WRITECOPY, 0, 4096);
  51. if (mem1) {
  52. memcpy(buffer1, mem1, 40);
  53. print_hex("Map Test 1 (0th - 40th): %s\n", buffer1, 40);
  54. memcpy(buffer2, mem1 + 200, 40);
  55. print_hex("Map Test 2 (200th - 240th): %s\n", buffer2, 40);
  56. DkStreamUnmap(mem1, 4096);
  57. } else {
  58. pal_printf("Map Test 1 & 2: Failed to map buffer\n");
  59. }
  60. /* DEP 11/24/17: For SGX writecopy exercises a different path in the PAL */
  61. void * mem2 = (void *) DkStreamMap(file1, NULL,
  62. PAL_PROT_READ | PAL_PROT_WRITECOPY, 4096, 4096);
  63. if (mem2) {
  64. memcpy(buffer3, mem2, 40);
  65. print_hex("Map Test 3 (4096th - 4136th): %s\n", buffer3, 40);
  66. memcpy(buffer3, mem2 + 200, 40);
  67. print_hex("Map Test 4 (4296th - 4336th): %s\n", buffer3, 40);
  68. DkStreamUnmap(mem2, 4096);
  69. }
  70. DkObjectClose(file1);
  71. }
  72. PAL_HANDLE file2 = DkStreamOpen("file:File",
  73. PAL_ACCESS_RDWR, 0, 0, 0);
  74. if (file2) {
  75. pal_printf("File Open Test 2 OK\n");
  76. DkObjectClose(file2);
  77. }
  78. PAL_HANDLE file3 = DkStreamOpen("file:../regression/File",
  79. PAL_ACCESS_RDWR, 0, 0, 0);
  80. if (file3) {
  81. pal_printf("File Open Test 3 OK\n");
  82. DkObjectClose(file3);
  83. }
  84. PAL_STREAM_ATTR attr2;
  85. if (DkStreamAttributesQuery("file:File", &attr2))
  86. pal_printf("Query: type = %d, size = %ld\n",
  87. attr2.handle_type, attr2.pending_size);
  88. /* test regular file creation */
  89. PAL_HANDLE file4 = DkStreamOpen("file:file_nonexist.tmp",
  90. PAL_ACCESS_RDWR,
  91. PAL_SHARE_OWNER_R|PAL_SHARE_OWNER_W,
  92. PAL_CREATE_TRY|PAL_CREATE_ALWAYS, 0);
  93. if (file4)
  94. pal_printf("File Creation Test 1 OK\n");
  95. PAL_HANDLE file5 = DkStreamOpen("file:file_nonexist.tmp",
  96. PAL_ACCESS_RDWR,
  97. PAL_SHARE_OWNER_R|PAL_SHARE_OWNER_W,
  98. PAL_CREATE_TRY|PAL_CREATE_ALWAYS, 0);
  99. if (file5) {
  100. DkObjectClose(file5);
  101. } else {
  102. pal_printf("File Creation Test 2 OK\n");
  103. }
  104. PAL_HANDLE file6 = DkStreamOpen("file:file_nonexist.tmp",
  105. PAL_ACCESS_RDWR,
  106. PAL_SHARE_OWNER_R|PAL_SHARE_OWNER_W,
  107. PAL_CREATE_TRY, 0);
  108. if (file6) {
  109. pal_printf("File Creation Test 3 OK\n");
  110. DkObjectClose(file6);
  111. }
  112. if (file4) {
  113. /* test file writing */
  114. ret = DkStreamWrite(file4, 0, 40, buffer1, NULL);
  115. if (ret < 0)
  116. goto fail_writing;
  117. ret = DkStreamWrite(file4, 0, 40, buffer2, NULL);
  118. if (ret < 0)
  119. goto fail_writing;
  120. ret = DkStreamWrite(file4, 200, 40, buffer1, NULL);
  121. if (ret < 0)
  122. goto fail_writing;
  123. /* test file truncate */
  124. DkStreamSetLength(file4, pal_control.alloc_align);
  125. fail_writing:
  126. DkObjectClose(file4);
  127. }
  128. PAL_HANDLE file7 = DkStreamOpen("file:file_delete.tmp",
  129. PAL_ACCESS_RDONLY, 0, 0, 0);
  130. if (file7) {
  131. DkStreamDelete(file7, 0);
  132. DkObjectClose(file7);
  133. }
  134. return 0;
  135. }