• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /**
2  * Copyright (c) 2022 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 <dlfcn.h>
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 #include <stdbool.h>
22 #include <ctype.h>
23 #include <unistd.h>
24 #include <sys/stat.h>
25 #include <fcntl.h>
26 #include <stdint.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
29 #include <netdb.h>
30 #include <sys/socket.h>
31 #include <poll.h>
32 #include <pthread.h>
33 #include "functionalext.h"
34 
35 #define PORT 2288
36 static const char *g_localHost = "127.0.0.1";
37 #define BUF_SIZE (100)
38 static pthread_barrier_t g_barrier;
39 #define WAIT() pthread_barrier_wait(&g_barrier)
40 typedef void (*TEST_FUN)();
SampleServerTask()41 void *SampleServerTask()
42 {
43     int *ret = (int *)malloc(sizeof(int));
44     int rets = -1;
45     int sListen = socket(AF_INET, SOCK_STREAM, 0);
46     if (sListen == -1) {
47         *ret = -1;
48         return ret;
49     }
50     int flag = 1;
51     rets = setsockopt(sListen, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int));
52     struct sockaddr_in srvAddr = {0};
53     srvAddr.sin_family = AF_INET;
54     srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
55     srvAddr.sin_port = htons(PORT);
56     rets = bind(sListen, (struct sockaddr *)&srvAddr, sizeof(srvAddr));
57     if (rets != 0) {
58         close(sListen);
59         *ret = -1;
60         return ret;
61     }
62     rets = listen(sListen, 2);
63     if (rets != 0) {
64         close(sListen);
65         *ret = -1;
66         return ret;
67     }
68     WAIT();
69     struct sockaddr_in clnAddr = {0};
70     socklen_t clnAddrLen = sizeof(clnAddr);
71     int sClient = accept(sListen, (struct sockaddr *)&clnAddr, &clnAddrLen);
72     struct sockaddr addr = {0};
73     socklen_t addrLen = sizeof(addr);
74     rets = getpeername(sClient, &addr, &addrLen);
75     static char recordAddr[BUF_SIZE + 1] = {0};
76     memset(recordAddr, '\0', BUF_SIZE);
77     strcpy(recordAddr, inet_ntoa(((struct sockaddr_in *)&addr)->sin_addr));
78     EXPECT_STREQ("getpeername_0100", g_localHost, recordAddr);
79     EXPECT_EQ("getpeername_0100", 0, rets);
80     close(sClient);
81     close(sListen);
82     return ret;
83 }
84 
SampleClientTask()85 void *SampleClientTask()
86 {
87     int clnFd = socket(AF_INET, SOCK_STREAM, 0);
88     WAIT();
89     struct sockaddr_in srvAddr = {0};
90     srvAddr.sin_family = AF_INET;
91     srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
92     srvAddr.sin_port = htons(PORT);
93     int ret = connect(clnFd, (struct sockaddr *)&srvAddr, sizeof(srvAddr));
94     EXPECT_EQ("getpeername_0100", 0, ret);
95     close(clnFd);
96     return NULL;
97 }
98 
99 /*
100  * @tc.name      : getpeername_0100
101  * @tc.desc      : Get the address associated with the socket
102  * @tc.level     : Level 0
103  */
getpeername_0100()104 void getpeername_0100()
105 {
106     pthread_t srv;
107     pthread_t cli;
108     int ret = pthread_barrier_init(&g_barrier, 0, 2);
109     EXPECT_EQ("getpeername_0100", 0, ret);
110     ret = pthread_create(&srv, NULL, SampleServerTask, NULL);
111     EXPECT_EQ("getpeername_0100", 0, ret);
112     ret = pthread_create(&cli, NULL, SampleClientTask, NULL);
113     EXPECT_EQ("getpeername_0100", 0, ret);
114     ret = pthread_join(cli, NULL);
115     EXPECT_EQ("getpeername_0100", 0, ret);
116     ret = pthread_join(srv, NULL);
117     EXPECT_EQ("getpeername_0100", 0, ret);
118     ret = pthread_barrier_destroy(&g_barrier);
119     EXPECT_EQ("getpeername_0100", 0, ret);
120 }
121 
122 TEST_FUN G_Fun_Array[] = {getpeername_0100};
main(int argc,char * argv[])123 int main(int argc, char *argv[])
124 {
125     int num = sizeof(G_Fun_Array) / sizeof(TEST_FUN);
126     for (int pos = 0; pos < num; ++pos) {
127         G_Fun_Array[pos]();
128     }
129 
130     return t_status;
131 }