• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2013-2019 Huawei Technologies Co., Ltd. All rights reserved.
3  * Copyright (c) 2020-2021 Huawei Device Co., Ltd. All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without modification,
6  * are permitted provided that the following conditions are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright notice, this list of
9  * conditions and the following disclaimer.
10  *
11  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
12  * of conditions and the following disclaimer in the documentation and/or other materials
13  * provided with the distribution.
14  *
15  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
16  * to endorse or promote products derived from this software without specific prior written
17  * permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
23  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
24  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
26  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
27  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
28  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
29  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <arpa/inet.h>
33 #include <osTest.h>
34 
35 #define SRV_MSG                 "Hi, I am TCP server"
36 #define INVALID_USER_ADDR       0X1200000
37 #define INVALID_KERNEL_ADDR     0x48000000
38 
TcpTest()39 static int TcpTest()
40 {
41     int ret;
42     int lsfd = -1;
43     struct msghdr msg = { 0 };
44 
45     lsfd = socket(AF_INET, SOCK_STREAM, 0);
46     ICUNIT_ASSERT_NOT_EQUAL(lsfd, -1, lsfd);
47 
48     ret = bind(lsfd, reinterpret_cast<const struct sockaddr *>(INVALID_USER_ADDR), sizeof(struct sockaddr_in));
49     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
50 
51     ret = bind(lsfd, reinterpret_cast<const struct sockaddr *>(INVALID_KERNEL_ADDR), sizeof(struct sockaddr_in));
52     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
53 
54     ret = connect(lsfd, reinterpret_cast<struct sockaddr *>(INVALID_USER_ADDR), sizeof(struct sockaddr_in));
55     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
56 
57     ret = connect(lsfd, reinterpret_cast<struct sockaddr *>(INVALID_KERNEL_ADDR), sizeof(struct sockaddr_in));
58     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
59 
60     ret = accept(lsfd, reinterpret_cast<struct sockaddr *>(INVALID_USER_ADDR),
61                  reinterpret_cast<socklen_t *>(INVALID_USER_ADDR));
62     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
63 
64     ret = accept(lsfd, reinterpret_cast<struct sockaddr *>(INVALID_KERNEL_ADDR),
65                  reinterpret_cast<socklen_t *>(INVALID_KERNEL_ADDR));
66     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
67 
68     ret = getsockname(lsfd, reinterpret_cast<struct sockaddr *>(INVALID_USER_ADDR),
69                       reinterpret_cast<socklen_t *>(INVALID_USER_ADDR));
70     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
71 
72     ret = getsockname(lsfd, reinterpret_cast<struct sockaddr *>(INVALID_KERNEL_ADDR),
73                       reinterpret_cast<socklen_t *>(INVALID_KERNEL_ADDR));
74     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
75 
76     ret = getpeername(lsfd, reinterpret_cast<struct sockaddr *>(INVALID_USER_ADDR),
77                       reinterpret_cast<socklen_t *>(INVALID_USER_ADDR));
78     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
79 
80     ret = getpeername(lsfd, reinterpret_cast<struct sockaddr *>(INVALID_KERNEL_ADDR),
81                       reinterpret_cast<socklen_t *>(INVALID_KERNEL_ADDR));
82     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
83 
84     ret = send(lsfd, reinterpret_cast<char *>(INVALID_USER_ADDR), strlen(SRV_MSG), 0);
85     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
86 
87     ret = send(lsfd, reinterpret_cast<char *>(INVALID_KERNEL_ADDR), strlen(SRV_MSG), 0);
88     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
89 
90     ret = sendto(lsfd, reinterpret_cast<char *>(INVALID_USER_ADDR), strlen(SRV_MSG), 0,
91         reinterpret_cast<struct sockaddr *>(INVALID_USER_ADDR), static_cast<socklen_t>(sizeof(struct sockaddr_in)));
92     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
93 
94     ret = sendto(lsfd, reinterpret_cast<char *>(INVALID_KERNEL_ADDR), strlen(SRV_MSG), 0,
95         reinterpret_cast<struct sockaddr *>(INVALID_KERNEL_ADDR), static_cast<socklen_t>(sizeof(struct sockaddr_in)));
96     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
97 
98     ret = recv(lsfd, reinterpret_cast<char *>(INVALID_USER_ADDR), sizeof(SRV_MSG), 0);
99     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
100 
101     ret = recv(lsfd, reinterpret_cast<char *>(INVALID_KERNEL_ADDR), sizeof(SRV_MSG), 0);
102     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
103 
104     ret = recvfrom(lsfd, reinterpret_cast<char *>(INVALID_USER_ADDR), sizeof(SRV_MSG), 0,
105         reinterpret_cast<struct sockaddr *>(INVALID_USER_ADDR), reinterpret_cast<socklen_t *>(INVALID_USER_ADDR));
106     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
107 
108     ret = recvfrom(lsfd, reinterpret_cast<char *>(INVALID_KERNEL_ADDR), sizeof(SRV_MSG), 0,
109         reinterpret_cast<struct sockaddr *>(INVALID_KERNEL_ADDR), reinterpret_cast<socklen_t *>(INVALID_KERNEL_ADDR));
110     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
111 
112     ret = setsockopt(lsfd, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<struct timeval *>(INVALID_USER_ADDR),
113                      static_cast<socklen_t>(sizeof(struct timeval)));
114     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
115 
116     ret = setsockopt(lsfd, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<struct timeval *>(INVALID_KERNEL_ADDR),
117                      static_cast<socklen_t>(sizeof(struct timeval)));
118     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
119 
120     ret = getsockopt(lsfd, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<struct timeval *>(INVALID_USER_ADDR),
121                      reinterpret_cast<socklen_t *>(sizeof(struct timeval)));
122     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
123 
124     ret = getsockopt(lsfd, SOL_SOCKET, SO_RCVTIMEO, reinterpret_cast<struct timeval *>(INVALID_KERNEL_ADDR),
125                      reinterpret_cast<socklen_t *>(sizeof(struct timeval)));
126     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
127 
128     ret = sendmsg(lsfd, reinterpret_cast<struct msghdr *>(INVALID_USER_ADDR), 0);
129     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
130 
131     ret = sendmsg(lsfd, reinterpret_cast<struct msghdr *>(INVALID_KERNEL_ADDR), 0);
132     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
133 
134     msg.msg_name = reinterpret_cast<char *>(INVALID_USER_ADDR);
135     msg.msg_namelen = sizeof(struct sockaddr_in);
136     msg.msg_iov = reinterpret_cast<struct iovec *>(INVALID_KERNEL_ADDR);
137     msg.msg_iovlen = 2;
138     ret = recvmsg(lsfd, static_cast<struct msghdr *>(&msg), 0);
139     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
140 
141     ret = recvmsg(lsfd, static_cast<struct msghdr *>(&msg), 0);
142     ICUNIT_ASSERT_EQUAL(ret, -1, ret);
143 
144 #ifdef TEST_BIG_MEM
145     const int bufSiz = 0x1400000; // 20M
146     void *buf = malloc(bufSiz);
147     if (!buf) {
148         printf("malloc 20M fail\n");
149     } else {
150         printf("malloc 20M success\n");
151         ret = memset_s(buf, bufSiz, 0, bufSiz);
152         ICUNIT_GOTO_EQUAL(ret, 0, ret, EXIT);
153         ret = send(lsfd, buf, bufSiz, 0);
154         printf("send ret = %d, errno :%d\n", ret, errno);
155         ICUNIT_GOTO_EQUAL(ret, -1, ret, EXIT);
156     }
157 
158 EXIT:
159     if (buf != NULL) {
160         free(buf);
161     }
162 #endif
163     close(lsfd);
164     return 0;
165 }
166 
NetSocketTest007(void)167 void NetSocketTest007(void)
168 {
169     TEST_ADD_CASE(__FUNCTION__, TcpTest, TEST_POSIX, TEST_MEM, TEST_LEVEL0, TEST_FUNCTION);
170 }
171