1d9f0492fSopenharmony_ci/*
2d9f0492fSopenharmony_ci * Copyright (c) 2021 Huawei Device Co., Ltd.
3d9f0492fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
4d9f0492fSopenharmony_ci * you may not use this file except in compliance with the License.
5d9f0492fSopenharmony_ci * You may obtain a copy of the License at
6d9f0492fSopenharmony_ci *
7d9f0492fSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
8d9f0492fSopenharmony_ci *
9d9f0492fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
10d9f0492fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
11d9f0492fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12d9f0492fSopenharmony_ci * See the License for the specific language governing permissions and
13d9f0492fSopenharmony_ci * limitations under the License.
14d9f0492fSopenharmony_ci */
15d9f0492fSopenharmony_ci#include <cerrno>
16d9f0492fSopenharmony_ci#include <dirent.h>
17d9f0492fSopenharmony_ci#include <fstream>
18d9f0492fSopenharmony_ci#include <unistd.h>
19d9f0492fSopenharmony_ci#include <sys/stat.h>
20d9f0492fSopenharmony_ci#include <sys/types.h>
21d9f0492fSopenharmony_ci#include <fcntl.h>
22d9f0492fSopenharmony_ci#include "param_stub.h"
23d9f0492fSopenharmony_ci#include "init_utils.h"
24d9f0492fSopenharmony_ci#include "ueventd_read_cfg.h"
25d9f0492fSopenharmony_ci#include "ueventd_parameter.h"
26d9f0492fSopenharmony_ci
27d9f0492fSopenharmony_ciextern "C" {
28d9f0492fSopenharmony_cibool IsMatch(const char *target, const char *pattern);
29d9f0492fSopenharmony_ci}
30d9f0492fSopenharmony_ci
31d9f0492fSopenharmony_ciusing namespace std;
32d9f0492fSopenharmony_ciusing namespace testing::ext;
33d9f0492fSopenharmony_ci
34d9f0492fSopenharmony_cinamespace UeventdUt {
35d9f0492fSopenharmony_ciclass UeventdConfigUnitTest : public testing::Test {
36d9f0492fSopenharmony_cipublic:
37d9f0492fSopenharmony_cistatic void SetUpTestCase(void) {};
38d9f0492fSopenharmony_cistatic void TearDownTestCase(void) {};
39d9f0492fSopenharmony_civoid SetUp() {};
40d9f0492fSopenharmony_civoid TearDown() {};
41d9f0492fSopenharmony_ci};
42d9f0492fSopenharmony_ci
43d9f0492fSopenharmony_ciHWTEST_F(UeventdConfigUnitTest, Init_UeventdConfigTest_SectionConfigParse001, TestSize.Level0)
44d9f0492fSopenharmony_ci{
45d9f0492fSopenharmony_ci    ParseUeventdConfigFile(STARTUP_INIT_UT_PATH"/ueventd_ut/valid.config");
46d9f0492fSopenharmony_ci    uid_t uid = 0;
47d9f0492fSopenharmony_ci    gid_t gid = 0;
48d9f0492fSopenharmony_ci    mode_t mode = 0;
49d9f0492fSopenharmony_ci    GetDeviceNodePermissions("/dev/test", &uid, &gid, &mode);
50d9f0492fSopenharmony_ci    EXPECT_EQ(uid, 1000);
51d9f0492fSopenharmony_ci    EXPECT_EQ(gid, 1000);
52d9f0492fSopenharmony_ci    EXPECT_EQ(mode, 0666);
53d9f0492fSopenharmony_ci    uid = 999;
54d9f0492fSopenharmony_ci    gid = 999;
55d9f0492fSopenharmony_ci    mode = 0777;
56d9f0492fSopenharmony_ci    // /dev/test1 is not a valid item, miss gid
57d9f0492fSopenharmony_ci    // UGO will be default value.
58d9f0492fSopenharmony_ci    GetDeviceNodePermissions("/dev/test1", &uid, &gid, &mode);
59d9f0492fSopenharmony_ci    EXPECT_EQ(uid, 999);
60d9f0492fSopenharmony_ci    EXPECT_EQ(gid, 999);
61d9f0492fSopenharmony_ci    EXPECT_EQ(mode, 0777);
62d9f0492fSopenharmony_ci    // /dev/test2 is not a valid item, too much items
63d9f0492fSopenharmony_ci    // UGO will be default value.
64d9f0492fSopenharmony_ci    GetDeviceNodePermissions("/dev/test2", &uid, &gid, &mode);
65d9f0492fSopenharmony_ci    EXPECT_EQ(uid, 999);
66d9f0492fSopenharmony_ci    EXPECT_EQ(gid, 999);
67d9f0492fSopenharmony_ci    EXPECT_EQ(mode, 0777);
68d9f0492fSopenharmony_ci    ChangeSysAttributePermissions("/dev/test2");
69d9f0492fSopenharmony_ci    ChangeSysAttributePermissions("/dir/to/nothing");
70d9f0492fSopenharmony_ci}
71d9f0492fSopenharmony_ci
72d9f0492fSopenharmony_ciHWTEST_F(UeventdConfigUnitTest, Init_UeventdConfigTest_ConfigEntry001, TestSize.Level0)
73d9f0492fSopenharmony_ci{
74d9f0492fSopenharmony_ci    string file = "[device";
75d9f0492fSopenharmony_ci    int rc = ParseUeventConfig(const_cast<char*>(file.c_str())); // Invalid section
76d9f0492fSopenharmony_ci    EXPECT_EQ(rc, -1);
77d9f0492fSopenharmony_ci    file = "[unknown]";
78d9f0492fSopenharmony_ci    rc = ParseUeventConfig(const_cast<char*>(file.c_str()));  // Unknown section
79d9f0492fSopenharmony_ci    EXPECT_EQ(rc, -1);
80d9f0492fSopenharmony_ci    file = "[device]";
81d9f0492fSopenharmony_ci    rc = ParseUeventConfig(const_cast<char*>(file.c_str())); // valid section
82d9f0492fSopenharmony_ci    EXPECT_EQ(rc, 0);
83d9f0492fSopenharmony_ci}
84d9f0492fSopenharmony_ci
85d9f0492fSopenharmony_ciHWTEST_F(UeventdConfigUnitTest, Init_UeventdConfigTest_Parameter001, TestSize.Level0)
86d9f0492fSopenharmony_ci{
87d9f0492fSopenharmony_ci    ParseUeventdConfigFile(STARTUP_INIT_UT_PATH"/ueventd_ut/valid.config");
88d9f0492fSopenharmony_ci    SetUeventDeviceParameter("/dev/testbinder1", 0);
89d9f0492fSopenharmony_ci    SetUeventDeviceParameter("/dev/testbinder2", 0);
90d9f0492fSopenharmony_ci    SetUeventDeviceParameter("/dev/testbinder3", 0);
91d9f0492fSopenharmony_ci    SetUeventDeviceParameter("/notpath", 0);
92d9f0492fSopenharmony_ci    SetUeventDeviceParameter(nullptr, 1);
93d9f0492fSopenharmony_ci    GetDeviceUdevConfByDevNode(nullptr);
94d9f0492fSopenharmony_ci    int ret = IsMatch("testtarget", "te?*a");
95d9f0492fSopenharmony_ci    IsMatch("testtarget", "a");
96d9f0492fSopenharmony_ci    ret = IsMatch("test", "t****");
97d9f0492fSopenharmony_ci    EXPECT_EQ(ret, true);
98d9f0492fSopenharmony_ci}
99d9f0492fSopenharmony_ci} // namespace UeventdUt
100