1570af302Sopenharmony_ci/*
2570af302Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd.
3570af302Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4570af302Sopenharmony_ci * you may not use this file except in compliance with the License.
5570af302Sopenharmony_ci * You may obtain a copy of the License at
6570af302Sopenharmony_ci *
7570af302Sopenharmony_ci *   http://www.apache.org/licenses/LICENSE-2.0
8570af302Sopenharmony_ci *
9570af302Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10570af302Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11570af302Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12570af302Sopenharmony_ci * See the License for the specific language governing permissions and
13570af302Sopenharmony_ci * limitations under the License.
14570af302Sopenharmony_ci */
15570af302Sopenharmony_ci
16570af302Sopenharmony_ci#include <stdio.h>
17570af302Sopenharmony_ci#include <unistd.h>
18570af302Sopenharmony_ci#include "test.h"
19570af302Sopenharmony_ci
20570af302Sopenharmony_ci/**
21570af302Sopenharmony_ci * @tc.name      : getopt_0100
22570af302Sopenharmony_ci * @tc.desc      : Each parameter is valid, argv is an existing command line parameter, and the analysis of the
23570af302Sopenharmony_ci *                 command line parameter is successful
24570af302Sopenharmony_ci * @tc.level     : Level 0
25570af302Sopenharmony_ci */
26570af302Sopenharmony_civoid getopt_0100(void)
27570af302Sopenharmony_ci{
28570af302Sopenharmony_ci    int argc = 2;
29570af302Sopenharmony_ci    char *argv[] = {"./getopt", "-b"};
30570af302Sopenharmony_ci    char *optstring = "a:bcde";
31570af302Sopenharmony_ci    int result = getopt(argc, argv, optstring);
32570af302Sopenharmony_ci    if (result != 'b') {
33570af302Sopenharmony_ci        t_error("%s getopt get result is '%c' are not want 'b'\n", __func__, result);
34570af302Sopenharmony_ci    }
35570af302Sopenharmony_ci}
36570af302Sopenharmony_ci
37570af302Sopenharmony_ci/**
38570af302Sopenharmony_ci * @tc.name      : getopt_0200
39570af302Sopenharmony_ci * @tc.desc      : Each parameter is valid, argv is an existing command line parameter with a colon, and the analysis
40570af302Sopenharmony_ci *                 of the command line parameter is failed
41570af302Sopenharmony_ci * @tc.level     : Level 0
42570af302Sopenharmony_ci */
43570af302Sopenharmony_civoid getopt_0200(void)
44570af302Sopenharmony_ci{
45570af302Sopenharmony_ci    int argc = 2;
46570af302Sopenharmony_ci    char *argv[] = {"./getopt", "-a12345"};
47570af302Sopenharmony_ci    char *optstring = "a:bcde";
48570af302Sopenharmony_ci    int result = getopt(argc, argv, optstring);
49570af302Sopenharmony_ci    if (result != -1) {
50570af302Sopenharmony_ci        t_error("%s getopt get result is %d are not want -1\n", __func__, result);
51570af302Sopenharmony_ci    }
52570af302Sopenharmony_ci}
53570af302Sopenharmony_ci
54570af302Sopenharmony_ci/**
55570af302Sopenharmony_ci * @tc.name      : getopt_0300
56570af302Sopenharmony_ci * @tc.desc      : The argv parameter is invalid without a colon, and the parsing of the command line parameters failed
57570af302Sopenharmony_ci * @tc.level     : Level 2
58570af302Sopenharmony_ci */
59570af302Sopenharmony_civoid getopt_0300(void)
60570af302Sopenharmony_ci{
61570af302Sopenharmony_ci    int argc = 2;
62570af302Sopenharmony_ci    char *argv[] = {"./getopt", "-a"};
63570af302Sopenharmony_ci    char *optstring = "a:bcde";
64570af302Sopenharmony_ci    int result = getopt(argc, argv, optstring);
65570af302Sopenharmony_ci    if (result != -1) {
66570af302Sopenharmony_ci        t_error("%s getopt get result is %d are not want -1\n", __func__, result);
67570af302Sopenharmony_ci    }
68570af302Sopenharmony_ci}
69570af302Sopenharmony_ci
70570af302Sopenharmony_ci/**
71570af302Sopenharmony_ci * @tc.name      : getopt_0400
72570af302Sopenharmony_ci * @tc.desc      : The argv parameter is invalid, the command line parameter does not exist, and the parsing of the
73570af302Sopenharmony_ci *                 command line parameter failed
74570af302Sopenharmony_ci * @tc.level     : Level 2
75570af302Sopenharmony_ci */
76570af302Sopenharmony_civoid getopt_0400(void)
77570af302Sopenharmony_ci{
78570af302Sopenharmony_ci    int argc = 2;
79570af302Sopenharmony_ci    char *argv[] = {"./getopt", "-f"};
80570af302Sopenharmony_ci    char *optstring = "a:bcde";
81570af302Sopenharmony_ci    int result = getopt(argc, argv, optstring);
82570af302Sopenharmony_ci    if (result != -1) {
83570af302Sopenharmony_ci        t_error("%s getopt get result is %d are not want -1\n", __func__, result);
84570af302Sopenharmony_ci    }
85570af302Sopenharmony_ci}
86570af302Sopenharmony_ci
87570af302Sopenharmony_ci/**
88570af302Sopenharmony_ci * @tc.name      : getopt_0500
89570af302Sopenharmony_ci * @tc.desc      : The argv argument is invalid, argv is NULL, and parsing command line arguments failed
90570af302Sopenharmony_ci * @tc.level     : Level 2
91570af302Sopenharmony_ci */
92570af302Sopenharmony_civoid getopt_0500(void)
93570af302Sopenharmony_ci{
94570af302Sopenharmony_ci    int argc = 2;
95570af302Sopenharmony_ci    char *argv[] = {"./getopt", NULL};
96570af302Sopenharmony_ci    char *optstring = "a:bcde";
97570af302Sopenharmony_ci    int result = getopt(argc, argv, optstring);
98570af302Sopenharmony_ci    if (result != -1) {
99570af302Sopenharmony_ci        t_error("%s getopt get result is %d are not want -1\n", __func__, result);
100570af302Sopenharmony_ci    }
101570af302Sopenharmony_ci}
102570af302Sopenharmony_ci
103570af302Sopenharmony_ci/**
104570af302Sopenharmony_ci * @tc.name      : getopt_0600
105570af302Sopenharmony_ci * @tc.desc      : Invalid argv argument, argv is -, failed to parse command line arguments
106570af302Sopenharmony_ci * @tc.level     : Level 2
107570af302Sopenharmony_ci */
108570af302Sopenharmony_civoid getopt_0600(void)
109570af302Sopenharmony_ci{
110570af302Sopenharmony_ci    int argc = 2;
111570af302Sopenharmony_ci    char *argv[] = {"./getopt", "-"};
112570af302Sopenharmony_ci    char *optstring = "a:bcde";
113570af302Sopenharmony_ci    int result = getopt(argc, argv, optstring);
114570af302Sopenharmony_ci    if (result != -1) {
115570af302Sopenharmony_ci        t_error("%s getopt get result is %d are not want -1\n", __func__, result);
116570af302Sopenharmony_ci    }
117570af302Sopenharmony_ci}
118570af302Sopenharmony_ci
119570af302Sopenharmony_ci/**
120570af302Sopenharmony_ci * @tc.name      : getopt_0700
121570af302Sopenharmony_ci * @tc.desc      : argv argument is valid, optstring starts with -, parsing command line arguments fails
122570af302Sopenharmony_ci * @tc.level     : Level 2
123570af302Sopenharmony_ci */
124570af302Sopenharmony_civoid getopt_0700(void)
125570af302Sopenharmony_ci{
126570af302Sopenharmony_ci    int argc = 2;
127570af302Sopenharmony_ci    char *argv[] = {"./getopt", "b"};
128570af302Sopenharmony_ci    char *optstring = "-a:bcde";
129570af302Sopenharmony_ci    int result = getopt(argc, argv, optstring);
130570af302Sopenharmony_ci    if (result != -1) {
131570af302Sopenharmony_ci        t_error("%s getopt get result is %d are not want -1\n", __func__, result);
132570af302Sopenharmony_ci    }
133570af302Sopenharmony_ci}
134570af302Sopenharmony_ci
135570af302Sopenharmony_ci/**
136570af302Sopenharmony_ci * @tc.name      : getopt_0800
137570af302Sopenharmony_ci * @tc.desc      : Invalid argc argument, failed to parse command line arguments
138570af302Sopenharmony_ci * @tc.level     : Level 2
139570af302Sopenharmony_ci */
140570af302Sopenharmony_civoid getopt_0800(void)
141570af302Sopenharmony_ci{
142570af302Sopenharmony_ci    int argc = 0;
143570af302Sopenharmony_ci    char *argv[] = {"./getopt", "-b"};
144570af302Sopenharmony_ci    char *optstring = "a:bcde";
145570af302Sopenharmony_ci    int result = getopt(argc, argv, optstring);
146570af302Sopenharmony_ci    if (result != -1) {
147570af302Sopenharmony_ci        t_error("%s getopt get result is %d are not want -1\n", __func__, result);
148570af302Sopenharmony_ci    }
149570af302Sopenharmony_ci}
150570af302Sopenharmony_ci
151570af302Sopenharmony_ciint main(int argc, char *argv[])
152570af302Sopenharmony_ci{
153570af302Sopenharmony_ci    getopt_0100();
154570af302Sopenharmony_ci    getopt_0200();
155570af302Sopenharmony_ci    getopt_0300();
156570af302Sopenharmony_ci    getopt_0400();
157570af302Sopenharmony_ci    getopt_0500();
158570af302Sopenharmony_ci    getopt_0600();
159570af302Sopenharmony_ci    getopt_0700();
160570af302Sopenharmony_ci    getopt_0800();
161570af302Sopenharmony_ci    return t_status;
162570af302Sopenharmony_ci}