• 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 <string.h>
18 #include <unistd.h>
19 
20 #include "test.h"
21 
22 const char name[] = "www.example.com";
23 
24 /**
25  * @tc.name      : sethostname_0100
26  * @tc.desc      : set hostname
27  * @tc.level     : Level 0
28  */
sethostname_0100(void)29 void sethostname_0100(void)
30 {
31     // store current hostname
32     char cbuf[BUFSIZ] = {0};
33     int result = gethostname(cbuf, sizeof(cbuf));
34     if (result != 0) {
35         t_error("%s failed: result = %d\n", __func__, result);
36         return;
37     }
38 
39     result = sethostname(name, strlen(name));
40     if (result != 0) {
41         t_error("%s failed: result = %d\n", __func__, result);
42         return;
43     }
44 
45     char buf[BUFSIZ] = {0};
46     result = gethostname(buf, sizeof(buf));
47     if (result != 0) {
48         t_error("%s failed: result = %d\n", __func__, result);
49     }
50 
51     if (strcmp(buf, name)) {
52         t_error("%s failed: buf = %s\n", __func__, buf);
53     }
54 
55     // restore hostname
56     result = sethostname(cbuf, strlen(cbuf));
57     if (result != 0) {
58         t_error("%s failed: result = %d\n", __func__, result);
59         return;
60     }
61 }
62 
63 /**
64  * @tc.name      : sethostname_0200
65  * @tc.desc      : set an empty hostname
66  * @tc.level     : Level 1
67  */
sethostname_0200(void)68 void sethostname_0200(void)
69 {
70     // store current hostname
71     char cbuf[BUFSIZ] = {0};
72     int result = gethostname(cbuf, sizeof(cbuf));
73     if (result != 0) {
74         t_error("%s failed: result = %d\n", __func__, result);
75         return;
76     }
77 
78     result = sethostname(NULL, 0);
79     if (result != 0) {
80         t_error("%s failed: result = %d\n", __func__, result);
81         return;
82     }
83 
84     char buf[BUFSIZ] = {0};
85     result = gethostname(buf, sizeof(buf));
86     if (result != 0) {
87         t_error("%s failed: result = %d\n", __func__, result);
88     }
89 
90     if (strlen(buf) != 0) {
91         t_error("%s failed: buf = %s\n", __func__, buf);
92     }
93 
94     // restore hostname
95     result = sethostname(cbuf, strlen(cbuf));
96     if (result != 0) {
97         t_error("%s failed: result = %d\n", __func__, result);
98         return;
99     }
100 }
101 
102 /**
103  * @tc.name      : sethostname_0300
104  * @tc.desc      : set hostname with an invalid length
105  * @tc.level     : Level 2
106  */
sethostname_0300(void)107 void sethostname_0300(void)
108 {
109     int result = sethostname(NULL, -1);
110     if (result == 0) {
111         t_error("%s failed: result = %d\n", __func__, result);
112     }
113 }
114 
main(int argc,char * argv[])115 int main(int argc, char *argv[])
116 {
117     sethostname_0100();
118     sethostname_0200();
119     sethostname_0300();
120 
121     return t_status;
122 }
123