sl@0: /* sl@0: * Copyright (c) 1997-2009 Nokia Corporation and/or its subsidiary(-ies). sl@0: * All rights reserved. sl@0: * This component and the accompanying materials are made available sl@0: * under the terms of "Eclipse Public License v1.0" sl@0: * which accompanies this distribution, and is available sl@0: * at the URL "http://www.eclipse.org/legal/epl-v10.html". sl@0: * sl@0: * Initial Contributors: sl@0: * Nokia Corporation - initial contribution. sl@0: * sl@0: * Contributors: sl@0: * sl@0: * Description: sl@0: * Test of AF_LOCAL stream sockets sl@0: * sl@0: * sl@0: */ sl@0: sl@0: sl@0: sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: #include sl@0: sl@0: #include "ctest.h" /* includes C interface to EPOC32 threads, and SpawnPosixServer */ sl@0: sl@0: test_Data; sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-STDLIB-CT-1115 sl@0: @SYMTestCaseDesc Tests for AF_LOCAL stream sockets sl@0: @SYMTestPriority High sl@0: @SYMTestActions Tests for create socket,binding and other simple socket operations sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: void testSimple() sl@0: { sl@0: int fd1, fd2; sl@0: size_t addrsize; sl@0: int err; sl@0: struct sockaddr addr1, addr2; sl@0: int optionbuf[20]; sl@0: size_t optionsize; sl@0: sl@0: test_Next("Create stream sockets"); sl@0: fd1=socket(AF_LOCAL, SOCK_STREAM, 0); sl@0: test_ok(fd1>=0); sl@0: sl@0: fd2=socket(AF_LOCAL, SOCK_STREAM, 0); sl@0: test_ok(fd2>=0); sl@0: sl@0: test_Next("Some binding tests"); sl@0: sl@0: #if 0 /* causes socket server panic with ESOCK 058 */ sl@0: addr1.sa_family=AF_UNSPEC; sl@0: addr1.sa_port=65530; sl@0: err=bind(fd1,&addr1, sizeof(addr1)); /* wrong family, port out of range */ sl@0: test_ok(err!=0); sl@0: #endif sl@0: sl@0: addr1.sa_family=AF_LOCAL; sl@0: addr1.sa_port=1; sl@0: err=bind(fd1, &addr1, sizeof(addr1)); sl@0: test_ok(err==0); sl@0: sl@0: addr1.sa_family=AF_LOCAL; sl@0: addr1.sa_port=2; sl@0: err=bind(fd1, &addr1, sizeof(addr1)); /* already bound */ sl@0: test_errno(err!=0, EEXIST); sl@0: sl@0: test_Next("Get associated addresses"); sl@0: sl@0: addrsize=sizeof(addr2); sl@0: err=getsockname(fd1,&addr2,&addrsize); sl@0: test_ok(err==0); sl@0: /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */ sl@0: test(addr2.sa_port==1); sl@0: test(addrsize<=sizeof(addr2)); sl@0: sl@0: addrsize=sizeof(addr2); sl@0: err=getpeername(fd1,&addr2,&addrsize); /* not connected */ sl@0: test_errno(err!=0,-2); /* OMISSION - can't report proper reason for failure */ sl@0: sl@0: addrsize=sizeof(addr2); sl@0: err=getsockname(fd2,&addr2,&addrsize); /* not bound */ sl@0: test_errno(err!=0,-2); /* OMISSION - can't report proper reason for failure */ sl@0: sl@0: test_Next("More binding"); sl@0: sl@0: addr1.sa_family=AF_LOCAL; sl@0: addr1.sa_port=1; sl@0: err=bind(fd2, &addr1, sizeof(addr1)); /* address in use */ sl@0: test_errno(err!=0, EACCES); sl@0: sl@0: #if 0 /* this isn't supported by AF_LOCAL */ sl@0: addr1.sa_family=AF_LOCAL; sl@0: addr1.sa_port=0; sl@0: err=bind(fd2, &addr1, sizeof(addr1)); /* unspecified port number */ sl@0: test_ok(err==0); sl@0: sl@0: addrsize=sizeof(addr2); sl@0: err=getsockname(fd2,&addr2,&addrsize); sl@0: test_ok(err==0); sl@0: /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */ sl@0: test(addr2.sa_port!=1); sl@0: test(addr2.sa_port!=0); sl@0: test(addrsize<=sizeof(addr2)); sl@0: #endif sl@0: sl@0: err=listen(fd1,1); sl@0: test_ok(err==0); sl@0: sl@0: test_Next("Socket options"); sl@0: sl@0: optionbuf[0]=3500000; /* implausible size */ sl@0: optionsize=sizeof(optionbuf[0]); sl@0: err=getsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,&optionsize); sl@0: test_ok(err==0); sl@0: test(optionbuf[0]!=3500000); sl@0: sl@0: optionbuf[0]=7*1024; sl@0: optionsize=sizeof(optionbuf[0]); sl@0: err=setsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,optionsize); sl@0: test_ok(err==0); sl@0: sl@0: optionbuf[0]=3500000; /* implausible size */ sl@0: optionsize=sizeof(optionbuf[0]); sl@0: err=getsockopt(fd1,SOL_SOCKET,SO_SNDBUF,optionbuf,&optionsize); sl@0: test_ok(err==0); sl@0: test(optionbuf[0]==7*1024); sl@0: sl@0: optionbuf[0]=1; sl@0: optionsize=sizeof(optionbuf[0]); sl@0: err=getsockopt(fd1,SOL_SOCKET,1234,optionbuf,&optionsize); /* invalid option */ sl@0: test_errno(err<0,ENOSYS); sl@0: test(optionbuf[0]==1); sl@0: sl@0: optionbuf[0]=13; sl@0: optionsize=sizeof(optionbuf[0]); sl@0: err=setsockopt(fd1,SOL_SOCKET,1234,optionbuf,optionsize); /* invalid option */ sl@0: test_errno(err<0,ENOSYS); sl@0: test(optionbuf[0]==13); sl@0: sl@0: err=close(fd1); sl@0: test_ok(err==0); sl@0: sl@0: err=close(fd2); sl@0: test_ok(err==0); sl@0: } sl@0: sl@0: /* Client and server take it in turns to send, starting with the client. sl@0: * Each matches the message they receive with the string expected sl@0: */ sl@0: char *message_sequence[] = { sl@0: "Hello from client", sl@0: "Hello from server", sl@0: "Test of send", sl@0: "Test of recv", sl@0: "Try sendto", sl@0: "Try recvfrom", sl@0: "Send to shutdown socket", sl@0: "Send to closed socket", sl@0: 0 sl@0: }; sl@0: sl@0: /** sl@0: @SYMTestCaseID SYSLIB-STDLIB-CT-1116 sl@0: @SYMTestCaseDesc Tests for server socket sl@0: @SYMTestPriority High sl@0: @SYMTestActions Tests for server socket,create,accept,send and receive functions sl@0: @SYMTestExpectedResults Test must not fail sl@0: @SYMREQ REQ0000 sl@0: */ sl@0: void testServer() sl@0: { sl@0: int fd1, fd2, nbytes, i; sl@0: size_t addrsize; sl@0: int err; sl@0: struct sockaddr addr1, addr2; sl@0: char buf[80]; sl@0: char **mp = message_sequence; sl@0: sl@0: test_Next("Create server socket"); sl@0: fd1=socket(AF_LOCAL, SOCK_STREAM, 0); sl@0: test_ok(fd1>=0); sl@0: sl@0: #if 0 sl@0: /* causes ESOCK to panic the client */ sl@0: addrsize=sizeof(addr2); sl@0: fd2=accept(fd1,&addr2,&addrsize); /* can't accept on an unbound socket */ sl@0: test_ok(fd2<0); sl@0: #endif sl@0: sl@0: addr1.sa_family=AF_LOCAL; sl@0: addr1.sa_port=1; sl@0: err=bind(fd1, &addr1, sizeof(addr1)); sl@0: test_ok(err==0); sl@0: sl@0: #if 0 sl@0: /* causes ESOCK to panic the client */ sl@0: addrsize=sizeof(addr2); sl@0: fd2=accept(fd1,&addr2,&addrsize); /* can't accept before listening */ sl@0: test_ok(fd2<0); sl@0: #endif sl@0: sl@0: err=listen(fd1,1); sl@0: test_ok(err==0); sl@0: sl@0: addrsize=sizeof(addr2); sl@0: fd2=accept(fd1,&addr2,&addrsize); sl@0: test_ok(fd2>=0); sl@0: /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */ sl@0: test(addr2.sa_port!=1); sl@0: test(addr2.sa_port!=0); sl@0: test(addrsize<=sizeof(addr2)); sl@0: sl@0: test_Next("Server read/write"); sl@0: sl@0: /* read */ sl@0: nbytes=strlen(*mp); sl@0: err=read(fd2, buf, nbytes+1); sl@0: test_ok(err==nbytes+1); sl@0: test(strcmp(buf,*mp)==0); sl@0: sl@0: /* write */ sl@0: mp++; sl@0: nbytes=strlen(*mp); sl@0: for (i=0; i=0); sl@0: sl@0: addr1.sa_family=AF_LOCAL; sl@0: addr1.sa_port=1; sl@0: addrsize=sizeof(addr1); sl@0: err=connect(fd1,&addr1,addrsize); sl@0: test_ok(err==0); sl@0: sl@0: addrsize=sizeof(addr2); sl@0: err=getpeername(fd1,&addr2,&addrsize); sl@0: test_ok(err==0); sl@0: /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */ sl@0: test(addr2.sa_port==1); sl@0: test(addrsize<=sizeof(addr2)); sl@0: sl@0: addrsize=sizeof(addr2); sl@0: err=getsockname(fd1,&addr2,&addrsize); sl@0: test_ok(err==0); sl@0: /* test(addr2.sa_family==AF_LOCAL); problem in IPC.PRT, for ESOCK 058 */ sl@0: test(addr2.sa_port!=1); sl@0: test(addr2.sa_port!=0); sl@0: test(addrsize<=sizeof(addr2)); sl@0: sl@0: test_Next("Client read/write"); sl@0: sl@0: /* write */ sl@0: nbytes=strlen(*mp); sl@0: err=write(fd1, *mp, nbytes+1); sl@0: test_ok(err==nbytes+1); sl@0: sl@0: /* read */ sl@0: mp++; sl@0: nbytes=strlen(*mp); sl@0: err=read(fd1, buf, nbytes+1); sl@0: test_ok(err==nbytes+1); sl@0: test(strcmp(buf,*mp)==0); sl@0: sl@0: test_Next("Client send/recv"); sl@0: sl@0: /* send */ sl@0: mp++; sl@0: nbytes=strlen(*mp); sl@0: for (i=0; i