test_lib.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. #include <asm/ioctl.h>
  2. #include <stdarg.h>
  3. #include <setjmp.h>
  4. #include <stddef.h>
  5. #include <stdint.h>
  6. #include <unistd.h>
  7. #include <stdlib.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <sys/stat.h>
  11. #include <fcntl.h>
  12. #include <sys/ioctl.h>
  13. #include <cmocka.h>
  14. int __real_open(const char*, mode_t);
  15. int __real_close(int);
  16. int __real_stat(const char*, struct stat *);
  17. ssize_t __real_read(int, void*, size_t);
  18. /* syscalls to mock */
  19. int __wrap_open(const char *path, mode_t mode)
  20. {
  21. check_expected(path);
  22. mode_t m = mock_type(mode_t);
  23. mode_t a = mode & m;
  24. assert_int_equal(a, m);
  25. int retval = mock_type(int);
  26. /* fake open if retval > 0 */
  27. if (retval > 0)
  28. return retval;
  29. return __real_open(path, mode);
  30. }
  31. int __wrap_close(int fd)
  32. {
  33. check_expected(fd);
  34. int retval = mock_type(int);
  35. if (retval != 0)
  36. return __real_close(fd);
  37. return retval;
  38. }
  39. int __wrap_ioctl(int fd, unsigned long req, ...)
  40. {
  41. assert_true(fd > 0);
  42. check_expected(req);
  43. int retval = mock_type(int);
  44. char *expected_arg = mock_type(char*);
  45. if (expected_arg == NULL)
  46. return retval;
  47. va_list ap;
  48. va_start(ap, req);
  49. char *arg = va_arg(ap, char *);
  50. va_end(ap);
  51. assert_non_null(arg);
  52. assert_memory_equal(expected_arg, arg, _IOC_SIZE(req));
  53. return retval;
  54. }
  55. int __wrap_read(int fd, char *buf, size_t len)
  56. {
  57. assert_true(fd > 0);
  58. assert_non_null(buf);
  59. check_expected(len);
  60. int retval = mock_type(int);
  61. int real = mock_type(int);
  62. if (real)
  63. return __real_read(fd, buf, len);
  64. return retval;
  65. }
  66. int __wrap_write(int fd, char *buf, size_t len)
  67. {
  68. assert_true(fd > 0);
  69. assert_non_null(buf);
  70. void *expected_buf = mock_type(void*);
  71. size_t expected_len = mock_type(size_t);
  72. assert_int_equal(expected_len, len);
  73. assert_memory_equal(expected_buf, buf, expected_len);
  74. return mock_type(int);
  75. }
  76. off_t __wrap_lseek(int fd, off_t seek, int whence)
  77. {
  78. assert_true(fd > 0);
  79. check_expected(seek);
  80. check_expected(whence);
  81. return mock_type(off_t);
  82. }
  83. #define expect_open(X,Y,Z) do { \
  84. expect_string(__wrap_open, path, X);\
  85. will_return(__wrap_open, Y);\
  86. will_return(__wrap_open, Z);\
  87. } while(0);
  88. #define expect_close(X,Y) do { \
  89. expect_value(__wrap_close, fd, X);\
  90. will_return(__wrap_close, Y);\
  91. } while(0);
  92. #define expect_ioctl(W,X,Y) do { \
  93. expect_value(__wrap_ioctl, req, W);\
  94. will_return(__wrap_ioctl, X);\
  95. will_return(__wrap_ioctl, Y);\
  96. } while(0);
  97. #define expect_ioctl_short(X,Y) do { \
  98. expect_value(__wrap_ioctl, req, X);\
  99. will_return(__wrap_ioctl, Y);\
  100. will_return(__wrap_ioctl, NULL);\
  101. } while(0);
  102. #define expect_write(X,Y,Z) do { \
  103. will_return(__wrap_write, X);\
  104. will_return(__wrap_write, Y);\
  105. will_return(__wrap_write, Z);\
  106. } while(0);
  107. #define expect_lseek(X,Y,Z) do { \
  108. expect_value(__wrap_lseek, seek, X);\
  109. expect_value(__wrap_lseek, whence, Y);\
  110. will_return(__wrap_lseek, Z);\
  111. } while(0);
  112. #define expect_read(X,Y) do { \
  113. expect_value(__wrap_read, len, X);\
  114. will_return(__wrap_read, Y);\
  115. will_return(__wrap_read, 0);\
  116. } while(0);
  117. #define expect_read_real(X,Y) do { \
  118. expect_value(__wrap_read, len, X);\
  119. will_return(__wrap_read, Y);\
  120. will_return(__wrap_read, 1);\
  121. } while(0);