]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: split out {dump,verify}_ra_message() 31440/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sun, 18 Feb 2024 17:21:31 +0000 (02:21 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 24 Feb 2024 03:37:50 +0000 (12:37 +0900)
Then, let's not modify the global object.

src/libsystemd-network/test-ndisc-ra.c

index 442b139f9b2a63cfc51e5c6de4d92f8606fdc293..c5f10cc7955e552d26af75f8d3251bfa5745c1c8 100644 (file)
@@ -20,37 +20,6 @@ static struct ether_addr mac_addr = {
         .ether_addr_octet = { 0x78, 0x2b, 0xcb, 0xb3, 0x6d, 0x53 }
 };
 
-static uint8_t advertisement[] = {
-        /* ICMPv6 Router Advertisement, no checksum */
-        0x86, 0x00, 0x00, 0x00,  0x40, 0xc0, 0x00, 0xb4,
-        0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
-        /* Source Link Layer Address Option */
-        0x01, 0x01, 0x78, 0x2b,  0xcb, 0xb3, 0x6d, 0x53,
-        /* Prefix Information Option */
-        0x03, 0x04, 0x40, 0xc0,  0x00, 0x00, 0x01, 0xf4,
-        0x00, 0x00, 0x01, 0xb8,  0x00, 0x00, 0x00, 0x00,
-        0x20, 0x01, 0x0d, 0xb8,  0xde, 0xad, 0xbe, 0xef,
-        0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
-        /* Prefix Information Option */
-        0x03, 0x04, 0x40, 0xc0,  0x00, 0x00, 0x0e, 0x10,
-        0x00, 0x00, 0x07, 0x08,  0x00, 0x00, 0x00, 0x00,
-        0x20, 0x01, 0x0d, 0xb8,  0x0b, 0x16, 0xd0, 0x0d,
-        0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
-        /* Prefix Information Option */
-        0x03, 0x04, 0x30, 0xc0,  0x00, 0x00, 0x0e, 0x10,
-        0x00, 0x00, 0x07, 0x08,  0x00, 0x00, 0x00, 0x00,
-        0x20, 0x01, 0x0d, 0xb8,  0xc0, 0x01, 0x0d, 0xad,
-        0x00, 0x00, 0x00, 0x00,  0x00, 0x00, 0x00, 0x00,
-        /* Recursive DNS Server Option */
-        0x19, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c,
-        0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef,
-        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
-        /* DNS Search List Option */
-        0x1f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c,
-        0x03, 0x6c, 0x61, 0x62, 0x05, 0x69, 0x6e, 0x74,
-        0x72, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
-};
-
 static bool test_stopped;
 static struct {
         struct in6_addr address;
@@ -271,49 +240,91 @@ TEST(radv) {
         assert_se(!ra);
 }
 
-static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
-        sd_radv *ra = userdata;
-        unsigned char buf[168];
-        size_t i;
-
-        assert_se(read(test_fd[0], &buf, sizeof(buf)) == sizeof(buf));
+static void dump_message(const uint8_t *buf, size_t len) {
+        assert(len >= sizeof(struct nd_router_advert));
 
-        /* router lifetime must be zero when test is stopped */
-        if (test_stopped) {
-                advertisement[6] = 0x00;
-                advertisement[7] = 0x00;
-        }
+        printf("Received Router Advertisement with lifetime %i sec\n",
+               (buf[6] << 8) + buf[7]);
 
-        printf ("Received Router Advertisement with lifetime %i\n",
-                (advertisement[6] << 8) + advertisement[7]);
-
-        /* test only up to buf size, rest is not yet implemented */
-        for (i = 0; i < sizeof(buf); i++) {
+        for (size_t i = 0; i < len; i++) {
                 if (!(i % 8))
                         printf("%3zu: ", i);
 
                 printf("0x%02x", buf[i]);
 
-                assert_se(buf[i] == advertisement[i]);
-
                 if ((i + 1) % 8)
                         printf(", ");
                 else
                         printf("\n");
         }
+}
 
-        if (test_stopped) {
-                sd_event *e;
+static void verify_message(const uint8_t *buf, size_t len) {
+        static const uint8_t advertisement[] = {
+                /* ICMPv6 Router Advertisement, no checksum */
+                0x86, 0x00, 0x00, 0x00, 0x40, 0xc0, 0x00, 0xb4,
+                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                /* Source Link Layer Address Option */
+                0x01, 0x01, 0x78, 0x2b, 0xcb, 0xb3, 0x6d, 0x53,
+                /* Prefix Information Option */
+                0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x01, 0xf4,
+                0x00, 0x00, 0x01, 0xb8, 0x00, 0x00, 0x00, 0x00,
+                0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef,
+                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                /* Prefix Information Option */
+                0x03, 0x04, 0x40, 0xc0, 0x00, 0x00, 0x0e, 0x10,
+                0x00, 0x00, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00,
+                0x20, 0x01, 0x0d, 0xb8, 0x0b, 0x16, 0xd0, 0x0d,
+                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                /* Prefix Information Option */
+                0x03, 0x04, 0x30, 0xc0, 0x00, 0x00, 0x0e, 0x10,
+                0x00, 0x00, 0x07, 0x08, 0x00, 0x00, 0x00, 0x00,
+                0x20, 0x01, 0x0d, 0xb8, 0xc0, 0x01, 0x0d, 0xad,
+                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+                /* Recursive DNS Server Option */
+                0x19, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c,
+                0x20, 0x01, 0x0d, 0xb8, 0xde, 0xad, 0xbe, 0xef,
+                0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
+                /* DNS Search List Option */
+                0x1f, 0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3c,
+                0x03, 0x6c, 0x61, 0x62, 0x05, 0x69, 0x6e, 0x74,
+                0x72, 0x61, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        };
+
+        /* verify only up to known options, rest is not yet implemented */
+        for (size_t i = 0, m = MIN(len, sizeof(advertisement)); i < m; i++) {
+                if (test_stopped)
+                        switch (i) {
+                        case 6 ... 7: /* router lifetime must be zero on stop. */
+                                assert_se(buf[i] == 0);
+                                continue;
+                        }
 
-                e = sd_radv_get_event(ra);
-                sd_event_exit(e, 0);
+                assert_se(buf[i] == advertisement[i]);
+        }
+}
+
+static int radv_recv(sd_event_source *s, int fd, uint32_t revents, void *userdata) {
+        sd_radv *ra = ASSERT_PTR(userdata);
+        _cleanup_free_ uint8_t *buf = NULL;
+        ssize_t buflen;
 
+        buflen = next_datagram_size_fd(fd);
+        assert_se(buflen >= 0);
+        assert_se(buf = new0(uint8_t, buflen));
+
+        assert_se(read(fd, buf, buflen) == buflen);
+
+        dump_message(buf, buflen);
+        verify_message(buf, buflen);
+
+        if (test_stopped) {
+                assert_se(sd_event_exit(sd_radv_get_event(ra), 0) >= 0);
                 return 0;
         }
 
         assert_se(sd_radv_stop(ra) >= 0);
         test_stopped = true;
-
         return 0;
 }