1# 2# Copyright 2018 - The Android Open Source Project 3# 4# Licensed under the Apache License, Version 2.0 (the "License"); 5# you may not use this file except in compliance with the License. 6# You may obtain a copy of the License at 7# 8# http://www.apache.org/licenses/LICENSE-2.0 9# 10# Unless required by applicable law or agreed to in writing, software 11# distributed under the License is distributed on an "AS IS" BASIS, 12# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13# See the License for the specific language governing permissions and 14# limitations under the License. 15 16import queue 17import threading 18import time 19 20from acts_contrib.test_utils.net import connectivity_const as cconst 21from acts import asserts 22 23MSG = "Test message " 24PKTS = 5 25""" Methods for android.system.Os based sockets """ 26 27 28def open_android_socket(ad, domain, sock_type, ip, port): 29 """ Open TCP or UDP using android.system.Os class 30 31 Args: 32 1. ad - android device object 33 2. domain - IPv4 or IPv6 type 34 3. sock_type - UDP or TCP socket 35 4. ip - IP addr on the device 36 5. port - open socket on port 37 38 Returns: 39 File descriptor key 40 """ 41 fd_key = ad.droid.openSocket(domain, sock_type, ip, port) 42 ad.log.info("File descriptor: %s" % fd_key) 43 asserts.assert_true(fd_key, "Failed to open socket") 44 return fd_key 45 46 47def close_android_socket(ad, fd_key): 48 """ Close socket 49 50 Args: 51 1. ad - android device object 52 2. fd_key - file descriptor key 53 """ 54 status = ad.droid.closeSocket(fd_key) 55 asserts.assert_true(status, "Failed to close socket") 56 57 58def listen_accept_android_socket(client, server, client_fd, server_fd, 59 server_ip, server_port): 60 """ Listen, accept TCP sockets 61 62 Args: 63 1. client : ad object for client device 64 2. server : ad object for server device 65 3. client_fd : client's socket handle 66 4. server_fd : server's socket handle 67 5. server_ip : send data to this IP 68 6. server_port : send data to this port 69 """ 70 server.droid.listenSocket(server_fd) 71 client.droid.connectSocket(client_fd, server_ip, server_port) 72 sock = server.droid.acceptSocket(server_fd) 73 asserts.assert_true(sock, "Failed to accept socket") 74 return sock 75 76 77def send_recv_data_android_sockets(client, server, client_fd, server_fd, 78 server_ip, server_port): 79 """ Send TCP or UDP data over android os sockets from client to server. 80 Verify that server received the data. 81 82 Args: 83 1. client : ad object for client device 84 2. server : ad object for server device 85 3. client_fd : client's socket handle 86 4. server_fd : server's socket handle 87 5. server_ip : send data to this IP 88 6. server_port : send data to this port 89 """ 90 send_list = [] 91 recv_list = [] 92 93 for _ in range(1, PKTS + 1): 94 msg = MSG + " %s" % _ 95 send_list.append(msg) 96 client.log.info("Sending message: %s" % msg) 97 client.droid.sendDataOverSocket(server_ip, server_port, msg, client_fd) 98 recv_msg = server.droid.recvDataOverSocket(server_fd) 99 server.log.info("Received message: %s" % recv_msg) 100 recv_list.append(recv_msg) 101 102 recv_list = [x.rstrip('\x00') if x else x for x in recv_list] 103 asserts.assert_true(send_list and recv_list and send_list == recv_list, 104 "Send and recv information is incorrect") 105 106 107""" Methods for java.net.DatagramSocket based sockets """ 108 109 110def open_datagram_socket(ad, ip, port): 111 """ Open datagram socket 112 113 Args: 114 1. ad : android device object 115 2. ip : IP addr on the device 116 3. port : socket port 117 118 Returns: 119 Hash key of the datagram socket 120 """ 121 socket_key = ad.droid.openDatagramSocket(ip, port) 122 ad.log.info("Datagram socket: %s" % socket_key) 123 asserts.assert_true(socket_key, "Failed to open datagram socket") 124 return socket_key 125 126 127def close_datagram_socket(ad, socket_key): 128 """ Close datagram socket 129 130 Args: 131 1. socket_key : hash key of datagram socket 132 """ 133 status = ad.droid.closeDatagramSocket(socket_key) 134 asserts.assert_true(status, "Failed to close datagram socket") 135 136 137def send_recv_data_datagram_sockets(client, server, client_sock, server_sock, 138 server_ip, server_port): 139 """ Send data over datagram socket from dut_a to dut_b. 140 Verify that dut_b received the data. 141 142 Args: 143 1. client : ad object for client device 144 2. server : ad object for server device 145 3. client_sock : client's socket handle 146 4. server_sock : server's socket handle 147 5. server_ip : send data to this IP 148 6. server_port : send data to this port 149 """ 150 send_list = [] 151 recv_list = [] 152 153 for _ in range(1, PKTS + 1): 154 msg = MSG + " %s" % _ 155 send_list.append(msg) 156 client.log.info("Sending message: %s" % msg) 157 client.droid.sendDataOverDatagramSocket(client_sock, msg, server_ip, 158 server_port) 159 recv_msg = server.droid.recvDataOverDatagramSocket(server_sock) 160 server.log.info("Received message: %s" % recv_msg) 161 recv_list.append(recv_msg) 162 163 recv_list = [x.rstrip('\x00') if x else x for x in recv_list] 164 asserts.assert_true(send_list and recv_list and send_list == recv_list, 165 "Send and recv information is incorrect") 166 167 168""" Utils methods for java.net.Socket based sockets """ 169 170 171def _accept_socket(server, server_ip, server_port, server_sock, q): 172 sock = server.droid.acceptTcpSocket(server_sock) 173 server.log.info("Server socket: %s" % sock) 174 q.put(sock) 175 176 177def _client_socket(client, server_ip, server_port, client_ip, client_port, q): 178 time.sleep(0.5) 179 sock = client.droid.openTcpSocket(server_ip, server_port, client_ip, 180 client_port) 181 client.log.info("Client socket: %s" % sock) 182 q.put(sock) 183 184 185def open_connect_socket(client, server, client_ip, server_ip, client_port, 186 server_port, server_sock): 187 """ Open tcp socket and connect to server 188 189 Args: 190 1. client : ad object for client device 191 2. server : ad object for server device 192 3. client_ip : client's socket handle 193 4. server_ip : send data to this IP 194 5. client_port : port on client socket 195 6. server_port : port on server socket 196 7. server_sock : server socket 197 198 Returns: 199 client and server socket from successful connect 200 """ 201 sq = queue.Queue() 202 cq = queue.Queue() 203 s = threading.Thread(target=_accept_socket, 204 args=(server, server_ip, server_port, server_sock, 205 sq)) 206 c = threading.Thread(target=_client_socket, 207 args=(client, server_ip, server_port, client_ip, 208 client_port, cq)) 209 s.start() 210 c.start() 211 c.join() 212 s.join() 213 214 client_sock = cq.get() 215 server_sock = sq.get() 216 asserts.assert_true(client_sock and server_sock, "Failed to open sockets") 217 218 return client_sock, server_sock 219 220 221def open_server_socket(server, server_ip, server_port): 222 """ Open tcp server socket 223 224 Args: 225 1. server : ad object for server device 226 2. server_ip : send data to this IP 227 3. server_port : send data to this port 228 """ 229 sock = server.droid.openTcpServerSocket(server_ip, server_port) 230 server.log.info("Server Socket: %s" % sock) 231 asserts.assert_true(sock, "Failed to open server socket") 232 return sock 233 234 235def close_socket(ad, socket): 236 """ Close socket 237 238 Args: 239 1. ad - android device object 240 2. socket - socket key 241 """ 242 status = ad.droid.closeTcpSocket(socket) 243 asserts.assert_true(status, "Failed to socket") 244 245 246def close_server_socket(ad, socket): 247 """ Close server socket 248 249 Args: 250 1. ad - android device object 251 2. socket - server socket key 252 """ 253 status = ad.droid.closeTcpServerSocket(socket) 254 asserts.assert_true(status, "Failed to socket") 255 256 257def shutdown_socket(ad, socket): 258 """ Shutdown socket 259 260 Args: 261 1. ad - androidandroid device object 262 2. socket - socket key 263 """ 264 fd = ad.droid.getFileDescriptorOfSocket(socket) 265 asserts.assert_true(fd, "Failed to get FileDescriptor key") 266 status = ad.droid.shutdownFileDescriptor(fd) 267 asserts.assert_true(status, "Failed to shutdown socket") 268 269 270def send_recv_data_sockets(client, server, client_sock, server_sock): 271 """ Send data over TCP socket from client to server. 272 Verify that server received the data 273 274 Args: 275 1. client : ad object for client device 276 2. server : ad object for server device 277 3. client_sock : client's socket handle 278 4. server_sock : server's socket handle 279 """ 280 send_list = [] 281 recv_list = [] 282 283 for _ in range(1, PKTS + 1): 284 msg = MSG + " %s" % _ 285 send_list.append(msg) 286 client.log.info("Sending message: %s" % msg) 287 client.droid.sendDataOverTcpSocket(client_sock, msg) 288 recv_msg = server.droid.recvDataOverTcpSocket(server_sock) 289 server.log.info("Received message: %s" % recv_msg) 290 recv_list.append(recv_msg) 291 292 recv_list = [x.rstrip('\x00') if x else x for x in recv_list] 293 asserts.assert_true(send_list and recv_list and send_list == recv_list, 294 "Send and recv information is incorrect") 295