File.c 4.7 KB

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