summaryrefslogtreecommitdiff
path: root/testing
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2017-01-20 17:03:30 +0000
committeriphydf <iphydf@users.noreply.github.com>2017-06-03 23:32:46 +0000
commitd4be41a3ad1ab01d7c5d475b353d9a1e3d17d31c (patch)
tree746b9c15bef07d156dc3f3ccacb299887ca2171c /testing
parenta8866fc17fd6c04860c8fa1f104297a5cd0cb9f2 (diff)
Use new encoding of `Maybe` in msgpack results.
The new encoding is `0` for `Nothing` and `[1, x]` for `Just x`.
Diffstat (limited to 'testing')
-rw-r--r--testing/hstox/binary_decode.c42
-rw-r--r--testing/hstox/binary_encode.c7
-rw-r--r--testing/hstox/driver.c29
-rw-r--r--testing/hstox/driver.h2
-rw-r--r--testing/hstox/fuzz_main.c2
-rw-r--r--testing/hstox/test_main.c2
-rw-r--r--testing/hstox/util.h2
7 files changed, 66 insertions, 20 deletions
diff --git a/testing/hstox/binary_decode.c b/testing/hstox/binary_decode.c
index fb3bca43..588b9a7c 100644
--- a/testing/hstox/binary_decode.c
+++ b/testing/hstox/binary_decode.c
@@ -1,3 +1,4 @@
1#define _BSD_SOURCE
1#include "methods.h" 2#include "methods.h"
2 3
3#include "byteswap.h" 4#include "byteswap.h"
@@ -9,19 +10,17 @@
9static void decode_bytestring(msgpack_object_bin args, msgpack_packer *res, 10static void decode_bytestring(msgpack_object_bin args, msgpack_packer *res,
10 int64_t min_length) 11 int64_t min_length)
11{ 12{
12 int64_t length;
13 uint64_t tmp;
14
15 SUCCESS { 13 SUCCESS {
16 if (args.size < sizeof(uint64_t)) 14 if (args.size < sizeof(uint64_t))
17 { 15 {
18 // Not enough space to even fit the size. 16 // Not enough space to even fit the size.
19 msgpack_pack_nil(res); 17 msgpack_pack_uint8(res, 0);
20 return; 18 return;
21 } 19 }
22 20
21 uint64_t tmp;
23 memcpy(&tmp, args.ptr, sizeof(uint64_t)); 22 memcpy(&tmp, args.ptr, sizeof(uint64_t));
24 length = be64toh(tmp); 23 int64_t length = be64toh(tmp);
25 24
26 // TODO(iphydf): Get rid of this case if/when 25 // TODO(iphydf): Get rid of this case if/when
27 // https://github.com/kolmodin/binary/issues/127 is fixed. This is a 26 // https://github.com/kolmodin/binary/issues/127 is fixed. This is a
@@ -37,14 +36,17 @@ static void decode_bytestring(msgpack_object_bin args, msgpack_packer *res,
37 { 36 {
38 if (length < min_length) { 37 if (length < min_length) {
39 // CipherTexts need at least a MAC. 38 // CipherTexts need at least a MAC.
40 msgpack_pack_nil(res); 39 msgpack_pack_uint8(res, 0);
41 return; 40 return;
42 } 41 }
43 42
43 msgpack_pack_array(res, 2);
44 msgpack_pack_uint8(res, 1);
45
44 msgpack_pack_bin(res, args.size - sizeof(uint64_t)); 46 msgpack_pack_bin(res, args.size - sizeof(uint64_t));
45 msgpack_pack_bin_body(res, args.ptr + sizeof(uint64_t), args.size - sizeof(uint64_t)); 47 msgpack_pack_bin_body(res, args.ptr + sizeof(uint64_t), args.size - sizeof(uint64_t));
46 } else { 48 } else {
47 msgpack_pack_nil(res); 49 msgpack_pack_uint8(res, 0);
48 } 50 }
49 } 51 }
50} 52}
@@ -61,6 +63,11 @@ METHOD(bin, Binary_decode, DhtPacket)
61 return pending; 63 return pending;
62} 64}
63 65
66METHOD(bin, Binary_decode, DhtRequestPacket)
67{
68 return pending;
69}
70
64METHOD(bin, Binary_decode, HostAddress) 71METHOD(bin, Binary_decode, HostAddress)
65{ 72{
66 return pending; 73 return pending;
@@ -81,9 +88,12 @@ METHOD(bin, Binary_decode, KeyPair)
81 SUCCESS { 88 SUCCESS {
82 if (args.size != 64) 89 if (args.size != 64)
83 { 90 {
84 msgpack_pack_nil(res); 91 msgpack_pack_uint8(res, 0);
85 } else { 92 } else {
86 msgpack_pack_array(res, 2); 93 msgpack_pack_array(res, 2);
94 msgpack_pack_uint8(res, 1);
95
96 msgpack_pack_array(res, 2);
87 msgpack_pack_bin(res, 32); 97 msgpack_pack_bin(res, 32);
88 msgpack_pack_bin_body(res, args.ptr, 32); 98 msgpack_pack_bin_body(res, args.ptr, 32);
89 msgpack_pack_bin(res, 32); 99 msgpack_pack_bin(res, 32);
@@ -113,6 +123,9 @@ METHOD(bin, Binary_decode, NodeInfo)
113 SUCCESS { 123 SUCCESS {
114 if (len > 0 && data_processed > 0 && data_processed == args.size) 124 if (len > 0 && data_processed > 0 && data_processed == args.size)
115 { 125 {
126 msgpack_pack_array(res, 2);
127 msgpack_pack_uint8(res, 1);
128
116 msgpack_pack_array(res, 3); 129 msgpack_pack_array(res, 3);
117 msgpack_pack_uint8(res, tcp); 130 msgpack_pack_uint8(res, tcp);
118 msgpack_pack_array(res, 2); 131 msgpack_pack_array(res, 2);
@@ -133,7 +146,7 @@ METHOD(bin, Binary_decode, NodeInfo)
133 msgpack_pack_bin(res, CRYPTO_PUBLIC_KEY_SIZE); 146 msgpack_pack_bin(res, CRYPTO_PUBLIC_KEY_SIZE);
134 msgpack_pack_bin_body(res, &node.public_key, CRYPTO_PUBLIC_KEY_SIZE); 147 msgpack_pack_bin_body(res, &node.public_key, CRYPTO_PUBLIC_KEY_SIZE);
135 } else { 148 } else {
136 msgpack_pack_nil(res); 149 msgpack_pack_uint8(res, 0);
137 } 150 }
138 } 151 }
139 152
@@ -160,7 +173,7 @@ METHOD(bin, Binary_decode, PacketKind)
160 SUCCESS { 173 SUCCESS {
161 if (args.size != 1) 174 if (args.size != 1)
162 { 175 {
163 msgpack_pack_nil(res); 176 msgpack_pack_uint8(res, 0);
164 } else { 177 } else {
165 uint8_t kind = args.ptr[0]; 178 uint8_t kind = args.ptr[0];
166 size_t i; 179 size_t i;
@@ -168,13 +181,15 @@ METHOD(bin, Binary_decode, PacketKind)
168 for (i = 0; i < sizeof packet_kinds / sizeof *packet_kinds; i++) 181 for (i = 0; i < sizeof packet_kinds / sizeof *packet_kinds; i++)
169 { 182 {
170 if (packet_kinds[i] == kind) { 183 if (packet_kinds[i] == kind) {
184 msgpack_pack_array(res, 2);
185 msgpack_pack_uint8(res, 1);
171 msgpack_pack_fix_uint8(res, i); 186 msgpack_pack_fix_uint8(res, i);
172 return 0; 187 return 0;
173 } 188 }
174 } 189 }
175 190
176 // Packet kind not found => error. 191 // Packet kind not found => error.
177 msgpack_pack_nil(res); 192 msgpack_pack_uint8(res, 0);
178 } 193 }
179 } 194 }
180 return 0; 195 return 0;
@@ -199,9 +214,11 @@ METHOD(bin, Binary_decode, PortNumber)
199 uint16_t tmp; 214 uint16_t tmp;
200 memcpy(&tmp, args.ptr, 2); 215 memcpy(&tmp, args.ptr, 2);
201 uint16_t port = ntohs(tmp); 216 uint16_t port = ntohs(tmp);
217 msgpack_pack_array(res, 2);
218 msgpack_pack_uint8(res, 1);
202 msgpack_pack_uint16(res, port); 219 msgpack_pack_uint16(res, port);
203 } else { 220 } else {
204 msgpack_pack_nil(res); 221 msgpack_pack_uint8(res, 0);
205 } 222 }
206 } 223 }
207 224
@@ -235,6 +252,7 @@ METHOD(array, Binary, decode)
235 return Binary_decode_##TYPE(args.ptr[1].via.bin, res) 252 return Binary_decode_##TYPE(args.ptr[1].via.bin, res)
236 DISPATCH(CipherText); 253 DISPATCH(CipherText);
237 DISPATCH(DhtPacket); 254 DISPATCH(DhtPacket);
255 DISPATCH(DhtRequestPacket);
238 DISPATCH(HostAddress); 256 DISPATCH(HostAddress);
239 DISPATCH(Word64); 257 DISPATCH(Word64);
240 DISPATCH(Key_PublicKey); 258 DISPATCH(Key_PublicKey);
diff --git a/testing/hstox/binary_encode.c b/testing/hstox/binary_encode.c
index 8809be28..aad94f5f 100644
--- a/testing/hstox/binary_encode.c
+++ b/testing/hstox/binary_encode.c
@@ -1,3 +1,4 @@
1#define _BSD_SOURCE
1#include "methods.h" 2#include "methods.h"
2 3
3#include "byteswap.h" 4#include "byteswap.h"
@@ -22,6 +23,11 @@ METHOD(array, Binary_encode, DhtPacket)
22 return pending; 23 return pending;
23} 24}
24 25
26METHOD(array, Binary_encode, DhtRequestPacket)
27{
28 return pending;
29}
30
25METHOD(array, Binary_encode, HostAddress) 31METHOD(array, Binary_encode, HostAddress)
26{ 32{
27 return pending; 33 return pending;
@@ -231,6 +237,7 @@ METHOD(array, Binary, encode)
231 } while (0) 237 } while (0)
232 DISPATCH(CipherText, BIN, bin); 238 DISPATCH(CipherText, BIN, bin);
233 DISPATCH(DhtPacket, ARRAY, array); 239 DISPATCH(DhtPacket, ARRAY, array);
240 DISPATCH(DhtRequestPacket, ARRAY, array);
234 DISPATCH(HostAddress, ARRAY, array); 241 DISPATCH(HostAddress, ARRAY, array);
235 DISPATCH(Word64, POSITIVE_INTEGER, u64); 242 DISPATCH(Word64, POSITIVE_INTEGER, u64);
236 DISPATCH(Key_PublicKey, BIN, bin); 243 DISPATCH(Key_PublicKey, BIN, bin);
diff --git a/testing/hstox/driver.c b/testing/hstox/driver.c
index 7a9a907e..2ae68f1b 100644
--- a/testing/hstox/driver.c
+++ b/testing/hstox/driver.c
@@ -1,3 +1,5 @@
1#define _XOPEN_SOURCE 600
2
1#include <errno.h> 3#include <errno.h>
2#include <fcntl.h> 4#include <fcntl.h>
3#include <netdb.h> 5#include <netdb.h>
@@ -19,7 +21,7 @@
19 21
20static void __attribute__((__noreturn__)) handle_interrupt(int signum) 22static void __attribute__((__noreturn__)) handle_interrupt(int signum)
21{ 23{
22 printf("Caught signal %d; exiting cleanly.\n", signum); 24 fprintf(stderr, "caught signal %d; exiting cleanly.\n", signum);
23 exit(0); 25 exit(0);
24} 26}
25 27
@@ -92,6 +94,12 @@ static int write_sample_input(msgpack_object req)
92 94
93static int handle_request(struct settings cfg, int write_fd, msgpack_object req) 95static int handle_request(struct settings cfg, int write_fd, msgpack_object req)
94{ 96{
97 if (cfg.trace) {
98 fprintf(stderr, "input: ");
99 msgpack_object_print(stderr, req);
100 fprintf(stderr, "\n");
101 }
102
95 msgpack_sbuffer sbuf __attribute__((__cleanup__(msgpack_sbuffer_destroy))); /* buffer */ 103 msgpack_sbuffer sbuf __attribute__((__cleanup__(msgpack_sbuffer_destroy))); /* buffer */
96 msgpack_sbuffer_init(&sbuf); /* initialize buffer */ 104 msgpack_sbuffer_init(&sbuf); /* initialize buffer */
97 105
@@ -121,7 +129,7 @@ static int handle_request(struct settings cfg, int write_fd, msgpack_object req)
121 if (name.size == (sizeof "rpc.capabilities") - 1 && 129 if (name.size == (sizeof "rpc.capabilities") - 1 &&
122 memcmp(name.ptr, "rpc.capabilities", name.size) == 0) { 130 memcmp(name.ptr, "rpc.capabilities", name.size) == 0) {
123 // 3. Error. 131 // 3. Error.
124 msgpack_pack_string(&pk, "Capabilities negiotiation not implemented"); 132 msgpack_pack_string(&pk, "capabilities negiotiation not implemented");
125 // 4. No result. 133 // 4. No result.
126 msgpack_pack_nil(&pk); 134 msgpack_pack_nil(&pk);
127 } else { 135 } else {
@@ -131,9 +139,9 @@ static int handle_request(struct settings cfg, int write_fd, msgpack_object req)
131 139
132 if (error) { 140 if (error) {
133 if (cfg.debug) { 141 if (cfg.debug) {
134 printf("Error '%s' in request: ", error); 142 fprintf(stderr, "error '%s' in request: ", error);
135 msgpack_object_print(stdout, req); 143 msgpack_object_print(stderr, req);
136 printf("\n"); 144 fprintf(stderr, "\n");
137 } 145 }
138 146
139 msgpack_pack_string(&pk, error); 147 msgpack_pack_string(&pk, error);
@@ -144,6 +152,15 @@ static int handle_request(struct settings cfg, int write_fd, msgpack_object req)
144 152
145 check_return(E_WRITE, write(write_fd, sbuf.data, sbuf.size)); 153 check_return(E_WRITE, write(write_fd, sbuf.data, sbuf.size));
146 154
155 if (cfg.trace) {
156 fprintf(stderr, "result: ");
157 msgpack_unpacked res __attribute__((__cleanup__(msgpack_unpacked_destroy)));
158 msgpack_unpacked_init(&res);
159 msgpack_unpack_next(&res, sbuf.data, sbuf.size, NULL);
160 msgpack_object_print(stderr, res.data);
161 fprintf(stderr, "\n");
162 }
163
147 return E_OK; 164 return E_OK;
148} 165}
149 166
@@ -177,7 +194,7 @@ int communicate(struct settings cfg, int read_fd, int write_fd)
177 break; 194 break;
178 195
179 case MSGPACK_UNPACK_EXTRA_BYTES: 196 case MSGPACK_UNPACK_EXTRA_BYTES:
180 printf("EXTRA_BYTES\n"); 197 fprintf(stderr, "EXTRA_BYTES\n");
181 break; 198 break;
182 199
183 case MSGPACK_UNPACK_CONTINUE: 200 case MSGPACK_UNPACK_CONTINUE:
diff --git a/testing/hstox/driver.h b/testing/hstox/driver.h
index 219df8a1..150d5cdd 100644
--- a/testing/hstox/driver.h
+++ b/testing/hstox/driver.h
@@ -7,6 +7,8 @@
7struct settings { 7struct settings {
8 // Print the msgpack object on test failure. 8 // Print the msgpack object on test failure.
9 bool debug; 9 bool debug;
10 // Print all inputs and outputs to stderr.
11 bool trace;
10 // Write test sample files into test-inputs/. These files, one per test 12 // Write test sample files into test-inputs/. These files, one per test
11 // method, are used to seed the fuzzer. 13 // method, are used to seed the fuzzer.
12 bool collect_samples; 14 bool collect_samples;
diff --git a/testing/hstox/fuzz_main.c b/testing/hstox/fuzz_main.c
index ff66a166..6f223eb2 100644
--- a/testing/hstox/fuzz_main.c
+++ b/testing/hstox/fuzz_main.c
@@ -12,7 +12,7 @@
12#endif 12#endif
13int main(int argc, char **argv) 13int main(int argc, char **argv)
14{ 14{
15 struct settings cfg = {false, false}; 15 struct settings cfg = {false, false, false};
16#ifdef __AFL_LOOP 16#ifdef __AFL_LOOP
17 17
18 while (__AFL_LOOP(ITERATIONS)) 18 while (__AFL_LOOP(ITERATIONS))
diff --git a/testing/hstox/test_main.c b/testing/hstox/test_main.c
index 7ac18da0..70f0b95c 100644
--- a/testing/hstox/test_main.c
+++ b/testing/hstox/test_main.c
@@ -57,7 +57,7 @@ static char const *error_desc(int code)
57#endif 57#endif
58int main(void) 58int main(void)
59{ 59{
60 struct settings cfg = {true, true}; 60 struct settings cfg = {true, false, true};
61 uint32_t result = network_main(cfg, PORT, TIMEOUT); 61 uint32_t result = network_main(cfg, PORT, TIMEOUT);
62 int line = result >> 16; 62 int line = result >> 16;
63 int error = (result >> 8) & 0xff; 63 int error = (result >> 8) & 0xff;
diff --git a/testing/hstox/util.h b/testing/hstox/util.h
index c00578a2..c4095994 100644
--- a/testing/hstox/util.h
+++ b/testing/hstox/util.h
@@ -1,5 +1,7 @@
1#pragma once 1#pragma once
2 2
3#include <stdarg.h>
4
3#include <msgpack.h> 5#include <msgpack.h>
4 6
5#define check_return(err, expr) \ 7#define check_return(err, expr) \