util.h 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* SPDX-License-Identifier: GPL-2.0-only */
  2. #ifndef UTIL_H
  3. #define UTIL_H
  4. #include <sys/socket.h>
  5. #include <linux/vm_sockets.h>
  6. /* Tests can either run as the client or the server */
  7. enum test_mode {
  8. TEST_MODE_UNSET,
  9. TEST_MODE_CLIENT,
  10. TEST_MODE_SERVER
  11. };
  12. #define DEFAULT_PEER_PORT 1234
  13. /* Test runner options */
  14. struct test_opts {
  15. enum test_mode mode;
  16. unsigned int peer_cid;
  17. unsigned int peer_port;
  18. };
  19. /* A test case definition. Test functions must print failures to stderr and
  20. * terminate with exit(EXIT_FAILURE).
  21. */
  22. struct test_case {
  23. const char *name; /* human-readable name */
  24. /* Called when test mode is TEST_MODE_CLIENT */
  25. void (*run_client)(const struct test_opts *opts);
  26. /* Called when test mode is TEST_MODE_SERVER */
  27. void (*run_server)(const struct test_opts *opts);
  28. bool skip;
  29. };
  30. void init_signals(void);
  31. unsigned int parse_cid(const char *str);
  32. unsigned int parse_port(const char *str);
  33. int vsock_connect(unsigned int cid, unsigned int port, int type);
  34. int vsock_accept(unsigned int cid, unsigned int port,
  35. struct sockaddr_vm *clientaddrp, int type);
  36. int vsock_stream_connect(unsigned int cid, unsigned int port);
  37. int vsock_bind_connect(unsigned int cid, unsigned int port,
  38. unsigned int bind_port, int type);
  39. int vsock_seqpacket_connect(unsigned int cid, unsigned int port);
  40. int vsock_stream_accept(unsigned int cid, unsigned int port,
  41. struct sockaddr_vm *clientaddrp);
  42. int vsock_stream_listen(unsigned int cid, unsigned int port);
  43. int vsock_seqpacket_accept(unsigned int cid, unsigned int port,
  44. struct sockaddr_vm *clientaddrp);
  45. void vsock_wait_remote_close(int fd);
  46. void send_buf(int fd, const void *buf, size_t len, int flags,
  47. ssize_t expected_ret);
  48. void recv_buf(int fd, void *buf, size_t len, int flags, ssize_t expected_ret);
  49. void send_byte(int fd, int expected_ret, int flags);
  50. void recv_byte(int fd, int expected_ret, int flags);
  51. void run_tests(const struct test_case *test_cases,
  52. const struct test_opts *opts);
  53. void list_tests(const struct test_case *test_cases);
  54. void skip_test(struct test_case *test_cases, size_t test_cases_len,
  55. const char *test_id_str);
  56. unsigned long hash_djb2(const void *data, size_t len);
  57. size_t iovec_bytes(const struct iovec *iov, size_t iovnum);
  58. unsigned long iovec_hash_djb2(const struct iovec *iov, size_t iovnum);
  59. struct iovec *alloc_test_iovec(const struct iovec *test_iovec, int iovnum);
  60. void free_test_iovec(const struct iovec *test_iovec,
  61. struct iovec *iovec, int iovnum);
  62. #endif /* UTIL_H */