Merge remote-tracking branch 'mintytoast/bug_19563'

This commit is contained in:
Nick Mathewson 2016-11-03 18:40:41 -04:00
commit 80a5091e4f
2 changed files with 31 additions and 14 deletions

View File

@ -5612,6 +5612,33 @@ test_util_monotonic_time_ratchet(void *arg)
; ;
} }
static void
test_util_htonll(void *arg)
{
(void)arg;
#ifdef WORDS_BIGENDIAN
const uint64_t res_be = 0x8877665544332211;
#else
const uint64_t res_le = 0x1122334455667788;
#endif
tt_u64_op(0, OP_EQ, tor_htonll(0));
tt_u64_op(0, OP_EQ, tor_ntohll(0));
tt_u64_op(UINT64_MAX, OP_EQ, tor_htonll(UINT64_MAX));
tt_u64_op(UINT64_MAX, OP_EQ, tor_ntohll(UINT64_MAX));
#ifdef WORDS_BIGENDIAN
tt_u64_op(res_be, OP_EQ, tor_htonll(0x8877665544332211));
tt_u64_op(res_be, OP_EQ, tor_ntohll(0x8877665544332211));
#else
tt_u64_op(res_le, OP_EQ, tor_htonll(0x8877665544332211));
tt_u64_op(res_le, OP_EQ, tor_ntohll(0x8877665544332211));
#endif
done:
;
}
#define UTIL_LEGACY(name) \ #define UTIL_LEGACY(name) \
{ #name, test_util_ ## name , 0, NULL, NULL } { #name, test_util_ ## name , 0, NULL, NULL }
@ -5705,6 +5732,7 @@ struct testcase_t util_tests[] = {
UTIL_TEST(calloc_check, 0), UTIL_TEST(calloc_check, 0),
UTIL_TEST(monotonic_time, 0), UTIL_TEST(monotonic_time, 0),
UTIL_TEST(monotonic_time_ratchet, TT_FORK), UTIL_TEST(monotonic_time_ratchet, TT_FORK),
UTIL_TEST(htonll, 0),
END_OF_TESTCASES END_OF_TESTCASES
}; };

View File

@ -11,25 +11,14 @@
#define NS_MODULE util_format #define NS_MODULE util_format
#if !defined(HAVE_HTONLL) && !defined(htonll)
#ifdef WORDS_BIGENDIAN
#define htonll(x) (x)
#else
static uint64_t
htonll(uint64_t a)
{
return htonl((uint32_t)(a>>32)) | (((uint64_t)htonl((uint32_t)a))<<32);
}
#endif
#endif
static void static void
test_util_format_unaligned_accessors(void *ignored) test_util_format_unaligned_accessors(void *ignored)
{ {
(void)ignored; (void)ignored;
char buf[9] = "onionsoup"; // 6f6e696f6e736f7570 char buf[9] = "onionsoup"; // 6f6e696f6e736f7570
tt_u64_op(get_uint64(buf+1), OP_EQ, htonll(U64_LITERAL(0x6e696f6e736f7570))); tt_u64_op(get_uint64(buf+1), OP_EQ,
tor_htonll(U64_LITERAL(0x6e696f6e736f7570)));
tt_uint_op(get_uint32(buf+1), OP_EQ, htonl(0x6e696f6e)); tt_uint_op(get_uint32(buf+1), OP_EQ, htonl(0x6e696f6e));
tt_uint_op(get_uint16(buf+1), OP_EQ, htons(0x6e69)); tt_uint_op(get_uint16(buf+1), OP_EQ, htons(0x6e69));
tt_uint_op(get_uint8(buf+1), OP_EQ, 0x6e); tt_uint_op(get_uint8(buf+1), OP_EQ, 0x6e);
@ -43,7 +32,7 @@ test_util_format_unaligned_accessors(void *ignored)
set_uint32(buf+1, htonl(0x78696465)); set_uint32(buf+1, htonl(0x78696465));
tt_mem_op(buf, OP_EQ, "oxidestop", 9); tt_mem_op(buf, OP_EQ, "oxidestop", 9);
set_uint64(buf+1, htonll(U64_LITERAL(0x6266757363617465))); set_uint64(buf+1, tor_htonll(U64_LITERAL(0x6266757363617465)));
tt_mem_op(buf, OP_EQ, "obfuscate", 9); tt_mem_op(buf, OP_EQ, "obfuscate", 9);
done: done:
; ;