]> git.dujemihanovic.xyz Git - u-boot.git/commitdiff
test: dm: eth: Add string_to_ip6 test
authorViacheslav Mitrofanov <v.v.mitrofanov@yadro.com>
Fri, 2 Dec 2022 09:18:09 +0000 (12:18 +0300)
committerTom Rini <trini@konsulko.com>
Mon, 5 Dec 2022 17:47:17 +0000 (12:47 -0500)
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 <v.v.mitrofanov@yadro.com>
Reviewed-by: Ramon Fried <rfried.dev@gmail.com>
Reviewed-by: Simon Glass <sjg@chromium.org>
test/dm/eth.c

index 5437f9ea4a07492cbb79cbc1f38d7e6fca99b630..a3d4fc061bfa85b1a9712435dfc39b6ff009d42b 100644 (file)
@@ -13,6 +13,7 @@
 #include <log.h>
 #include <malloc.h>
 #include <net.h>
+#include <net6.h>
 #include <asm/eth.h>
 #include <dm/test.h>
 #include <dm/device-internal.h>
 
 #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");