1 /** 2 * Copyright (c) 2024 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 <gtest/gtest.h> 17 18 #include "utils/string_helpers.h" 19 20 #include "configs/guard_args_parser.h" 21 22 using namespace testing::ext; 23 using namespace panda::guard; 24 25 /** 26 * @tc.name: guard_args_parser_test_001 27 * @tc.desc: test panda_guard args parse with abnormal args 28 * @tc.type: FUNC 29 * @tc.require: 30 */ 31 HWTEST(GuardArgsParserUnitTest, guard_args_parser_test_001, TestSize.Level4) 32 { 33 testing::internal::CaptureStderr(); 34 35 GuardArgsParser parser; 36 int argc = 2; 37 char *argv[2]; 38 argv[0] = const_cast<char *>("xxx"); 39 argv[1] = const_cast<char *>("--debug="); 40 bool ret = parser.Parse(argc, const_cast<const char **>(argv)); 41 EXPECT_EQ(ret, false); 42 43 std::string err = testing::internal::GetCapturedStderr(); 44 std::string res = panda::helpers::string::Format( 45 "Usage:\n" 46 "panda_guard [options] config-file-path\n" 47 "Supported options:\n" 48 "--debug: enable debug messages (will be printed to standard output if no --debug-file was specified)\n" 49 "--debug-file: (--debug-file FILENAME) set debug file name. default is std::cout\n" 50 "--help: Print this message and exit\n" 51 "Tail arguments:\n" 52 "config-file-path: configuration file path\n\n"); 53 EXPECT_EQ(err, res); 54 } 55 56 /** 57 * @tc.name: guard_args_parser_test_002 58 * @tc.desc: test panda_guard args parse with no debug file and config file args 59 * @tc.type: FUNC 60 * @tc.require: 61 */ 62 HWTEST(GuardArgsParserUnitTest, guard_args_parser_test_002, TestSize.Level4) 63 { 64 testing::internal::CaptureStderr(); 65 66 int argc = 3; 67 char *argv[3]; 68 argv[0] = const_cast<char *>("xxx"); 69 argv[1] = const_cast<char *>("--debug"); 70 argv[2] = const_cast<char *>("--debug-file"); 71 72 GuardArgsParser parser; 73 bool ret = parser.Parse(argc, const_cast<const char **>(argv)); 74 EXPECT_EQ(ret, false); 75 76 std::string err = testing::internal::GetCapturedStderr(); 77 std::string res = panda::helpers::string::Format( 78 "The config-file-path value is empty. Please check if the config-file-path is set correctly.\n"); 79 EXPECT_EQ(err, res); 80 }