From 3e91230d8746564c83c2f3c0f61b8dfd7b523eee Mon Sep 17 00:00:00 2001
From: Neal Cardwell <ncardwell@google.com>
Date: Fri, 22 Nov 2013 21:13:58 -0500
Subject: [PATCH] net-test: packetdrill MPLS support: test for printing MPLS
 label stacks

Test code to print on-the-wire MPLS label stacks in a human-readable
format matching the script syntax.

Change-Id: I7e5d0d8d07490fee1fb744bbd03804bb12e22ae4
---
 .../net/packetdrill/packet_to_string_test.c   | 53 +++++++++++++++++++
 1 file changed, 53 insertions(+)

diff --git a/gtests/net/packetdrill/packet_to_string_test.c b/gtests/net/packetdrill/packet_to_string_test.c
index 7bd2bd16..fc3b21c7 100644
--- a/gtests/net/packetdrill/packet_to_string_test.c
+++ b/gtests/net/packetdrill/packet_to_string_test.c
@@ -196,9 +196,62 @@ static void test_tcp_ipv6_packet_to_string(void)
 	packet_free(packet);
 }
 
+static void test_gre_mpls_tcp_ipv4_packet_to_string(void)
+{
+	/* An IPv4/GRE/MPLS/IPv4/TCP packet. */
+	u8 data[] = {
+		/* IPv4: */
+		0x45, 0x00, 0x00, 0x54, 0x00, 0x00, 0x40, 0x00,
+		0x40, 0x2f, 0xb7, 0xcf, 0xc0, 0xa8, 0x00, 0x01,
+		0xc0, 0x00, 0x02, 0x02,
+		/* GRE: */
+		0x00, 0x00, 0x88, 0x47,
+		/* MPLS: */
+		0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff,
+		/* IPv4, TCP: */
+		0x45, 0x00, 0x00, 0x34, 0x86, 0x99, 0x40, 0x00,
+		0x40, 0x06, 0x31, 0x80, 0xc0, 0xa8, 0x00, 0x01,
+		0xc0, 0x00, 0x02, 0x01, 0x1f, 0x90, 0xdb, 0xcc,
+		0x7b, 0x81, 0xc5, 0x7c, 0x00, 0x00, 0x00, 0x01,
+		0x80, 0x11, 0x01, 0xc5, 0xa6, 0xa6, 0x00, 0x00,
+		0x01, 0x01, 0x08, 0x0a, 0x07, 0x02, 0x08, 0x43,
+		0x00, 0x00, 0x00, 0x05
+	};
+
+	struct packet *packet = packet_new(sizeof(data));
+
+	/* Populate and parse a packet */
+	memcpy(packet->buffer, data, sizeof(data));
+	char *error = NULL;
+	enum packet_parse_result_t result =
+		parse_packet(packet, sizeof(data), PACKET_LAYER_3_IP,
+				     &error);
+	assert(result == PACKET_OK);
+	assert(error == NULL);
+
+	int status = 0;
+	char *dump = NULL, *expected = NULL;
+
+	/* Test a DUMP_FULL dump */
+	status = packet_to_string(packet, DUMP_FULL, &dump, &error);
+	assert(status == STATUS_OK);
+	assert(error == NULL);
+	printf("dump = '%s'\n", dump);
+	expected =
+		"ipv4 192.168.0.1 > 192.0.2.2: gre: "
+		"mpls (label 0, tc 0, ttl 0) "
+		"(label 1048575, tc 7, [S], ttl 255): "
+		"192.168.0.1:8080 > 192.0.2.1:56268 "
+		"F. 2072102268:2072102268(0) ack 1 win 453 "
+		"<nop,nop,TS val 117573699 ecr 5>";
+	assert(strcmp(dump, expected) == 0);
+	free(dump);
+}
+
 int main(void)
 {
 	test_tcp_ipv4_packet_to_string();
 	test_tcp_ipv6_packet_to_string();
+	test_gre_mpls_tcp_ipv4_packet_to_string();
 	return 0;
 }
-- 
GitLab