mirror of
https://github.com/mozilla/gecko-dev.git
synced 2025-02-05 22:05:40 +00:00
Added the pipeping, pipepong, and pipeself tests.
This commit is contained in:
parent
8e47728941
commit
3472340f17
@ -87,6 +87,9 @@ CSRCS = \
|
||||
op_nofil.c \
|
||||
parent.c \
|
||||
perf.c \
|
||||
pipeping.c \
|
||||
pipepong.c \
|
||||
pipeself.c \
|
||||
poll_er.c \
|
||||
poll_nm.c \
|
||||
poll_to.c \
|
||||
|
147
nsprpub/pr/tests/pipeping.c
Normal file
147
nsprpub/pr/tests/pipeping.c
Normal file
@ -0,0 +1,147 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: pipeping.c
|
||||
*
|
||||
* Description:
|
||||
* This test runs in conjunction with the pipepong test.
|
||||
* This test creates two pipes and redirects the stdin and
|
||||
* stdout of the pipepong test to the pipes. Then this
|
||||
* test writes "ping" to the pipepong test and the pipepong
|
||||
* test writes "pong" back. To run this pair of tests,
|
||||
* just invoke pipeping.
|
||||
*
|
||||
* Tested areas: process creation, pipes, file descriptor
|
||||
* inheritance, standard I/O redirection.
|
||||
*/
|
||||
|
||||
#include "nspr.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
static char *child_argv[] = { "pipepong", NULL };
|
||||
|
||||
#define NUM_ITERATIONS 10
|
||||
|
||||
int main()
|
||||
{
|
||||
PRFileDesc *in_pipe[2];
|
||||
PRFileDesc *out_pipe[2];
|
||||
PRStatus status;
|
||||
PRProcess *process;
|
||||
PRProcessAttr *attr;
|
||||
char buf[1024];
|
||||
PRInt32 nBytes;
|
||||
PRInt32 exitCode;
|
||||
int idx;
|
||||
|
||||
status = PR_CreatePipe(&in_pipe[0], &in_pipe[1]);
|
||||
if (status == PR_FAILURE) {
|
||||
fprintf(stderr, "PR_CreatePipe failed\n");
|
||||
exit(1);
|
||||
}
|
||||
status = PR_CreatePipe(&out_pipe[0], &out_pipe[1]);
|
||||
if (status == PR_FAILURE) {
|
||||
fprintf(stderr, "PR_CreatePipe failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
status = PR_SetFDInheritable(in_pipe[1], PR_TRUE);
|
||||
if (status == PR_FAILURE) {
|
||||
fprintf(stderr, "PR_SetFDInheritable failed\n");
|
||||
exit(1);
|
||||
}
|
||||
status = PR_SetFDInheritable(out_pipe[0], PR_TRUE);
|
||||
if (status == PR_FAILURE) {
|
||||
fprintf(stderr, "PR_SetFDInheritable failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
attr = PR_NewProcessAttr();
|
||||
if (attr == NULL) {
|
||||
fprintf(stderr, "PR_NewProcessAttr failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
PR_ProcessAttrSetStdioRedirect(attr, PR_StandardInput, out_pipe[0]);
|
||||
PR_ProcessAttrSetStdioRedirect(attr, PR_StandardOutput, in_pipe[1]);
|
||||
|
||||
process = PR_CreateProcess(child_argv[0], child_argv, NULL, attr);
|
||||
if (process == NULL) {
|
||||
fprintf(stderr, "PR_CreateProcess failed\n");
|
||||
exit(1);
|
||||
}
|
||||
PR_DestroyProcessAttr(attr);
|
||||
status = PR_Close(out_pipe[0]);
|
||||
if (status == PR_FAILURE) {
|
||||
fprintf(stderr, "PR_Close failed\n");
|
||||
exit(1);
|
||||
}
|
||||
status = PR_Close(in_pipe[1]);
|
||||
if (status == PR_FAILURE) {
|
||||
fprintf(stderr, "PR_Close failed\n");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
for (idx = 0; idx < NUM_ITERATIONS; idx++) {
|
||||
strcpy(buf, "ping");
|
||||
printf("pipeping: sending \"%s\"\n", buf);
|
||||
nBytes = PR_Write(out_pipe[1], buf, 5);
|
||||
if (nBytes == -1) {
|
||||
fprintf(stderr, "PR_Write failed: (%d, %d)\n", PR_GetError(),
|
||||
PR_GetOSError());
|
||||
exit(1);
|
||||
}
|
||||
nBytes = PR_Read(in_pipe[0], buf, sizeof(buf));
|
||||
if (nBytes == -1) {
|
||||
fprintf(stderr, "PR_Read failed\n");
|
||||
exit(1);
|
||||
}
|
||||
printf("pipeping: received \"%s\"\n", buf);
|
||||
if (strcmp(buf, "pong") != 0) {
|
||||
fprintf(stderr, "pipeping: expected \"pong\" but got \"%s\"\n",
|
||||
buf);
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
|
||||
status = PR_Close(in_pipe[0]);
|
||||
if (status == PR_FAILURE) {
|
||||
fprintf(stderr, "PR_Close failed\n");
|
||||
exit(1);
|
||||
}
|
||||
status = PR_Close(out_pipe[1]);
|
||||
if (status == PR_FAILURE) {
|
||||
fprintf(stderr, "PR_Close failed\n");
|
||||
exit(1);
|
||||
}
|
||||
status = PR_WaitProcess(process, &exitCode);
|
||||
if (status == PR_FAILURE) {
|
||||
fprintf(stderr, "PR_WaitProcess failed\n");
|
||||
exit(1);
|
||||
}
|
||||
if (exitCode == 0) {
|
||||
printf("PASS\n");
|
||||
return 0;
|
||||
} else {
|
||||
printf("FAIL\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
70
nsprpub/pr/tests/pipepong.c
Normal file
70
nsprpub/pr/tests/pipepong.c
Normal file
@ -0,0 +1,70 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: pipepong.c
|
||||
*
|
||||
* Description:
|
||||
* This test runs in conjunction with the pipeping test.
|
||||
* The pipeping test creates two pipes and redirects the
|
||||
* stdin and stdout of this test to the pipes. Then the
|
||||
* pipeping test writes "ping" to this test and this test
|
||||
* writes "pong" back. Note that this test does not depend
|
||||
* on NSPR at all. To run this pair of tests, just invoke
|
||||
* pipeping.
|
||||
*
|
||||
* Tested areas: process creation, pipes, file descriptor
|
||||
* inheritance, standard I/O redirection.
|
||||
*/
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#define NUM_ITERATIONS 10
|
||||
|
||||
int main()
|
||||
{
|
||||
char buf[1024];
|
||||
size_t nBytes;
|
||||
int idx;
|
||||
|
||||
memset(buf, 0, sizeof(buf));
|
||||
|
||||
for (idx = 0; idx < NUM_ITERATIONS; idx++) {
|
||||
nBytes = fread(buf, 1, 5, stdin);
|
||||
fprintf(stderr, "pipepong: received \"%s\"\n", buf);
|
||||
if (nBytes != 5) {
|
||||
fprintf(stderr, "pipepong: expected 5 bytes but got %d bytes\n",
|
||||
nBytes);
|
||||
exit(1);
|
||||
}
|
||||
if (strcmp(buf, "ping") != 0) {
|
||||
fprintf(stderr, "pipepong: expected \"ping\" but got \"%s\"\n",
|
||||
buf);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
strcpy(buf, "pong");
|
||||
fprintf(stderr, "pipepong: sending \"%s\"\n", buf);
|
||||
printf("%s", buf);
|
||||
fflush(stdout);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
98
nsprpub/pr/tests/pipeself.c
Normal file
98
nsprpub/pr/tests/pipeself.c
Normal file
@ -0,0 +1,98 @@
|
||||
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
||||
/*
|
||||
* The contents of this file are subject to the Netscape Public License
|
||||
* Version 1.0 (the "NPL"); you may not use this file except in
|
||||
* compliance with the NPL. You may obtain a copy of the NPL at
|
||||
* http://www.mozilla.org/NPL/
|
||||
*
|
||||
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
||||
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
||||
* for the specific language governing rights and limitations under the
|
||||
* NPL.
|
||||
*
|
||||
* The Initial Developer of this code under the NPL is Netscape
|
||||
* Communications Corporation. Portions created by Netscape are
|
||||
* Copyright (C) 1999 Netscape Communications Corporation. All Rights
|
||||
* Reserved.
|
||||
*/
|
||||
|
||||
/*
|
||||
* File: pipeself.c
|
||||
*
|
||||
* Description:
|
||||
* This test creates a pipe, writes to it, and reads from it.
|
||||
*/
|
||||
|
||||
#include "prio.h"
|
||||
#include "prerror.h"
|
||||
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
#define NUM_ITERATIONS 10
|
||||
|
||||
#define ENGLISH_GREETING "Hello"
|
||||
#define SPANISH_GREETING "Hola"
|
||||
|
||||
int main()
|
||||
{
|
||||
PRFileDesc *readPipe, *writePipe;
|
||||
char buf[128];
|
||||
int len;
|
||||
int idx;
|
||||
|
||||
if (PR_CreatePipe(&readPipe, &writePipe) == PR_FAILURE) {
|
||||
fprintf(stderr, "cannot create pipe: (%d, %d)\n",
|
||||
PR_GetError(), PR_GetOSError());
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (idx = 0; idx < NUM_ITERATIONS; idx++) {
|
||||
strcpy(buf, ENGLISH_GREETING);
|
||||
len = strlen(buf) + 1;
|
||||
if (PR_Write(writePipe, buf, len) < len) {
|
||||
fprintf(stderr, "write failed\n");
|
||||
return 1;
|
||||
}
|
||||
memset(buf, 0, sizeof(buf));
|
||||
if (PR_Read(readPipe, buf, sizeof(buf)) == -1) {
|
||||
fprintf(stderr, "read failed: (%d, %d)\n",
|
||||
PR_GetError(), PR_GetOSError());
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(buf, ENGLISH_GREETING) != 0) {
|
||||
fprintf(stderr, "expected \"%s\" but got \"%s\"\n",
|
||||
ENGLISH_GREETING, buf);
|
||||
return 1;
|
||||
}
|
||||
|
||||
strcpy(buf, SPANISH_GREETING);
|
||||
len = strlen(buf) + 1;
|
||||
if (PR_Write(writePipe, buf, len) < len) {
|
||||
fprintf(stderr, "write failed\n");
|
||||
return 1;
|
||||
}
|
||||
memset(buf, 0, sizeof(buf));
|
||||
if (PR_Read(readPipe, buf, sizeof(buf)) == -1) {
|
||||
fprintf(stderr, "read failed: (%d, %d)\n",
|
||||
PR_GetError(), PR_GetOSError());
|
||||
return 1;
|
||||
}
|
||||
if (strcmp(buf, SPANISH_GREETING) != 0) {
|
||||
fprintf(stderr, "expected \"%s\" but got \"%s\"\n",
|
||||
SPANISH_GREETING, buf);
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
|
||||
if (PR_Close(readPipe) == PR_FAILURE) {
|
||||
fprintf(stderr, "close failed\n");
|
||||
return 1;
|
||||
}
|
||||
if (PR_Close(writePipe) == PR_FAILURE) {
|
||||
fprintf(stderr, "close failed\n");
|
||||
return 1;
|
||||
}
|
||||
printf("PASS\n");
|
||||
return 0;
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user