Signed-off-by: Ru Jia <[email protected]>
---
 helper/test/Makefile.am     |   4 +-
 helper/test/iplookuptable.c | 150 ++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 153 insertions(+), 1 deletion(-)
 create mode 100644 helper/test/iplookuptable.c

diff --git a/helper/test/Makefile.am b/helper/test/Makefile.am
index 7f0b67d..6ecb235 100644
--- a/helper/test/Makefile.am
+++ b/helper/test/Makefile.am
@@ -9,7 +9,8 @@ EXECUTABLES = chksum$(EXEEXT) \
               thread$(EXEEXT) \
               parse$(EXEEXT)\
               process$(EXEEXT)\
-              table$(EXEEXT)
+              table$(EXEEXT)\
+                         iplookuptable$(EXEEXT)
 
 COMPILE_ONLY =
 
@@ -31,3 +32,4 @@ dist_process_SOURCES = process.c
 dist_parse_SOURCES = parse.c
 process_LDADD = $(LIB)/libodphelper-linux.la $(LIB)/libodp-linux.la
 dist_table_SOURCES = table.c
+dist_iplookuptable_SOURCES = iplookuptable.c
diff --git a/helper/test/iplookuptable.c b/helper/test/iplookuptable.c
new file mode 100644
index 0000000..59574a4
--- /dev/null
+++ b/helper/test/iplookuptable.c
@@ -0,0 +1,150 @@
+/* Copyright (c) 2015, Linaro Limited
+ * All rights reserved.
+ *
+ * SPDX-License-Identifier:     BSD-3-Clause
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <string.h>
+#include <stdlib.h>
+#include <errno.h>
+
+#include <odp_api.h>
+#include <test_debug.h>
+#include <../odph_iplookuptable.h>
+#include <odp/helper/ip.h>
+
+/*
+ * Check condition and return an error if true. Assumes that "table" is the
+ * name of the hash structure pointer to be freed.
+ */
+#define RETURN_IF_ERROR(cond, str, ...) do {                           \
+       if (cond) {                                                     \
+               printf("ERROR line %d: " str "\n", __LINE__, ##__VA_ARGS__); \
+               if (table) \
+                       odph_iplookup_table_destroy(table);             \
+               return -1;                                              \
+       }                                                               \
+} while (0)
+
+static void print_prefix_info(
+               const char *msg, uint32_t ip, uint8_t cidr)
+{
+       int i = 0;
+       uint8_t *ptr = (uint8_t *)(&ip);
+
+       printf("%s IP prefix: ", msg);
+       for (i = 3; i >= 0; i--) {
+               if (i != 3)
+                       printf(".");
+               printf("%d", ptr[i]);
+       }
+       printf("/%d\n", cidr);
+}
+
+/*
+ * Basic sequence of operations for a single key:
+ *     - put short prefix
+ *     - put long prefix
+ *     - get (hit long prefix)
+ *     - remove long prefix
+ *     - get (hit short prefix)
+ */
+static int test_ip_lookup_table(void)
+{
+       odph_iplookup_prefix_t prefix1, prefix2;
+       odph_table_t table;
+       int ret;
+       uint32_t value1 = 1, value2 = 2, result = 0, lkp_ip = 0;
+
+       table = odph_iplookup_table_create(
+                       "prefix_test", 0, 0, sizeof(uint32_t));
+       RETURN_IF_ERROR(table == NULL,
+                       "IP prefix lookup table creation failed");
+
+       ret = odph_ipv4_addr_parse(&prefix1.ip, "192.168.0.0");
+       RETURN_IF_ERROR(ret < 0,
+                       "Failed to get IP addr from str");
+       prefix1.cidr = 11;
+
+       ret = odph_ipv4_addr_parse(&prefix2.ip, "192.168.0.0");
+       RETURN_IF_ERROR(ret < 0,
+                       "Failed to get IP addr from str");
+       prefix2.cidr = 24;
+
+       ret = odph_ipv4_addr_parse(&lkp_ip, "192.168.0.1");
+       RETURN_IF_ERROR(ret < 0,
+                       "Failed to get IP addr from str");
+
+       /* test with standard put/get/remove functions */
+       ret = odph_iplookup_table_put_value(table, &prefix1, &value1);
+       print_prefix_info("Add", prefix1.ip, prefix1.cidr);
+       RETURN_IF_ERROR(ret < 0, "failed to add ip prefix");
+
+       ret = odph_iplookup_table_get_value(table, &lkp_ip, &result, 0);
+       print_prefix_info("Lkp", lkp_ip, 32);
+       RETURN_IF_ERROR((ret < 0 || result != 1),
+                       "failed to find longest prefix");
+
+       /* add a longer prefix */
+       ret = odph_iplookup_table_put_value(table, &prefix2, &value2);
+       print_prefix_info("Add", prefix2.ip, prefix2.cidr);
+       RETURN_IF_ERROR(ret < 0, "failed to add ip prefix");
+
+       ret = odph_iplookup_table_get_value(table, &lkp_ip, &result, 0);
+       print_prefix_info("Lkp", lkp_ip, 32);
+       RETURN_IF_ERROR((ret < 0 || result != 2),
+                       "failed to find longest prefix");
+
+       ret = odph_iplookup_table_remove_value(table, &prefix2);
+       print_prefix_info("Del", prefix2.ip, prefix2.cidr);
+       RETURN_IF_ERROR(ret < 0, "failed to delete prefix");
+
+       ret = odph_iplookup_table_get_value(table, &lkp_ip, &result, 0);
+       print_prefix_info("Lkp", lkp_ip, 32);
+       RETURN_IF_ERROR((ret < 0 || result != 1),
+                       "fail: found result after deleting!");
+
+       ret = odph_iplookup_table_remove_value(table, &prefix1);
+       print_prefix_info("Del", prefix1.ip, prefix1.cidr);
+       RETURN_IF_ERROR(ret < 0, "failed to delete prefix");
+
+       odph_iplookup_table_destroy(table);
+       return 0;
+}
+
+int main(int argc TEST_UNUSED, char *argv[] TEST_UNUSED)
+{
+       odp_instance_t instance;
+       int ret = 0;
+
+       ret = odp_init_global(&instance, NULL, NULL);
+       if (ret != 0) {
+               fprintf(stderr, "Error: ODP global init failed.\n");
+               exit(EXIT_FAILURE);
+       }
+
+       ret = odp_init_local(instance, ODP_THREAD_WORKER);
+       if (ret != 0) {
+               fprintf(stderr, "Error: ODP local init failed.\n");
+               exit(EXIT_FAILURE);
+       }
+
+       if (test_ip_lookup_table() < 0)
+               printf("Test failed\n");
+       else
+               printf("All tests passed\n");
+
+       if (odp_term_local()) {
+               fprintf(stderr, "Error: ODP local term failed.\n");
+               exit(EXIT_FAILURE);
+       }
+
+       if (odp_term_global(instance)) {
+               fprintf(stderr, "Error: ODP global term failed.\n");
+               exit(EXIT_FAILURE);
+       }
+
+       return ret;
+}
-- 
1.9.1


_______________________________________________
lng-odp mailing list
[email protected]
https://lists.linaro.org/mailman/listinfo/lng-odp

Reply via email to