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
22const char name[] = "www.example.com";
23
24/**
25 * @tc.name      : setdomainname_0100
26 * @tc.desc      : set NIS domain name
27 * @tc.level     : Level 0
28 */
29void setdomainname_0100(void)
30{
31    // store current NIS domain name
32    char cbuf[BUFSIZ] = {0};
33    int result = getdomainname(cbuf, sizeof(cbuf));
34    if (result != 0) {
35        t_error("%s failed: result = %d\n", __func__, result);
36        return;
37    }
38
39    result = setdomainname(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 = getdomainname(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 NIS domain name
56    result = setdomainname(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      : setdomainname_0200
65 * @tc.desc      : set an empty NIS domain name
66 * @tc.level     : Level 1
67 */
68void setdomainname_0200(void)
69{
70    // store current NIS domain name
71    char cbuf[BUFSIZ] = {0};
72    int result = getdomainname(cbuf, sizeof(cbuf));
73    if (result != 0) {
74        t_error("%s failed: result = %d\n", __func__, result);
75        return;
76    }
77
78    result = setdomainname(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 = getdomainname(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 NIS domain name
95    result = setdomainname(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      : setdomainname_0300
104 * @tc.desc      : set NIS domain name with an invalid length
105 * @tc.level     : Level 2
106 */
107void setdomainname_0300(void)
108{
109    int result = setdomainname(NULL, -1);
110    if (result == 0) {
111        t_error("%s failed: result = %d\n", __func__, result);
112    }
113}
114
115int main(int argc, char *argv[])
116{
117    setdomainname_0100();
118    setdomainname_0200();
119    setdomainname_0300();
120
121    return t_status;
122}
123