|
| 1 | +#include "mbed.h" |
| 2 | +#include "greentea-client/test_env.h" |
| 3 | +#include "unity.h" |
| 4 | +#include "utest.h" |
| 5 | + |
| 6 | +using namespace utest::v1; |
| 7 | + |
| 8 | + |
| 9 | +// IP parsing verification |
| 10 | +void test_ip_accept(const char *string, nsapi_addr_t addr) { |
| 11 | + SocketAddress address; |
| 12 | + TEST_ASSERT(address.set_ip_address(string)); |
| 13 | + TEST_ASSERT(address == SocketAddress(addr)); |
| 14 | +} |
| 15 | + |
| 16 | +template <const char *string> |
| 17 | +void test_ip_reject() { |
| 18 | + SocketAddress address; |
| 19 | + TEST_ASSERT(!address.set_ip_address(string)); |
| 20 | + TEST_ASSERT(!address); |
| 21 | +} |
| 22 | + |
| 23 | +#define TEST_IP_ACCEPT(name, string, ...) \ |
| 24 | +void name() { \ |
| 25 | + nsapi_addr_t addr = __VA_ARGS__; \ |
| 26 | + test_ip_accept(string, addr); \ |
| 27 | +} |
| 28 | + |
| 29 | +#define TEST_IP_REJECT(name, string) \ |
| 30 | +void name() { \ |
| 31 | + test_ip_reject(string); \ |
| 32 | +} |
| 33 | + |
| 34 | + |
| 35 | +// Test cases |
| 36 | +TEST_IP_ACCEPT(test_simple_ipv4_address, |
| 37 | + "12.34.56.78", |
| 38 | + {NSAPI_IPv4,{12,34,56,78}}) |
| 39 | +TEST_IP_ACCEPT(test_left_weighted_ipv4_address, |
| 40 | + "255.0.0.0", |
| 41 | + {NSAPI_IPv4,{255,0,0,0}}) |
| 42 | +TEST_IP_ACCEPT(test_right_weighted_ipv4_address, |
| 43 | + "0.0.0.255", |
| 44 | + {NSAPI_IPv4,{0,0,0,255}}) |
| 45 | +TEST_IP_ACCEPT(test_null_ipv4_address, |
| 46 | + "0.0.0.0", |
| 47 | + {NSAPI_IPv4,{0,0,0,0}}) |
| 48 | + |
| 49 | +TEST_IP_ACCEPT(test_simple_ipv6_address, |
| 50 | + "1234:5678:9abc:def0:1234:5678:9abc:def0", |
| 51 | + {NSAPI_IPv6,{0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0, |
| 52 | + 0x12,0x34,0x56,0x78,0x9a,0xbc,0xde,0xf0}}) |
| 53 | +TEST_IP_ACCEPT(test_left_weighted_ipv6_address, |
| 54 | + "1234:5678::", |
| 55 | + {NSAPI_IPv6,{0x12,0x34,0x56,0x78,0x00,0x00,0x00,0x00, |
| 56 | + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}) |
| 57 | +TEST_IP_ACCEPT(test_right_weighted_ipv6_address, |
| 58 | + "::1234:5678", |
| 59 | + {NSAPI_IPv6,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 60 | + 0x00,0x00,0x00,0x00,0x12,0x34,0x56,0x78}}) |
| 61 | +TEST_IP_ACCEPT(test_hollowed_ipv6_address, |
| 62 | + "1234:5678::9abc:def8", |
| 63 | + {NSAPI_IPv6,{0x12,0x34,0x56,0x78,0x00,0x00,0x00,0x00, |
| 64 | + 0x00,0x00,0x00,0x00,0x9a,0xbc,0xde,0xf8}}) |
| 65 | +TEST_IP_ACCEPT(test_null_ipv6_address, |
| 66 | + "::", |
| 67 | + {NSAPI_IPv6,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, |
| 68 | + 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}) |
| 69 | + |
| 70 | + |
| 71 | +// Test setup |
| 72 | +utest::v1::status_t test_setup(const size_t number_of_cases) { |
| 73 | + GREENTEA_SETUP(10, "default_auto"); |
| 74 | + return verbose_test_setup_handler(number_of_cases); |
| 75 | +} |
| 76 | + |
| 77 | +Case cases[] = { |
| 78 | + Case("Simple IPv4 address", test_simple_ipv4_address), |
| 79 | + Case("Left-weighted IPv4 address", test_left_weighted_ipv4_address), |
| 80 | + Case("Right-weighted IPv4 address", test_right_weighted_ipv4_address), |
| 81 | + Case("Null IPv4 address", test_null_ipv4_address), |
| 82 | + |
| 83 | + Case("Simple IPv6 address", test_simple_ipv6_address), |
| 84 | + Case("Left-weighted IPv6 address", test_left_weighted_ipv6_address), |
| 85 | + Case("Right-weighted IPv6 address", test_right_weighted_ipv6_address), |
| 86 | + Case("Hollowed IPv6 address", test_hollowed_ipv6_address), |
| 87 | + Case("Null IPv6 address", test_null_ipv6_address), |
| 88 | +}; |
| 89 | + |
| 90 | +Specification specification(test_setup, cases); |
| 91 | + |
| 92 | +int main() { |
| 93 | + return !Harness::run(specification); |
| 94 | +} |
0 commit comments