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