!2337 Feat: saveparameters interface add test cases

Merge pull request !2337 from cjw123qq/master
This commit is contained in:
openharmony_ci 2023-11-09 11:32:52 +00:00 committed by Gitee
commit 0e6a8afa1d
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
10 changed files with 156 additions and 6 deletions

View File

@ -147,6 +147,7 @@
GetDistributionOSVersion;
GetDistributionOSApiVersion;
GetDistributionOSReleaseType;
SaveParameters;
local:
*;
};

View File

@ -107,11 +107,8 @@ int SystemWaitParameter(const char *name, const char *value, int32_t timeout)
int SystemSaveParameters(void)
{
uid_t processUid = getuid();
int ret = CheckIfUidInGroup(processUid, "servicectrl");
PARAM_CHECK(ret == 0, return ret, "Failed to process save parameters : ret %d", ret);
CheckAndSavePersistParam();
return ret;
return PARAM_CODE_SUCCESS;
}
int WatchParamCheck(const char *keyprefix)

View File

@ -786,9 +786,9 @@ INIT_LOCAL_API int CheckIfUidInGroup(const gid_t groupId, const char *groupCheck
{
PARAM_CHECK(groupCheckName != NULL, return -1, "Invalid groupCheckName");
struct group *groupName = getgrnam(groupCheckName);
PARAM_CHECK(groupName != NULL, return -1, "Not find %s", groupName);
PARAM_CHECK(groupName != NULL, return -1, "Not find %s group", groupCheckName);
char **gr_mem = groupName->gr_mem;
PARAM_CHECK(gr_mem != NULL, return -1, "No member in servicectrl");
PARAM_CHECK(gr_mem != NULL, return -1, "No member in %s", groupCheckName);
for (int i = 0; gr_mem[i] != NULL; ++i) {
struct group *userGroup = getgrnam(gr_mem[i]);
if (userGroup != NULL) {

View File

@ -3098,6 +3098,31 @@ ohos_fuzztest("RefreshWatcherFuzzTest") {
defines = [ "STARTUP_INIT_TEST" ]
}
ohos_fuzztest("SaveParametersFuzzTest") {
module_out_path = module_output_path
fuzz_config_file = "//base/startup/init/test/fuzztest/saveparameters_fuzzer"
include_dirs = [
"//base/startup/init/services/include/param",
"//base/startup/init/test/fuzztest/utils/include",
]
deps = [
"//base/startup/init/interfaces/innerkits:libbegetutil",
"//third_party/bounds_checking_function:libsec_static",
]
external_deps = [ "hilog:libhilog" ]
cflags = [
"-g",
"-O0",
"-Wno-unused-variable",
"-fno-omit-frame-pointer",
]
sources = [ "saveparameters_fuzzer/saveparameters_fuzzer.cpp" ]
defines = [ "STARTUP_INIT_TEST" ]
}
##############################################################################################
group("fuzztest") {
testonly = true

View File

@ -0,0 +1,16 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
FUZZ

View File

@ -0,0 +1,25 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (c) 2023 Huawei Device Co., Ltd.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<fuzz_config>
<fuzztest>
<!-- maximum length of a test input -->
<max_len>100</max_len>
<!-- maximum total time in seconds to run the fuzzer -->
<max_total_time>30</max_total_time>
<!-- memory usage limit in Mb -->
<rss_limit_mb>2048</rss_limit_mb>
</fuzztest>
</fuzz_config>

View File

@ -0,0 +1,37 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "saveparameters_fuzzer.h"
#include <string>
#include "parameter.h"
namespace OHOS {
bool FuzzSaveParameters(const uint8_t* data, size_t size)
{
bool result = false;
if (SaveParameters()) {
result = true;
};
return result;
}
}
/* Fuzzer entry point */
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
{
/* Run your code on data */
OHOS::FuzzSaveParameters(data, size);
return 0;
}

View File

@ -0,0 +1,20 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef TEST_FUZZTEST_GET_FIRST_API_VERSION_H
#define TEST_FUZZTEST_GET_FIRST_API_VERSION_H
#include "fuzz_utils.h"
#define FUZZ_PROJECT_NAME "saveparameters_fuzzer"
#endif

View File

@ -445,4 +445,19 @@ HWTEST_F(SysparaModuleTest, Syspara_CacheParameter_test_002, TestSize.Level0)
CachedParameterDestroy(cacheHandle3);
GTEST_LOG_(INFO) << "Syspara_CacheParameter_test_002 end";
}
HWTEST_F(SysparaModuleTest, Syspara_SaveParameters_test_001, TestSize.Level0)
{
GTEST_LOG_(INFO) << "Syspara_SaveParameters_test_001 start";
char key1[] = "test.param.int1";
char value1[] = "0x111111";
EXPECT_EQ(SetParameter(key1, value1), 0);
char key2[] = "persist.test.param.int2";
char value2[] = "-0x111111";
EXPECT_EQ(SetParameter(key2, value2), 0);
EXPECT_EQ(SaveParameters(), 0);
GTEST_LOG_(INFO) << "Syspara_SaveParameters_test_001 end";
}
}

View File

@ -388,4 +388,18 @@ HWTEST_F(SysparaUnitTest, parameterTest0017, TestSize.Level0)
EXPECT_STRNE(GetDistributionOSReleaseType(), nullptr);
}
#endif
HWTEST_F(SysparaUnitTest, parameterTest0018, TestSize.Level0)
{
char key1[] = "test.ro.sys.version";
char value1[] = "set read only key";
int ret = SetParameter(key1, value1);
EXPECT_EQ(ret, EC_SUCCESS);
char key2[] = "persist.test.ro.sys.version";
char value2[] = "set persist read only key";
ret = SetParameter(key2, value2);
EXPECT_EQ(ret, EC_SUCCESS);
ret = SaveParameters();
EXPECT_EQ(ret, 0);
}
} // namespace OHOS