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 <gtest/gtest.h>
17 #include <cstddef>
18 #include <cstdint>
19 #include <iostream>
20 #include <cstdio>
21 #include <ctime>
22 #include <memory>
23 #include <cstring>
24 #include <securec.h>
25 #include <unistd.h>
26 #include <sys/types.h>
27 #include <sys/ioctl.h>
28 #include "load_sec_file.h"
29 #include "tee_client_app_load.h"
30 #include "tee_client_socket.h"
31
32 using namespace testing::ext;
33 class LoadSecfileTest : public testing::Test {
34 public:
35 static void SetUpTestCase(void);
36 static void TearDownTestCase(void);
37 void SetUp();
38 void TearDown();
39 };
40
SetUpTestCase(void)41 void LoadSecfileTest::SetUpTestCase(void)
42 {
43 printf("SetUp\n");
44 }
45
TearDownTestCase(void)46 void LoadSecfileTest::TearDownTestCase(void)
47 {
48 printf("TearDownTestCase\n");
49 }
50
SetUp(void)51 void LoadSecfileTest::SetUp(void)
52 {
53 printf("SetUpTestCase\n");
54 }
55
TearDown(void)56 void LoadSecfileTest::TearDown(void)
57 {
58 printf("TearDown\n");
59 }
60
61 namespace {
62 HWTEST_F(LoadSecfileTest, LoadSecfile_001, TestSize.Level1)
63 {
64 int tzFd = -1;
65 int ret = 0;
66
67 ret = LoadSecFile(tzFd, nullptr, LOAD_TA, nullptr);
68 EXPECT_TRUE(ret != 0);
69
70 tzFd = 0;
71 ret = LoadSecFile(tzFd, nullptr, LOAD_TA, nullptr);
72 EXPECT_TRUE(ret != 0);
73 }
74 #define MAX_BUFFER_LEN (8 * 1024 * 1024)
75 HWTEST_F(LoadSecfileTest, LoadSecfile_002, TestSize.Level1)
76 {
77 int tzFd = 0;
78 int ret = 0;
79
80 FILE *fp = fopen("./LoadSecfile_002.txt", "w+");
81 ret = LoadSecFile(tzFd, nullptr, LOAD_TA, nullptr);
82 fclose(fp);
83 EXPECT_TRUE(ret != 0);
84
85 fp = fopen("./LoadSecfile_002.txt", "w+");
86 (void)fseek(fp, MAX_BUFFER_LEN + 1, SEEK_SET);
87 char chr = 0;
88 fwrite(&chr, 1, sizeof(chr), fp);
89 ret = LoadSecFile(tzFd, fp, LOAD_TA, nullptr);
90 fclose(fp);
91 EXPECT_TRUE(ret != 0);
92
93 fp = fopen("./LoadSecfile_002.txt", "w+");
94 (void)fprintf(fp, "%s", "LoadSecfile_002");
95 ret = LoadSecFile(tzFd, fp, LOAD_TA, nullptr);
96 fclose(fp);
97 EXPECT_TRUE(ret != 0);
98 }
99
100 HWTEST_F(LoadSecfileTest, ClientAppLoad_001, TestSize.Level1)
101 {
102 int ret = 0;
103 TaFileInfo taFile = { 0 };
104 TEEC_UUID srvUuid = { 0 };
105 TC_NS_ClientContext cliContext = { { 0 } };
106
107 ret = TEEC_GetApp(nullptr, nullptr, nullptr);
108 EXPECT_TRUE(ret != 0);
109
110 ret = TEEC_GetApp(&taFile, nullptr, nullptr);
111 EXPECT_TRUE(ret != 0);
112
113 ret = TEEC_GetApp(&taFile, &srvUuid, nullptr);
114 EXPECT_TRUE(ret != 0);
115
116 FILE *fp = fopen("./ClientAppLoad_001.sec", "w+");
117 taFile.taFp = fp;
118 ret = TEEC_GetApp(&taFile, &srvUuid, &cliContext);
119 fclose(fp);
120 EXPECT_TRUE(ret != 0);
121 }
122
123 HWTEST_F(LoadSecfileTest, ClientAppLoad_002, TestSize.Level1)
124 {
125 int ret = 0;
126 FILE *fp = fopen("./ClientAppLoad_002.txt", "w+");
127
128 ret = TEEC_LoadSecfile(nullptr, -1, nullptr);
129 EXPECT_TRUE(ret != 0);
130
131 ret = TEEC_LoadSecfile(nullptr, 0, fp);
132 EXPECT_TRUE(ret != 0);
133
134 string str("./ClientAppLoad_002.txt");
135 ret = TEEC_LoadSecfile(str.c_str(), 0, fp);
136 EXPECT_TRUE(ret != 0);
137
138 ret = TEEC_LoadSecfile(str.c_str(), 0, nullptr);
139 EXPECT_TRUE(ret != 0);
140
141 fclose(fp);
142 }
143
144 HWTEST_F(LoadSecfileTest, ClientSocket_001, TestSize.Level1)
145 {
146 int ret = 0;
147 CaAuthInfo caInfo = { { 0 } };
148
149 ret = CaDaemonConnectWithCaInfo(nullptr, 0);
150 EXPECT_TRUE(ret != 0);
151
152 ret = CaDaemonConnectWithCaInfo(&caInfo, 0);
153 EXPECT_TRUE(ret != 0);
154 }
155 }