Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
OpenHarmony
Startup Init Lite
提交
f2416fae
S
Startup Init Lite
项目概览
OpenHarmony
/
Startup Init Lite
1 年多 前同步成功
通知
3
Star
37
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Startup Init Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
f2416fae
编写于
5月 24, 2023
作者:
O
openharmony_ci
提交者:
Gitee
5月 24, 2023
浏览文件
操作
浏览文件
下载
差异文件
!1973 添加root包ut
Merge pull request !1973 from cheng_jinsong/init_ut
上级
836cc267
3461d269
变更
4
隐藏空白更改
内联
并排
Showing
4 changed file
with
298 addition
and
16 deletion
+298
-16
services/modules/init_eng/init_eng.c
services/modules/init_eng/init_eng.c
+8
-15
services/modules/init_eng/init_eng.h
services/modules/init_eng/init_eng.h
+34
-0
test/unittest/BUILD.gn
test/unittest/BUILD.gn
+4
-1
test/unittest/modules/eng_unittest.cpp
test/unittest/modules/eng_unittest.cpp
+252
-0
未找到文件。
services/modules/init_eng/init_eng.c
浏览文件 @
f2416fae
...
...
@@ -13,6 +13,8 @@
* limitations under the License.
*/
#include "init_eng.h"
#include <dirent.h>
#include <limits.h>
#include <sys/mount.h>
...
...
@@ -23,19 +25,10 @@
#include "init_module_engine.h"
#include "securec.h"
#define MOUNT_CMD_MAX_LEN 128U
#define ENG_SYSTEM_DEVICE_PATH "/dev/block/by-name/eng_system"
#define ENG_CHIPSET_DEVICE_PATH "/dev/block/by-name/eng_chipset"
typedef
enum
{
TYPE_DIR
=
0
,
TYPE_REG
,
TYPE_LINK
,
TYPE_ANY
}
FileType
;
static
bool
IsFileExistWithType
(
const
char
*
file
,
FileType
type
)
ENG_STATIC
bool
IsFileExistWithType
(
const
char
*
file
,
FileType
type
)
{
bool
isExist
=
false
;
struct
stat
st
=
{};
...
...
@@ -75,7 +68,7 @@ static bool IsExistFile(const char *file)
return
file
==
NULL
?
false
:
IsFileExistWithType
(
file
,
TYPE_ANY
);
}
static
void
BuildMountCmd
(
char
*
buffer
,
size_t
len
,
const
char
*
mp
,
const
char
*
dev
,
const
char
*
fstype
)
ENG_STATIC
void
BuildMountCmd
(
char
*
buffer
,
size_t
len
,
const
char
*
mp
,
const
char
*
dev
,
const
char
*
fstype
)
{
int
ret
=
snprintf_s
(
buffer
,
len
,
len
-
1
,
"%s %s %s ro barrier=1"
,
fstype
,
dev
,
mp
);
...
...
@@ -85,7 +78,7 @@ static void BuildMountCmd(char *buffer, size_t len, const char *mp, const char *
}
}
static
void
MountEngPartitions
(
void
)
ENG_STATIC
void
MountEngPartitions
(
void
)
{
char
mountCmd
[
MOUNT_CMD_MAX_LEN
]
=
{};
...
...
@@ -102,7 +95,7 @@ static void MountEngPartitions(void)
DoCmdByName
(
"mount "
,
mountCmd
);
}
static
void
BindMountFile
(
const
char
*
source
,
const
char
*
target
)
ENG_STATIC
void
BindMountFile
(
const
char
*
source
,
const
char
*
target
)
{
char
targetFullPath
[
PATH_MAX
]
=
{};
const
char
*
p
=
source
;
...
...
@@ -148,7 +141,7 @@ static void BindMountFile(const char *source, const char *target)
}
}
static
void
DebugFilesOverlay
(
const
char
*
source
,
const
char
*
target
)
ENG_STATIC
void
DebugFilesOverlay
(
const
char
*
source
,
const
char
*
target
)
{
DIR
*
dir
=
NULL
;
struct
dirent
*
de
=
NULL
;
...
...
@@ -185,7 +178,7 @@ static void DebugFilesOverlay(const char *source, const char *target)
dir
=
NULL
;
}
static
void
EngineerOverlay
(
void
)
ENG_STATIC
void
EngineerOverlay
(
void
)
{
PLUGIN_LOGI
(
"system overlay..."
);
DebugFilesOverlay
(
"/eng_system"
,
"/"
);
...
...
services/modules/init_eng/init_eng.h
0 → 100644
浏览文件 @
f2416fae
/*
* Copyright (c) 2022 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 _PLUGIN_INITENG_H
#define _PLUGIN_INITENG_H
typedef
enum
{
TYPE_DIR
=
0
,
TYPE_REG
,
TYPE_LINK
,
TYPE_ANY
}
FileType
;
#define MOUNT_CMD_MAX_LEN 128U
#ifdef STARTUP_INIT_TEST
#define ENG_STATIC
#else
#define ENG_STATIC static
#endif
#endif
/* _PLUGIN_INITENG_H */
test/unittest/BUILD.gn
浏览文件 @
f2416fae
...
...
@@ -25,7 +25,6 @@ config("utest_config") {
"-Dmount=MountStub",
"-Dumount=UmountStub",
"-Dumount2=Umount2Stub",
"-Dsymlink=SymlinkStub",
"-Dprctl=PrctlStub",
"-Dexecv=ExecvStub",
"-Dlchown=LchownStub",
...
...
@@ -105,6 +104,8 @@ ohos_unittest("init_unittest") {
"//base/startup/init/services/modules/bootchart/bootchart.c",
"//base/startup/init/services/modules/bootchart/bootchart_static.c",
"//base/startup/init/services/modules/bootevent/bootevent.c",
"//base/startup/init/services/modules/init_eng/init_eng.c",
"//base/startup/init/services/modules/init_eng/init_eng_static.c",
"//base/startup/init/services/modules/init_hook/init_hook.c",
"//base/startup/init/services/modules/init_hook/param_hook.c",
"//base/startup/init/services/modules/reboot/reboot.c",
...
...
@@ -181,6 +182,7 @@ ohos_unittest("init_unittest") {
"innerkits/innerkits_unittest.cpp",
"loopevent/loopevent_unittest.cpp",
"loopevent/loopsignal_unittest.cpp",
"modules/eng_unittest.cpp",
"modules/modules_unittest.cpp",
"param/client_unittest.cpp",
"param/dac_unittest.cpp",
...
...
@@ -236,6 +238,7 @@ ohos_unittest("init_unittest") {
"//base/startup/init/services/modules/selinux",
"//base/startup/init/services/modules/reboot",
"//base/startup/init/services/modules/udid",
"//base/startup/init/services/modules/init_eng",
"//base/startup/init/services/loopevent/utils",
"//base/startup/init/services/param/adapter",
"//base/startup/init/services/param/base",
...
...
test/unittest/modules/eng_unittest.cpp
0 → 100644
浏览文件 @
f2416fae
/*
* Copyright (c) 2022 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 <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <dirent.h>
#include "init_eng.h"
#include "init_utils.h"
#include "param_stub.h"
#include "bootstage.h"
#include "securec.h"
using
namespace
std
;
using
namespace
testing
::
ext
;
namespace
init_ut
{
extern
"C"
{
void
EngineerOverlay
(
void
);
void
DebugFilesOverlay
(
const
char
*
source
,
const
char
*
target
);
void
BindMountFile
(
const
char
*
source
,
const
char
*
target
);
void
MountEngPartitions
(
void
);
void
BuildMountCmd
(
char
*
buffer
,
size_t
len
,
const
char
*
mp
,
const
char
*
dev
,
const
char
*
fstype
);
bool
IsFileExistWithType
(
const
char
*
file
,
FileType
type
);
}
static
const
std
::
string
g_srcFilePath
=
STARTUP_INIT_UT_PATH
"/eng/source/test.txt"
;
static
const
std
::
string
g_targetPath
=
STARTUP_INIT_UT_PATH
"/eng/link_name"
;
static
const
std
::
string
g_engRootPath
=
STARTUP_INIT_UT_PATH
"/eng/"
;
static
bool
RemoveDir
(
const
std
::
string
&
path
)
{
if
(
path
.
empty
())
{
return
false
;
}
std
::
string
strPath
=
path
;
if
(
strPath
.
at
(
strPath
.
length
()
-
1
)
!=
'/'
)
{
strPath
.
append
(
"/"
);
}
DIR
*
d
=
opendir
(
strPath
.
c_str
());
if
(
d
!=
nullptr
)
{
struct
dirent
*
dt
=
nullptr
;
dt
=
readdir
(
d
);
while
(
dt
!=
nullptr
)
{
if
(
strcmp
(
dt
->
d_name
,
".."
)
==
0
||
strcmp
(
dt
->
d_name
,
"."
)
==
0
)
{
dt
=
readdir
(
d
);
continue
;
}
struct
stat
st
{};
auto
file_name
=
strPath
+
std
::
string
(
dt
->
d_name
);
stat
(
file_name
.
c_str
(),
&
st
);
if
(
S_ISDIR
(
st
.
st_mode
))
{
RemoveDir
(
file_name
);
}
else
{
remove
(
file_name
.
c_str
());
}
dt
=
readdir
(
d
);
}
closedir
(
d
);
}
return
rmdir
(
strPath
.
c_str
())
==
0
?
true
:
false
;
}
static
bool
IsFileExist
(
const
std
::
string
&
path
)
{
if
(
path
.
empty
())
{
return
false
;
}
struct
stat
st
{};
if
(
stat
(
path
.
c_str
(),
&
st
)
==
0
&&
S_ISREG
(
st
.
st_mode
))
{
return
true
;
}
return
false
;
}
static
bool
IsDirExist
(
const
std
::
string
&
path
)
{
if
(
path
.
empty
())
{
return
false
;
}
struct
stat
st
{};
if
(
stat
(
path
.
c_str
(),
&
st
)
==
0
&&
S_ISDIR
(
st
.
st_mode
))
{
return
true
;
}
return
false
;
}
class
EngUnitTest
:
public
testing
::
Test
{
public:
static
void
SetUpTestCase
(
void
)
{};
static
void
TearDownTestCase
(
void
)
{};
void
SetUp
()
{};
void
TearDown
()
{};
};
HWTEST_F
(
EngUnitTest
,
TestFilesOverlay
,
TestSize
.
Level1
)
{
bool
isDel
=
false
;
bool
isExist
=
IsDirExist
(
g_engRootPath
.
c_str
());
if
(
isExist
)
{
isDel
=
RemoveDir
(
g_engRootPath
.
c_str
());
EXPECT_EQ
(
isDel
,
true
);
}
isExist
=
IsDirExist
(
g_targetPath
.
c_str
());
if
(
isExist
)
{
isDel
=
RemoveDir
(
g_targetPath
.
c_str
());
EXPECT_EQ
(
isDel
,
true
);
}
DebugFilesOverlay
(
g_engRootPath
.
c_str
(),
g_targetPath
.
c_str
());
CreateTestFile
(
g_srcFilePath
.
c_str
(),
"test"
);
isExist
=
IsFileExist
(
g_srcFilePath
.
c_str
());
EXPECT_EQ
(
isExist
,
true
);
DebugFilesOverlay
(
g_srcFilePath
.
c_str
(),
g_targetPath
.
c_str
());
isExist
=
IsFileExistWithType
(
g_srcFilePath
.
c_str
(),
TYPE_REG
);
EXPECT_EQ
(
isExist
,
true
);
if
(
IsFileExistWithType
(
g_targetPath
.
c_str
(),
TYPE_LINK
))
{
if
(
unlink
(
g_targetPath
.
c_str
())
<
0
)
{
EXPECT_TRUE
(
false
);
}
}
int
ret
=
symlink
(
g_engRootPath
.
c_str
(),
g_targetPath
.
c_str
());
EXPECT_EQ
(
ret
,
0
);
isExist
=
IsFileExistWithType
(
g_targetPath
.
c_str
(),
TYPE_LINK
);
EXPECT_EQ
(
isExist
,
true
);
DebugFilesOverlay
(
g_targetPath
.
c_str
(),
g_engRootPath
.
c_str
());
EXPECT_EQ
(
ret
,
0
);
}
HWTEST_F
(
EngUnitTest
,
TestBindMountFile
,
TestSize
.
Level1
)
{
BindMountFile
(
"data/init_ut"
,
""
);
BindMountFile
(
"data"
,
"target"
);
BindMountFile
(
"/data/init_ut//"
,
"/"
);
BindMountFile
(
"/data/init_ut"
,
"/"
);
bool
isExist
=
false
;
if
(
!
IsFileExist
(
g_srcFilePath
.
c_str
()))
{
CreateTestFile
(
g_srcFilePath
.
c_str
(),
"test reg file mount"
);
isExist
=
IsFileExist
(
g_srcFilePath
.
c_str
());
EXPECT_EQ
(
isExist
,
true
);
BindMountFile
(
g_srcFilePath
.
c_str
(),
"/"
);
}
if
(
IsFileExist
(
g_srcFilePath
.
c_str
()))
{
RemoveDir
(
STARTUP_INIT_UT_PATH
"/eng/source"
);
isExist
=
IsFileExist
(
g_srcFilePath
.
c_str
());
EXPECT_EQ
(
isExist
,
false
);
}
if
(
IsFileExistWithType
(
g_targetPath
.
c_str
(),
TYPE_LINK
))
{
if
(
unlink
(
g_targetPath
.
c_str
())
<
0
)
{
EXPECT_TRUE
(
false
);
}
}
bool
isLinkFile
=
IsFileExist
(
g_targetPath
.
c_str
());
EXPECT_EQ
(
isLinkFile
,
false
);
BindMountFile
(
g_srcFilePath
.
c_str
(),
g_targetPath
.
c_str
());
int
ret
=
symlink
(
g_srcFilePath
.
c_str
(),
g_targetPath
.
c_str
());
EXPECT_EQ
(
ret
,
0
);
isLinkFile
=
IsFileExistWithType
(
g_targetPath
.
c_str
(),
TYPE_LINK
);
EXPECT_EQ
(
isLinkFile
,
true
);
BindMountFile
(
g_srcFilePath
.
c_str
(),
g_targetPath
.
c_str
());
if
(
!
IsDirExist
(
g_engRootPath
.
c_str
()))
{
CheckAndCreateDir
(
STARTUP_INIT_UT_PATH
"/eng/"
);
}
BindMountFile
(
g_engRootPath
.
c_str
(),
g_targetPath
.
c_str
());
}
HWTEST_F
(
EngUnitTest
,
TestMountCmd
,
TestSize
.
Level1
)
{
char
mountCmd
[
MOUNT_CMD_MAX_LEN
]
=
{};
MountEngPartitions
();
BuildMountCmd
(
mountCmd
,
MOUNT_CMD_MAX_LEN
,
"/eng/source"
,
"/eng/target"
,
"ext4"
);
}
HWTEST_F
(
EngUnitTest
,
TestFileType
,
TestSize
.
Level1
)
{
std
::
string
targetFile
=
"/data/init_ut/eng/target_file"
;
std
::
string
linkName
=
"/data/init_ut/eng/link_name_test"
;
bool
isExist
=
false
;
if
(
!
IsFileExist
(
g_srcFilePath
.
c_str
()))
{
CreateTestFile
(
g_srcFilePath
.
c_str
(),
"test"
);
isExist
=
IsFileExist
(
g_srcFilePath
.
c_str
());
EXPECT_EQ
(
isExist
,
true
);
}
EXPECT_EQ
(
IsFileExistWithType
(
STARTUP_INIT_UT_PATH
"/eng"
,
TYPE_DIR
),
true
);
EXPECT_EQ
(
IsFileExistWithType
(
g_srcFilePath
.
c_str
(),
TYPE_REG
),
true
);
if
(
IsFileExist
(
targetFile
))
{
if
(
unlink
(
targetFile
.
c_str
())
<
0
)
{
std
::
cout
<<
"Failed to unlink file "
<<
targetFile
<<
" err = "
<<
errno
<<
std
::
endl
;
EXPECT_TRUE
(
false
);
}
}
int
fd
=
open
(
targetFile
.
c_str
(),
O_CREAT
|
O_CLOEXEC
|
O_WRONLY
,
0644
);
if
(
fd
<
0
)
{
std
::
cout
<<
"Failed to create file "
<<
targetFile
<<
" err = "
<<
errno
<<
std
::
endl
;
EXPECT_TRUE
(
false
);
}
else
{
std
::
string
buffer
=
"hello"
;
write
(
fd
,
buffer
.
c_str
(),
buffer
.
size
());
close
(
fd
);
// avoid leak
}
if
(
IsFileExist
(
linkName
))
{
if
(
unlink
(
linkName
.
c_str
())
<
0
)
{
std
::
cout
<<
"Failed to unlink file "
<<
linkName
<<
" err = "
<<
errno
<<
std
::
endl
;
EXPECT_TRUE
(
false
);
}
}
int
ret
=
symlink
(
targetFile
.
c_str
(),
linkName
.
c_str
());
EXPECT_EQ
(
ret
,
0
);
bool
isFileExist
=
IsFileExistWithType
(
linkName
.
c_str
(),
TYPE_LINK
);
EXPECT_EQ
(
isFileExist
,
true
);
isFileExist
=
IsFileExistWithType
(
"/eng/target"
,
TYPE_LINK
);
EXPECT_EQ
(
isFileExist
,
false
);
isFileExist
=
IsFileExistWithType
(
"/eng/target"
,
TYPE_REG
);
EXPECT_EQ
(
isFileExist
,
false
);
}
HWTEST_F
(
EngUnitTest
,
TestHook
,
TestSize
.
Level1
)
{
SystemWriteParam
(
"ohos.boot.eng_mode"
,
"on"
);
HookMgrExecute
(
GetBootStageHookMgr
(),
INIT_GLOBAL_INIT
,
NULL
,
NULL
);
}
}
// namespace init_ut
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录