• 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 <resolv.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19 
20 #include "test.h"
21 
22 const char dname[] = "www.example.com";
23 const int class = C_IN;
24 const int type = T_TXT;
25 const char options[] = "options timeout:0";
26 
set_timeout(void)27 void set_timeout(void)
28 {
29     system("cp /etc/resolv.conf /etc/resolv.conf.bak");
30 
31     FILE *f = fopen(_PATH_RESCONF, "a+");
32     if (f == NULL) {
33         t_error("%s failed: fopen\n", __func__);
34         return;
35     }
36 
37     fwrite(options, sizeof(options), 1, f);
38     fclose(f);
39 }
40 
restore_conf(void)41 void restore_conf(void)
42 {
43     remove(_PATH_RESCONF);
44     rename("/etc/resolv.conf.bak", _PATH_RESCONF);
45 }
46 
47 /**
48  * @tc.name      : res_query_0100
49  * @tc.desc      : query the name server
50  * @tc.level     : Level 0
51  */
res_query_0100(void)52 void res_query_0100(void)
53 {
54     set_timeout();
55 
56     unsigned char buf[BUFSIZ] = {0};
57     int result = res_query(dname, class, type, buf, sizeof(buf));
58     if (result != 0) {
59         t_error("%s failed: result = %d\n", __func__, result);
60     }
61 
62     restore_conf();
63 }
64 
65 /**
66  * @tc.name      : res_query_0200
67  * @tc.desc      : query the name server with an invalid type
68  * @tc.level     : Level 2
69  */
res_query_0200(void)70 void res_query_0200(void)
71 {
72     set_timeout();
73 
74     int result = res_query(dname, class, T_ANY + 1, NULL, 0);
75     if (result == 0) {
76         t_error("%s failed: result = %d\n", __func__, result);
77     }
78 
79     restore_conf();
80 }
81 
main(int argc,char * argv[])82 int main(int argc, char *argv[])
83 {
84     res_query_0100();
85     res_query_0200();
86 
87     return t_status;
88 }
89