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 <arpa/inet.h>
28 #include <netdb.h>
29 #include <sys/socket.h>
30 #include <bits/ioctl.h>
31 #include <net/if.h>
32 #include <resolv.h>
33 #include <arpa/nameser.h>
34 #include <poll.h>
35 #include <pthread.h>
36 #include "functionalext.h"
37
38 #define BUF_SIZE (100)
39 #define PORT 2288
40 #define WAIT() pthread_barrier_wait(&g_barrier)
41 static const char *g_cliMsg = "Hello, this is client";
42 static const char *g_localHost = "127.0.0.1";
43 static pthread_barrier_t g_barrier;
44 int bufSize = 0;
45
SampleServerTask()46 void *SampleServerTask()
47 {
48 int *sign = (int *)malloc(sizeof(int));
49 int rets = -1;
50 int sListen = socket(AF_INET, SOCK_STREAM, 0);
51 if (sListen == -1)
52 {
53 printf("[Server] socket fail!\n");
54 *sign = -1;
55 return sign;
56 }
57 int flag = 1;
58 rets = setsockopt(sListen, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(int));
59 if (rets != 0)
60 {
61 printf("[server] setsockopt fail, rets: %d!\n", rets);
62 }
63 struct sockaddr_in srvAddr = {0};
64 srvAddr.sin_family = AF_INET;
65 srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
66 srvAddr.sin_port = htons(PORT);
67 rets = bind(sListen, (struct sockaddr *)&srvAddr, sizeof(srvAddr));
68 if (rets != 0)
69 {
70 printf("[Server] bind fail!\n");
71 close(sListen);
72 *sign = -1;
73 return sign;
74 }
75 rets = listen(sListen, 2);
76 if (rets != 0)
77 {
78 printf("[Server] listen fail!\n");
79 close(sListen);
80 *sign = -1;
81 return sign;
82 }
83 WAIT();
84 struct sockaddr_in clnAddr = {0};
85 socklen_t clnAddrLen = sizeof(clnAddr);
86 int sClient = accept(sListen, (struct sockaddr *)&clnAddr, &clnAddrLen);
87 printf("[server] accept <%s:%d>\n", inet_ntoa(clnAddr.sin_addr), ntohs(clnAddr.sin_port));
88 static char buf[BUF_SIZE + 1] = {0};
89 memset(buf, '\0', BUF_SIZE);
90 rets = recv(sClient, buf, sizeof(buf), 0);
91 EXPECT_EQ("recv_0100", rets, bufSize);
92 close(sClient);
93 close(sListen);
94 return sign;
95 }
96
SampleClientTask()97 void *SampleClientTask()
98 {
99 int clnFd = socket(AF_INET, SOCK_STREAM, 0);
100 WAIT();
101 struct sockaddr_in srvAddr = {0};
102 srvAddr.sin_family = AF_INET;
103 srvAddr.sin_addr.s_addr = inet_addr(g_localHost);
104 srvAddr.sin_port = htons(PORT);
105 int sign = connect(clnFd, (struct sockaddr *)&srvAddr, sizeof(srvAddr));
106 EXPECT_EQ("recv_0100", 0, sign);
107 if (sign == 0)
108 {
109 printf("[client] connect success\n");
110 }
111 static char buf[BUF_SIZE + 1] = {0};
112 memset(buf, '\0', BUF_SIZE);
113 strcpy(buf, g_cliMsg);
114 bufSize = sizeof(buf);
115 int sendRet = send(clnFd, buf, sizeof(buf), 0);
116 EXPECT_EQ("recv_0100", sendRet, sizeof(buf));
117 close(clnFd);
118 return NULL;
119 }
120
121 /*
122 * @tc.name : recv_0100
123 * @tc.desc : Verify that the server recv successfully
124 * @tc.level : Level 0
125 */
recv_0100()126 void recv_0100()
127 {
128 pthread_t srv;
129 pthread_t cli;
130
131 int sign = pthread_barrier_init(&g_barrier, 0, 2);
132 EXPECT_EQ("recv_0100", 0, sign);
133
134 sign = pthread_create(&srv, NULL, SampleServerTask, NULL);
135 EXPECT_EQ("recv_0100", 0, sign);
136 sign = pthread_create(&cli, NULL, SampleClientTask, NULL);
137 EXPECT_EQ("recv_0100", 0, sign);
138
139 sign = pthread_join(cli, NULL);
140 EXPECT_EQ("recv_0100", 0, sign);
141 sign = pthread_join(srv, NULL);
142 EXPECT_EQ("recv_0100", 0, sign);
143
144 sign = pthread_barrier_destroy(&g_barrier);
145 EXPECT_EQ("recv_0100", 0, sign);
146 }
147
main(int argc,char * argv[])148 int main(int argc, char *argv[])
149 {
150 recv_0100();
151 return t_status;
152 }