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 <unistd.h>
18 #include "test.h"
19
20 /**
21 * @tc.name : getopt_0100
22 * @tc.desc : Each parameter is valid, argv is an existing command line parameter, and the analysis of the
23 * command line parameter is successful
24 * @tc.level : Level 0
25 */
getopt_0100(void)26 void getopt_0100(void)
27 {
28 int argc = 2;
29 char *argv[] = {"./getopt", "-b"};
30 char *optstring = "a:bcde";
31 int result = getopt(argc, argv, optstring);
32 if (result != 'b') {
33 t_error("%s getopt get result is '%c' are not want 'b'\n", __func__, result);
34 }
35 }
36
37 /**
38 * @tc.name : getopt_0200
39 * @tc.desc : Each parameter is valid, argv is an existing command line parameter with a colon, and the analysis
40 * of the command line parameter is failed
41 * @tc.level : Level 0
42 */
getopt_0200(void)43 void getopt_0200(void)
44 {
45 int argc = 2;
46 char *argv[] = {"./getopt", "-a12345"};
47 char *optstring = "a:bcde";
48 int result = getopt(argc, argv, optstring);
49 if (result != -1) {
50 t_error("%s getopt get result is %d are not want -1\n", __func__, result);
51 }
52 }
53
54 /**
55 * @tc.name : getopt_0300
56 * @tc.desc : The argv parameter is invalid without a colon, and the parsing of the command line parameters failed
57 * @tc.level : Level 2
58 */
getopt_0300(void)59 void getopt_0300(void)
60 {
61 int argc = 2;
62 char *argv[] = {"./getopt", "-a"};
63 char *optstring = "a:bcde";
64 int result = getopt(argc, argv, optstring);
65 if (result != -1) {
66 t_error("%s getopt get result is %d are not want -1\n", __func__, result);
67 }
68 }
69
70 /**
71 * @tc.name : getopt_0400
72 * @tc.desc : The argv parameter is invalid, the command line parameter does not exist, and the parsing of the
73 * command line parameter failed
74 * @tc.level : Level 2
75 */
getopt_0400(void)76 void getopt_0400(void)
77 {
78 int argc = 2;
79 char *argv[] = {"./getopt", "-f"};
80 char *optstring = "a:bcde";
81 int result = getopt(argc, argv, optstring);
82 if (result != -1) {
83 t_error("%s getopt get result is %d are not want -1\n", __func__, result);
84 }
85 }
86
87 /**
88 * @tc.name : getopt_0500
89 * @tc.desc : The argv argument is invalid, argv is NULL, and parsing command line arguments failed
90 * @tc.level : Level 2
91 */
getopt_0500(void)92 void getopt_0500(void)
93 {
94 int argc = 2;
95 char *argv[] = {"./getopt", NULL};
96 char *optstring = "a:bcde";
97 int result = getopt(argc, argv, optstring);
98 if (result != -1) {
99 t_error("%s getopt get result is %d are not want -1\n", __func__, result);
100 }
101 }
102
103 /**
104 * @tc.name : getopt_0600
105 * @tc.desc : Invalid argv argument, argv is -, failed to parse command line arguments
106 * @tc.level : Level 2
107 */
getopt_0600(void)108 void getopt_0600(void)
109 {
110 int argc = 2;
111 char *argv[] = {"./getopt", "-"};
112 char *optstring = "a:bcde";
113 int result = getopt(argc, argv, optstring);
114 if (result != -1) {
115 t_error("%s getopt get result is %d are not want -1\n", __func__, result);
116 }
117 }
118
119 /**
120 * @tc.name : getopt_0700
121 * @tc.desc : argv argument is valid, optstring starts with -, parsing command line arguments fails
122 * @tc.level : Level 2
123 */
getopt_0700(void)124 void getopt_0700(void)
125 {
126 int argc = 2;
127 char *argv[] = {"./getopt", "b"};
128 char *optstring = "-a:bcde";
129 int result = getopt(argc, argv, optstring);
130 if (result != -1) {
131 t_error("%s getopt get result is %d are not want -1\n", __func__, result);
132 }
133 }
134
135 /**
136 * @tc.name : getopt_0800
137 * @tc.desc : Invalid argc argument, failed to parse command line arguments
138 * @tc.level : Level 2
139 */
getopt_0800(void)140 void getopt_0800(void)
141 {
142 int argc = 0;
143 char *argv[] = {"./getopt", "-b"};
144 char *optstring = "a:bcde";
145 int result = getopt(argc, argv, optstring);
146 if (result != -1) {
147 t_error("%s getopt get result is %d are not want -1\n", __func__, result);
148 }
149 }
150
main(int argc, char *argv[])151 int main(int argc, char *argv[])
152 {
153 getopt_0100();
154 getopt_0200();
155 getopt_0300();
156 getopt_0400();
157 getopt_0500();
158 getopt_0600();
159 getopt_0700();
160 getopt_0800();
161 return t_status;
162 }