Ram Gandikota
/
ABCD
A metronome using the FRDM K64F board
pal/Test/Unitest/pal_socket_test.c@0:a7a43371b306, 2017-05-14 (annotated)
- Committer:
- ram54288
- Date:
- Sun May 14 18:40:18 2017 +0000
- Revision:
- 0:a7a43371b306
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ram54288 | 0:a7a43371b306 | 1 | /* |
ram54288 | 0:a7a43371b306 | 2 | * Copyright (c) 2016 ARM Limited. All rights reserved. |
ram54288 | 0:a7a43371b306 | 3 | * SPDX-License-Identifier: Apache-2.0 |
ram54288 | 0:a7a43371b306 | 4 | * Licensed under the Apache License, Version 2.0 (the License); you may |
ram54288 | 0:a7a43371b306 | 5 | * not use this file except in compliance with the License. |
ram54288 | 0:a7a43371b306 | 6 | * You may obtain a copy of the License at |
ram54288 | 0:a7a43371b306 | 7 | * |
ram54288 | 0:a7a43371b306 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
ram54288 | 0:a7a43371b306 | 9 | * |
ram54288 | 0:a7a43371b306 | 10 | * Unless required by applicable law or agreed to in writing, software |
ram54288 | 0:a7a43371b306 | 11 | * distributed under the License is distributed on an AS IS BASIS, WITHOUT |
ram54288 | 0:a7a43371b306 | 12 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
ram54288 | 0:a7a43371b306 | 13 | * See the License for the specific language governing permissions and |
ram54288 | 0:a7a43371b306 | 14 | * limitations under the License. |
ram54288 | 0:a7a43371b306 | 15 | */ |
ram54288 | 0:a7a43371b306 | 16 | |
ram54288 | 0:a7a43371b306 | 17 | #include "pal.h" |
ram54288 | 0:a7a43371b306 | 18 | #include "pal_network.h" |
ram54288 | 0:a7a43371b306 | 19 | #include "pal_plat_network.h" |
ram54288 | 0:a7a43371b306 | 20 | #include "unity.h" |
ram54288 | 0:a7a43371b306 | 21 | #include "unity_fixture.h" |
ram54288 | 0:a7a43371b306 | 22 | #include "pal_test_utils.h" |
ram54288 | 0:a7a43371b306 | 23 | #include "pal_socket_test_utils.h" |
ram54288 | 0:a7a43371b306 | 24 | #include "string.h" |
ram54288 | 0:a7a43371b306 | 25 | |
ram54288 | 0:a7a43371b306 | 26 | |
ram54288 | 0:a7a43371b306 | 27 | |
ram54288 | 0:a7a43371b306 | 28 | TEST_GROUP(pal_socket); |
ram54288 | 0:a7a43371b306 | 29 | |
ram54288 | 0:a7a43371b306 | 30 | //sometimes you may want to get at local data in a module. |
ram54288 | 0:a7a43371b306 | 31 | //for example: If you plan to pass by reference, this could be useful |
ram54288 | 0:a7a43371b306 | 32 | //however, it should often be avoided |
ram54288 | 0:a7a43371b306 | 33 | //extern int Counter; |
ram54288 | 0:a7a43371b306 | 34 | |
ram54288 | 0:a7a43371b306 | 35 | #define PAL_NET_SUPPORT_LWIP 1 |
ram54288 | 0:a7a43371b306 | 36 | #define PAL_NET_TEST_SERVER_NAME "e109180-lin.kfn.arm.com" |
ram54288 | 0:a7a43371b306 | 37 | #define PAL_NET_TEST_SERVER_IP {10,45,48,190} |
ram54288 | 0:a7a43371b306 | 38 | #define PAL_NET_TEST_SERVER_IP_STRING "10.45.48.190" |
ram54288 | 0:a7a43371b306 | 39 | #define PAL_NET_TEST_SERVER_HTTP_PORT 8686 |
ram54288 | 0:a7a43371b306 | 40 | #define PAL_NET_TEST_SERVER_UDP_PORT 8383 |
ram54288 | 0:a7a43371b306 | 41 | #define PAL_NET_TEST_INCOMING_PORT 8000 |
ram54288 | 0:a7a43371b306 | 42 | |
ram54288 | 0:a7a43371b306 | 43 | void * g_networkInterface = NULL; |
ram54288 | 0:a7a43371b306 | 44 | |
ram54288 | 0:a7a43371b306 | 45 | |
ram54288 | 0:a7a43371b306 | 46 | static uint32_t s_callbackcounter = 0; |
ram54288 | 0:a7a43371b306 | 47 | |
ram54288 | 0:a7a43371b306 | 48 | void socketCallback() |
ram54288 | 0:a7a43371b306 | 49 | { |
ram54288 | 0:a7a43371b306 | 50 | s_callbackcounter++; |
ram54288 | 0:a7a43371b306 | 51 | } |
ram54288 | 0:a7a43371b306 | 52 | |
ram54288 | 0:a7a43371b306 | 53 | TEST_SETUP(pal_socket) |
ram54288 | 0:a7a43371b306 | 54 | { |
ram54288 | 0:a7a43371b306 | 55 | uint32_t index = 0; |
ram54288 | 0:a7a43371b306 | 56 | palStatus_t status = PAL_SUCCESS; |
ram54288 | 0:a7a43371b306 | 57 | static void * interfaceCTX = NULL; |
ram54288 | 0:a7a43371b306 | 58 | //This is run before EACH TEST |
ram54288 | 0:a7a43371b306 | 59 | if (!interfaceCTX) |
ram54288 | 0:a7a43371b306 | 60 | { |
ram54288 | 0:a7a43371b306 | 61 | status = pal_init(); |
ram54288 | 0:a7a43371b306 | 62 | if (PAL_SUCCESS == status) |
ram54288 | 0:a7a43371b306 | 63 | { |
ram54288 | 0:a7a43371b306 | 64 | interfaceCTX = palTestGetNetWorkInterfaceContext(); |
ram54288 | 0:a7a43371b306 | 65 | pal_registerNetworkInterface(interfaceCTX , &index); |
ram54288 | 0:a7a43371b306 | 66 | g_networkInterface = interfaceCTX; |
ram54288 | 0:a7a43371b306 | 67 | } |
ram54288 | 0:a7a43371b306 | 68 | } |
ram54288 | 0:a7a43371b306 | 69 | } |
ram54288 | 0:a7a43371b306 | 70 | |
ram54288 | 0:a7a43371b306 | 71 | TEST_TEAR_DOWN(pal_socket) |
ram54288 | 0:a7a43371b306 | 72 | { |
ram54288 | 0:a7a43371b306 | 73 | } |
ram54288 | 0:a7a43371b306 | 74 | |
ram54288 | 0:a7a43371b306 | 75 | #define PAL_TEST_BUFFER_SIZE 50 |
ram54288 | 0:a7a43371b306 | 76 | |
ram54288 | 0:a7a43371b306 | 77 | TEST(pal_socket, socketUDPCreationOptionsTest) |
ram54288 | 0:a7a43371b306 | 78 | { |
ram54288 | 0:a7a43371b306 | 79 | palStatus_t result = PAL_SUCCESS; |
ram54288 | 0:a7a43371b306 | 80 | palSocket_t sock = 0; |
ram54288 | 0:a7a43371b306 | 81 | palSocket_t sock2 = 0; |
ram54288 | 0:a7a43371b306 | 82 | palSocket_t sock3 = 0; |
ram54288 | 0:a7a43371b306 | 83 | palSocket_t sock5 = 0; |
ram54288 | 0:a7a43371b306 | 84 | uint32_t numInterface = 0; |
ram54288 | 0:a7a43371b306 | 85 | palNetInterfaceInfo_t interfaceInfo; |
ram54288 | 0:a7a43371b306 | 86 | uint32_t interfaceIndex = 0; |
ram54288 | 0:a7a43371b306 | 87 | uint32_t sockOptVal = 5000; |
ram54288 | 0:a7a43371b306 | 88 | uint32_t sockOptLen = sizeof(sockOptVal); |
ram54288 | 0:a7a43371b306 | 89 | |
ram54288 | 0:a7a43371b306 | 90 | TEST_PRINTF("start socket test\r\n"); |
ram54288 | 0:a7a43371b306 | 91 | |
ram54288 | 0:a7a43371b306 | 92 | memset(&interfaceInfo,0,sizeof(interfaceInfo)); |
ram54288 | 0:a7a43371b306 | 93 | // check that re-addignt he network interface returns the same index |
ram54288 | 0:a7a43371b306 | 94 | pal_registerNetworkInterface(g_networkInterface, &interfaceIndex); |
ram54288 | 0:a7a43371b306 | 95 | TEST_ASSERT_EQUAL(interfaceIndex, 0); |
ram54288 | 0:a7a43371b306 | 96 | pal_registerNetworkInterface(g_networkInterface, &interfaceIndex); |
ram54288 | 0:a7a43371b306 | 97 | TEST_ASSERT_EQUAL(interfaceIndex, 0); |
ram54288 | 0:a7a43371b306 | 98 | |
ram54288 | 0:a7a43371b306 | 99 | TEST_PRINTF("create sockets\r\n"); |
ram54288 | 0:a7a43371b306 | 100 | |
ram54288 | 0:a7a43371b306 | 101 | |
ram54288 | 0:a7a43371b306 | 102 | //blocking |
ram54288 | 0:a7a43371b306 | 103 | result = pal_socket(PAL_AF_INET, PAL_SOCK_DGRAM, false, interfaceIndex, &sock); |
ram54288 | 0:a7a43371b306 | 104 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 105 | result = pal_socket(PAL_AF_INET, PAL_SOCK_DGRAM, false, interfaceIndex, &sock2); |
ram54288 | 0:a7a43371b306 | 106 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 107 | //non-blocking |
ram54288 | 0:a7a43371b306 | 108 | result = pal_socket(PAL_AF_INET, PAL_SOCK_DGRAM, true, interfaceIndex, &sock5); |
ram54288 | 0:a7a43371b306 | 109 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 110 | |
ram54288 | 0:a7a43371b306 | 111 | result = pal_asynchronousSocket(PAL_AF_INET, PAL_SOCK_STREAM, false, interfaceIndex, socketCallback, &sock3); |
ram54288 | 0:a7a43371b306 | 112 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 113 | |
ram54288 | 0:a7a43371b306 | 114 | |
ram54288 | 0:a7a43371b306 | 115 | result = pal_getNumberOfNetInterfaces(&numInterface); |
ram54288 | 0:a7a43371b306 | 116 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 117 | TEST_ASSERT_EQUAL(numInterface, 1); |
ram54288 | 0:a7a43371b306 | 118 | |
ram54288 | 0:a7a43371b306 | 119 | result = pal_getNetInterfaceInfo(0, &interfaceInfo); |
ram54288 | 0:a7a43371b306 | 120 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 121 | TEST_PRINTF("interface addr: %d %d %d %d \r\n", interfaceInfo.address.addressData[2], interfaceInfo.address.addressData[3], interfaceInfo.address.addressData[4], interfaceInfo.address.addressData[5]); |
ram54288 | 0:a7a43371b306 | 122 | |
ram54288 | 0:a7a43371b306 | 123 | |
ram54288 | 0:a7a43371b306 | 124 | result = pal_setSocketOptions(sock, PAL_SO_RCVTIMEO, &sockOptVal, sockOptLen); |
ram54288 | 0:a7a43371b306 | 125 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 126 | |
ram54288 | 0:a7a43371b306 | 127 | TEST_PRINTF("close sockets\r\n"); |
ram54288 | 0:a7a43371b306 | 128 | |
ram54288 | 0:a7a43371b306 | 129 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 130 | result = pal_close(&sock); |
ram54288 | 0:a7a43371b306 | 131 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 132 | result = pal_close(&sock2); |
ram54288 | 0:a7a43371b306 | 133 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 134 | result = pal_close(&sock5); |
ram54288 | 0:a7a43371b306 | 135 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 136 | result = pal_close(&sock3); |
ram54288 | 0:a7a43371b306 | 137 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 138 | |
ram54288 | 0:a7a43371b306 | 139 | TEST_PRINTF("end\r\n"); |
ram54288 | 0:a7a43371b306 | 140 | |
ram54288 | 0:a7a43371b306 | 141 | } |
ram54288 | 0:a7a43371b306 | 142 | |
ram54288 | 0:a7a43371b306 | 143 | |
ram54288 | 0:a7a43371b306 | 144 | TEST(pal_socket, basicTCPclinetSendRecieve) |
ram54288 | 0:a7a43371b306 | 145 | { |
ram54288 | 0:a7a43371b306 | 146 | |
ram54288 | 0:a7a43371b306 | 147 | |
ram54288 | 0:a7a43371b306 | 148 | palStatus_t result = PAL_SUCCESS; |
ram54288 | 0:a7a43371b306 | 149 | palSocket_t sock = 0; |
ram54288 | 0:a7a43371b306 | 150 | palSocketAddress_t address = { 0 }; |
ram54288 | 0:a7a43371b306 | 151 | const char* message = "GET / HTTP/1.0\r\n\r\n"; |
ram54288 | 0:a7a43371b306 | 152 | size_t sent = 0; |
ram54288 | 0:a7a43371b306 | 153 | char buffer[100] = { 0 }; |
ram54288 | 0:a7a43371b306 | 154 | size_t read = 0; |
ram54288 | 0:a7a43371b306 | 155 | palSocketLength_t addrlen = 0; |
ram54288 | 0:a7a43371b306 | 156 | |
ram54288 | 0:a7a43371b306 | 157 | result = pal_socket(PAL_AF_INET, PAL_SOCK_STREAM, false, 0, &sock); |
ram54288 | 0:a7a43371b306 | 158 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 159 | |
ram54288 | 0:a7a43371b306 | 160 | result = pal_getAddressInfo(PAL_NET_TEST_SERVER_NAME, &address, &addrlen); |
ram54288 | 0:a7a43371b306 | 161 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 162 | |
ram54288 | 0:a7a43371b306 | 163 | result = pal_setSockAddrPort(&address, PAL_NET_TEST_SERVER_HTTP_PORT); |
ram54288 | 0:a7a43371b306 | 164 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 165 | |
ram54288 | 0:a7a43371b306 | 166 | result = pal_connect(sock, &address, 16); |
ram54288 | 0:a7a43371b306 | 167 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 168 | |
ram54288 | 0:a7a43371b306 | 169 | result = pal_send(sock, message, 45, &sent); |
ram54288 | 0:a7a43371b306 | 170 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 171 | |
ram54288 | 0:a7a43371b306 | 172 | result = pal_recv(sock, buffer, 99, &read); |
ram54288 | 0:a7a43371b306 | 173 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 174 | TEST_PRINTF(buffer); |
ram54288 | 0:a7a43371b306 | 175 | |
ram54288 | 0:a7a43371b306 | 176 | TEST_ASSERT(read >= 4); |
ram54288 | 0:a7a43371b306 | 177 | TEST_ASSERT(buffer[0] == 'H' && buffer[1] == 'T'&& buffer[2] == 'T' && buffer[3] == 'P'); |
ram54288 | 0:a7a43371b306 | 178 | pal_close(&sock); |
ram54288 | 0:a7a43371b306 | 179 | |
ram54288 | 0:a7a43371b306 | 180 | TEST_PRINTF("test Done"); |
ram54288 | 0:a7a43371b306 | 181 | |
ram54288 | 0:a7a43371b306 | 182 | } |
ram54288 | 0:a7a43371b306 | 183 | |
ram54288 | 0:a7a43371b306 | 184 | TEST(pal_socket, basicUDPclinetSendRecieve) |
ram54288 | 0:a7a43371b306 | 185 | { |
ram54288 | 0:a7a43371b306 | 186 | |
ram54288 | 0:a7a43371b306 | 187 | palStatus_t result = PAL_SUCCESS; |
ram54288 | 0:a7a43371b306 | 188 | palSocket_t sock = 0; |
ram54288 | 0:a7a43371b306 | 189 | palSocketAddress_t address = { 0 }; |
ram54288 | 0:a7a43371b306 | 190 | palSocketAddress_t address2 = { 0 }; |
ram54288 | 0:a7a43371b306 | 191 | uint8_t buffer[10] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 }; |
ram54288 | 0:a7a43371b306 | 192 | size_t sent = 0; |
ram54288 | 0:a7a43371b306 | 193 | size_t read = 0; |
ram54288 | 0:a7a43371b306 | 194 | palSocketLength_t addrlen = 0; |
ram54288 | 0:a7a43371b306 | 195 | |
ram54288 | 0:a7a43371b306 | 196 | result = pal_socket(PAL_AF_INET, PAL_SOCK_DGRAM, false, 0, &sock); |
ram54288 | 0:a7a43371b306 | 197 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 198 | |
ram54288 | 0:a7a43371b306 | 199 | result = pal_getAddressInfo(PAL_NET_TEST_SERVER_NAME, &address, &addrlen); |
ram54288 | 0:a7a43371b306 | 200 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 201 | result = pal_setSockAddrPort(&address, PAL_NET_TEST_SERVER_UDP_PORT); |
ram54288 | 0:a7a43371b306 | 202 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 203 | |
ram54288 | 0:a7a43371b306 | 204 | TEST_PRINTF("udp send \r\n"); |
ram54288 | 0:a7a43371b306 | 205 | result = pal_sendTo(sock, buffer, 10, &address, 16, &sent); |
ram54288 | 0:a7a43371b306 | 206 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 207 | TEST_ASSERT_EQUAL(sent, 10); |
ram54288 | 0:a7a43371b306 | 208 | result = pal_plat_receiveFrom(sock, buffer, 10, &address2, &addrlen, &read); |
ram54288 | 0:a7a43371b306 | 209 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 210 | TEST_ASSERT_EQUAL(read, 10); |
ram54288 | 0:a7a43371b306 | 211 | TEST_PRINTF("udp done \r\n"); |
ram54288 | 0:a7a43371b306 | 212 | pal_close(&sock); |
ram54288 | 0:a7a43371b306 | 213 | } |
ram54288 | 0:a7a43371b306 | 214 | |
ram54288 | 0:a7a43371b306 | 215 | |
ram54288 | 0:a7a43371b306 | 216 | TEST(pal_socket, basicSocketScenario3) |
ram54288 | 0:a7a43371b306 | 217 | { |
ram54288 | 0:a7a43371b306 | 218 | palStatus_t result = PAL_SUCCESS; |
ram54288 | 0:a7a43371b306 | 219 | palSocket_t sock = 0; |
ram54288 | 0:a7a43371b306 | 220 | palSocketAddress_t address = { 0 }; |
ram54288 | 0:a7a43371b306 | 221 | const char* message = "GET / HTTP/1.0\r\nHost:10.45.48.68:8000\r\n\r\n"; |
ram54288 | 0:a7a43371b306 | 222 | size_t sent = 0; |
ram54288 | 0:a7a43371b306 | 223 | char buffer[100] = { 0 }; |
ram54288 | 0:a7a43371b306 | 224 | size_t read = 0; |
ram54288 | 0:a7a43371b306 | 225 | s_callbackcounter = 0; |
ram54288 | 0:a7a43371b306 | 226 | palSocketLength_t addrlen = 0; |
ram54288 | 0:a7a43371b306 | 227 | |
ram54288 | 0:a7a43371b306 | 228 | result = pal_asynchronousSocket(PAL_AF_INET, PAL_SOCK_STREAM, false, 0, socketCallback, &sock); |
ram54288 | 0:a7a43371b306 | 229 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 230 | |
ram54288 | 0:a7a43371b306 | 231 | result = pal_getAddressInfo(PAL_NET_TEST_SERVER_NAME, &address, &addrlen); |
ram54288 | 0:a7a43371b306 | 232 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 233 | |
ram54288 | 0:a7a43371b306 | 234 | result = pal_setSockAddrPort(&address, PAL_NET_TEST_SERVER_HTTP_PORT); |
ram54288 | 0:a7a43371b306 | 235 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 236 | |
ram54288 | 0:a7a43371b306 | 237 | result = pal_connect(sock, &address, 16); |
ram54288 | 0:a7a43371b306 | 238 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 239 | |
ram54288 | 0:a7a43371b306 | 240 | result = pal_send(sock, message, 45, &sent); |
ram54288 | 0:a7a43371b306 | 241 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 242 | |
ram54288 | 0:a7a43371b306 | 243 | result = pal_recv(sock, buffer, 99, &read); |
ram54288 | 0:a7a43371b306 | 244 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 245 | TEST_PRINTF(buffer); |
ram54288 | 0:a7a43371b306 | 246 | |
ram54288 | 0:a7a43371b306 | 247 | TEST_ASSERT(read >= 4); |
ram54288 | 0:a7a43371b306 | 248 | TEST_ASSERT(buffer[0] == 'H' && buffer[1] == 'T'&& buffer[2] == 'T' && buffer[3] == 'P'); |
ram54288 | 0:a7a43371b306 | 249 | TEST_ASSERT(s_callbackcounter > 0); |
ram54288 | 0:a7a43371b306 | 250 | TEST_PRINTF("callback counter %d \r\n", s_callbackcounter); |
ram54288 | 0:a7a43371b306 | 251 | pal_close(&sock); |
ram54288 | 0:a7a43371b306 | 252 | |
ram54288 | 0:a7a43371b306 | 253 | TEST_PRINTF("test Done"); |
ram54288 | 0:a7a43371b306 | 254 | } |
ram54288 | 0:a7a43371b306 | 255 | |
ram54288 | 0:a7a43371b306 | 256 | TEST(pal_socket, basicSocketScenario4) |
ram54288 | 0:a7a43371b306 | 257 | { |
ram54288 | 0:a7a43371b306 | 258 | palStatus_t result = PAL_SUCCESS; |
ram54288 | 0:a7a43371b306 | 259 | palSocket_t sock = 0; |
ram54288 | 0:a7a43371b306 | 260 | palSocket_t sock2 = 0; |
ram54288 | 0:a7a43371b306 | 261 | palSocketAddress_t address = { 0 }; |
ram54288 | 0:a7a43371b306 | 262 | const char* message = "GET / HTTP/1.0\r\n\r\n"; |
ram54288 | 0:a7a43371b306 | 263 | size_t sent = 0; |
ram54288 | 0:a7a43371b306 | 264 | char buffer[100] = { 0 }; |
ram54288 | 0:a7a43371b306 | 265 | size_t read = 0; |
ram54288 | 0:a7a43371b306 | 266 | palSocketLength_t addlen = 0; |
ram54288 | 0:a7a43371b306 | 267 | uint32_t numSockets = 0; |
ram54288 | 0:a7a43371b306 | 268 | palSocket_t socketsToCheck[PAL_NET_SOCKET_SELECT_MAX_SOCKETS] = { 0 }; |
ram54288 | 0:a7a43371b306 | 269 | pal_timeVal_t tv = {0}; |
ram54288 | 0:a7a43371b306 | 270 | uint8_t palSocketStatus[PAL_NET_SOCKET_SELECT_MAX_SOCKETS] = { 0 }; |
ram54288 | 0:a7a43371b306 | 271 | |
ram54288 | 0:a7a43371b306 | 272 | result = pal_socket(PAL_AF_INET, PAL_SOCK_STREAM, false, 0, &sock); |
ram54288 | 0:a7a43371b306 | 273 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 274 | |
ram54288 | 0:a7a43371b306 | 275 | result = pal_socket(PAL_AF_INET, PAL_SOCK_STREAM, false, 0, &sock2); |
ram54288 | 0:a7a43371b306 | 276 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 277 | |
ram54288 | 0:a7a43371b306 | 278 | result = pal_getAddressInfo("www.w3.org", &address, &addlen); |
ram54288 | 0:a7a43371b306 | 279 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 280 | TEST_PRINTF("addr lookup: %d %d %d %d \r\n", address.addressData[2], address.addressData[3], address.addressData[4], address.addressData[5]); |
ram54288 | 0:a7a43371b306 | 281 | |
ram54288 | 0:a7a43371b306 | 282 | result = pal_setSockAddrPort(&address, 80); |
ram54288 | 0:a7a43371b306 | 283 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 284 | |
ram54288 | 0:a7a43371b306 | 285 | result = pal_connect(sock, &address, 16); |
ram54288 | 0:a7a43371b306 | 286 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 287 | |
ram54288 | 0:a7a43371b306 | 288 | result = pal_send(sock, message, 45, &sent); |
ram54288 | 0:a7a43371b306 | 289 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 290 | |
ram54288 | 0:a7a43371b306 | 291 | socketsToCheck[0] = sock; |
ram54288 | 0:a7a43371b306 | 292 | socketsToCheck[1] = sock2; |
ram54288 | 0:a7a43371b306 | 293 | tv.pal_tv_sec = 5; |
ram54288 | 0:a7a43371b306 | 294 | tv.pal_tv_usec = 1000; |
ram54288 | 0:a7a43371b306 | 295 | |
ram54288 | 0:a7a43371b306 | 296 | result = pal_plat_socketMiniSelect(socketsToCheck, 2, &tv, palSocketStatus, &numSockets); |
ram54288 | 0:a7a43371b306 | 297 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 298 | //TEST_ASSERT_EQUAL(numSockets, 1); |
ram54288 | 0:a7a43371b306 | 299 | //TEST_ASSERT(palSocketStatus[0] >= 0); |
ram54288 | 0:a7a43371b306 | 300 | |
ram54288 | 0:a7a43371b306 | 301 | result = pal_recv(sock, buffer, 99, &read); |
ram54288 | 0:a7a43371b306 | 302 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 303 | TEST_PRINTF(buffer); |
ram54288 | 0:a7a43371b306 | 304 | |
ram54288 | 0:a7a43371b306 | 305 | TEST_ASSERT(read >= 4); |
ram54288 | 0:a7a43371b306 | 306 | TEST_ASSERT(buffer[0] == 'H' && buffer[1] == 'T'&& buffer[2] == 'T' && buffer[3] == 'P'); |
ram54288 | 0:a7a43371b306 | 307 | |
ram54288 | 0:a7a43371b306 | 308 | socketsToCheck[0] = sock2; |
ram54288 | 0:a7a43371b306 | 309 | socketsToCheck[1] = 0; |
ram54288 | 0:a7a43371b306 | 310 | tv.pal_tv_sec = 0; |
ram54288 | 0:a7a43371b306 | 311 | tv.pal_tv_usec = 20000; |
ram54288 | 0:a7a43371b306 | 312 | result = pal_plat_socketMiniSelect(socketsToCheck, 1, &tv, palSocketStatus, &numSockets); |
ram54288 | 0:a7a43371b306 | 313 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 314 | TEST_ASSERT_EQUAL(numSockets, 0); |
ram54288 | 0:a7a43371b306 | 315 | TEST_ASSERT(palSocketStatus[0] == 0); |
ram54288 | 0:a7a43371b306 | 316 | |
ram54288 | 0:a7a43371b306 | 317 | pal_close(&sock); |
ram54288 | 0:a7a43371b306 | 318 | pal_close(&sock2); |
ram54288 | 0:a7a43371b306 | 319 | |
ram54288 | 0:a7a43371b306 | 320 | TEST_PRINTF("test Done"); |
ram54288 | 0:a7a43371b306 | 321 | |
ram54288 | 0:a7a43371b306 | 322 | } |
ram54288 | 0:a7a43371b306 | 323 | |
ram54288 | 0:a7a43371b306 | 324 | TEST(pal_socket, basicSocketScenario5) |
ram54288 | 0:a7a43371b306 | 325 | { |
ram54288 | 0:a7a43371b306 | 326 | palStatus_t result = PAL_SUCCESS; |
ram54288 | 0:a7a43371b306 | 327 | palSocket_t sock = 0; |
ram54288 | 0:a7a43371b306 | 328 | palSocket_t sock2 = 0; |
ram54288 | 0:a7a43371b306 | 329 | palSocket_t sock3 = 0; |
ram54288 | 0:a7a43371b306 | 330 | |
ram54288 | 0:a7a43371b306 | 331 | palSocketAddress_t address2 = { 0 }; |
ram54288 | 0:a7a43371b306 | 332 | |
ram54288 | 0:a7a43371b306 | 333 | char buffer[100] = { 0 }; |
ram54288 | 0:a7a43371b306 | 334 | const char* messageOut = "HTTP/1.0 200 OK"; |
ram54288 | 0:a7a43371b306 | 335 | size_t sent = 0; |
ram54288 | 0:a7a43371b306 | 336 | size_t read = 0; |
ram54288 | 0:a7a43371b306 | 337 | palSocketLength_t addrlen = 16; |
ram54288 | 0:a7a43371b306 | 338 | palNetInterfaceInfo_t interfaceInfo; |
ram54288 | 0:a7a43371b306 | 339 | |
ram54288 | 0:a7a43371b306 | 340 | memset(&interfaceInfo,0,sizeof(interfaceInfo)); |
ram54288 | 0:a7a43371b306 | 341 | |
ram54288 | 0:a7a43371b306 | 342 | |
ram54288 | 0:a7a43371b306 | 343 | result = pal_socket(PAL_AF_INET, PAL_SOCK_STREAM_SERVER, false, 0, &sock); |
ram54288 | 0:a7a43371b306 | 344 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 345 | |
ram54288 | 0:a7a43371b306 | 346 | result = pal_socket(PAL_AF_INET, PAL_SOCK_STREAM, false, 0, &sock2); |
ram54288 | 0:a7a43371b306 | 347 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 348 | |
ram54288 | 0:a7a43371b306 | 349 | result = pal_getNetInterfaceInfo(0, &interfaceInfo); |
ram54288 | 0:a7a43371b306 | 350 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 351 | |
ram54288 | 0:a7a43371b306 | 352 | result = pal_setSockAddrPort(&(interfaceInfo.address), PAL_NET_TEST_INCOMING_PORT); |
ram54288 | 0:a7a43371b306 | 353 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 354 | |
ram54288 | 0:a7a43371b306 | 355 | result = pal_bind(sock, &(interfaceInfo.address), interfaceInfo.addressSize); |
ram54288 | 0:a7a43371b306 | 356 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 357 | |
ram54288 | 0:a7a43371b306 | 358 | result = pal_listen(sock, 10); |
ram54288 | 0:a7a43371b306 | 359 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 360 | |
ram54288 | 0:a7a43371b306 | 361 | TEST_PRINTF("waiting for connection:\r\n"); |
ram54288 | 0:a7a43371b306 | 362 | result = pal_accept(sock, &address2, &addrlen, &sock2); |
ram54288 | 0:a7a43371b306 | 363 | TEST_PRINTF("after accept:\r\n"); |
ram54288 | 0:a7a43371b306 | 364 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 365 | |
ram54288 | 0:a7a43371b306 | 366 | |
ram54288 | 0:a7a43371b306 | 367 | result = pal_recv(sock2, buffer, 99, &read); |
ram54288 | 0:a7a43371b306 | 368 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 369 | TEST_PRINTF(buffer); |
ram54288 | 0:a7a43371b306 | 370 | |
ram54288 | 0:a7a43371b306 | 371 | result = pal_send(sock2, messageOut, 15, &sent); |
ram54288 | 0:a7a43371b306 | 372 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 373 | |
ram54288 | 0:a7a43371b306 | 374 | |
ram54288 | 0:a7a43371b306 | 375 | result = pal_socket(PAL_AF_INET, PAL_SOCK_STREAM, false, 0, &sock3); |
ram54288 | 0:a7a43371b306 | 376 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 377 | |
ram54288 | 0:a7a43371b306 | 378 | |
ram54288 | 0:a7a43371b306 | 379 | |
ram54288 | 0:a7a43371b306 | 380 | pal_close(&sock2); |
ram54288 | 0:a7a43371b306 | 381 | pal_close(&sock3); |
ram54288 | 0:a7a43371b306 | 382 | pal_close(&sock); |
ram54288 | 0:a7a43371b306 | 383 | |
ram54288 | 0:a7a43371b306 | 384 | TEST_PRINTF("test Done"); |
ram54288 | 0:a7a43371b306 | 385 | } |
ram54288 | 0:a7a43371b306 | 386 | |
ram54288 | 0:a7a43371b306 | 387 | |
ram54288 | 0:a7a43371b306 | 388 | TEST(pal_socket, tProvUDPTest) |
ram54288 | 0:a7a43371b306 | 389 | { |
ram54288 | 0:a7a43371b306 | 390 | |
ram54288 | 0:a7a43371b306 | 391 | palStatus_t result = PAL_SUCCESS; |
ram54288 | 0:a7a43371b306 | 392 | palSocket_t sock = 0; |
ram54288 | 0:a7a43371b306 | 393 | palSocketAddress_t address = { 0 }; |
ram54288 | 0:a7a43371b306 | 394 | palSocketAddress_t address2 = { 0 }; |
ram54288 | 0:a7a43371b306 | 395 | char buffer[100] = { 0 }; |
ram54288 | 0:a7a43371b306 | 396 | const char* messageOut = "HTTP/1.0 200 OK"; |
ram54288 | 0:a7a43371b306 | 397 | size_t sent = 0; |
ram54288 | 0:a7a43371b306 | 398 | size_t read = 0; |
ram54288 | 0:a7a43371b306 | 399 | palSocketLength_t addrlen = 16; |
ram54288 | 0:a7a43371b306 | 400 | palSocketLength_t addrlen2 = 16; |
ram54288 | 0:a7a43371b306 | 401 | int timeout = 10000; |
ram54288 | 0:a7a43371b306 | 402 | result = pal_socket(PAL_AF_INET, PAL_SOCK_DGRAM, false, 0, &sock); |
ram54288 | 0:a7a43371b306 | 403 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 404 | |
ram54288 | 0:a7a43371b306 | 405 | result = pal_getAddressInfo(PAL_NET_TEST_SERVER_IP_STRING, &address, &addrlen); |
ram54288 | 0:a7a43371b306 | 406 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 407 | |
ram54288 | 0:a7a43371b306 | 408 | result = pal_setSockAddrPort(&address, PAL_NET_TEST_SERVER_UDP_PORT); |
ram54288 | 0:a7a43371b306 | 409 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 410 | |
ram54288 | 0:a7a43371b306 | 411 | result = pal_setSocketOptions(sock, PAL_SO_SNDTIMEO, &timeout, sizeof(timeout)); |
ram54288 | 0:a7a43371b306 | 412 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 413 | |
ram54288 | 0:a7a43371b306 | 414 | result = pal_sendTo(sock, messageOut, 16, &address, addrlen, &sent); |
ram54288 | 0:a7a43371b306 | 415 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 416 | TEST_ASSERT_EQUAL(sent, 16); |
ram54288 | 0:a7a43371b306 | 417 | |
ram54288 | 0:a7a43371b306 | 418 | result = pal_receiveFrom(sock, buffer, 100, NULL, NULL, &read); |
ram54288 | 0:a7a43371b306 | 419 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 420 | TEST_ASSERT_EQUAL(read, 16); |
ram54288 | 0:a7a43371b306 | 421 | |
ram54288 | 0:a7a43371b306 | 422 | timeout = 1; |
ram54288 | 0:a7a43371b306 | 423 | result = pal_setSocketOptions(sock, PAL_SO_RCVTIMEO, &timeout, sizeof(timeout)); |
ram54288 | 0:a7a43371b306 | 424 | TEST_ASSERT_EQUAL(result, PAL_SUCCESS); |
ram54288 | 0:a7a43371b306 | 425 | |
ram54288 | 0:a7a43371b306 | 426 | result = pal_receiveFrom(sock, buffer, 100, &address2, &addrlen2, &read); // should get timeout |
ram54288 | 0:a7a43371b306 | 427 | TEST_ASSERT_EQUAL(result, PAL_ERR_SOCKET_WOULD_BLOCK); |
ram54288 | 0:a7a43371b306 | 428 | } |