diff options
Diffstat (limited to 'lib/src/test')
-rw-r--r-- | lib/src/test/CMakeLists.txt | 47 | ||||
-rw-r--r-- | lib/src/test/main.cc | 23 | ||||
-rw-r--r-- | lib/src/test/test_name.cc | 339 | ||||
-rw-r--r-- | lib/src/test/test_new_header.cc | 340 | ||||
-rw-r--r-- | lib/src/test/test_udp_header.cc | 355 | ||||
-rw-r--r-- | lib/src/test/test_validation.cc | 55 |
6 files changed, 1159 insertions, 0 deletions
diff --git a/lib/src/test/CMakeLists.txt b/lib/src/test/CMakeLists.txt new file mode 100644 index 000000000..3e3c025c7 --- /dev/null +++ b/lib/src/test/CMakeLists.txt @@ -0,0 +1,47 @@ +# Copyright (c) 2021-2022 Cisco and/or its affiliates. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at: +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +find_package(Threads REQUIRED) + +############################################################## +# Test sources +############################################################## +list(APPEND TESTS_SRC + main.cc + test_name.cc + test_new_header.cc + test_udp_header.cc + test_validation.cc +) + +############################################################## +# Build single unit test executable and add it to test list +############################################################## +build_executable(lib_tests + NO_INSTALL + SOURCES ${TESTS_SRC} + LINK_LIBRARIES + PRIVATE ${LIBHICN_STATIC} + PRIVATE ${GTEST_LIBRARIES} + PRIVATE ${CMAKE_THREAD_LIBS_INIT} + INCLUDE_DIRS + PRIVATE ${Libhicn_INCLUDE_DIRS} + PRIVATE ${GTEST_INCLUDE_DIRS} + DEPENDS gtest ${LIBHICN_SHARED} + COMPONENT ${LIBHICN_COMPONENT} + DEFINITIONS ${COMPILER_DEFINITIONS} + COMPILE_OPTIONS ${DEFAULT_COMPILER_OPTIONS} + LINK_FLAGS ${LINK_FLAGS} +) + +add_test_internal(lib_tests) diff --git a/lib/src/test/main.cc b/lib/src/test/main.cc new file mode 100644 index 000000000..042fcd6c0 --- /dev/null +++ b/lib/src/test/main.cc @@ -0,0 +1,23 @@ +/* + * Copyright (c) 2021 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +int +main (int argc, char **argv) +{ + ::testing::InitGoogleTest (&argc, argv); + return RUN_ALL_TESTS (); +} diff --git a/lib/src/test/test_name.cc b/lib/src/test/test_name.cc new file mode 100644 index 000000000..207725adb --- /dev/null +++ b/lib/src/test/test_name.cc @@ -0,0 +1,339 @@ +/* + * Copyright (c) 2021 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +extern "C" +{ +#include <hicn/name.h> +#include <hicn/common.h> +#include <hicn/error.h> +} + +class NameTest : public ::testing::Test +{ +protected: + const char *ipv6_prefix = "b001::abcd:1234:abcd:1234"; + const char *ipv4_prefix = "12.13.14.15"; + const uint32_t suffix = 12345; + + NameTest () : name_{}, name4_{}, name6_{} + { + int rc = inet_pton (AF_INET6, ipv6_prefix, &ipv6_prefix_bytes.v6); + EXPECT_EQ (rc, 1); + + rc = inet_pton (AF_INET, ipv4_prefix, &ipv4_prefix_bytes.v4); + EXPECT_EQ (rc, 1); + + rc = hicn_name_create (ipv4_prefix, suffix, &name4_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_name_create (ipv6_prefix, suffix, &name6_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + } + + virtual ~NameTest () {} + + void + nameHashTest (const char *prefix) + { + // Create 2 names + uint32_t suffix = 13579; + hicn_name_t name_a, name_b; + int rc = hicn_name_create (prefix, suffix, &name_a); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_name_create (prefix, suffix, &name_b); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // The hash should be equal, with and without considering the suffix + uint32_t hash_a, hash_b; + rc = hicn_name_hash (&name_a, &hash_a, 1); + EXPECT_EQ (rc, 0); + rc = hicn_name_hash (&name_b, &hash_b, 1); + EXPECT_EQ (rc, 0); + EXPECT_EQ (hash_a, hash_b); + + rc = hicn_name_hash (&name_a, &hash_a, 0); + EXPECT_EQ (rc, 0); + rc = hicn_name_hash (&name_b, &hash_b, 0); + EXPECT_EQ (rc, 0); + EXPECT_EQ (hash_a, hash_b); + + // Now let's change the suffix + rc = hicn_name_set_seq_number (&name_a, 97531); + // They should result equal if we do not consider the suffix + rc = hicn_name_hash (&name_a, &hash_a, 0); + EXPECT_EQ (rc, 0); + rc = hicn_name_hash (&name_b, &hash_b, 0); + EXPECT_EQ (rc, 0); + EXPECT_EQ (hash_a, hash_b); + + // And different if we consider it + rc = hicn_name_hash (&name_a, &hash_a, 1); + EXPECT_EQ (rc, 0); + rc = hicn_name_hash (&name_b, &hash_b, 1); + EXPECT_EQ (rc, 0); + EXPECT_NE (hash_a, hash_b); + } + + void + nameCopyTest (const char *prefix) + { + uint32_t suffix = 13579; + hicn_name_t name_a, name_b; + int rc = hicn_name_create (prefix, suffix, &name_a); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_name_copy (&name_b, &name_a); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_name_compare (&name_a, &name_b, 1); + EXPECT_EQ (rc, 0); + } + + void + nameCompareTest (const char *prefix) + { + // Create 2 names + uint32_t suffix = 13579; + hicn_name_t name_a, name_b; + int rc = hicn_name_create (prefix, suffix, &name_a); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_name_create (prefix, suffix, &name_b); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // They should be equal, with and without considering the suffix + rc = hicn_name_compare (&name_a, &name_b, 1); + EXPECT_EQ (rc, 0); + rc = hicn_name_compare (&name_a, &name_b, 0); + EXPECT_EQ (rc, 0); + + // Now let's change the suffix + rc = hicn_name_set_seq_number (&name_a, 97531); + // They should result equal if we do not consider the suffix + rc = hicn_name_compare (&name_a, &name_b, 0); + EXPECT_EQ (rc, 0); + // And different if we consider the suffix + rc = hicn_name_compare (&name_a, &name_b, 1); + EXPECT_NE (rc, 0); + } + + void + nameFromIpPrefixTest (const ip_prefix_t &ip_prefix) + { + uint32_t suffix = 54321; + hicn_name_t name; + int rc = hicn_name_create_from_ip_prefix (&ip_prefix, suffix, &name); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = memcmp (ip_prefix.address.v6.as_u8, name.prefix.v6.as_u8, + sizeof (name.prefix.v6)); + EXPECT_EQ (rc, 0); + EXPECT_EQ (suffix, name.suffix); + } + + void + nameToIpPrefixTest (const char *prefix) + { + uint32_t suffix = 54321; + hicn_name_t name; + int rc = hicn_name_create (prefix, suffix, &name); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Get family + int family; + rc = hicn_name_get_family (&name, &family); + + ip_prefix_t ip_prefix; + rc = hicn_name_to_ip_prefix (&name, &ip_prefix); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (ip_prefix.family, family); + rc = ip_address_cmp (&ip_prefix.address, &name.prefix, AF_INET6); + EXPECT_EQ (rc, 0); + } + + hicn_name_t name_, name4_, name6_; + ip_address_t ipv6_prefix_bytes, ipv4_prefix_bytes; +}; + +/** + * Name Initialization + */ +TEST_F (NameTest, NameInitialization) +{ + EXPECT_TRUE (_is_unspec (&name_)); + uint32_t suffix = 12345; + + // Initialize ipv6 name + hicn_name_t name6; + int rc = hicn_name_create (ipv6_prefix, suffix, &name6); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check name is correctly created + rc = ip_address_cmp (&name6.prefix, &ipv6_prefix_bytes, AF_INET6); + EXPECT_EQ (rc, 0); + EXPECT_EQ (name6.suffix, suffix); + + // Initialize ipv4 name + hicn_name_t name4; + rc = hicn_name_create (ipv4_prefix, suffix, &name4); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check name is correctly created + rc = ip_address_cmp (&name4.prefix, &ipv4_prefix_bytes, AF_INET); + EXPECT_EQ (name4.prefix.pad[0], 0UL); + EXPECT_EQ (name4.prefix.pad[1], 0UL); + EXPECT_EQ (name4.prefix.pad[2], 0UL); + EXPECT_EQ (rc, 0); + EXPECT_EQ (name4.suffix, suffix); + + // Try also to reuse previously initialized name + rc = hicn_name_create (ipv4_prefix, suffix, &name6); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check name is correctly created + rc = ip_address_cmp (&name6.prefix, &ipv4_prefix_bytes, AF_INET); + EXPECT_EQ (name6.prefix.pad[0], 0UL); + EXPECT_EQ (name6.prefix.pad[1], 0UL); + EXPECT_EQ (name6.prefix.pad[2], 0UL); + EXPECT_EQ (rc, 0); + EXPECT_EQ (name6.suffix, suffix); +} + +/** + * Name from ip prefix + */ +TEST_F (NameTest, NameFromIpPrefix6) +{ + ip_prefix_t ip_prefix = { .family = AF_INET6, .address = {}, .len = 64 }; + + ip_prefix.address.v6.as_u64[0] = ipv6_prefix_bytes.v6.as_u64[0]; + ip_prefix.address.v6.as_u64[1] = ipv6_prefix_bytes.v6.as_u64[1]; + + nameFromIpPrefixTest (ip_prefix); +} + +TEST_F (NameTest, NameFromIpPrefix4) +{ + ip_prefix_t ip_prefix = { .family = AF_INET, .address = {}, .len = 64 }; + ip_prefix.address.v4.as_u32 = ipv4_prefix_bytes.v4.as_u32; + ip_prefix.address.pad[0] = 0; + ip_prefix.address.pad[1] = 0; + ip_prefix.address.pad[2] = 0; + nameFromIpPrefixTest (ip_prefix); +} + +TEST_F (NameTest, NameCompare6) { nameCompareTest (ipv6_prefix); } + +TEST_F (NameTest, NameCompare4) { nameCompareTest (ipv4_prefix); } + +TEST_F (NameTest, NameHash6) { nameHashTest (ipv6_prefix); } + +TEST_F (NameTest, NameHash4) { nameHashTest (ipv4_prefix); } + +TEST_F (NameTest, NameEmpty) +{ + int rc = hicn_name_empty (&name_); + EXPECT_EQ (rc, 1); + + name_.prefix.v6 = ipv6_prefix_bytes.v6; + rc = hicn_name_empty (&name_); + EXPECT_EQ (rc, 0); +} + +TEST_F (NameTest, NameCopy6) { nameCopyTest (ipv6_prefix); } + +TEST_F (NameTest, NameCopy4) { nameCopyTest (ipv4_prefix); } + +TEST_F (NameTest, NameCopyToDestination) +{ + ip4_address_t dst4; + ip6_address_t dst6; + + // Copy names to destination + int rc = hicn_name_copy_prefix_to_destination (dst4.as_u8, &name4_); + EXPECT_EQ (rc, 0); + rc = hicn_name_copy_prefix_to_destination (dst6.as_u8, &name6_); + EXPECT_EQ (rc, 0); + + // Check copy succeeded + EXPECT_TRUE (dst4.as_u32 == name4_.prefix.v4.as_u32); + EXPECT_TRUE (dst6.as_u64[0] == name6_.prefix.v6.as_u64[0]); + EXPECT_TRUE (dst6.as_u64[1] == name6_.prefix.v6.as_u64[1]); +} + +TEST_F (NameTest, SetGetSuffix) +{ + uint32_t suffix2 = 55555, suffix_ret; + + // Check if suffix is correct + int rc = hicn_name_get_seq_number (&name6_, &suffix_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (suffix, suffix_ret); + + // Set new suffix + rc = hicn_name_set_seq_number (&name6_, suffix2); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check suffix was set + rc = hicn_name_get_seq_number (&name6_, &suffix_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (suffix2, suffix_ret); +} + +TEST_F (NameTest, NameToSockAddr) +{ + struct sockaddr_in saddr4; + struct sockaddr_in6 saddr6; + + int rc = + hicn_name_to_sockaddr_address (&name6_, (struct sockaddr *) (&saddr6)); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = memcmp (name6_.prefix.v6.as_u8, saddr6.sin6_addr.s6_addr, + sizeof (name6_.prefix.v6)); + EXPECT_EQ (rc, 0); + + rc = hicn_name_to_sockaddr_address (&name4_, (struct sockaddr *) (&saddr4)); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (name4_.prefix.v4.as_u32, saddr4.sin_addr.s_addr); +} + +TEST_F (NameTest, NameToIpPrefix) +{ + nameToIpPrefixTest (ipv4_prefix); + nameToIpPrefixTest (ipv6_prefix); +} + +TEST_F (NameTest, NameNToP) +{ + char dst[128]; + + // V6 + int rc = hicn_name_ntop (&name6_, dst, 128); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Build expected name + std::stringstream expected6; + expected6 << ipv6_prefix << "|" << suffix; + + rc = strcmp (dst, expected6.str ().c_str ()); + EXPECT_EQ (rc, 0); + + // V4 + rc = hicn_name_ntop (&name4_, dst, 128); + std::stringstream expected4; + expected4 << ipv4_prefix << "|" << suffix; + + rc = strcmp (dst, expected4.str ().c_str ()); + EXPECT_EQ (rc, 0); +} diff --git a/lib/src/test/test_new_header.cc b/lib/src/test/test_new_header.cc new file mode 100644 index 000000000..33c9e13c9 --- /dev/null +++ b/lib/src/test/test_new_header.cc @@ -0,0 +1,340 @@ +/* + * Copyright (c) 2021 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +extern "C" +{ +#include <hicn/name.h> +#include <hicn/common.h> +#include <hicn/error.h> +#include <hicn/protocol/new.h> +#include <hicn/protocol/ah.h> +#include <hicn/header.h> +#include <hicn/compat.h> +} + +class NewHeaderTest : public ::testing::Test +{ +protected: + const char *ipv6_prefix = "b001::abcd:1234:abcd:1234"; + const char *ipv4_prefix = "12.13.14.15"; + const uint32_t suffix = 12345; + + NewHeaderTest (size_t hdr_size, hicn_format_t format) + : buffer_ (new uint8_t[hdr_size]), header_ ((hicn_header_t *) (buffer_)), + format_ (format), name_{}, name4_{}, name6_{} + { + int rc = inet_pton (AF_INET6, ipv6_prefix, &ipv6_prefix_bytes.v6); + EXPECT_EQ (rc, 1); + + rc = inet_pton (AF_INET, ipv4_prefix, &ipv4_prefix_bytes.v4); + EXPECT_EQ (rc, 1); + + rc = hicn_name_create (ipv4_prefix, suffix, &name4_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_name_create (ipv6_prefix, suffix, &name6_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + } + + NewHeaderTest () : NewHeaderTest (NEW_HDRLEN, HF_NEW) {} + + virtual ~NewHeaderTest () { delete[] buffer_; } + + void + checkCommon (const _new_header_t *new_hdr) + { + // Initialize header + int rc = hicn_packet_init_header (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check fields + EXPECT_EQ (new_hdr->prefix.v6.as_u64[0], 0UL); + EXPECT_EQ (new_hdr->prefix.v6.as_u64[1], 0UL); + EXPECT_EQ (new_hdr->suffix, 0UL); + EXPECT_EQ (new_hdr->lifetime, 0UL); + EXPECT_EQ (new_hdr->path_label, 0UL); + EXPECT_EQ (new_hdr->payload_length, 0UL); + EXPECT_EQ (_get_new_header_version (new_hdr), 0x9); + } + + virtual void + SetUp () override + { + auto new_hdr = &header_->protocol.newhdr; + checkCommon (new_hdr); + EXPECT_EQ (new_hdr->flags, 0); + } + + uint8_t *buffer_; + hicn_header_t *header_; + hicn_format_t format_; + hicn_name_t name_, name4_, name6_; + ip_address_t ipv6_prefix_bytes, ipv4_prefix_bytes; +}; + +class NewHeaderAHTest : public NewHeaderTest +{ +protected: + NewHeaderAHTest () : NewHeaderTest (NEW_HDRLEN + AH_HDRLEN, HF_NEW_AH) {} + + virtual void + SetUp () override + { + auto new_hdr = &header_->protocol.newhdr; + checkCommon (new_hdr); + EXPECT_NE (new_hdr->flags, 0); + } +}; + +/** + * Header Initialization + */ +TEST_F (NewHeaderTest, GetFormat) +{ + // Get format from existing packet + hicn_format_t format; + int rc = hicn_packet_get_format (header_, &format); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check it corresponds to the new header format + EXPECT_EQ (format, HF_NEW); +} + +TEST_F (NewHeaderAHTest, GetFormat) +{ + // Get format from existing packet + hicn_format_t format; + int rc = hicn_packet_get_format (header_, &format); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check it corresponds to the new header format + EXPECT_EQ (format, HF_NEW_AH); +} + +/** + * @brief Checksum functions are not required, but we keep them for + * compatibility. + */ +TEST_F (NewHeaderTest, Checksum) +{ + // Get format from existing packet + int rc = hicn_packet_compute_checksum (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_compute_header_checksum (format_, header_, 0); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_check_integrity_no_payload (format_, header_, 0); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); +} + +TEST_F (NewHeaderAHTest, Checksum) +{ + // Get format from existing packet + int rc = hicn_packet_compute_checksum (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_compute_header_checksum (format_, header_, 0); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_check_integrity_no_payload (format_, header_, 0); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); +} + +TEST_F (NewHeaderTest, GetHeaderLengthFromFormat) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length_from_format (format_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, NEW_HDRLEN); +} + +TEST_F (NewHeaderAHTest, GetHeaderLengthFromFormat) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length_from_format (format_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, NEW_HDRLEN + AH_HDRLEN); +} + +TEST_F (NewHeaderTest, GetHeaderLength) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length (format_, header_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, NEW_HDRLEN); +} + +TEST_F (NewHeaderAHTest, GetHeaderLength) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length (format_, header_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, NEW_HDRLEN + AH_HDRLEN); +} + +TEST_F (NewHeaderTest, SetGetPayloadLength) +{ + // Get format from existing packet + std::size_t payload_len = 1000, payload_len_ret; + int rc = hicn_packet_set_payload_length (format_, header_, payload_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_packet_get_payload_length (format_, header_, &payload_len_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (payload_len, payload_len_ret); +} + +TEST_F (NewHeaderAHTest, SetGetPayloadLength) +{ + // Get format from existing packet + std::size_t payload_len = 1000, payload_len_ret; + int rc = hicn_packet_set_payload_length (format_, header_, payload_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_packet_get_payload_length (format_, header_, &payload_len_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (payload_len, payload_len_ret); +} + +TEST_F (NewHeaderTest, SetGetName) +{ + // Get v6 name and set it to new_header + hicn_name_t name_ret; + int rc = hicn_packet_set_name (format_, header_, &name6_, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_name (format_, header_, &name_ret, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_name_compare (&name6_, &name_ret, 1); + EXPECT_EQ (rc, 0); +} + +TEST_F (NewHeaderTest, SetGetLocator) +{ + // This function does nothing but it is set for compatibility + ip_address_t locator; + memset (&locator, 0, sizeof (locator)); + locator.v6.as_u8[15] = 1; + int rc = hicn_packet_set_interest (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_set_locator (format_, header_, &locator, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_locator (format_, header_, &locator, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); +} + +TEST_F (NewHeaderTest, SetGetSignatureSize) +{ + // No AH, so we should get an error + // FixMe no error raised here + size_t signature_size = 128; + int rc = hicn_packet_set_signature_size (format_, header_, signature_size); + (void) rc; + // EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Same for hicn_packet_get_signature_size + rc = hicn_packet_get_signature_size (format_, header_, &signature_size); + // EXPECT_NE (rc, HICN_LIB_ERROR_NONE); +} + +TEST_F (NewHeaderAHTest, SetGetSignatureSize) +{ + // No AH, so we should get an error + size_t signature_size = 128, signature_size_ret; + int rc = hicn_packet_set_signature_size (format_, header_, signature_size); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Same for hicn_packet_get_signature_size + rc = hicn_packet_get_signature_size (format_, header_, &signature_size_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (signature_size, signature_size_ret); +} + +TEST_F (NewHeaderTest, IsInterestIsData) +{ + // Mark packet as interest + int rc = hicn_packet_set_interest (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + int ret; + rc = hicn_packet_is_interest (format_, header_, &ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (ret, 1); + + // Mark packet as data + rc = hicn_packet_set_data (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_is_interest (format_, header_, &ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (ret, 0); +} + +TEST_F (NewHeaderTest, SetGetLifetime) +{ + // Lifetime + u32 lifetime = 20000, lifetime_ret; // 20 sec. + int rc = hicn_packet_set_lifetime (format_, header_, lifetime); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_lifetime (format_, header_, &lifetime_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (lifetime, lifetime_ret); +} + +TEST_F (NewHeaderAHTest, SetGetLifetime) +{ + // Lifetime + u32 lifetime = 20000, lifetime_ret; // 20 sec. + int rc = hicn_packet_set_lifetime (format_, header_, lifetime); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_lifetime (format_, header_, &lifetime_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (lifetime, lifetime_ret); +} + +TEST_F (NewHeaderTest, SetGetPayloadType) +{ + // Lifetime + hicn_payload_type_t payload_type = HPT_MANIFEST, payload_type_ret; + int rc = hicn_packet_set_payload_type (format_, header_, payload_type); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_payload_type (format_, header_, &payload_type_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (payload_type, payload_type_ret); + + payload_type = HPT_DATA; + + rc = hicn_packet_set_payload_type (format_, header_, payload_type); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_payload_type (format_, header_, &payload_type_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (payload_type, payload_type_ret); +} diff --git a/lib/src/test/test_udp_header.cc b/lib/src/test/test_udp_header.cc new file mode 100644 index 000000000..5d9f4d1eb --- /dev/null +++ b/lib/src/test/test_udp_header.cc @@ -0,0 +1,355 @@ +/* + * Copyright (c) 2021 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +extern "C" +{ +#include <hicn/name.h> +#include <hicn/common.h> +#include <hicn/error.h> +#include <hicn/protocol/new.h> +#include <hicn/protocol/ah.h> +#include <hicn/header.h> +#include <hicn/compat.h> +} + +class UdpHeaderTest : public ::testing::Test +{ +protected: + const char *ipv6_prefix = "b001::abcd:1234:abcd:1234"; + const char *ipv4_prefix = "12.13.14.15"; + const uint32_t suffix = 12345; + + UdpHeaderTest (size_t hdr_size, hicn_format_t format) + : buffer_ (new uint8_t[hdr_size]), header_ ((hicn_header_t *) (buffer_)), + format_ (format), name_{}, name4_{}, name6_{} + { + int rc = inet_pton (AF_INET6, ipv6_prefix, &ipv6_prefix_bytes.v6); + EXPECT_EQ (rc, 1); + + rc = inet_pton (AF_INET, ipv4_prefix, &ipv4_prefix_bytes.v4); + EXPECT_EQ (rc, 1); + + rc = hicn_name_create (ipv4_prefix, suffix, &name4_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_name_create (ipv6_prefix, suffix, &name6_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + } + + UdpHeaderTest () + : UdpHeaderTest (NEW_HDRLEN + UDP_HDRLEN + IPV6_HDRLEN, HF_INET6_UDP) + { + } + + virtual ~UdpHeaderTest () { delete[] buffer_; } + + void + checkCommon (const _ipv6_header_t *ip6_hdr) + { + // Initialize header + int rc = hicn_packet_init_header (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check fields + EXPECT_EQ (ip6_hdr->saddr.as_u64[0], 0UL); + EXPECT_EQ (ip6_hdr->saddr.as_u64[1], 0UL); + EXPECT_EQ (ip6_hdr->daddr.as_u64[0], 0UL); + EXPECT_EQ (ip6_hdr->daddr.as_u64[1], 0UL); + EXPECT_EQ (ip6_hdr->nxt, IPPROTO_UDP); + + _udp_header_t *udp_hdr = (_udp_header_t *) (ip6_hdr + 1); + EXPECT_EQ (udp_hdr->src_port, 0UL); + EXPECT_EQ (udp_hdr->dst_port, 0UL); + EXPECT_EQ (udp_hdr->checksum, 0UL); + // EXPECT_EQ (ntohs (udp_hdr->length), NEW_HDRLEN + AH_HDRLEN); + + _new_header_t *new_hdr = (_new_header_t *) (udp_hdr + 1); + EXPECT_EQ (new_hdr->prefix.v6.as_u64[0], 0UL); + EXPECT_EQ (new_hdr->prefix.v6.as_u64[1], 0UL); + EXPECT_EQ (new_hdr->suffix, 0UL); + EXPECT_EQ (new_hdr->lifetime, 0UL); + EXPECT_EQ (new_hdr->path_label, 0UL); + EXPECT_EQ (new_hdr->payload_length, 0UL); + EXPECT_EQ (_get_new_header_version (new_hdr), 0x9); + } + + virtual void + SetUp () override + { + auto ip6_hdr = &header_->protocol.ipv6; + checkCommon (ip6_hdr); + } + + uint8_t *buffer_; + hicn_header_t *header_; + hicn_format_t format_; + hicn_name_t name_, name4_, name6_; + ip_address_t ipv6_prefix_bytes, ipv4_prefix_bytes; +}; + +class UdpHeaderAHTest : public UdpHeaderTest +{ +protected: + UdpHeaderAHTest () + : UdpHeaderTest (AH_HDRLEN + NEW_HDRLEN + UDP_HDRLEN + IPV6_HDRLEN, + HF_INET6_UDP_AH) + { + } +}; + +/** + * Header Initialization + */ +TEST_F (UdpHeaderTest, GetFormat) +{ + // Get format from existing packet + hicn_format_t format; + int rc = hicn_packet_get_format (header_, &format); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check it corresponds to the new header format + EXPECT_EQ (format, HF_INET6_UDP); +} + +TEST_F (UdpHeaderAHTest, GetFormat) +{ + // Get format from existing packet + hicn_format_t format; + int rc = hicn_packet_get_format (header_, &format); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Check it corresponds to the new header format + EXPECT_EQ (format, HF_INET6_UDP_AH); +} + +// /** +// * @brief Checksum functions are not required, but we keep them for +// * compatibility. +// */ +// TEST_F (NewHeaderTest, Checksum) +// { +// // Get format from existing packet +// int rc = hicn_packet_compute_checksum (format_, header_); +// EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + +// rc = hicn_packet_compute_header_checksum (format_, header_, 0); +// EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + +// rc = hicn_packet_check_integrity_no_payload (format_, header_, 0); +// EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); +// } + +// TEST_F (NewHeaderAHTest, Checksum) +// { +// // Get format from existing packet +// int rc = hicn_packet_compute_checksum (format_, header_); +// EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + +// rc = hicn_packet_compute_header_checksum (format_, header_, 0); +// EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + +// rc = hicn_packet_check_integrity_no_payload (format_, header_, 0); +// EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); +// } + +TEST_F (UdpHeaderTest, GetHeaderLengthFromFormat) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length_from_format (format_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, UDP_HDRLEN + IPV6_HDRLEN + NEW_HDRLEN); +} + +TEST_F (UdpHeaderAHTest, GetHeaderLengthFromFormat) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length_from_format (format_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, UDP_HDRLEN + IPV6_HDRLEN + NEW_HDRLEN + AH_HDRLEN); +} + +TEST_F (UdpHeaderTest, GetHeaderLength) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length (format_, header_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, UDP_HDRLEN + IPV6_HDRLEN + NEW_HDRLEN); +} + +TEST_F (UdpHeaderAHTest, GetHeaderLength) +{ + // Get format from existing packet + std::size_t hdr_len; + int rc = hicn_packet_get_header_length (format_, header_, &hdr_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (hdr_len, UDP_HDRLEN + IPV6_HDRLEN + NEW_HDRLEN + AH_HDRLEN); +} + +TEST_F (UdpHeaderTest, SetGetPayloadLength) +{ + // Get format from existing packet + std::size_t payload_len = 1000, payload_len_ret; + int rc = hicn_packet_set_payload_length (format_, header_, payload_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_packet_get_payload_length (format_, header_, &payload_len_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (payload_len, payload_len_ret); +} + +TEST_F (UdpHeaderAHTest, SetGetPayloadLength) +{ + // Get format from existing packet + std::size_t payload_len = 1000, payload_len_ret; + int rc = hicn_packet_set_payload_length (format_, header_, payload_len); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + rc = hicn_packet_get_payload_length (format_, header_, &payload_len_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (payload_len, payload_len_ret); +} + +TEST_F (UdpHeaderTest, SetGetName) +{ + // Get v6 name and set it to new_header + hicn_name_t name_ret; + + int rc = hicn_packet_set_interest (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_set_name (format_, header_, &name6_, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_name (format_, header_, &name_ret, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_name_compare (&name6_, &name_ret, 1); + EXPECT_EQ (rc, 0); +} + +TEST_F (UdpHeaderTest, SetGetLocator) +{ + // This function does nothing but it is set for compatibility + ip_address_t locator; + memset (&locator, 0, sizeof (locator)); + locator.v6.as_u8[15] = 1; + int rc = hicn_packet_set_interest (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_set_locator (format_, header_, &locator, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_locator (format_, header_, &locator, 1); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); +} + +TEST_F (UdpHeaderTest, SetGetSignatureSize) +{ + // No AH, so we should get an error + // FixMe no error raised here + size_t signature_size = 128; + int rc = hicn_packet_set_signature_size (format_, header_, signature_size); + (void) rc; + // EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Same for hicn_packet_get_signature_size + rc = hicn_packet_get_signature_size (format_, header_, &signature_size); + // EXPECT_NE (rc, HICN_LIB_ERROR_NONE); +} + +TEST_F (UdpHeaderAHTest, SetGetSignatureSize) +{ + // No AH, so we should get an error + size_t signature_size = 128, signature_size_ret; + int rc = hicn_packet_set_signature_size (format_, header_, signature_size); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + // Same for hicn_packet_get_signature_size + rc = hicn_packet_get_signature_size (format_, header_, &signature_size_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (signature_size, signature_size_ret); +} + +TEST_F (UdpHeaderTest, IsInterestIsData) +{ + // Mark packet as interest + int rc = hicn_packet_set_interest (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + int ret; + rc = hicn_packet_is_interest (format_, header_, &ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (ret, 1); + + // Mark packet as data + rc = hicn_packet_set_data (format_, header_); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_is_interest (format_, header_, &ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + EXPECT_EQ (ret, 0); +} + +TEST_F (UdpHeaderTest, SetGetLifetime) +{ + // Lifetime + u32 lifetime = 20000, lifetime_ret; // 20 sec. + int rc = hicn_packet_set_lifetime (format_, header_, lifetime); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_lifetime (format_, header_, &lifetime_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (lifetime, lifetime_ret); +} + +TEST_F (UdpHeaderAHTest, SetGetLifetime) +{ + // Lifetime + u32 lifetime = 20000, lifetime_ret; // 20 sec. + int rc = hicn_packet_set_lifetime (format_, header_, lifetime); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_lifetime (format_, header_, &lifetime_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (lifetime, lifetime_ret); +} + +TEST_F (UdpHeaderTest, SetGetPayloadType) +{ + // Lifetime + hicn_payload_type_t payload_type = HPT_MANIFEST, payload_type_ret; + int rc = hicn_packet_set_payload_type (format_, header_, payload_type); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_payload_type (format_, header_, &payload_type_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (payload_type, payload_type_ret); + + payload_type = HPT_DATA; + + rc = hicn_packet_set_payload_type (format_, header_, payload_type); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + rc = hicn_packet_get_payload_type (format_, header_, &payload_type_ret); + EXPECT_EQ (rc, HICN_LIB_ERROR_NONE); + + EXPECT_EQ (payload_type, payload_type_ret); +} diff --git a/lib/src/test/test_validation.cc b/lib/src/test/test_validation.cc new file mode 100644 index 000000000..091f26d65 --- /dev/null +++ b/lib/src/test/test_validation.cc @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2022 Cisco and/or its affiliates. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at: + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include <gtest/gtest.h> + +extern "C" +{ +#include <hicn/validation.h> +} + +static constexpr int BUF_SIZE = 10; + +class ValidationTest : public ::testing::Test +{ +}; + +TEST_F (ValidationTest, SymbolicName) +{ + const char symbolic_name_correct[BUF_SIZE] = "conn0"; + const char symbolic_name_empty[BUF_SIZE] = ""; + const char symbolic_name_wrong[BUF_SIZE] = "1conn0"; + + EXPECT_TRUE (is_symbolic_name (symbolic_name_correct, BUF_SIZE)); + EXPECT_FALSE (is_symbolic_name (symbolic_name_empty, BUF_SIZE)); + EXPECT_FALSE (is_symbolic_name (symbolic_name_wrong, BUF_SIZE)); +} + +TEST_F (ValidationTest, Number) +{ + const char number_correct[BUF_SIZE] = "123"; + const char number_empty[BUF_SIZE] = ""; + const char number_wrong[BUF_SIZE] = "a123"; + const char number_wrong_2[BUF_SIZE] = "12T3"; + const char number_wrong_3[BUF_SIZE] = "a"; + const char number_wrong_negative[BUF_SIZE] = "-123"; + + EXPECT_TRUE (is_number (number_correct, BUF_SIZE)); + EXPECT_FALSE (is_number (number_empty, BUF_SIZE)); + EXPECT_FALSE (is_number (number_wrong, BUF_SIZE)); + EXPECT_FALSE (is_number (number_wrong_2, BUF_SIZE)); + EXPECT_FALSE (is_number (number_wrong_3, BUF_SIZE)); + EXPECT_FALSE (is_number (number_wrong_negative, BUF_SIZE)); +}
\ No newline at end of file |