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 16 #include <errno.h> 17 #include <gtest/gtest.h> 18 #include "log.h" 19 #include "utils.h" 20 #include "libfs.h" 21 #include "KernelConstants.h" 22 23 using namespace testing::ext; 24 25 #define RES_DIR_DYLOAD RES_DIR_KERNEL "dyload/" 26 27 class ExecveTest : public testing::Test { 28 }; 29 30 /** 31 * @tc.number SUB_KERNEL_DL_ELF_0100 32 * @tc.name execve fixed address elf, non-pie elf 33 * @tc.desc [C- SOFTWARE -0200] 34 */ 35 HWTEST_F(ExecveTest, testExecFixedAddressELF, Function | MediumTest | Level1) 36 { 37 char* testELF = RES_DIR_DYLOAD "hello_nopie"; 38 int rt = RunElf(testELF, NULL, NULL); 39 EXPECT_EQ(rt, 0) << "fixed address elf test failed! exitcode=" << rt; 40 } 41 42 /** 43 * @tc.number SUB_KERNEL_DL_ELF_0200 44 * @tc.name execve static link elf 45 * @tc.desc [C- SOFTWARE -0200] 46 */ 47 HWTEST_F(ExecveTest, testExecStaticELF, Function | MediumTest | Level1) 48 { 49 char* testELF = RES_DIR_DYLOAD "hello_static"; 50 int rt = RunElf(testELF, NULL, NULL); 51 EXPECT_EQ(rt, 0) << "static link elf test failed! exitcode=" << rt; 52 } 53 54 55 /** 56 * @tc.number SUB_KERNEL_DL_Execve_fname_0100 57 * @tc.name execve parameter1 fname test 58 * @tc.desc [C- SOFTWARE -0200] 59 */ 60 HWTEST_F(ExecveTest, testParaFilename, Function | MediumTest | Level1) 61 { 62 char* resELF = RES_DIR_DYLOAD "executor1"; 63 char* testELF1 = resELF; 64 char* testELF2 = "/storage/executor2"; 65 char* testELF3 = "/storage/data/executor3"; 66 67 // test SetUp 68 ASSERT_EQ(CopyFile(testELF1, testELF2), 0); 69 EXPECT_EQ(CopyFile(testELF1, testELF3), 0); 70 int rt = chmod(testELF2, 0777); 71 if (rt) { 72 // chmod may fail if DAC is not enabled, so this failure is not 'record' 73 LOG("chmod '%s' failed: rt=%d, errno=%d", testELF2, rt, errno); 74 } 75 rt = chmod(testELF3, 0777); 76 if (rt) { 77 LOG("chmod '%s' failed: rt=%d, errno=%d", testELF3, rt, errno); 78 } 79 char* curPath = GetCurrentPath(); 80 EXPECT_EQ(chdir("/storage/data"), 0); 81 LOG("SetUp ok"); 82 83 // test 84 rt = RunElf(testELF1, NULL, NULL); 85 EXPECT_EQ(rt, 0) << "execve absolute path elf failed! exitcode=" << rt; 86 87 rt = RunElf("./executor3", NULL, NULL); 88 EXPECT_EQ(rt, 0) << "execve relative-path1 elf failed! exitcode=" << rt; 89 rt = RunElf("../data/executor3", NULL, NULL); 90 EXPECT_EQ(rt, 0) << "execve relative-path2 elf failed! exitcode=" << rt; 91 92 rt = RunElf("../executor2", NULL, NULL); 93 EXPECT_EQ(rt, 0) << "execve upper-path-file failed! exitcode=" << rt; 94 95 rt = StartExecveError("./executorNoExist", NULL, NULL); 96 EXPECT_EQ(rt, ENOENT) << "execve non-exist-elf errno failed! errno=" << rt; 97 98 // test TearDown 99 EXPECT_EQ(RemoveFile(testELF2), 0); 100 EXPECT_EQ(RemoveFile(testELF3), 0); 101 ASSERT_EQ(chdir(curPath), 0); 102 LOG("TearDown ok "); 103 } 104 105 /** 106 * @tc.number SUB_KERNEL_DL_Execve_argv_0100 107 * @tc.name execve parameter2 argv test 108 * @tc.desc [C- SOFTWARE -0200] 109 */ 110 HWTEST_F(ExecveTest, testParaArgv, Function | MediumTest | Level1) 111 { 112 int rt; 113 char* resELF = RES_DIR_DYLOAD "executor2"; 114 115 char *arg[] = {"1", "!@#$%^&*()_+", NULL}; 116 rt = RunElf(resELF, arg, NULL); 117 EXPECT_FALSE(rt) << "execve elf with normal argv parameter failed !"; 118 sleep(1); 119 120 char *arg2[] = {"2", "aaaa", "bbbb", NULL}; 121 rt = RunElf(resELF, arg2, NULL); 122 EXPECT_FALSE(rt) << "execve elf with normal argv parameter failed !"; 123 sleep(1); 124 125 char *arg3[] = {"3", "-f", "/home/result/log.txt", "-d", "!@#$%^&*()_+", NULL}; 126 rt = RunElf(resELF, arg3, NULL); 127 EXPECT_FALSE(rt) << "execve elf with normal argv parameter failed !"; 128 } 129 130 /** 131 * @tc.number SUB_KERNEL_DL_Execve_envp_0100 132 * @tc.name execve parameter3 envp test 133 * @tc.desc [C- SOFTWARE -0200] 134 */ 135 HWTEST_F(ExecveTest, testParaEnvp, Function | MediumTest | Level1) 136 { 137 int rt; 138 char* resELF = RES_DIR_DYLOAD "executor2"; 139 140 char *arg[] = {"1", "!@#$%^&*()_+", NULL}; 141 rt = RunElf(resELF, NULL, arg); 142 EXPECT_FALSE(rt) << "execve elf with normal envp parameter failed !"; 143 sleep(1); 144 145 char *arg2[]={"2", "aaaa", "bbbb", NULL}; 146 rt = RunElf(resELF, NULL, arg2); 147 EXPECT_FALSE(rt) << "execve elf with normal envp parameter failed !"; 148 sleep(1); 149 150 char *arg3[] = {"3", "-f", "/home/result/log.txt", "-d", "!@#$%^&*()_+", NULL}; 151 rt = RunElf(resELF, NULL, arg3); 152 EXPECT_FALSE(rt) << "execve elf with normal envp parameter failed !"; 153 }