summaryrefslogtreecommitdiff
path: root/testing/hstox/binary_decode.c
diff options
context:
space:
mode:
authoriphydf <iphydf@users.noreply.github.com>2016-11-09 00:01:06 +0000
committeriphydf <iphydf@users.noreply.github.com>2016-11-11 00:00:37 +0000
commitfe1fea82c324598baa3facf2767c5ea5bf9dc956 (patch)
treee556e1b2b793749f19e9acb4a0e43e3b7cfde719 /testing/hstox/binary_decode.c
parentb2dd50f9fc3a5c3ed1fb58c410e23dff0bf16135 (diff)
Add decode/encode PlainText test support.
These are implemented in terms of decode/encode CipherText. They do the exact same thing, since they are both simple length-prefixed byte arrays.
Diffstat (limited to 'testing/hstox/binary_decode.c')
-rw-r--r--testing/hstox/binary_decode.c38
1 files changed, 35 insertions, 3 deletions
diff --git a/testing/hstox/binary_decode.c b/testing/hstox/binary_decode.c
index f43bae64..49a5550a 100644
--- a/testing/hstox/binary_decode.c
+++ b/testing/hstox/binary_decode.c
@@ -5,23 +5,54 @@
5 5
6#include "../../toxcore/DHT.h" 6#include "../../toxcore/DHT.h"
7 7
8METHOD(bin, Binary_decode, CipherText) 8
9static void decode_bytestring(msgpack_object_bin args, msgpack_packer *res,
10 int64_t min_length)
9{ 11{
10 uint64_t length; 12 int64_t length;
11 uint64_t tmp; 13 uint64_t tmp;
12 14
13 SUCCESS { 15 SUCCESS {
16 if (args.size < sizeof(uint64_t))
17 {
18 // Not enough space to even fit the size.
19 msgpack_pack_nil(res);
20 return;
21 }
22
14 memcpy(&tmp, args.ptr, sizeof(uint64_t)); 23 memcpy(&tmp, args.ptr, sizeof(uint64_t));
15 length = be64toh(tmp); 24 length = be64toh(tmp);
16 25
26 // TODO(iphydf): Get rid of this case if/when
27 // https://github.com/kolmodin/binary/issues/127 is fixed. This is a
28 // workaround for a Haskell library bug. Our implementation here without
29 // the special case for negative length, and instead interpreting the
30 // length as unsigned integer, was correct.
31 if (length < 0)
32 {
33 length = 0;
34 }
35
17 if (args.size >= sizeof(uint64_t) && args.size == length + sizeof(uint64_t)) 36 if (args.size >= sizeof(uint64_t) && args.size == length + sizeof(uint64_t))
18 { 37 {
38 if (length < min_length) {
39 // CipherTexts need at least a MAC.
40 msgpack_pack_nil(res);
41 return;
42 }
43
19 msgpack_pack_bin(res, args.size - sizeof(uint64_t)); 44 msgpack_pack_bin(res, args.size - sizeof(uint64_t));
20 msgpack_pack_bin_body(res, args.ptr + sizeof(uint64_t), args.size - sizeof(uint64_t)); 45 msgpack_pack_bin_body(res, args.ptr + sizeof(uint64_t), args.size - sizeof(uint64_t));
21 } else { 46 } else {
22 msgpack_pack_nil(res); 47 msgpack_pack_nil(res);
23 } 48 }
24 } 49 }
50}
51
52
53METHOD(bin, Binary_decode, CipherText)
54{
55 decode_bytestring(args, res, crypto_box_MACBYTES);
25 return 0; 56 return 0;
26} 57}
27 58
@@ -156,7 +187,8 @@ METHOD(bin, Binary_decode, PingPacket)
156 187
157METHOD(bin, Binary_decode, PlainText) 188METHOD(bin, Binary_decode, PlainText)
158{ 189{
159 return pending; 190 decode_bytestring(args, res, 0);
191 return 0;
160} 192}
161 193
162METHOD(bin, Binary_decode, PortNumber) 194METHOD(bin, Binary_decode, PortNumber)