1 /*
2 * Copyright (C) 2021 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 #include <exception>
16 #include <iostream>
17 #include <string>
18 #include <thread>
19 #include <gtest/gtest.h>
20 #include <unistd.h>
21 #include <cstring>
22 #include <cstdint>
23 #include <cstdio>
24 #include <functional>
25 #include "sp_utils.h"
26 #include "common.h"
27
28 using namespace testing::ext;
29 using namespace std;
30
31 namespace OHOS {
32 namespace SmartPerf {
33 class SPdaemonMainTest : public testing::Test {
34 public:
SetUpTestCase()35 static void SetUpTestCase() {}
TearDownTestCase()36 static void TearDownTestCase() {}
SetUp()37 void SetUp() {}
TearDown()38 void TearDown() {}
39 };
40
g_getOptions(std::vector<std::string> & argv)41 std::string &g_getOptions(std::vector<std::string> &argv)
42 {
43 std::string str = "";
44 std::string strFlag;
45 bool isFill = false;
46 for (std::size_t i = 0; i < argv.size(); i++) {
47 if (!isFill) {
48 strFlag = argv[i];
49 if (std::string::npos != strFlag.find("SP_daemon")) {
50 isFill = true;
51 }
52 } else {
53 str += argv[i];
54 if (i + 1 != argv.size()) {
55 str += " ";
56 }
57 }
58 }
59 return str;
60 }
61
CheckCMDParam(std::vector<std::string> & argv,std::string & errorInfo)62 bool CheckCMDParam(std::vector<std::string> &argv, std::string &errorInfo)
63 {
64 std::string str = g_getOptions(argv);
65 std::set<std::string> keys;
66
67 if (str.empty()) {
68 return true;
69 }
70
71 if (std::string::npos != str.find("--help") || std::string::npos != str.find("--version")) {
72 std::vector<std::string> out;
73 OHOS::SmartPerf::SPUtils::StrSplit(str, "-", out);
74 if (1 != out.size()) {
75 errorInfo = "--help and --version cannot be used together with other options";
76 return false;
77 } else {
78 return true;
79 }
80 }
81
82 keys.insert("editor");
83 keys.insert("profilerfps");
84 keys.insert("start");
85 keys.insert("stop");
86 keys.insert("screen");
87 keys.insert("clear");
88 keys.insert("server");
89 keys.insert("sections");
90 keys.insert("deviceinfo");
91 keys.insert("ohtestfps");
92 keys.insert("editorServer");
93
94 for (auto a : OHOS::SmartPerf::COMMAND_MAP) {
95 keys.insert(a.first.substr(1)); // No prefix required '-'
96 }
97
98 auto itr = keys.find("f1");
99 if (keys.end() != itr) {
100 keys.erase(itr);
101 }
102 itr = keys.find("f2");
103 if (keys.end() != itr) {
104 keys.erase(itr);
105 }
106 itr = keys.find("fl");
107 if (keys.end() != itr) {
108 keys.erase(itr);
109 }
110 itr = keys.find("ftl");
111 if (keys.end() != itr) {
112 keys.erase(itr);
113 }
114 return OHOS::SmartPerf::SPUtils::VeriyParameter(keys, str, errorInfo);
115 }
116
117 /**
118 * @tc.name: GetOptionsTestCase
119 * @tc.desc: Test GetOptions
120 * @tc.type: FUNC
121 */
122 HWTEST_F(SPdaemonMainTest, GetOptionsTestCase001, TestSize.Level1)
123 {
124 bool ret = false;
125
126 std::vector<std::string> argv;
127 argv.push_back("Test");
128 argv.push_back("GetOptions");
129 argv.push_back("SP_daemon");
130 argv.push_back("-start");
131 argv.push_back("-c");
132
133 std::string str = g_getOptions(argv);
134
135 if (!str.empty()) {
136 ret = true;
137 }
138
139 EXPECT_TRUE(ret);
140 }
141
142 HWTEST_F(SPdaemonMainTest, CheckCMDParamTestCase002, TestSize.Level1)
143 {
144 std::string errorInfo = "";
145 std::vector<std::string> argv;
146 argv.push_back("SP_daemon");
147 argv.push_back("-start");
148 argv.push_back("-fl");
149 argv.push_back("-ftl");
150
151 bool ret = CheckCMDParam(argv, errorInfo);
152 EXPECT_EQ(ret, false);
153 }
154
155 HWTEST_F(SPdaemonMainTest, CheckCMDParamTestCase003, TestSize.Level1)
156 {
157 std::string errorInfo = "";
158 std::vector<std::string> argv;
159 argv.push_back("SP_daemon");
160 argv.push_back("");
161
162 bool ret = CheckCMDParam(argv, errorInfo);
163 EXPECT_EQ(ret, true);
164 }
165
166 }
167 }