X-Git-Url: http://git.onelab.eu/?a=blobdiff_plain;f=tests%2Ftest-csum.c;h=c74133a7f320fad2cc02e48427c8fa357888dea9;hb=0affd45bcb05e41dc18a9091e948eaf6526c4f48;hp=fd16dd3f1f43a62139bd3a07c920a3c4aefcdf4f;hpb=d932cf70b1fe1cdf7d97e598847d9b82e4be3e0b;p=sliver-openvswitch.git diff --git a/tests/test-csum.c b/tests/test-csum.c index fd16dd3f1..c74133a7f 100644 --- a/tests/test-csum.c +++ b/tests/test-csum.c @@ -1,5 +1,5 @@ /* - * Copyright (c) 2009, 2010 Nicira Networks. + * Copyright (c) 2009, 2010, 2011, 2014 Nicira, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -16,13 +16,16 @@ #include #include "csum.h" +#include "crc32c.h" #include #include #include #include #include #include "random.h" +#include "unaligned.h" #include "util.h" +#include "ovstest.h" #undef NDEBUG #include @@ -39,7 +42,7 @@ static const struct test_case test_cases[] = { /* RFC 1071 section 3. */ TEST_CASE("\x00\x01\xf2\x03" "\xf4\xf5\xf6\xf7", - (uint16_t) ~0xddf2), + 0xffff - 0xddf2 /* ~0xddf2 */), /* http://www.sbprojects.com/projects/tcpip/theory/theory14.htm */ TEST_CASE("\x45\x00\x00\x28" @@ -93,12 +96,15 @@ static void test_rfc1624(void) { /* "...an IP packet header in which a 16-bit field m = 0x5555..." */ - uint8_t data[32] = - "\xfe\x8f\xc1\x14\x4b\x6f\x70\x2a\x80\x29\x78\xc0\x58\x81\x77\xaa" - "\x66\x64\xfc\x96\x63\x97\x64\xee\x12\x53\x1d\xa9\x2d\xa9\x55\x55"; + uint8_t data[32] = { + 0xfe, 0x8f, 0xc1, 0x14, 0x4b, 0x6f, 0x70, 0x2a, + 0x80, 0x29, 0x78, 0xc0, 0x58, 0x81, 0x77, 0xaa, + 0x66, 0x64, 0xfc, 0x96, 0x63, 0x97, 0x64, 0xee, + 0x12, 0x53, 0x1d, 0xa9, 0x2d, 0xa9, 0x55, 0x55 + }; /* "...the one's complement sum of all other header octets is 0xCD7A." */ - assert(ntohs(csum(data, sizeof data - 2)) == (uint16_t) ~0xcd7a); + assert(ntohs(csum(data, sizeof data - 2)) == 0xffff - 0xcd7a); /* "...the header checksum would be: @@ -126,20 +132,61 @@ test_rfc1624(void) = ~(0x22D0 + ~0x5555 + 0x3285) = ~0xFFFF = 0x0000" */ - assert(recalc_csum16(0xdd2f, 0x5555, 0x3285) == 0x0000); + assert(recalc_csum16(htons(0xdd2f), htons(0x5555), htons(0x3285)) + == htons(0x0000)); mark('#'); } -int -main(void) +/* CRC32C checksum tests, based on Intel IPPs, Chapter 13, + * ippsCRC32C_8u() example, found at the following location: + * http://software.intel.com/sites/products/documentation/hpc/ipp/ipps/ */ +static void +test_crc32c(void) +{ + int i; + uint8_t data[48] = { + 0x01, 0xc0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x04, 0x00, + 0x00, 0x00, 0x00, 0x14, 0x00, 0x00, 0x00, 0x18, + 0x28, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 + }; + + /* iSCSI Read PDU */ + assert(ntohl(crc32c(data, 48)) == 0x563a96d9L); + + /* 32 bytes of all zeroes */ + for (i = 0; i < 32; i++) data[i] = 0x00; + assert(ntohl(crc32c(data, 32)) == 0xaa36918aL); + + /* 32 bytes of all ones */ + for (i = 0; i < 32; i++) data[i] = 0xff; + assert(ntohl(crc32c(data, 32)) == 0x43aba862L); + + /* 32 bytes of incrementing 00..1f */ + for (i = 0; i < 32; i++) data[i] = i; + assert(ntohl(crc32c(data, 32)) == 0x4e79dd46L); + + /* 32 bytes of decrementing 1f..00 */ + for (i = 0; i < 32; i++) data[i] = 31 - i; + assert(ntohl(crc32c(data, 32)) == 0x5cdb3f11L); + + mark('#'); +} + + +static void +test_csum_main(int argc OVS_UNUSED, char *argv[] OVS_UNUSED) { const struct test_case *tc; int i; for (tc = test_cases; tc < &test_cases[ARRAY_SIZE(test_cases)]; tc++) { - const uint16_t *data16 = (const uint16_t *) tc->data; - const uint32_t *data32 = (const uint32_t *) tc->data; + const void *data = tc->data; + const ovs_be16 *data16 = (OVS_FORCE const ovs_be16 *) data; + const ovs_be32 *data32 = (OVS_FORCE const ovs_be32 *) data; uint32_t partial; /* Test csum(). */ @@ -149,7 +196,7 @@ main(void) /* Test csum_add16(). */ partial = 0; for (i = 0; i < tc->size / 2; i++) { - partial = csum_add16(partial, data16[i]); + partial = csum_add16(partial, get_unaligned_be16(&data16[i])); } assert(ntohs(csum_finish(partial)) == tc->csum); mark('.'); @@ -157,7 +204,7 @@ main(void) /* Test csum_add32(). */ partial = 0; for (i = 0; i < tc->size / 4; i++) { - partial = csum_add32(partial, data32[i]); + partial = csum_add32(partial, get_unaligned_be32(&data32[i])); } assert(ntohs(csum_finish(partial)) == tc->csum); mark('.'); @@ -166,10 +213,12 @@ main(void) partial = 0; for (i = 0; i < tc->size / 4; i++) { if (i % 2) { - partial = csum_add32(partial, data32[i]); + partial = csum_add32(partial, get_unaligned_be32(&data32[i])); } else { - partial = csum_add16(partial, data16[i * 2]); - partial = csum_add16(partial, data16[i * 2 + 1]); + ovs_be16 u0 = get_unaligned_be16(&data16[i * 2]); + ovs_be16 u1 = get_unaligned_be16(&data16[i * 2 + 1]); + partial = csum_add16(partial, u0); + partial = csum_add16(partial, u1); } } assert(ntohs(csum_finish(partial)) == tc->csum); @@ -190,21 +239,22 @@ main(void) } test_rfc1624(); + test_crc32c(); /* Test recalc_csum16(). */ for (i = 0; i < 32; i++) { - uint16_t old_u16, new_u16; - uint16_t old_csum; - uint16_t data[16]; + ovs_be16 old_u16, new_u16; + ovs_be16 old_csum; + ovs_be16 data[16]; int j, index; for (j = 0; j < ARRAY_SIZE(data); j++) { - data[j] = random_uint32(); + data[j] = (OVS_FORCE ovs_be16) random_uint32(); } old_csum = csum(data, sizeof data); index = random_range(ARRAY_SIZE(data)); old_u16 = data[index]; - new_u16 = data[index] = random_uint32(); + new_u16 = data[index] = (OVS_FORCE ovs_be16) random_uint32(); assert(csum(data, sizeof data) == recalc_csum16(old_csum, old_u16, new_u16)); mark('.'); @@ -213,18 +263,18 @@ main(void) /* Test recalc_csum32(). */ for (i = 0; i < 32; i++) { - uint32_t old_u32, new_u32; - uint16_t old_csum; - uint32_t data[16]; + ovs_be32 old_u32, new_u32; + ovs_be16 old_csum; + ovs_be32 data[16]; int j, index; for (j = 0; j < ARRAY_SIZE(data); j++) { - data[j] = random_uint32(); + data[j] = (OVS_FORCE ovs_be32) random_uint32(); } old_csum = csum(data, sizeof data); index = random_range(ARRAY_SIZE(data)); old_u32 = data[index]; - new_u32 = data[index] = random_uint32(); + new_u32 = data[index] = (OVS_FORCE ovs_be32) random_uint32(); assert(csum(data, sizeof data) == recalc_csum32(old_csum, old_u32, new_u32)); mark('.'); @@ -232,6 +282,6 @@ main(void) mark('#'); putchar('\n'); - - return 0; } + +OVSTEST_REGISTER("test-csum", test_csum_main);