unittest_curve_encoding.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. Copyright (c) 2018 Contributors as noted in the AUTHORS file
  3. This file is part of 0MQ.
  4. 0MQ is free software; you can redistribute it and/or modify it under
  5. the terms of the GNU Lesser General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. 0MQ is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU Lesser General Public License for more details.
  12. You should have received a copy of the GNU Lesser General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>.
  14. */
  15. #include "../tests/testutil_unity.hpp"
  16. // TODO: remove this ugly hack
  17. #ifdef close
  18. #undef close
  19. #endif
  20. #include <curve_mechanism_base.hpp>
  21. #include <msg.hpp>
  22. #include <random.hpp>
  23. #include <unity.h>
  24. #include <vector>
  25. void setUp ()
  26. {
  27. }
  28. void tearDown ()
  29. {
  30. }
  31. void test_roundtrip (zmq::msg_t *msg_)
  32. {
  33. #ifdef ZMQ_HAVE_CURVE
  34. const std::vector<uint8_t> original (static_cast<uint8_t *> (msg_->data ()),
  35. static_cast<uint8_t *> (msg_->data ())
  36. + msg_->size ());
  37. zmq::curve_encoding_t encoding_client ("CurveZMQMESSAGEC",
  38. "CurveZMQMESSAGES",
  39. false);
  40. zmq::curve_encoding_t encoding_server ("CurveZMQMESSAGES",
  41. "CurveZMQMESSAGEC",
  42. false);
  43. uint8_t client_public[32];
  44. uint8_t client_secret[32];
  45. TEST_ASSERT_SUCCESS_ERRNO (
  46. crypto_box_keypair (client_public, client_secret));
  47. uint8_t server_public[32];
  48. uint8_t server_secret[32];
  49. TEST_ASSERT_SUCCESS_ERRNO (
  50. crypto_box_keypair (server_public, server_secret));
  51. TEST_ASSERT_SUCCESS_ERRNO (
  52. crypto_box_beforenm (encoding_client.get_writable_precom_buffer (),
  53. server_public, client_secret));
  54. TEST_ASSERT_SUCCESS_ERRNO (
  55. crypto_box_beforenm (encoding_server.get_writable_precom_buffer (),
  56. client_public, server_secret));
  57. TEST_ASSERT_SUCCESS_ERRNO (encoding_client.encode (msg_));
  58. // TODO: This is hacky...
  59. encoding_server.set_peer_nonce (0);
  60. int error_event_code;
  61. TEST_ASSERT_SUCCESS_ERRNO (
  62. encoding_server.decode (msg_, &error_event_code));
  63. TEST_ASSERT_EQUAL_INT (original.size (), msg_->size ());
  64. if (!original.empty ()) {
  65. TEST_ASSERT_EQUAL_UINT8_ARRAY (&original[0], msg_->data (),
  66. original.size ());
  67. }
  68. #else
  69. TEST_IGNORE_MESSAGE ("CURVE support is disabled");
  70. #endif
  71. }
  72. void test_roundtrip_empty ()
  73. {
  74. zmq::msg_t msg;
  75. msg.init ();
  76. test_roundtrip (&msg);
  77. msg.close ();
  78. }
  79. void test_roundtrip_small ()
  80. {
  81. zmq::msg_t msg;
  82. msg.init_size (32);
  83. memcpy (msg.data (), "0123456789ABCDEF0123456789ABCDEF", 32);
  84. test_roundtrip (&msg);
  85. msg.close ();
  86. }
  87. void test_roundtrip_large ()
  88. {
  89. zmq::msg_t msg;
  90. msg.init_size (2048);
  91. for (size_t pos = 0; pos < 2048; pos += 32) {
  92. memcpy (static_cast<char *> (msg.data ()) + pos,
  93. "0123456789ABCDEF0123456789ABCDEF", 32);
  94. }
  95. test_roundtrip (&msg);
  96. msg.close ();
  97. }
  98. void test_roundtrip_empty_more ()
  99. {
  100. zmq::msg_t msg;
  101. msg.init ();
  102. msg.set_flags (zmq::msg_t::more);
  103. test_roundtrip (&msg);
  104. TEST_ASSERT_TRUE (msg.flags () & zmq::msg_t::more);
  105. msg.close ();
  106. }
  107. int main ()
  108. {
  109. setup_test_environment ();
  110. zmq::random_open ();
  111. UNITY_BEGIN ();
  112. RUN_TEST (test_roundtrip_empty);
  113. RUN_TEST (test_roundtrip_small);
  114. RUN_TEST (test_roundtrip_large);
  115. RUN_TEST (test_roundtrip_empty_more);
  116. zmq::random_close ();
  117. return UNITY_END ();
  118. }