unittest_curve_encoding.cpp
Go to the documentation of this file.
1 /* SPDX-License-Identifier: MPL-2.0 */
2 
3 #include "../tests/testutil_unity.hpp"
4 
5 // TODO: remove this ugly hack
6 #ifdef close
7 #undef close
8 #endif
9 
10 #include <curve_mechanism_base.hpp>
11 #include <msg.hpp>
12 #include <random.hpp>
13 
14 #include <unity.h>
15 
16 #include <vector>
17 
18 void setUp ()
19 {
20 }
21 
22 void tearDown ()
23 {
24 }
25 
27 {
28 #ifdef ZMQ_HAVE_CURVE
29  const std::vector<uint8_t> original (static_cast<uint8_t *> (msg_->data ()),
30  static_cast<uint8_t *> (msg_->data ())
31  + msg_->size ());
32 
33  zmq::curve_encoding_t encoding_client ("CurveZMQMESSAGEC",
34  "CurveZMQMESSAGES", false);
35  zmq::curve_encoding_t encoding_server ("CurveZMQMESSAGES",
36  "CurveZMQMESSAGEC", false);
37 
38  uint8_t client_public[32];
39  uint8_t client_secret[32];
41  crypto_box_keypair (client_public, client_secret));
42 
43  uint8_t server_public[32];
44  uint8_t server_secret[32];
46  crypto_box_keypair (server_public, server_secret));
47 
49  crypto_box_beforenm (encoding_client.get_writable_precom_buffer (),
50  server_public, client_secret));
52  crypto_box_beforenm (encoding_server.get_writable_precom_buffer (),
53  client_public, server_secret));
54 
55  TEST_ASSERT_SUCCESS_ERRNO (encoding_client.encode (msg_));
56 
57  // TODO: This is hacky...
58  encoding_server.set_peer_nonce (0);
59  int error_event_code;
61  encoding_server.decode (msg_, &error_event_code));
62 
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  LIBZMQ_UNUSED (msg_);
70 #endif
71 }
72 
74 {
75 #ifndef ZMQ_HAVE_CURVE
76  TEST_IGNORE_MESSAGE ("CURVE support is disabled");
77 #endif
78  zmq::msg_t msg;
79  msg.init ();
80 
81  test_roundtrip (&msg);
82 
83  msg.close ();
84 }
85 
87 {
88 #ifndef ZMQ_HAVE_CURVE
89  TEST_IGNORE_MESSAGE ("CURVE support is disabled");
90 #endif
91  zmq::msg_t msg;
92  msg.init_size (32);
93  memcpy (msg.data (), "0123456789ABCDEF0123456789ABCDEF", 32);
94 
95  test_roundtrip (&msg);
96 
97  msg.close ();
98 }
99 
101 {
102 #ifndef ZMQ_HAVE_CURVE
103  TEST_IGNORE_MESSAGE ("CURVE support is disabled");
104 #endif
105  zmq::msg_t msg;
106  msg.init_size (2048);
107  for (size_t pos = 0; pos < 2048; pos += 32) {
108  memcpy (static_cast<char *> (msg.data ()) + pos,
109  "0123456789ABCDEF0123456789ABCDEF", 32);
110  }
111 
112  test_roundtrip (&msg);
113 
114  msg.close ();
115 }
116 
118 {
119 #ifndef ZMQ_HAVE_CURVE
120  TEST_IGNORE_MESSAGE ("CURVE support is disabled");
121 #endif
122  zmq::msg_t msg;
123  msg.init ();
125 
126  test_roundtrip (&msg);
128 
129  msg.close ();
130 }
131 
132 int main ()
133 {
135  zmq::random_open ();
136 
137  UNITY_BEGIN ();
138 
142 
144 
146 
147  return UNITY_END ();
148 }
zmq::random_open
void random_open()
Definition: random.cpp:59
UNITY_END
return UNITY_END()
main
int main()
Definition: unittest_curve_encoding.cpp:132
TEST_ASSERT_TRUE
#define TEST_ASSERT_TRUE(condition)
Definition: unity.h:121
tearDown
void tearDown()
Definition: unittest_curve_encoding.cpp:22
RUN_TEST
#define RUN_TEST(func)
Definition: unity_internals.h:615
curve_mechanism_base.hpp
random.hpp
TEST_ASSERT_EQUAL_UINT8_ARRAY
#define TEST_ASSERT_EQUAL_UINT8_ARRAY(expected, actual, num_elements)
Definition: unity.h:246
zmq::msg_t::init_size
int init_size(size_t size_)
Definition: msg.cpp:62
zmq::random_close
void random_close()
Definition: random.cpp:64
zmq::msg_t::flags
unsigned char flags
Definition: msg.hpp:232
LIBZMQ_UNUSED
#define LIBZMQ_UNUSED(object)
Definition: macros.hpp:6
zmq::msg_t::close
int close()
Definition: msg.cpp:242
TEST_ASSERT_EQUAL_INT
#define TEST_ASSERT_EQUAL_INT(expected, actual)
Definition: unity.h:128
zmq::msg_t::init
int init()
Definition: msg.cpp:50
msg.hpp
TEST_IGNORE_MESSAGE
#define TEST_IGNORE_MESSAGE(message)
Definition: unity.h:103
unity.h
test_roundtrip_empty
void test_roundtrip_empty()
Definition: unittest_curve_encoding.cpp:73
zmq::msg_t::more
@ more
Definition: msg.hpp:55
test_roundtrip_empty_more
void test_roundtrip_empty_more()
Definition: unittest_curve_encoding.cpp:117
test_roundtrip_large
void test_roundtrip_large()
Definition: unittest_curve_encoding.cpp:100
setup_test_environment
void setup_test_environment(int timeout_seconds_)
Definition: testutil.cpp:201
UNITY_BEGIN
UNITY_BEGIN()
test_roundtrip
void test_roundtrip(zmq::msg_t *msg_)
Definition: unittest_curve_encoding.cpp:26
setUp
void setUp()
Definition: unittest_curve_encoding.cpp:18
zmq::msg_t::size
unsigned char size
Definition: msg.hpp:240
zmq::msg_t::data
unsigned char data[max_vsm_size]
Definition: msg.hpp:239
test_roundtrip_small
void test_roundtrip_small()
Definition: unittest_curve_encoding.cpp:86
zmq::msg_t::set_flags
void set_flags(unsigned char flags_)
Definition: msg.cpp:433
TEST_ASSERT_SUCCESS_ERRNO
#define TEST_ASSERT_SUCCESS_ERRNO(expr)
Definition: proxy_thr.cpp:47
zmq::msg_t
Definition: msg.hpp:33


libaditof
Author(s):
autogenerated on Wed May 21 2025 02:07:00