1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/nsprpub/pr/tests/pipeself.c Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,228 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + * File: pipeself.c 1.11 + * 1.12 + * Description: 1.13 + * This test has two threads communicating with each other using 1.14 + * two unidirectional pipes. The primordial thread is the ping 1.15 + * thread and the other thread is the pong thread. The ping 1.16 + * thread writes "ping" to the pong thread and the pong thread 1.17 + * writes "pong" back. 1.18 + */ 1.19 + 1.20 +#include "prio.h" 1.21 +#include "prerror.h" 1.22 +#include "prthread.h" 1.23 + 1.24 +#include <stdio.h> 1.25 +#include <stdlib.h> 1.26 +#include <string.h> 1.27 + 1.28 +#define NUM_ITERATIONS 10 1.29 + 1.30 +static PRFileDesc *ping_in, *ping_out; 1.31 +static PRFileDesc *pong_in, *pong_out; 1.32 + 1.33 +static void PongThreadFunc(void *arg) 1.34 +{ 1.35 + char buf[1024]; 1.36 + int idx; 1.37 + PRInt32 nBytes; 1.38 + PRStatus status; 1.39 + 1.40 + for (idx = 0; idx < NUM_ITERATIONS; idx++) { 1.41 + memset(buf, 0, sizeof(buf)); 1.42 + nBytes = PR_Read(pong_in, buf, sizeof(buf)); 1.43 + if (nBytes == -1) { 1.44 + fprintf(stderr, "PR_Read failed\n"); 1.45 + exit(1); 1.46 + } 1.47 + printf("pong thread: received \"%s\"\n", buf); 1.48 + if (nBytes != 5) { 1.49 + fprintf(stderr, "pong thread: expected 5 bytes but got %d bytes\n", 1.50 + nBytes); 1.51 + exit(1); 1.52 + } 1.53 + if (strcmp(buf, "ping") != 0) { 1.54 + fprintf(stderr, "pong thread: expected \"ping\" but got \"%s\"\n", 1.55 + buf); 1.56 + exit(1); 1.57 + } 1.58 + strcpy(buf, "pong"); 1.59 + printf("pong thread: sending \"%s\"\n", buf); 1.60 + nBytes = PR_Write(pong_out, buf, 5); 1.61 + if (nBytes == -1) { 1.62 + fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(), 1.63 + PR_GetOSError()); 1.64 + exit(1); 1.65 + } 1.66 + } 1.67 + 1.68 + status = PR_Close(pong_in); 1.69 + if (status == PR_FAILURE) { 1.70 + fprintf(stderr, "PR_Close failed\n"); 1.71 + exit(1); 1.72 + } 1.73 + status = PR_Close(pong_out); 1.74 + if (status == PR_FAILURE) { 1.75 + fprintf(stderr, "PR_Close failed\n"); 1.76 + exit(1); 1.77 + } 1.78 +} 1.79 + 1.80 +int main(int argc, char **argv) 1.81 +{ 1.82 + PRStatus status; 1.83 + PRThread *pongThread; 1.84 + char buf[1024]; 1.85 + PRInt32 nBytes; 1.86 + int idx; 1.87 + 1.88 + status = PR_CreatePipe(&ping_in, &pong_out); 1.89 + if (status == PR_FAILURE) { 1.90 + fprintf(stderr, "PR_CreatePipe failed\n"); 1.91 + exit(1); 1.92 + } 1.93 + status = PR_CreatePipe(&pong_in, &ping_out); 1.94 + if (status == PR_FAILURE) { 1.95 + fprintf(stderr, "PR_CreatePipe failed\n"); 1.96 + exit(1); 1.97 + } 1.98 + 1.99 + pongThread = PR_CreateThread(PR_USER_THREAD, PongThreadFunc, NULL, 1.100 + PR_PRIORITY_NORMAL, PR_GLOBAL_THREAD, PR_JOINABLE_THREAD, 0); 1.101 + if (pongThread == NULL) { 1.102 + fprintf(stderr, "PR_CreateThread failed\n"); 1.103 + exit(1); 1.104 + } 1.105 + 1.106 + for (idx = 0; idx < NUM_ITERATIONS; idx++) { 1.107 + strcpy(buf, "ping"); 1.108 + printf("ping thread: sending \"%s\"\n", buf); 1.109 + nBytes = PR_Write(ping_out, buf, 5); 1.110 + if (nBytes == -1) { 1.111 + fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(), 1.112 + PR_GetOSError()); 1.113 + exit(1); 1.114 + } 1.115 + memset(buf, 0, sizeof(buf)); 1.116 + nBytes = PR_Read(ping_in, buf, sizeof(buf)); 1.117 + if (nBytes == -1) { 1.118 + fprintf(stderr, "PR_Read failed\n"); 1.119 + exit(1); 1.120 + } 1.121 + printf("ping thread: received \"%s\"\n", buf); 1.122 + if (nBytes != 5) { 1.123 + fprintf(stderr, "ping thread: expected 5 bytes but got %d bytes\n", 1.124 + nBytes); 1.125 + exit(1); 1.126 + } 1.127 + if (strcmp(buf, "pong") != 0) { 1.128 + fprintf(stderr, "ping thread: expected \"pong\" but got \"%s\"\n", 1.129 + buf); 1.130 + exit(1); 1.131 + } 1.132 + } 1.133 + 1.134 + status = PR_Close(ping_in); 1.135 + if (status == PR_FAILURE) { 1.136 + fprintf(stderr, "PR_Close failed\n"); 1.137 + exit(1); 1.138 + } 1.139 + status = PR_Close(ping_out); 1.140 + if (status == PR_FAILURE) { 1.141 + fprintf(stderr, "PR_Close failed\n"); 1.142 + exit(1); 1.143 + } 1.144 + status = PR_JoinThread(pongThread); 1.145 + if (status == PR_FAILURE) { 1.146 + fprintf(stderr, "PR_JoinThread failed\n"); 1.147 + exit(1); 1.148 + } 1.149 + 1.150 +#if defined(XP_UNIX) && !defined(SYMBIAN) 1.151 + /* 1.152 + * Test PR_Available for pipes 1.153 + */ 1.154 + status = PR_CreatePipe(&ping_in, &ping_out); 1.155 + if (status == PR_FAILURE) { 1.156 + fprintf(stderr, "PR_CreatePipe failed\n"); 1.157 + exit(1); 1.158 + } 1.159 + nBytes = PR_Write(ping_out, buf, 250); 1.160 + if (nBytes == -1) { 1.161 + fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(), 1.162 + PR_GetOSError()); 1.163 + exit(1); 1.164 + } 1.165 + nBytes = PR_Available(ping_in); 1.166 + if (nBytes < 0) { 1.167 + fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(), 1.168 + PR_GetOSError()); 1.169 + exit(1); 1.170 + } else if (nBytes != 250) { 1.171 + fprintf(stderr, "PR_Available: expected 250 bytes but got %d bytes\n", 1.172 + nBytes); 1.173 + exit(1); 1.174 + } 1.175 + printf("PR_Available: expected %d, got %d bytes\n",250, nBytes); 1.176 + /* read some data */ 1.177 + nBytes = PR_Read(ping_in, buf, 7); 1.178 + if (nBytes == -1) { 1.179 + fprintf(stderr, "PR_Read failed\n"); 1.180 + exit(1); 1.181 + } 1.182 + /* check available data */ 1.183 + nBytes = PR_Available(ping_in); 1.184 + if (nBytes < 0) { 1.185 + fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(), 1.186 + PR_GetOSError()); 1.187 + exit(1); 1.188 + } else if (nBytes != (250 - 7)) { 1.189 + fprintf(stderr, "PR_Available: expected 243 bytes but got %d bytes\n", 1.190 + nBytes); 1.191 + exit(1); 1.192 + } 1.193 + printf("PR_Available: expected %d, got %d bytes\n",243, nBytes); 1.194 + /* read all data */ 1.195 + nBytes = PR_Read(ping_in, buf, sizeof(buf)); 1.196 + if (nBytes == -1) { 1.197 + fprintf(stderr, "PR_Read failed\n"); 1.198 + exit(1); 1.199 + } else if (nBytes != 243) { 1.200 + fprintf(stderr, "PR_Read failed: expected %d, got %d bytes\n", 1.201 + 243, nBytes); 1.202 + exit(1); 1.203 + } 1.204 + /* check available data */ 1.205 + nBytes = PR_Available(ping_in); 1.206 + if (nBytes < 0) { 1.207 + fprintf(stderr, "PR_Available failed: (%d, %d)\n", PR_GetError(), 1.208 + PR_GetOSError()); 1.209 + exit(1); 1.210 + } else if (nBytes != 0) { 1.211 + fprintf(stderr, "PR_Available: expected 0 bytes but got %d bytes\n", 1.212 + nBytes); 1.213 + exit(1); 1.214 + } 1.215 + printf("PR_Available: expected %d, got %d bytes\n", 0, nBytes); 1.216 + 1.217 + status = PR_Close(ping_in); 1.218 + if (status == PR_FAILURE) { 1.219 + fprintf(stderr, "PR_Close failed\n"); 1.220 + exit(1); 1.221 + } 1.222 + status = PR_Close(ping_out); 1.223 + if (status == PR_FAILURE) { 1.224 + fprintf(stderr, "PR_Close failed\n"); 1.225 + exit(1); 1.226 + } 1.227 +#endif /* XP_UNIX */ 1.228 + 1.229 + printf("PASS\n"); 1.230 + return 0; 1.231 +}