From 655aa0404ce747af823be5cfc01f357ba7fa18cf Mon Sep 17 00:00:00 2001
From: Martine Lenders <mlenders@inf.fu-berlin.de>
Date: Fri, 22 Apr 2016 10:09:13 +0200
Subject: [PATCH] tests: rework IPv6 extension header tests for changes

---
 tests/gnrc_ipv6_ext/Makefile        |   5 +-
 tests/gnrc_ipv6_ext/main.c          | 182 +++++++++++++++++++++++-----
 tests/gnrc_ipv6_ext/tests/01-run.py |  11 +-
 3 files changed, 163 insertions(+), 35 deletions(-)

diff --git a/tests/gnrc_ipv6_ext/Makefile b/tests/gnrc_ipv6_ext/Makefile
index ffa542c1af..9b2846712e 100644
--- a/tests/gnrc_ipv6_ext/Makefile
+++ b/tests/gnrc_ipv6_ext/Makefile
@@ -21,9 +21,10 @@ USEMODULE += gnrc_ipv6_router_default
 # IPv6 extension headers
 USEMODULE += gnrc_ipv6_ext
 USEMODULE += gnrc_rpl_srh
+USEMODULE += gnrc_sixlowpan_iphc_nhc
+# UDP
+USEMODULE += gnrc_udp
 # Add also the shell, some shell commands
-USEMODULE += shell
-USEMODULE += shell_commands
 USEMODULE += ps
 
 # Comment this out to disable code in RIOT that does safety checking
diff --git a/tests/gnrc_ipv6_ext/main.c b/tests/gnrc_ipv6_ext/main.c
index ddf0c939ae..b34993f71d 100644
--- a/tests/gnrc_ipv6_ext/main.c
+++ b/tests/gnrc_ipv6_ext/main.c
@@ -30,11 +30,10 @@
 #include "net/gnrc/netreg.h"
 #include "net/gnrc/netapi.h"
 #include "net/gnrc/netif.h"
+#include "net/gnrc/netif/hdr.h"
 
-#define MAIN_QUEUE_SIZE     (8)
-static msg_t _main_msg_queue[MAIN_QUEUE_SIZE];
-
-static void _send_packet(void) {
+static void _init_interface(void)
+{
     kernel_pid_t ifs[GNRC_NETIF_NUMOF];
     ipv6_addr_t addr = IPV6_ADDR_UNSPECIFIED;
 
@@ -49,13 +48,102 @@ static void _send_packet(void) {
     addr.u8[15] = 0x03;
     /* fd01::03 */
     gnrc_ipv6_netif_add_addr(ifs[0], &addr, 64, GNRC_IPV6_NETIF_ADDR_FLAGS_UNICAST);
+}
+
+static void _send_packet_raw(void)
+{
+    kernel_pid_t ifs[GNRC_NETIF_NUMOF];
+
+    gnrc_netif_get(ifs);
+
+    gnrc_netif_hdr_t netif_hdr;
+
+    gnrc_netif_hdr_init(&netif_hdr, 8, 8);
+
+    netif_hdr.if_pid = ifs[0];
 
     uint8_t data[] = {
         /* IPv6 Header */
         0x60, 0x00, 0x00, 0x00, /* version, traffic class, flow label */
-        0x00, 0x28, /* payload length: 40 */
-        0x00, /* next header: Hop-by-Hop Option */
-        0x10, /* hop limit: 16 */
+        0x00, 0x2a,             /* payload length: 42 */
+        0x00,                   /* next header: Hop-by-Hop Option */
+        0x10,                   /* hop limit: 16 */
+        /* source address: fd01::1 */
+        0xfd, 0x01, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x01,
+        /* destination address: fd01::2 */
+        0xfd, 0x01, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x02,
+
+        /* Hop-by-Hop Options Header */
+        /* https://tools.ietf.org/html/rfc6553 */
+        0x2b,       /* next header: IPv6-Route */
+        0x00,       /* hdr ext len: 0 * 8 + 8 = 8 */
+        0x63,       /* option type: RPL Option */
+        0x04,       /* opt data len: 4 */
+        0x80,       /* flags, Down: 1, Rank-Error: 0, Forwarding-Error: 0 */
+        0x00,       /* RPLInstanceID */
+        0x80, 0x00, /* SenderRank */
+
+        /* RPL Routing Header */
+        /* https://tools.ietf.org/html/rfc6554 */
+        0x11,               /* next header: UDP */
+        0x02,               /* hdr ext len: 2 * 8 + 8 = 24 */
+        0x03,               /* routing type: SRH */
+        0x02,               /* segments left: 2 */
+        0xef,               /* ComprI: 14, ComprE: 15 */
+        0xd0, 0x00, 0x00,   /* pad and reserved */
+        /* address: fd01::3, fd01::2 */
+        0x00, 0x03, 0x02, 0x00,
+        0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00,
+
+        /* UDP (ignored) */
+        0x1f, 0x90, /* source port: 8080 */
+        0x1f, 0x90, /* destination port: 8080 */
+        0x00, 0x0a, /* length: 10 */
+        0xff, 0xff, /* checksum */
+        0x00, 0x00, /* payload */
+    };
+
+    gnrc_pktsnip_t *netif = gnrc_pktbuf_add(NULL,
+                                            &netif_hdr,
+                                            sizeof(netif_hdr),
+                                            GNRC_NETTYPE_NETIF);
+    gnrc_pktsnip_t *pkt = gnrc_pktbuf_add(netif,
+                                          data,
+                                          sizeof(data),
+                                          GNRC_NETTYPE_UNDEF);
+
+    gnrc_netapi_dispatch_receive(GNRC_NETTYPE_IPV6, GNRC_NETREG_DEMUX_CTX_ALL, pkt);
+
+    printf("pkt->users: %d\n", pkt->users);
+    assert(pkt->users == 0);
+}
+
+static void _send_packet_parsed(void)
+{
+    kernel_pid_t ifs[GNRC_NETIF_NUMOF];
+
+    gnrc_netif_get(ifs);
+
+    gnrc_netif_hdr_t netif_hdr;
+
+    gnrc_netif_hdr_init(&netif_hdr, 8, 8);
+
+    netif_hdr.if_pid = ifs[0];
+
+    uint8_t ipv6_data[] = {
+        /* IPv6 Header */
+        0x60, 0x00, 0x00, 0x00, /* version, traffic class, flow label */
+        0x00, 0x2a,             /* payload length: 42 */
+        0x00,                   /* next header: Hop-by-Hop Option */
+        0x10,                   /* hop limit: 16 */
         /* source address: fd01::1 */
         0xfd, 0x01, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00,
@@ -66,39 +154,78 @@ static void _send_packet(void) {
         0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x02,
+    };
 
+    uint8_t hop_by_hop_options_data[] = {
         /* Hop-by-Hop Options Header */
         /* https://tools.ietf.org/html/rfc6553 */
-        0x2b, /* next header: IPv6-Route */
-        0x00, /* hdr ext len: 0 * 8 + 8 = 8 */
-        0x63, /* option type: RPL Option */
-        0x04, /* opt data len: 4 */
-        0x80, /* flags, Down: 1, Rank-Error: 0, Forwarding-Error: 0 */
-        0x00, /* RPLInstanceID */
+        0x2b,       /* next header: IPv6-Route */
+        0x00,       /* hdr ext len: 0 * 8 + 8 = 8 */
+        0x63,       /* option type: RPL Option */
+        0x04,       /* opt data len: 4 */
+        0x80,       /* flags, Down: 1, Rank-Error: 0, Forwarding-Error: 0 */
+        0x00,       /* RPLInstanceID */
         0x80, 0x00, /* SenderRank */
+    };
 
+    uint8_t rpl_routing_data[] = {
         /* RPL Routing Header */
         /* https://tools.ietf.org/html/rfc6554 */
-        0x11, /* next header: UDP */
-        0x02, /* hdr ext len: 2 * 8 + 8 = 24 */
-        0x03, /* routing type: SRH */
-        0x02, /* segments left: 2 */
-        0xef, /* ComprI: 14, ComprE: 15 */
-        0xd0, 0x00, 0x00, /* pad and reserved */
+        0x11,               /* next header: UDP */
+        0x02,               /* hdr ext len: 2 * 8 + 8 = 24 */
+        0x03,               /* routing type: SRH */
+        0x02,               /* segments left: 2 */
+        0xef,               /* ComprI: 14, ComprE: 15 */
+        0xd0, 0x00, 0x00,   /* pad and reserved */
         /* address: fd01::3, fd01::2 */
         0x00, 0x03, 0x02, 0x00,
         0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00,
         0x00, 0x00, 0x00, 0x00,
+    };
 
+    uint8_t udp_data[] = {
         /* UDP (ignored) */
         0x1f, 0x90, /* source port: 8080 */
         0x1f, 0x90, /* destination port: 8080 */
-        0x00, 0x08, /* length: 8 */
+        0x00, 0x0a, /* length: 10 */
         0xff, 0xff, /* checksum */
     };
 
-    gnrc_pktsnip_t *pkt = gnrc_pktbuf_add(NULL, data, sizeof(data), GNRC_NETTYPE_UNDEF);
+    uint8_t udp_payload[] = {
+        0x00, 0x00,
+    };
+
+    gnrc_pktsnip_t *netif =
+        gnrc_pktbuf_add(NULL,
+                        &netif_hdr,
+                        sizeof(netif_hdr),
+                        GNRC_NETTYPE_NETIF);
+    gnrc_pktsnip_t *ipv6 =
+        gnrc_pktbuf_add(netif,
+                        &ipv6_data,
+                        sizeof(ipv6_data),
+                        GNRC_NETTYPE_IPV6);
+    gnrc_pktsnip_t *hop_by_hop_options =
+        gnrc_pktbuf_add(ipv6,
+                        &hop_by_hop_options_data,
+                        sizeof(hop_by_hop_options_data),
+                        GNRC_NETTYPE_IPV6_EXT);
+    gnrc_pktsnip_t *rpl_routing =
+        gnrc_pktbuf_add(hop_by_hop_options,
+                        &rpl_routing_data,
+                        sizeof(rpl_routing_data),
+                        GNRC_NETTYPE_IPV6_EXT);
+    gnrc_pktsnip_t *udp =
+        gnrc_pktbuf_add(rpl_routing,
+                        udp_data,
+                        sizeof(udp_data),
+                        GNRC_NETTYPE_UDP);
+    gnrc_pktsnip_t *pkt =
+        gnrc_pktbuf_add(udp,
+                        &udp_payload,
+                        sizeof(udp_payload),
+                        GNRC_NETTYPE_UNDEF);
 
     gnrc_netapi_dispatch_receive(GNRC_NETTYPE_IPV6, GNRC_NETREG_DEMUX_CTX_ALL, pkt);
 
@@ -106,19 +233,14 @@ static void _send_packet(void) {
     assert(pkt->users == 0);
 }
 
+
 int main(void)
 {
-    /* we need a message queue for the thread running the shell in order to
-     * receive potentially fast incoming networking packets */
-    msg_init_queue(_main_msg_queue, MAIN_QUEUE_SIZE);
     puts("RIOT network stack example application");
 
-    _send_packet();
-
-    /* start shell */
-    puts("All up, running the shell now");
-    char line_buf[SHELL_DEFAULT_BUFSIZE];
-    shell_run(NULL, line_buf, SHELL_DEFAULT_BUFSIZE);
+    _init_interface();
+    _send_packet_raw();
+    _send_packet_parsed();
 
     /* should be never reached */
     return 0;
diff --git a/tests/gnrc_ipv6_ext/tests/01-run.py b/tests/gnrc_ipv6_ext/tests/01-run.py
index 0e50cb66dd..35b3f31a35 100755
--- a/tests/gnrc_ipv6_ext/tests/01-run.py
+++ b/tests/gnrc_ipv6_ext/tests/01-run.py
@@ -21,16 +21,21 @@ def testfunc(child):
 
     if index == 1:
         # debug is disabled
+        child.expect_exact("pkt->users: 0")
         return
 
     child.expect_exact("ipv6: handle extension header (nh = 0)")
-    child.expect_exact("ipv6: handle extension header (nh = 43)")
     child.expect_exact("ipv6: Received (src = fd01::1, dst = fd01::3, next header = 0, length = 40)")
     child.expect_exact("ipv6: handle extension header (nh = 0)")
-    child.expect_exact("ipv6: handle extension header (nh = 43)")
     child.expect_exact("ipv6: Received (src = fd01::1, dst = fd01::2, next header = 0, length = 40)")
     child.expect_exact("ipv6: handle extension header (nh = 0)")
-    child.expect_exact("ipv6: handle extension header (nh = 43)")
+    child.expect_exact("ipv6: forward nh = 17 to other threads")
+    child.expect_exact("pkt->users: 0")
+    child.expect_exact("ipv6: handle extension header (nh = 0)")
+    child.expect_exact("ipv6: Received (src = fd01::1, dst = fd01::3, next header = 0, length = 40)")
+    child.expect_exact("ipv6: handle extension header (nh = 0)")
+    child.expect_exact("ipv6: Received (src = fd01::1, dst = fd01::2, next header = 0, length = 40)")
+    child.expect_exact("ipv6: handle extension header (nh = 0)")
     child.expect_exact("ipv6: forward nh = 17 to other threads")
     child.expect_exact("pkt->users: 0")
 
-- 
GitLab