• 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 #include <cerrno>
16 #include <unistd.h>
17 #include <sys/socket.h>
18 #include "param_stub.h"
19 #include "init_utils.h"
20 #include "init_service_socket.h"
21 #include "init_socket.h"
22 #include "securec.h"
23 using namespace std;
24 using namespace testing::ext;
25 
26 extern "C" {
27 float ConvertMicrosecondToSecond(int x);
28 }
29 
30 namespace init_ut {
31 class UtilsUnitTest : public testing::Test {
32 public:
SetUpTestCase(void)33     static void SetUpTestCase(void) {}
TearDownTestCase(void)34     static void TearDownTestCase(void) {}
SetUp()35     void SetUp() {};
TearDown()36     void TearDown() {};
37 };
38 
39 HWTEST_F(UtilsUnitTest, TestMakeDir, TestSize.Level0)
40 {
41     const char *dir = "/data/init_ut/mkdir_test";
42     mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IXOTH | S_IROTH;
43     int ret = MakeDirRecursive(dir, mode);
44     EXPECT_EQ(ret, 0);
45     dir = nullptr;
46     ret = MakeDirRecursive(dir, mode);
47     EXPECT_EQ(ret, -1);
48     ret = MakeDir(dir, 9999);
49     EXPECT_EQ(ret, -1);
50 }
51 
52 HWTEST_F(UtilsUnitTest, TestString, TestSize.Level0)
53 {
54     const char *str = nullptr;
55     int defaultValue = 1;
56     int ret = StringToInt(str, defaultValue);
57     EXPECT_EQ(ret, 1);
58     str = "10";
59     ret = StringToInt(str, defaultValue);
60     EXPECT_EQ(ret, 10);
61     char rStr[] = "abc";
62     char oldChr = 'a';
63     char newChr = 'd';
64     ret = StringReplaceChr(rStr, oldChr, newChr);
65     EXPECT_EQ(ret, 0);
66     EXPECT_STREQ(rStr, "dbc");
67 }
68 HWTEST_F(UtilsUnitTest, TestUtilsApi, TestSize.Level0)
69 {
70     float sec = ConvertMicrosecondToSecond(1000000); // 1000000 microseconds
71     EXPECT_EQ(sec, 1);
72     EXPECT_EQ(WriteAll(2, "test", strlen("test")), 4);
73     mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
74     CheckAndCreatFile("/data/init_ut/testcreatfile", mode);
75     CheckAndCreatFile("/data/init_ut/nodir/testcreatfile", mode);
76     char testStr[] = ".trim";
77     EXPECT_STREQ(TrimHead(testStr, '.'), "trim");
78 }
79 } // namespace init_ut
80