This is an automated email from the ASF dual-hosted git repository.

pengzheng pushed a commit to branch feature/coverity-fix
in repository https://gitbox.apache.org/repos/asf/celix.git

commit ddefef83cdf7846c6d5183a8ecb54753f45ff6d9
Author: PengZheng <[email protected]>
AuthorDate: Mon Jan 1 12:44:44 2024 +0800

    Remove ip_utils and its tests.
---
 libs/utils/CMakeLists.txt                          |   5 -
 libs/utils/gtest/CMakeLists.txt                    |   1 -
 .../gtest/src/IpUtilsErrorInjectionTestSuite.cc    |  47 ------
 libs/utils/include_deprecated/ip_utils.h           |  52 -------
 libs/utils/private/test/ip_utils_test.cpp          | 108 -------------
 libs/utils/src/ip_utils.c                          | 170 ---------------------
 6 files changed, 383 deletions(-)

diff --git a/libs/utils/CMakeLists.txt b/libs/utils/CMakeLists.txt
index f9b01260..8438af2e 100644
--- a/libs/utils/CMakeLists.txt
+++ b/libs/utils/CMakeLists.txt
@@ -40,7 +40,6 @@ if (UTILS)
             src/version_range.c
             src/properties.c
             src/utils.c
-            src/ip_utils.c
             src/filter.c
             src/celix_log_level.c
             src/celix_log_utils.c
@@ -157,10 +156,6 @@ if (UTILS)
             target_include_directories(linked_list_test PRIVATE 
include_deprecated)
             target_link_libraries(linked_list_test utils_cut CppUTest pthread)
 
-            add_executable(ip_utils_test private/test/ip_utils_test.cpp)
-            target_include_directories(ip_utils_test PRIVATE 
include_deprecated)
-            target_link_libraries(ip_utils_test CppUTest  utils_cut pthread)
-
             add_test(NAME run_array_list_test COMMAND array_list_test)
             add_test(NAME run_hash_map_test COMMAND hash_map_test)
             add_test(NAME run_linked_list_test COMMAND linked_list_test)
diff --git a/libs/utils/gtest/CMakeLists.txt b/libs/utils/gtest/CMakeLists.txt
index 8b6381dc..782554ad 100644
--- a/libs/utils/gtest/CMakeLists.txt
+++ b/libs/utils/gtest/CMakeLists.txt
@@ -93,7 +93,6 @@ if (EI_TESTS)
     add_executable(test_utils_with_ei
             src/FileUtilsErrorInjectionTestSuite.cc
             src/ConvertUtilsErrorInjectionTestSuite.cc
-            src/IpUtilsErrorInjectionTestSuite.cc
             src/ArrayListErrorInjectionTestSuite.cc
             src/ErrErrorInjectionTestSuite.cc
             src/PropertiesErrorInjectionTestSuite.cc
diff --git a/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc 
b/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc
deleted file mode 100644
index 57a4c83c..00000000
--- a/libs/utils/gtest/src/IpUtilsErrorInjectionTestSuite.cc
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- Licensed to the Apache Software Foundation (ASF) under one
- or more contributor license agreements.  See the NOTICE file
- distributed with this work for additional information
- regarding copyright ownership.  The ASF licenses this file
- to you 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 <errno.h>
-#include <gtest/gtest.h>
-#include "ifaddrs_ei.h"
-#include "ip_utils.h"
-#include "celix_utils_ei.h"
-
-class IpUtilsWithErrorInjectionTestSuite : public ::testing::Test {
-public:
-    IpUtilsWithErrorInjectionTestSuite() = default;
-    ~IpUtilsWithErrorInjectionTestSuite() override {
-        celix_ei_expect_getifaddrs(nullptr, 0, 0);
-        celix_ei_expect_celix_utils_strdup(nullptr, 0, 0);
-    }
-};
-
-TEST_F(IpUtilsWithErrorInjectionTestSuite, failToGetInterfaceAddresses) {
-    celix_ei_expect_getifaddrs((void *)&ipUtils_findIpBySubnet, 0, -1);
-    auto ipAddresses = ipUtils_findIpBySubnet("192.168.1.0/24");
-    EXPECT_EQ(ipAddresses, nullptr);
-    EXPECT_EQ(errno, EMFILE);
-}
-
-TEST_F(IpUtilsWithErrorInjectionTestSuite, failToDuplicateString) {
-    celix_ei_expect_celix_utils_strdup((void *) &ipUtils_findIpBySubnet, 0, 
nullptr);
-    auto ipAddresses = ipUtils_findIpBySubnet("192.168.1.0/24");
-    EXPECT_EQ(ipAddresses, nullptr);
-    EXPECT_EQ(errno, ENOMEM);
-}
\ No newline at end of file
diff --git a/libs/utils/include_deprecated/ip_utils.h 
b/libs/utils/include_deprecated/ip_utils.h
deleted file mode 100644
index 75adcb59..00000000
--- a/libs/utils/include_deprecated/ip_utils.h
+++ /dev/null
@@ -1,52 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.
- */
-/**
- * ip_utils.h
- *
- *  \date       Jun 24, 2019
- *  \author     <a href="mailto:[email protected]";>Apache Celix Project 
Team</a>
- *  \copyright  Apache License, Version 2.0
- */
-
-#ifndef IP_UTILS_H_
-#define IP_UTILS_H_
-
-#include <ctype.h>
-
-#include "celix_errno.h"
-#include "celix_utils_export.h"
-
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-CELIX_UTILS_DEPRECATED_EXPORT unsigned int ipUtils_ipToUnsignedInt(char *ip);
-
-CELIX_UTILS_DEPRECATED_EXPORT char *ipUtils_unsignedIntToIp(unsigned int ip);
-
-CELIX_UTILS_DEPRECATED_EXPORT unsigned int ipUtils_prefixToBitmask(unsigned 
int prefix);
-
-CELIX_UTILS_DEPRECATED_EXPORT int ipUtils_netmaskToPrefix(const char *netmask);
-
-CELIX_UTILS_DEPRECATED_EXPORT char *ipUtils_findIpBySubnet(const char 
*ipWithPrefix);
-
-#ifdef __cplusplus
-}
-#endif
-#endif /* IP_UTILS_H_ */
diff --git a/libs/utils/private/test/ip_utils_test.cpp 
b/libs/utils/private/test/ip_utils_test.cpp
deleted file mode 100644
index 7b576b3a..00000000
--- a/libs/utils/private/test/ip_utils_test.cpp
+++ /dev/null
@@ -1,108 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.
- */
-/**
- * ip_utils_test.cpp
- *
- *  \date       Jun 24, 2019
- *  \author     <a href="mailto:[email protected]";>Apache Celix Project 
Team</a>
- *  \copyright  Apache License, Version 2.0
- */
-
-#include <errno.h>
-#include <string.h>
-#include <stdlib.h>
-#include <string.h>
-
-#include "CppUTest/TestHarness.h"
-#include "CppUTest/TestHarness_c.h"
-#include "CppUTest/CommandLineTestRunner.h"
-
-extern "C"
-{
-#include "ip_utils.h"
-}
-
-int main(int argc, char** argv) {
-    MemoryLeakWarningPlugin::turnOffNewDeleteOverloads();
-    return RUN_ALL_TESTS(argc, argv);
-}
-
-static char* my_strdup(const char* s){
-    if(s==NULL){
-        return NULL;
-    }
-
-    size_t len = strlen(s);
-
-    char *d = (char*) calloc (len + 1,sizeof(char));
-
-    if (d == NULL){
-        return NULL;
-    }
-
-    strncpy (d,s,len);
-    return d;
-}
-
-TEST_GROUP(ip_utils) {
-    void setup(void) {
-    }
-
-    void teardown() {
-    }
-};
-
-TEST(ip_utils, ipToUnsignedInt){
-    char *ip = my_strdup("192.168.1.64");
-
-    unsigned int expected = 3232235840;
-    unsigned int actual = ipUtils_ipToUnsignedInt(ip);
-    UNSIGNED_LONGS_EQUAL(expected, actual);
-
-    free(ip);
-}
-
-TEST(ip_utils, unsignedIntToIp){
-    unsigned int ipAsUint = 3232235840;
-
-    const char *expected = "192.168.1.64";
-    char *actual = ipUtils_unsignedIntToIp(ipAsUint);
-    STRCMP_EQUAL(expected, actual);
-    free(actual);
-}
-
-TEST(ip_utils, prefixToBitmask){
-    unsigned int expected = 4294967264;
-    unsigned int actual = ipUtils_prefixToBitmask(27);
-
-    UNSIGNED_LONGS_EQUAL(expected, actual);
-}
-
-TEST(ip_utils, netmaskToPrefix){
-    char *netmask = my_strdup("255.255.255.0");
-
-    int expected = 24;
-    int actual = ipUtils_netmaskToPrefix(netmask);
-    LONGS_EQUAL(expected, actual);
-
-    free(netmask);
-
-    actual = ipUtils_netmaskToPrefix("a.b.c.d");
-    LONGS_EQUAL(-1, actual);
-}
diff --git a/libs/utils/src/ip_utils.c b/libs/utils/src/ip_utils.c
deleted file mode 100644
index d168fa36..00000000
--- a/libs/utils/src/ip_utils.c
+++ /dev/null
@@ -1,170 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements.  See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership.  The ASF licenses this file
- * to you 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.
- */
-/**
- * ip_utils.c
- *
- *  \date       Jun 24, 2019
- *  \author     <a href="mailto:[email protected]";>Apache Celix Project 
Team</a>
- *  \copyright  Apache License, Version 2.0
- */
-
-#include "ip_utils.h"
-#include "celix_utils.h"
-
-#include <errno.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdbool.h>
-#include <string.h>
-#include <math.h>
-
-#include <arpa/inet.h>
-#include <sys/socket.h>
-#include <netdb.h>
-#include <ifaddrs.h>
-#include <unistd.h>
-
-unsigned int ipUtils_ipToUnsignedInt(char *ip) {
-    unsigned int ipAsUint = 0;
-
-    char *partOfIp = NULL, *savePtr = NULL;
-    char *input = strdup(ip); // Make a copy because otherwise strtok_r 
manipulates the input string
-    partOfIp = strtok_r(input, ".\0", &savePtr); ipAsUint += strtoul(partOfIp, 
NULL, 10) * (unsigned int) pow(256, 3);
-    partOfIp = strtok_r(NULL, ".\0", &savePtr);  ipAsUint += strtoul(partOfIp, 
NULL, 10) * (unsigned int) pow(256, 2);
-    partOfIp = strtok_r(NULL, ".\0", &savePtr);  ipAsUint += strtoul(partOfIp, 
NULL, 10) * (unsigned int) pow(256, 1);
-    partOfIp = strtok_r(NULL, ".\0", &savePtr);  ipAsUint += strtoul(partOfIp, 
NULL, 10) * (unsigned int) pow(256, 0);
-    free(input);
-
-    return ipAsUint;
-}
-
-char *ipUtils_unsignedIntToIp(unsigned int ip) {
-    char *ipStr = calloc(16, sizeof(char));
-
-    int ipPart1 = ip / (int) pow(256, 3); ip -= ipPart1 * (int) pow(256, 3);
-    int ipPart2 = ip / (int) pow(256, 2); ip -= ipPart2 * (int) pow(256, 2);
-    int ipPart3 = ip / (int) pow(256, 1); ip -= ipPart3 * (int) pow(256, 1);
-    int ipPart4 = ip / (int) pow(256, 0);
-
-    snprintf(ipStr, 16, "%d.%d.%d.%d", ipPart1, ipPart2, ipPart3, ipPart4);
-
-    return ipStr;
-}
-
-unsigned int ipUtils_prefixToBitmask(unsigned int prefix) {
-    return (0xFFFFFFFF << (32 - prefix)) & 0xFFFFFFFF;
-}
-
-int ipUtils_netmaskToPrefix(const char *netmask) {
-    // Convert netmask to in_addr object
-    struct in_addr in;
-    int ret = inet_pton(AF_INET, netmask, &in);
-    if (ret != 1) {
-        return -1;
-    }
-
-    // Now convert the mask to a prefix
-    int prefix = 0;
-    bool processed_one = false;
-    unsigned int i = ntohl(in.s_addr);
-
-    while (i > 0) {
-        if (i & 1) {
-            prefix++;
-            processed_one = true;
-        } else {
-            if (processed_one) return -1;
-        }
-
-        i >>= 1;
-    }
-
-    return prefix;
-}
-
-/** Finds an IP of the available network interfaces of the machine by 
specifying an CIDR subnet.
- *
- * @param ipWithPrefix  IP with prefix, e.g. 192.168.1.0/24
- * @return ip           In case a matching interface could be found, an 
allocated string containing the IP of the
- *                      interface will be returned, e.g. 192.168.1.16. Memory 
for the new string can be freed with free().
- *                      When no matching interface is found NULL will be 
returned.
- */
-char *ipUtils_findIpBySubnet(const char *ipWithPrefix) {
-    char *ip = NULL;
-
-    char *input = celix_utils_strdup(ipWithPrefix); // Make a copy as 
otherwise strtok_r manipulates the input string
-    if (input == NULL) {
-        goto strdup_failed;
-    }
-
-    char *savePtr;
-    char *inputIp = strtok_r(input, "/", &savePtr);
-    char *inputPrefixStr = strtok_r(NULL, "\0", &savePtr);
-    unsigned int inputPrefix = (unsigned int) strtoul(inputPrefixStr, NULL, 
10);
-
-    unsigned int ipAsUint = ipUtils_ipToUnsignedInt(inputIp);
-    unsigned int bitmask = ipUtils_prefixToBitmask(inputPrefix);
-
-    unsigned int ipRangeStart = ipAsUint & bitmask;
-    unsigned int ipRangeStop = ipAsUint | ~bitmask;
-
-    // Requested IP range is known now, now loop through network interfaces
-    struct ifaddrs *ifap, *ifa;
-
-    if(getifaddrs (&ifap) == -1) {
-        goto getifaddrs_failed;
-    }
-    for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
-        if (ifa->ifa_addr == NULL)
-            continue;
-
-        if (ifa->ifa_addr->sa_family != AF_INET)
-            continue;
-
-        // Retrieve IP address for interface
-        char if_addr[NI_MAXHOST];
-        int rv = getnameinfo(ifa->ifa_addr, sizeof(struct sockaddr_in),
-                             if_addr, NI_MAXHOST, NULL, 0, NI_NUMERICHOST);
-
-        if (rv != 0) {
-            printf("getnameinfo() failed: %s\n", gai_strerror(rv));
-            continue;
-        }
-
-        // Retrieve netmask
-        struct sockaddr_in *sa = (struct sockaddr_in *) ifa->ifa_netmask;
-        char *if_netmask = inet_ntoa(sa->sin_addr);
-
-        unsigned int ifIpAsUint = ipUtils_ipToUnsignedInt(if_addr);
-        int ifPrefix = ipUtils_netmaskToPrefix(if_netmask);
-        if (ifPrefix == -1) {
-            break;
-        }
-
-        if (ifIpAsUint >= ipRangeStart && ifIpAsUint <= ipRangeStop && 
inputPrefix >= ifPrefix) {
-            ip = strndup(if_addr, 1024);
-            break;
-        }
-    }
-    freeifaddrs(ifap);
-getifaddrs_failed:
-    free(input);
-strdup_failed:
-    return ip;
-}

Reply via email to