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 <arpa/inet.h>
17 #include "test.h"
18
19 /**
20 * @tc.name : inet_aton_0100
21 * @tc.desc : The format of the parameter _cp is a.b.c.d
22 * @tc.level : Level 0
23 */
inet_aton_0100()24 void inet_aton_0100()
25 {
26 struct in_addr a;
27 a.s_addr = 0;
28 int ret = inet_aton("127.1.2.3", &a);
29 if (ret != 1) {
30 t_error("%s inet_aton failed\n", __func__);
31 }
32 if (a.s_addr != (htonl)(0x7f010203)) {
33 t_error("%s inet_aton invalid\n", __func__);
34 }
35 }
36
37 /**
38 * @tc.name : inet_aton_0200
39 * @tc.desc : The format of the parameter _cp is a.b.c
40 * @tc.level : Level 0
41 */
inet_aton_0200()42 void inet_aton_0200()
43 {
44 struct in_addr a;
45 a.s_addr = 0;
46 int ret = inet_aton("127.1.2", &a);
47 if (ret != 1) {
48 t_error("%s inet_aton failed\n", __func__);
49 }
50 if (a.s_addr != (htonl)(0x7f010002)) {
51 t_error("%s inet_aton invalid\n", __func__);
52 }
53 }
54
55 /**
56 * @tc.name : inet_aton_0300
57 * @tc.desc : The format of the parameter _cp is a.b
58 * @tc.level : Level 0
59 */
inet_aton_0300()60 void inet_aton_0300()
61 {
62 struct in_addr a;
63 a.s_addr = 0;
64 int ret = inet_aton("127.1", &a);
65 if (ret != 1) {
66 t_error("%s inet_aton failed\n", __func__);
67 }
68 if (a.s_addr != (htonl)(0x7f000001)) {
69 t_error("%s inet_aton invalid\n", __func__);
70 }
71 }
72
73 /**
74 * @tc.name : inet_aton_0400
75 * @tc.desc : The format of the parameter _cp is a
76 * @tc.level : Level 0
77 */
inet_aton_0400()78 void inet_aton_0400()
79 {
80 struct in_addr a;
81 a.s_addr = 0;
82 int ret = inet_aton("0x7f000001", &a);
83 if (ret != 1) {
84 t_error("%s inet_aton failed\n", __func__);
85 }
86 if (a.s_addr != (htonl)(0x7f000001)) {
87 t_error("%s inet_aton invalid\n", __func__);
88 }
89 }
90
91 /**
92 * @tc.name : inet_aton_0500
93 * @tc.desc : Hex (0x) and mixed-case hex digits.
94 * @tc.level : Level 0
95 */
inet_aton_0500()96 void inet_aton_0500()
97 {
98 struct in_addr a;
99 a.s_addr = 0;
100 int ret = inet_aton("0xFf.0.0.1", &a);
101 if (ret != 1) {
102 t_error("%s inet_aton failed\n", __func__);
103 }
104 if (a.s_addr != (htonl)(0xff000001)) {
105 t_error("%s inet_aton invalid\n", __func__);
106 }
107 }
108
109 /**
110 * @tc.name : inet_aton_0600
111 * @tc.desc : When the parameter is invalid, test the return value of the function
112 * @tc.level : Level 2
113 */
inet_aton_0600()114 void inet_aton_0600()
115 {
116 int ret = inet_aton("", NULL);
117 if (ret != 0) {
118 t_error("%s inet_aton failed\n", __func__);
119 }
120 }
121
main(int argc,char * argv[])122 int main(int argc, char *argv[])
123 {
124 inet_aton_0100();
125 inet_aton_0200();
126 inet_aton_0300();
127 inet_aton_0400();
128 inet_aton_0500();
129 inet_aton_0600();
130 return t_status;
131 }