• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "string_ex.h"
18 #include "securec.h"
19 using namespace testing::ext;
20 using namespace OHOS;
21 
22 class UtilsSecurecTest : public testing::Test
23 {
24 public :
25     static void SetUpTestCase(void);
26     static void TearDownTestCase(void);
27     void SetUp();
28     void TearDown();
29 };
30 
SetUpTestCase(void)31 void UtilsSecurecTest::SetUpTestCase(void)
32 {
33     // step 2: input testsuit setup step
34 }
35 
TearDownTestCase(void)36 void UtilsSecurecTest::TearDownTestCase(void)
37 {
38     // step 2: input testsuit teardown step
39 }
40 
SetUp(void)41 void UtilsSecurecTest::SetUp(void)
42 {
43     // step 3: input testcase setup step
44 }
45 
TearDown(void)46 void UtilsSecurecTest::TearDown(void)
47 {
48     // step 3: input testcase teardown step
49 }
50 
51 HWTEST_F(UtilsSecurecTest, test_memset_s_01, TestSize.Level0)
52 {
53     char cBase[20];
54     errno_t result = memset_s((void*)cBase, sizeof(char) * 20, 1, sizeof(char) * 20);
55     EXPECT_EQ(result, 0);
56 }
57 
58 HWTEST_F(UtilsSecurecTest, test_memset_s_02, TestSize.Level0)
59 {
60     char cBase[20];
61     errno_t result = memset_s((void*)cBase, sizeof(char) * 20, 1, sizeof(char) * 21);
62     EXPECT_NE(result, 0);
63 }
64 
65 HWTEST_F(UtilsSecurecTest, test_memcpy_s_01, TestSize.Level0)
66 {
67     char cBase[20] = "memcpy_s";
68     char cTemp[20];
69     errno_t result = memcpy_s((void*)cTemp, sizeof(char) * 20, cBase, sizeof(cBase));
70     EXPECT_EQ(result, 0);
71     EXPECT_EQ(0, strcmp(cTemp, cBase));
72 }
73 
74 HWTEST_F(UtilsSecurecTest, test_memcpy_s_02, TestSize.Level0)
75 {
76     char cBase[20] = "memcpy_s";
77     char cTemp[5];
78     errno_t result = memcpy_s((void*)cTemp, sizeof(char) * 5, cBase, sizeof(cBase));
79     EXPECT_NE(result, 0);
80 }
81 
82 HWTEST_F(UtilsSecurecTest, test_strcpy_s_01, TestSize.Level0)
83 {
84     const char cBase[] = "strcpy_base";
85     char cTemp[20];
86     errno_t result = strcpy_s(cTemp, sizeof(cTemp), cBase);
87     EXPECT_EQ(result, 0);
88     EXPECT_EQ(0, strcmp(cTemp, cBase));
89 }
90 
91 HWTEST_F(UtilsSecurecTest, test_strcpy_s_02, TestSize.Level0)
92 {
93     const char cBase[] = "strcpy_base";
94     char cTemp[10];
95     errno_t result = strcpy_s(cTemp, sizeof(cTemp), cBase);
96     EXPECT_NE(result, 0);
97 }
98 
99 HWTEST_F(UtilsSecurecTest, test_strncpy_s_01, TestSize.Level0)
100 {
101     const char cBase[] = "strcpy_base";
102     char cTemp[20];
103     errno_t result = strncpy_s(cTemp, sizeof(cTemp), cBase, sizeof(cBase));
104     EXPECT_EQ(result, 0);
105     EXPECT_EQ(0, strcmp(cTemp, cBase));
106 }
107 
108 HWTEST_F(UtilsSecurecTest, test_strncpy_s_02, TestSize.Level0)
109 {
110     const char cBase[] = "strcpy_base";
111     char cTemp[10];
112     errno_t result = strncpy_s(cTemp, sizeof(cTemp), cBase, (sizeof(cTemp) -1));
113     EXPECT_EQ(result, 0);
114     result = strncpy_s(cTemp, sizeof(cTemp), cBase, sizeof(cBase));
115     EXPECT_NE(result, 0);
116 }
117 
118 HWTEST_F(UtilsSecurecTest, test_strcat_s_01, TestSize.Level0)
119 {
120     const char cBase[] = "strcpy_base";
121     char cTemp[20] = "temp ";
122     errno_t result = strcat_s(cTemp, sizeof(cTemp), cBase);
123     EXPECT_EQ(result, 0);
124     EXPECT_EQ(0, strcmp(cTemp, "temp strcpy_base"));
125 }
126 
127 HWTEST_F(UtilsSecurecTest, test_strcat_s_02, TestSize.Level0)
128 {
129     const char cBase[] = "strcpy_base";
130     char cTemp[10];
131     errno_t result = strcat_s(cTemp, sizeof(cTemp), cBase);
132     EXPECT_NE(result, 0);
133 }
134 
135 HWTEST_F(UtilsSecurecTest, test_sprintf_s_01, TestSize.Level0)
136 {
137     char cBase[64] = { 0 };
138     errno_t result = sprintf_s(cBase, sizeof(cBase), "%d", 12345);
139     EXPECT_EQ(string(cBase), "12345");
140     EXPECT_NE(result, 0);
141 }