From fe1fea82c324598baa3facf2767c5ea5bf9dc956 Mon Sep 17 00:00:00 2001 From: iphydf Date: Wed, 9 Nov 2016 00:01:06 +0000 Subject: [PATCH] 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. --- testing/hstox/binary_decode.c | 38 ++++++++++++++++++++++++++++++++--- testing/hstox/binary_encode.c | 2 +- 2 files changed, 36 insertions(+), 4 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 @@ #include "../../toxcore/DHT.h" -METHOD(bin, Binary_decode, CipherText) + +static void decode_bytestring(msgpack_object_bin args, msgpack_packer *res, + int64_t min_length) { - uint64_t length; + int64_t length; uint64_t tmp; SUCCESS { + if (args.size < sizeof(uint64_t)) + { + // Not enough space to even fit the size. + msgpack_pack_nil(res); + return; + } + memcpy(&tmp, args.ptr, sizeof(uint64_t)); length = be64toh(tmp); + // TODO(iphydf): Get rid of this case if/when + // https://github.com/kolmodin/binary/issues/127 is fixed. This is a + // workaround for a Haskell library bug. Our implementation here without + // the special case for negative length, and instead interpreting the + // length as unsigned integer, was correct. + if (length < 0) + { + length = 0; + } + if (args.size >= sizeof(uint64_t) && args.size == length + sizeof(uint64_t)) { + if (length < min_length) { + // CipherTexts need at least a MAC. + msgpack_pack_nil(res); + return; + } + msgpack_pack_bin(res, args.size - sizeof(uint64_t)); msgpack_pack_bin_body(res, args.ptr + sizeof(uint64_t), args.size - sizeof(uint64_t)); } else { msgpack_pack_nil(res); } } +} + + +METHOD(bin, Binary_decode, CipherText) +{ + decode_bytestring(args, res, crypto_box_MACBYTES); return 0; } @@ -156,7 +187,8 @@ METHOD(bin, Binary_decode, PingPacket) METHOD(bin, Binary_decode, PlainText) { - return pending; + decode_bytestring(args, res, 0); + return 0; } METHOD(bin, Binary_decode, PortNumber) diff --git a/testing/hstox/binary_encode.c b/testing/hstox/binary_encode.c index 9b9d3876..99c9d4a1 100644 --- a/testing/hstox/binary_encode.c +++ b/testing/hstox/binary_encode.c @@ -183,7 +183,7 @@ METHOD(u64, Binary_encode, PingPacket) METHOD(bin, Binary_encode, PlainText) { - return pending; + return Binary_encode_CipherText(args, res); } METHOD(u64, Binary_encode, PortNumber)