test_socket.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /*
  2. * Copyright (C) 2014 Martin Willi
  3. * Copyright (C) 2014 revosec AG
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
  12. * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
  13. * for more details.
  14. */
  15. #include <test_suite.h>
  16. #include "../vici_socket.h"
  17. #include <unistd.h>
  18. typedef struct {
  19. vici_socket_t *s;
  20. int disconnect;
  21. int bytes;
  22. u_int id;
  23. } test_data_t;
  24. static void echo_inbound(void *user, u_int id, chunk_t buf)
  25. {
  26. test_data_t *data = user;
  27. ck_assert_int_eq(data->id, id);
  28. /* count number of bytes, including the header */
  29. data->bytes += buf.len + sizeof(uint32_t);
  30. /* echo back data chunk */
  31. data->s->send(data->s, id, chunk_clone(buf));
  32. }
  33. static void echo_connect(void *user, u_int id)
  34. {
  35. test_data_t *data = user;
  36. data->id = id;
  37. }
  38. static void echo_disconnect(void *user, u_int id)
  39. {
  40. test_data_t *data = user;
  41. ck_assert(id == data->id);
  42. data->disconnect++;
  43. }
  44. static struct {
  45. char *uri;
  46. u_int chunksize;
  47. } echo_tests[] = {
  48. { "tcp://127.0.0.1:6543", ~0 },
  49. { "tcp://127.0.0.1:6543", 1 },
  50. { "tcp://127.0.0.1:6543", 2 },
  51. { "tcp://127.0.0.1:6543", 3 },
  52. { "tcp://127.0.0.1:6543", 7 },
  53. #ifndef WIN32
  54. { "unix:///tmp/strongswan-tests-vici-socket", ~0 },
  55. { "unix:///tmp/strongswan-tests-vici-socket", 1 },
  56. { "unix:///tmp/strongswan-tests-vici-socket", 2 },
  57. { "unix:///tmp/strongswan-tests-vici-socket", 3 },
  58. { "unix:///tmp/strongswan-tests-vici-socket", 7 },
  59. #endif /* !WIN32 */
  60. };
  61. START_TEST(test_echo)
  62. {
  63. stream_t *c;
  64. test_data_t data = {};
  65. chunk_t x, m = chunk_from_chars(
  66. 0x00,0x00,0x00,0x00,
  67. 0x00,0x00,0x00,0x01, 0x01,
  68. 0x00,0x00,0x00,0x05, 0x11,0x12,0x13,0x14,0x15,
  69. 0x00,0x00,0x00,0x0A, 0x21,0x22,0x23,0x24,0x25,0x26,0x27,0x28,0x29,0x02A,
  70. );
  71. char buf[m.len];
  72. uint32_t len;
  73. lib->processor->set_threads(lib->processor, 4);
  74. /* create socket, connect with stream */
  75. data.s = vici_socket_create(echo_tests[_i].uri, echo_inbound, echo_connect,
  76. echo_disconnect, &data);
  77. ck_assert(data.s != NULL);
  78. c = lib->streams->connect(lib->streams, echo_tests[_i].uri);
  79. ck_assert(c != NULL);
  80. /* write arbitrary chunks of messages blob depending on test */
  81. x = m;
  82. while (x.len)
  83. {
  84. len = min(x.len, echo_tests[_i].chunksize);
  85. ck_assert(c->write_all(c, x.ptr, len));
  86. x = chunk_skip(x, len);
  87. }
  88. /* verify echo */
  89. ck_assert(c->read_all(c, buf, sizeof(buf)));
  90. ck_assert(chunk_equals(m, chunk_from_thing(buf)));
  91. /* wait for completion */
  92. c->destroy(c);
  93. while (data.disconnect != 1)
  94. {
  95. usleep(1000);
  96. }
  97. /* check that we got correct number of bytes/invocations */
  98. ck_assert_int_eq(data.bytes, m.len);
  99. data.s->destroy(data.s);
  100. }
  101. END_TEST
  102. Suite *socket_suite_create()
  103. {
  104. Suite *s;
  105. TCase *tc;
  106. s = suite_create("vici socket");
  107. tc = tcase_create("echo");
  108. tcase_add_loop_test(tc, test_echo, 0, countof(echo_tests));
  109. suite_add_tcase(s, tc);
  110. return s;
  111. }