• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *   http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <stdio.h>
17 #include <ifaddrs.h>
18 #include <netinet/in.h>
19 #include <string.h>
20 #include "test.h"
21 
22 /**
23  * @tc.name      : freeifaddrs_0100
24  * @tc.desc      : Verifies that the specified memory can be freed (parameter valid)
25  * @tc.level     : Level 0
26  */
freeifaddrs_0100(void)27 void freeifaddrs_0100(void)
28 {
29     struct ifaddrs *addrs = NULL;
30     struct ifaddrs *lo_inet4 = NULL;
31     struct ifaddrs *lo_inet6 = NULL;
32     struct ifaddrs *lo_packet = NULL;
33 
34     int result = getifaddrs(&addrs);
35     if (result != 0) {
36         t_error("%s getifaddrs failed\n", __func__);
37     }
38     if (!addrs) {
39         t_error("%s addrs is NULL\n", __func__);
40     }
41 
42     for (struct ifaddrs *addr = addrs; addr != NULL; addr = addr->ifa_next) {
43         if (addr->ifa_name && strcmp(addr->ifa_name, "lo") == 0) {
44             if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET) {
45                 lo_inet4 = addr;
46             } else if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_INET6) {
47                 lo_inet6 = addr;
48             } else if (addr->ifa_addr && addr->ifa_addr->sa_family == AF_PACKET) {
49                 lo_packet = addr;
50             }
51         }
52     }
53 
54     if (lo_inet4 == NULL) {
55         t_error("%s lo_inet4 is NULL\n", __func__);
56     }
57     if (lo_inet6 == NULL) {
58         t_error("%s lo_inet4 is NULL\n", __func__);
59     }
60     if (lo_packet == NULL) {
61         t_error("%s lo_inet4 is NULL\n", __func__);
62     }
63 
64     freeifaddrs(addrs);
65 }
66 
main(int argc,char * argv[])67 int main(int argc, char *argv[])
68 {
69     freeifaddrs_0100();
70     return t_status;
71 }