File.c 5.3 KB

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