• 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 "init_service.h"
19 #include "init_service_manager.h"
20 #include "init_service_socket.h"
21 #include "init_socket.h"
22 #include "init_unittest.h"
23 #include "securec.h"
24 using namespace std;
25 using namespace testing::ext;
26 namespace init_ut {
27 class ServiceSocketUnitTest : public testing::Test {
28 public:
SetUpTestCase(void)29     static void SetUpTestCase(void) {}
TearDownTestCase(void)30     static void TearDownTestCase(void) {}
SetUp()31     void SetUp() {};
TearDown()32     void TearDown() {};
33 };
34 HWTEST_F(ServiceSocketUnitTest, TestCreateSocket, TestSize.Level0)
35 {
36     const char *testSocName = "test_socket";
37     Service *service = (Service *)AddService("TestCreateSocket");
38     ASSERT_NE(service, nullptr);
39     ServiceSocket *sockopt = (ServiceSocket *)calloc(1, sizeof(ServiceSocket) + strlen(testSocName) + 1);
40     ASSERT_NE(sockopt, nullptr);
41     sockopt->type = SOCK_STREAM;
42     sockopt->protocol = 0;
43     sockopt->family = PF_UNIX;
44     sockopt->sockFd = -1;
45     sockopt->uid = 1000;
46     sockopt->gid = 1000;
47     sockopt->perm = 0660;
48     sockopt->option = SOCKET_OPTION_PASSCRED;
49     errno_t ret = strncpy_s(sockopt->name, strlen(testSocName) + 1, testSocName, strlen(testSocName));
50     sockopt->next = NULL;
51     EXPECT_EQ(ret, EOK);
52     if (service->socketCfg == NULL) {
53         service->socketCfg = sockopt;
54     } else {
55         sockopt->next = service->socketCfg->next;
56         service->socketCfg->next = sockopt;
57     }
58     int ret1 = CreateServiceSocket(service);
59     EXPECT_EQ(ret1, 0);
60     ret1 = GetControlSocket(testSocName);
61     EXPECT_GE(ret1, 0);
62     CloseServiceSocket(service);
63     ReleaseService(service);
64 }
65 } // namespace init_ut
66