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 <cerrno>
16#include <dirent.h>
17#include <fstream>
18#include <unistd.h>
19#include <sys/stat.h>
20#include <sys/types.h>
21#include <fcntl.h>
22#include "param_stub.h"
23#include "init_utils.h"
24#include "ueventd_read_cfg.h"
25#include "ueventd_parameter.h"
26
27extern "C" {
28bool IsMatch(const char *target, const char *pattern);
29}
30
31using namespace std;
32using namespace testing::ext;
33
34namespace UeventdUt {
35class UeventdConfigUnitTest : public testing::Test {
36public:
37static void SetUpTestCase(void) {};
38static void TearDownTestCase(void) {};
39void SetUp() {};
40void TearDown() {};
41};
42
43HWTEST_F(UeventdConfigUnitTest, Init_UeventdConfigTest_SectionConfigParse001, TestSize.Level0)
44{
45    ParseUeventdConfigFile(STARTUP_INIT_UT_PATH"/ueventd_ut/valid.config");
46    uid_t uid = 0;
47    gid_t gid = 0;
48    mode_t mode = 0;
49    GetDeviceNodePermissions("/dev/test", &uid, &gid, &mode);
50    EXPECT_EQ(uid, 1000);
51    EXPECT_EQ(gid, 1000);
52    EXPECT_EQ(mode, 0666);
53    uid = 999;
54    gid = 999;
55    mode = 0777;
56    // /dev/test1 is not a valid item, miss gid
57    // UGO will be default value.
58    GetDeviceNodePermissions("/dev/test1", &uid, &gid, &mode);
59    EXPECT_EQ(uid, 999);
60    EXPECT_EQ(gid, 999);
61    EXPECT_EQ(mode, 0777);
62    // /dev/test2 is not a valid item, too much items
63    // UGO will be default value.
64    GetDeviceNodePermissions("/dev/test2", &uid, &gid, &mode);
65    EXPECT_EQ(uid, 999);
66    EXPECT_EQ(gid, 999);
67    EXPECT_EQ(mode, 0777);
68    ChangeSysAttributePermissions("/dev/test2");
69    ChangeSysAttributePermissions("/dir/to/nothing");
70}
71
72HWTEST_F(UeventdConfigUnitTest, Init_UeventdConfigTest_ConfigEntry001, TestSize.Level0)
73{
74    string file = "[device";
75    int rc = ParseUeventConfig(const_cast<char*>(file.c_str())); // Invalid section
76    EXPECT_EQ(rc, -1);
77    file = "[unknown]";
78    rc = ParseUeventConfig(const_cast<char*>(file.c_str()));  // Unknown section
79    EXPECT_EQ(rc, -1);
80    file = "[device]";
81    rc = ParseUeventConfig(const_cast<char*>(file.c_str())); // valid section
82    EXPECT_EQ(rc, 0);
83}
84
85HWTEST_F(UeventdConfigUnitTest, Init_UeventdConfigTest_Parameter001, TestSize.Level0)
86{
87    ParseUeventdConfigFile(STARTUP_INIT_UT_PATH"/ueventd_ut/valid.config");
88    SetUeventDeviceParameter("/dev/testbinder1", 0);
89    SetUeventDeviceParameter("/dev/testbinder2", 0);
90    SetUeventDeviceParameter("/dev/testbinder3", 0);
91    SetUeventDeviceParameter("/notpath", 0);
92    SetUeventDeviceParameter(nullptr, 1);
93    GetDeviceUdevConfByDevNode(nullptr);
94    int ret = IsMatch("testtarget", "te?*a");
95    IsMatch("testtarget", "a");
96    ret = IsMatch("test", "t****");
97    EXPECT_EQ(ret, true);
98}
99} // namespace UeventdUt
100