From 184ded4becced88170ed7c3296c97e6b1a174896 Mon Sep 17 00:00:00 2001 From: Viacheslav Mitrofanov Date: Fri, 2 Dec 2022 12:18:09 +0300 Subject: test: dm: eth: Add string_to_ip6 test Add a test to check convertation from char* to struct in6_addr. Use in sandbox Series-changes: 3 - Fixed tests to use length param in string_to_ip6() Series-changes: 5 - Add test under #ifdef Signed-off-by: Viacheslav Mitrofanov Reviewed-by: Ramon Fried Reviewed-by: Simon Glass --- test/dm/eth.c | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 55 insertions(+) (limited to 'test') diff --git a/test/dm/eth.c b/test/dm/eth.c index 5437f9ea4a0..a3d4fc061bf 100644 --- a/test/dm/eth.c +++ b/test/dm/eth.c @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -22,6 +23,60 @@ #define DM_TEST_ETH_NUM 4 +#if IS_ENABLED(CONFIG_IPV6) +static int dm_test_string_to_ip6(struct unit_test_state *uts) +{ + char *str; + struct test_ip6_pair { + char *string_addr; + struct in6_addr ip6_addr; + }; + + struct in6_addr ip6 = {0}; + + /* Correct statements */ + struct test_ip6_pair test_suite[] = { + {"2001:db8::0:1234:1", {.s6_addr32[0] = 0xb80d0120, + .s6_addr32[1] = 0x00000000, + .s6_addr32[2] = 0x00000000, + .s6_addr32[3] = 0x01003412}}, + {"2001:0db8:0000:0000:0000:0000:1234:0001", + {.s6_addr32[0] = 0xb80d0120, + .s6_addr32[1] = 0x00000000, + .s6_addr32[2] = 0x00000000, + .s6_addr32[3] = 0x01003412}}, + {"::1", {.s6_addr32[0] = 0x00000000, + .s6_addr32[1] = 0x00000000, + .s6_addr32[2] = 0x00000000, + .s6_addr32[3] = 0x01000000}}, + {"::ffff:192.168.1.1", {.s6_addr32[0] = 0x00000000, + .s6_addr32[1] = 0x00000000, + .s6_addr32[2] = 0xffff0000, + .s6_addr32[3] = 0x0101a8c0}}, + }; + + for (int i = 0; i < ARRAY_SIZE(test_suite); ++i) { + ut_assertok(string_to_ip6(test_suite[i].string_addr, + strlen(test_suite[i].string_addr), &ip6)); + ut_asserteq_mem(&ip6, &test_suite[i].ip6_addr, + sizeof(struct in6_addr)); + } + + /* Incorrect statements */ + str = "hello:world"; + ut_assertok(!string_to_ip6(str, strlen(str), &ip6)); + str = "2001:db8::0::0"; + ut_assertok(!string_to_ip6(str, strlen(str), &ip6)); + str = "2001:db8:192.168.1.1::1"; + ut_assertok(!string_to_ip6(str, strlen(str), &ip6)); + str = "192.168.1.1"; + ut_assertok(!string_to_ip6(str, strlen(str), &ip6)); + + return 0; +} +DM_TEST(dm_test_string_to_ip6, 0); +#endif + static int dm_test_eth(struct unit_test_state *uts) { net_ping_ip = string_to_ip("1.1.2.2"); -- cgit v1.2.3 From d9f5c41e9ac64c255a88832488a425d6d4f275ae Mon Sep 17 00:00:00 2001 From: Viacheslav Mitrofanov Date: Fri, 2 Dec 2022 12:18:10 +0300 Subject: test: dm: eth: Add csum_ipv6_magic test Test checksum computation. csum_ipv6_magic() uses in upper layer protocols as TCP/UDP/ICMPv6/etc to calculate payload checksum. Series-changes: 3 - Fixed style problems Signed-off-by: Viacheslav Mitrofanov Reviewed-by: Ramon Fried Reviewed-by: Simon Glass --- test/dm/eth.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'test') diff --git a/test/dm/eth.c b/test/dm/eth.c index a3d4fc061bf..5ffa0c41d77 100644 --- a/test/dm/eth.c +++ b/test/dm/eth.c @@ -75,6 +75,35 @@ static int dm_test_string_to_ip6(struct unit_test_state *uts) return 0; } DM_TEST(dm_test_string_to_ip6, 0); + +static int dm_test_csum_ipv6_magic(struct unit_test_state *uts) +{ + unsigned short csum = 0xbeef; + /* Predefined correct parameters */ + unsigned short correct_csum = 0xd8ac; + struct in6_addr saddr = {.s6_addr32[0] = 0x000080fe, + .s6_addr32[1] = 0x00000000, + .s6_addr32[2] = 0xffe9f242, + .s6_addr32[3] = 0xe8f66dfe}; + struct in6_addr daddr = {.s6_addr32[0] = 0x000080fe, + .s6_addr32[1] = 0x00000000, + .s6_addr32[2] = 0xffd5b372, + .s6_addr32[3] = 0x3ef692fe}; + u16 len = 1460; + unsigned short proto = 17; + unsigned int head_csum = 0x91f0; + + csum = csum_ipv6_magic(&saddr, &daddr, len, proto, head_csum); + ut_asserteq(csum, correct_csum); + + /* Broke a parameter */ + proto--; + csum = csum_ipv6_magic(&saddr, &daddr, len, proto, head_csum); + ut_assert(csum != correct_csum); + + return 0; +} +DM_TEST(dm_test_csum_ipv6_magic, 0); #endif static int dm_test_eth(struct unit_test_state *uts) -- cgit v1.2.3 From 8576dcdf0036be116a3424505984a9b2de7c9bf8 Mon Sep 17 00:00:00 2001 From: Viacheslav Mitrofanov Date: Fri, 2 Dec 2022 12:18:11 +0300 Subject: test: dm: eth: Add ip6_addr_in_subnet test Add a test if two address are in the same subnet. Use in sandbox Signed-off-by: Viacheslav Mitrofanov Reviewed-by: Ramon Fried Reviewed-by: Simon Glass --- test/dm/eth.c | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'test') diff --git a/test/dm/eth.c b/test/dm/eth.c index 5ffa0c41d77..4c502582889 100644 --- a/test/dm/eth.c +++ b/test/dm/eth.c @@ -104,6 +104,31 @@ static int dm_test_csum_ipv6_magic(struct unit_test_state *uts) return 0; } DM_TEST(dm_test_csum_ipv6_magic, 0); + +static int dm_test_ip6_addr_in_subnet(struct unit_test_state *uts) +{ + struct in6_addr our = {.s6_addr32[0] = 0x000080fe, + .s6_addr32[1] = 0x00000000, + .s6_addr32[2] = 0xffe9f242, + .s6_addr32[3] = 0xe8f66dfe}; + struct in6_addr neigh1 = {.s6_addr32[0] = 0x000080fe, + .s6_addr32[1] = 0x00000000, + .s6_addr32[2] = 0xffd5b372, + .s6_addr32[3] = 0x3ef692fe}; + struct in6_addr neigh2 = {.s6_addr32[0] = 0x60480120, + .s6_addr32[1] = 0x00006048, + .s6_addr32[2] = 0x00000000, + .s6_addr32[3] = 0x00008888}; + + /* in */ + ut_assert(ip6_addr_in_subnet(&our, &neigh1, 64)); + /* outside */ + ut_assert(!ip6_addr_in_subnet(&our, &neigh2, 64)); + ut_assert(!ip6_addr_in_subnet(&our, &neigh1, 128)); + + return 0; +} +DM_TEST(dm_test_ip6_addr_in_subnet, 0); #endif static int dm_test_eth(struct unit_test_state *uts) -- cgit v1.2.3 From 789a2c7d37ed81e369a570afd939fcd8f7729d31 Mon Sep 17 00:00:00 2001 From: Viacheslav Mitrofanov Date: Fri, 2 Dec 2022 12:18:12 +0300 Subject: test: dm: eth: Add ip6_make_snma test Add a test that checks generated Solicited Node Multicast Address from our ipv6 address. Use in sandbox Signed-off-by: Viacheslav Mitrofanov Reviewed-by: Ramon Fried Reviewed-by: Simon Glass --- test/dm/eth.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'test') diff --git a/test/dm/eth.c b/test/dm/eth.c index 4c502582889..a3ee231e20f 100644 --- a/test/dm/eth.c +++ b/test/dm/eth.c @@ -129,6 +129,26 @@ static int dm_test_ip6_addr_in_subnet(struct unit_test_state *uts) return 0; } DM_TEST(dm_test_ip6_addr_in_subnet, 0); + +static int dm_test_ip6_make_snma(struct unit_test_state *uts) +{ + struct in6_addr mult = {0}; + struct in6_addr correct_addr = { + .s6_addr32[0] = 0x000002ff, + .s6_addr32[1] = 0x00000000, + .s6_addr32[2] = 0x01000000, + .s6_addr32[3] = 0xe8f66dff}; + struct in6_addr addr = { .s6_addr32[0] = 0x000080fe, + .s6_addr32[1] = 0x00000000, + .s6_addr32[2] = 0xffe9f242, + .s6_addr32[3] = 0xe8f66dfe}; + + ip6_make_snma(&mult, &addr); + ut_asserteq_mem(&mult, &correct_addr, sizeof(struct in6_addr)); + + return 0; +} +DM_TEST(dm_test_ip6_make_snma, 0); #endif static int dm_test_eth(struct unit_test_state *uts) -- cgit v1.2.3 From e4d30fd110116e503c215c9327fc4332caa4789d Mon Sep 17 00:00:00 2001 From: Viacheslav Mitrofanov Date: Fri, 2 Dec 2022 12:18:13 +0300 Subject: test: dm: eth: Add ip6_make_lladdr test Add a test that checks generated Link Local Address. Use in sandbox Signed-off-by: Viacheslav Mitrofanov Reviewed-by: Ramon Fried Reviewed-by: Simon Glass --- test/dm/eth.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'test') diff --git a/test/dm/eth.c b/test/dm/eth.c index a3ee231e20f..ebf01d8cf38 100644 --- a/test/dm/eth.c +++ b/test/dm/eth.c @@ -149,6 +149,24 @@ static int dm_test_ip6_make_snma(struct unit_test_state *uts) return 0; } DM_TEST(dm_test_ip6_make_snma, 0); + +static int dm_test_ip6_make_lladdr(struct unit_test_state *uts) +{ + struct in6_addr generated_lladdr = {0}; + struct in6_addr correct_lladdr = { + .s6_addr32[0] = 0x000080fe, + .s6_addr32[1] = 0x00000000, + .s6_addr32[2] = 0xffabf33a, + .s6_addr32[3] = 0xfbb352fe}; + const unsigned char mac[6] = {0x38, 0xf3, 0xab, 0x52, 0xb3, 0xfb}; + + ip6_make_lladdr(&generated_lladdr, mac); + ut_asserteq_mem(&generated_lladdr, &correct_lladdr, + sizeof(struct in6_addr)); + + return 0; +} +DM_TEST(dm_test_ip6_make_lladdr, UT_TESTF_SCAN_FDT); #endif static int dm_test_eth(struct unit_test_state *uts) -- cgit v1.2.3