1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 2f08c3bdfSopenharmony_ci/* 3f08c3bdfSopenharmony_ci * Copyright (c) 2019 Martin Doucha <mdoucha@suse.cz> 4f08c3bdfSopenharmony_ci */ 5f08c3bdfSopenharmony_ci 6f08c3bdfSopenharmony_ci/* 7f08c3bdfSopenharmony_ci * Create and bind socket for various standard stream protocols. 8f08c3bdfSopenharmony_ci * Then connect to it and send some test data. 9f08c3bdfSopenharmony_ci */ 10f08c3bdfSopenharmony_ci 11f08c3bdfSopenharmony_ci#include <string.h> 12f08c3bdfSopenharmony_ci#include <stdlib.h> 13f08c3bdfSopenharmony_ci#include <time.h> 14f08c3bdfSopenharmony_ci#include <pthread.h> 15f08c3bdfSopenharmony_ci 16f08c3bdfSopenharmony_ci#include "tst_test.h" 17f08c3bdfSopenharmony_ci#include "tst_net.h" 18f08c3bdfSopenharmony_ci#include "tst_safe_pthread.h" 19f08c3bdfSopenharmony_ci#include "libbind.h" 20f08c3bdfSopenharmony_ci 21f08c3bdfSopenharmony_cistatic struct sockaddr_un unix_addr = { 22f08c3bdfSopenharmony_ci .sun_family = AF_UNIX, 23f08c3bdfSopenharmony_ci .sun_path = MAIN_SOCKET_FILE 24f08c3bdfSopenharmony_ci}; 25f08c3bdfSopenharmony_cistatic struct sockaddr_un abstract_addr = { 26f08c3bdfSopenharmony_ci .sun_family = AF_UNIX, 27f08c3bdfSopenharmony_ci .sun_path = ABSTRACT_SOCKET_PATH 28f08c3bdfSopenharmony_ci}; 29f08c3bdfSopenharmony_cistatic struct sockaddr_in ipv4_addr; 30f08c3bdfSopenharmony_cistatic struct sockaddr_in ipv4_any_addr; 31f08c3bdfSopenharmony_cistatic struct sockaddr_in6 ipv6_addr; 32f08c3bdfSopenharmony_cistatic struct sockaddr_in6 ipv6_any_addr; 33f08c3bdfSopenharmony_ci 34f08c3bdfSopenharmony_cistatic struct test_case testcase_list[] = { 35f08c3bdfSopenharmony_ci /* UNIX sockets */ 36f08c3bdfSopenharmony_ci {SOCK_STREAM, 0, (struct sockaddr *)&unix_addr, sizeof(unix_addr), 37f08c3bdfSopenharmony_ci "AF_UNIX pathname stream"}, 38f08c3bdfSopenharmony_ci {SOCK_SEQPACKET, 0, (struct sockaddr *)&unix_addr, sizeof(unix_addr), 39f08c3bdfSopenharmony_ci "AF_UNIX pathname seqpacket"}, 40f08c3bdfSopenharmony_ci {SOCK_STREAM, 0, (struct sockaddr *)&abstract_addr, 41f08c3bdfSopenharmony_ci sizeof(abstract_addr), "AF_UNIX abstract stream"}, 42f08c3bdfSopenharmony_ci {SOCK_SEQPACKET, 0, (struct sockaddr *)&abstract_addr, 43f08c3bdfSopenharmony_ci sizeof(abstract_addr), "AF_UNIX abstract seqpacket"}, 44f08c3bdfSopenharmony_ci 45f08c3bdfSopenharmony_ci /* IPv4 sockets */ 46f08c3bdfSopenharmony_ci {SOCK_STREAM, 0, (struct sockaddr *)&ipv4_addr, sizeof(ipv4_addr), 47f08c3bdfSopenharmony_ci "IPv4 loop TCP variant 1"}, 48f08c3bdfSopenharmony_ci {SOCK_STREAM, IPPROTO_TCP, (struct sockaddr *)&ipv4_addr, 49f08c3bdfSopenharmony_ci sizeof(ipv4_addr), "IPv4 loop TCP variant 2"}, 50f08c3bdfSopenharmony_ci {SOCK_STREAM, IPPROTO_SCTP, (struct sockaddr *)&ipv4_addr, 51f08c3bdfSopenharmony_ci sizeof(ipv4_addr), "IPv4 loop SCTP"}, 52f08c3bdfSopenharmony_ci {SOCK_STREAM, 0, (struct sockaddr *)&ipv4_any_addr, 53f08c3bdfSopenharmony_ci sizeof(ipv4_any_addr), "IPv4 any TCP variant 1"}, 54f08c3bdfSopenharmony_ci {SOCK_STREAM, IPPROTO_TCP, (struct sockaddr *)&ipv4_any_addr, 55f08c3bdfSopenharmony_ci sizeof(ipv4_any_addr), "IPv4 any TCP variant 2"}, 56f08c3bdfSopenharmony_ci {SOCK_STREAM, IPPROTO_SCTP, (struct sockaddr *)&ipv4_any_addr, 57f08c3bdfSopenharmony_ci sizeof(ipv4_any_addr), "IPv4 any SCTP"}, 58f08c3bdfSopenharmony_ci 59f08c3bdfSopenharmony_ci /* IPv6 sockets */ 60f08c3bdfSopenharmony_ci {SOCK_STREAM, 0, (struct sockaddr *)&ipv6_addr, sizeof(ipv6_addr), 61f08c3bdfSopenharmony_ci "IPv6 loop TCP variant 1"}, 62f08c3bdfSopenharmony_ci {SOCK_STREAM, IPPROTO_TCP, (struct sockaddr *)&ipv6_addr, 63f08c3bdfSopenharmony_ci sizeof(ipv6_addr), "IPv6 loop TCP variant 2"}, 64f08c3bdfSopenharmony_ci {SOCK_STREAM, IPPROTO_SCTP, (struct sockaddr *)&ipv6_addr, 65f08c3bdfSopenharmony_ci sizeof(ipv6_addr), "IPv6 loop SCTP"}, 66f08c3bdfSopenharmony_ci {SOCK_STREAM, 0, (struct sockaddr *)&ipv6_any_addr, 67f08c3bdfSopenharmony_ci sizeof(ipv6_any_addr), "IPv6 any TCP variant 1"}, 68f08c3bdfSopenharmony_ci {SOCK_STREAM, IPPROTO_TCP, (struct sockaddr *)&ipv6_any_addr, 69f08c3bdfSopenharmony_ci sizeof(ipv6_any_addr), "IPv6 any TCP variant 2"}, 70f08c3bdfSopenharmony_ci {SOCK_STREAM, IPPROTO_SCTP, (struct sockaddr *)&ipv6_any_addr, 71f08c3bdfSopenharmony_ci sizeof(ipv6_any_addr), "IPv6 any SCTP"} 72f08c3bdfSopenharmony_ci}; 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_cistatic void setup(void) 75f08c3bdfSopenharmony_ci{ 76f08c3bdfSopenharmony_ci srand(time(0)); 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_ci tst_init_sockaddr_inet(&ipv4_addr, IPV4_ADDRESS, 0); 79f08c3bdfSopenharmony_ci tst_init_sockaddr_inet_bin(&ipv4_any_addr, INADDR_ANY, 0); 80f08c3bdfSopenharmony_ci tst_init_sockaddr_inet6_bin(&ipv6_addr, &in6addr_loopback, 0); 81f08c3bdfSopenharmony_ci tst_init_sockaddr_inet6_bin(&ipv6_any_addr, &in6addr_any, 0); 82f08c3bdfSopenharmony_ci} 83f08c3bdfSopenharmony_ci 84f08c3bdfSopenharmony_cistatic void *peer_thread(void *tc_ptr) 85f08c3bdfSopenharmony_ci{ 86f08c3bdfSopenharmony_ci const struct test_case *tc = tc_ptr; 87f08c3bdfSopenharmony_ci int sock; 88f08c3bdfSopenharmony_ci unsigned int request; 89f08c3bdfSopenharmony_ci const char *response; 90f08c3bdfSopenharmony_ci 91f08c3bdfSopenharmony_ci sock = SAFE_SOCKET(tc->address->sa_family, tc->type, tc->protocol); 92f08c3bdfSopenharmony_ci SAFE_CONNECT(sock, tc->address, tc->addrlen); 93f08c3bdfSopenharmony_ci SAFE_READ(1, sock, &request, sizeof(request)); 94f08c3bdfSopenharmony_ci 95f08c3bdfSopenharmony_ci if (request < ARRAY_SIZE(testcase_list)) 96f08c3bdfSopenharmony_ci response = testcase_list[request].description; 97f08c3bdfSopenharmony_ci else 98f08c3bdfSopenharmony_ci response = "Invalid request value"; 99f08c3bdfSopenharmony_ci 100f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ALL, sock, response, strlen(response) + 1); 101f08c3bdfSopenharmony_ci SAFE_CLOSE(sock); 102f08c3bdfSopenharmony_ci return NULL; 103f08c3bdfSopenharmony_ci} 104f08c3bdfSopenharmony_ci 105f08c3bdfSopenharmony_cistatic void test_bind(unsigned int n) 106f08c3bdfSopenharmony_ci{ 107f08c3bdfSopenharmony_ci struct test_case tc_copy, *tc = testcase_list + n; 108f08c3bdfSopenharmony_ci struct sockaddr_storage listen_addr, remote_addr; 109f08c3bdfSopenharmony_ci struct sockaddr_un *tmp_addr; 110f08c3bdfSopenharmony_ci socklen_t remote_len = sizeof(struct sockaddr_storage); 111f08c3bdfSopenharmony_ci int listen_sock, sock, size; 112f08c3bdfSopenharmony_ci unsigned int rand_index; 113f08c3bdfSopenharmony_ci pthread_t thread_id; 114f08c3bdfSopenharmony_ci char buffer[BUFFER_SIZE]; 115f08c3bdfSopenharmony_ci const char *exp_data; 116f08c3bdfSopenharmony_ci 117f08c3bdfSopenharmony_ci tst_res(TINFO, "Testing %s", tc->description); 118f08c3bdfSopenharmony_ci listen_sock = SAFE_SOCKET(tc->address->sa_family, tc->type, 119f08c3bdfSopenharmony_ci tc->protocol); 120f08c3bdfSopenharmony_ci 121f08c3bdfSopenharmony_ci TST_EXP_PASS_SILENT(bind(listen_sock, tc->address, tc->addrlen), "bind()"); 122f08c3bdfSopenharmony_ci 123f08c3bdfSopenharmony_ci if (!TST_PASS) { 124f08c3bdfSopenharmony_ci SAFE_CLOSE(listen_sock); 125f08c3bdfSopenharmony_ci return; 126f08c3bdfSopenharmony_ci } 127f08c3bdfSopenharmony_ci 128f08c3bdfSopenharmony_ci /* 129f08c3bdfSopenharmony_ci * IPv4/IPv6 tests use wildcard addresses, resolve a valid connection 130f08c3bdfSopenharmony_ci * address for peer thread 131f08c3bdfSopenharmony_ci */ 132f08c3bdfSopenharmony_ci memcpy(&tc_copy, tc, sizeof(struct test_case)); 133f08c3bdfSopenharmony_ci tc_copy.addrlen = tst_get_connect_address(listen_sock, &listen_addr); 134f08c3bdfSopenharmony_ci tc_copy.address = (struct sockaddr *)&listen_addr; 135f08c3bdfSopenharmony_ci 136f08c3bdfSopenharmony_ci SAFE_LISTEN(listen_sock, 1); 137f08c3bdfSopenharmony_ci SAFE_PTHREAD_CREATE(&thread_id, NULL, peer_thread, &tc_copy); 138f08c3bdfSopenharmony_ci sock = SAFE_ACCEPT(listen_sock, (struct sockaddr *)&remote_addr, 139f08c3bdfSopenharmony_ci &remote_len); 140f08c3bdfSopenharmony_ci 141f08c3bdfSopenharmony_ci rand_index = rand() % ARRAY_SIZE(testcase_list); 142f08c3bdfSopenharmony_ci SAFE_WRITE(SAFE_WRITE_ALL, sock, &rand_index, sizeof(rand_index)); 143f08c3bdfSopenharmony_ci 144f08c3bdfSopenharmony_ci size = SAFE_READ(0, sock, buffer, BUFFER_SIZE - 1); 145f08c3bdfSopenharmony_ci buffer[size] = '\0'; 146f08c3bdfSopenharmony_ci exp_data = testcase_list[rand_index].description; 147f08c3bdfSopenharmony_ci 148f08c3bdfSopenharmony_ci if (!strcmp(buffer, exp_data)) 149f08c3bdfSopenharmony_ci tst_res(TPASS, "Communication successful"); 150f08c3bdfSopenharmony_ci else 151f08c3bdfSopenharmony_ci tst_res(TFAIL, "Received invalid data. Expected: \"%s\". " 152f08c3bdfSopenharmony_ci "Received: \"%s\"", exp_data, buffer); 153f08c3bdfSopenharmony_ci 154f08c3bdfSopenharmony_ci SAFE_CLOSE(sock); 155f08c3bdfSopenharmony_ci SAFE_CLOSE(listen_sock); 156f08c3bdfSopenharmony_ci pthread_join(thread_id, NULL); 157f08c3bdfSopenharmony_ci tmp_addr = (struct sockaddr_un *)tc->address; 158f08c3bdfSopenharmony_ci 159f08c3bdfSopenharmony_ci if (tc->address->sa_family == AF_UNIX && tmp_addr->sun_path[0]) 160f08c3bdfSopenharmony_ci SAFE_UNLINK(tmp_addr->sun_path); 161f08c3bdfSopenharmony_ci} 162f08c3bdfSopenharmony_ci 163f08c3bdfSopenharmony_cistatic struct tst_test test = { 164f08c3bdfSopenharmony_ci .test = test_bind, 165f08c3bdfSopenharmony_ci .tcnt = ARRAY_SIZE(testcase_list), 166f08c3bdfSopenharmony_ci .needs_tmpdir = 1, 167f08c3bdfSopenharmony_ci .setup = setup, 168f08c3bdfSopenharmony_ci}; 169