test.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. #include "mman.h"
  2. #include <errno.h>
  3. #include <fcntl.h>
  4. #include <sys/stat.h>
  5. #ifndef NULL
  6. #define NULL (void*)0
  7. #endif
  8. const char* map_file_name = "map_file.dat";
  9. int test_anon_map_readwrite()
  10. {
  11. void* map = mmap(NULL, 1024, PROT_READ | PROT_WRITE,
  12. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  13. if (map == MAP_FAILED)
  14. {
  15. printf("mmap (MAP_ANONYMOUS, PROT_READ | PROT_WRITE) returned unexpected error: %d\n", errno);
  16. return -1;
  17. }
  18. *((unsigned char*)map) = 1;
  19. int result = munmap(map, 1024);
  20. if (result != 0)
  21. printf("munmap (MAP_ANONYMOUS, PROT_READ | PROT_WRITE) returned unexpected error: %d\n", errno);
  22. return result;
  23. }
  24. int test_anon_map_readonly()
  25. {
  26. void* map = mmap(NULL, 1024, PROT_READ,
  27. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  28. if (map == MAP_FAILED)
  29. {
  30. printf("mmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
  31. return -1;
  32. }
  33. *((unsigned char*)map) = 1;
  34. int result = munmap(map, 1024);
  35. if (result != 0)
  36. printf("munmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
  37. return result;
  38. }
  39. int test_anon_map_writeonly()
  40. {
  41. void* map = mmap(NULL, 1024, PROT_WRITE,
  42. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  43. if (map == MAP_FAILED)
  44. {
  45. printf("mmap (MAP_ANONYMOUS, PROT_WRITE) returned unexpected error: %d\n", errno);
  46. return -1;
  47. }
  48. *((unsigned char*)map) = 1;
  49. int result = munmap(map, 1024);
  50. if (result != 0)
  51. printf("munmap (MAP_ANONYMOUS, PROT_WRITE) returned unexpected error: %d\n", errno);
  52. return result;
  53. }
  54. int test_anon_map_readonly_nowrite()
  55. {
  56. void* map = mmap(NULL, 1024, PROT_READ,
  57. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  58. if (map == MAP_FAILED)
  59. {
  60. printf("mmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
  61. return -1;
  62. }
  63. if (*((unsigned char*)map) != 0)
  64. printf("test_anon_map_readonly_nowrite (MAP_ANONYMOUS, PROT_READ) returned unexpected value: %d\n",
  65. (int)*((unsigned char*)map));
  66. int result = munmap(map, 1024);
  67. if (result != 0)
  68. printf("munmap (MAP_ANONYMOUS, PROT_READ) returned unexpected error: %d\n", errno);
  69. return result;
  70. }
  71. int test_file_map_readwrite()
  72. {
  73. mode_t mode = S_IRUSR | S_IWUSR;
  74. int o = open(map_file_name, O_TRUNC | O_BINARY | O_RDWR | O_CREAT, mode);
  75. void* map = mmap(NULL, 1024, PROT_READ | PROT_WRITE, MAP_PRIVATE, o, 0);
  76. if (map == MAP_FAILED)
  77. {
  78. printf("mmap returned unexpected error: %d\n", errno);
  79. return -1;
  80. }
  81. *((unsigned char*)map) = 1;
  82. int result = munmap(map, 1024);
  83. if (result != 0)
  84. printf("munmap returned unexpected error: %d\n", errno);
  85. close(o);
  86. /*TODO: get file info and content and compare it with the sources conditions */
  87. unlink(map_file_name);
  88. return result;
  89. }
  90. int test_file_map_mlock_munlock()
  91. {
  92. const size_t map_size = 1024;
  93. int result = 0;
  94. mode_t mode = S_IRUSR | S_IWUSR;
  95. int o = open(map_file_name, O_TRUNC | O_BINARY | O_RDWR | O_CREAT, mode);
  96. if (o == -1)
  97. {
  98. printf("unable to create file %s: %d\n", map_file_name, errno);
  99. return -1;
  100. }
  101. void* map = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, o, 0);
  102. if (map == MAP_FAILED)
  103. {
  104. printf("mmap returned unexpected error: %d\n", errno);
  105. result = -1;
  106. goto done_close;
  107. }
  108. if (mlock(map, map_size) != 0)
  109. {
  110. printf("mlock returned unexpected error: %d\n", errno);
  111. result = -1;
  112. goto done_munmap;
  113. }
  114. *((unsigned char*)map) = 1;
  115. if (munlock(map, map_size) != 0)
  116. {
  117. printf("munlock returned unexpected error: %d\n", errno);
  118. result = -1;
  119. }
  120. done_munmap:
  121. result = munmap(map, map_size);
  122. if (result != 0)
  123. printf("munmap returned unexpected error: %d\n", errno);
  124. done_close:
  125. close(o);
  126. unlink(map_file_name);
  127. done:
  128. return result;
  129. }
  130. int test_file_map_msync()
  131. {
  132. const size_t map_size = 1024;
  133. int result = 0;
  134. mode_t mode = S_IRUSR | S_IWUSR;
  135. int o = open(map_file_name, O_TRUNC | O_BINARY | O_RDWR | O_CREAT, mode);
  136. if (o == -1)
  137. {
  138. printf("unable to create file %s: %d\n", map_file_name, errno);
  139. return -1;
  140. }
  141. void* map = mmap(NULL, map_size, PROT_READ | PROT_WRITE, MAP_PRIVATE, o, 0);
  142. if (map == MAP_FAILED)
  143. {
  144. printf("mmap returned unexpected error: %d\n", errno);
  145. result = -1;
  146. goto done_close;
  147. }
  148. *((unsigned char*)map) = 1;
  149. if (msync(map, map_size, MS_SYNC) != 0)
  150. {
  151. printf("msync returned unexpected error: %d\n", errno);
  152. result = -1;
  153. }
  154. result = munmap(map, map_size);
  155. if (result != 0)
  156. printf("munmap returned unexpected error: %d\n", errno);
  157. done_close:
  158. close(o);
  159. unlink(map_file_name);
  160. done:
  161. return result;
  162. }
  163. #define EXEC_TEST(name) \
  164. if (name() != 0) { result = -1; printf( #name ": fail\n"); } \
  165. else { printf(#name ": pass\n"); }
  166. int main()
  167. {
  168. int result = 0;
  169. EXEC_TEST(test_anon_map_readwrite);
  170. //NOTE: this test must cause an access violation exception
  171. //EXEC_TEST(test_anon_map_readonly);
  172. EXEC_TEST(test_anon_map_readonly_nowrite);
  173. EXEC_TEST(test_anon_map_writeonly);
  174. EXEC_TEST(test_file_map_readwrite);
  175. EXEC_TEST(test_file_map_mlock_munlock);
  176. EXEC_TEST(test_file_map_msync);
  177. //TODO: EXEC_TEST(test_file_map_mprotect);
  178. return result;
  179. }