1
/*===========================================================================
2
Copyright (C) 1995-2005 European Southern Observatory (ESO)
4
This program is free software; you can redistribute it and/or
5
modify it under the terms of the GNU General Public License as
6
published by the Free Software Foundation; either version 2 of
7
the License, or (at your option) any later version.
9
This program is distributed in the hope that it will be useful,
10
but WITHOUT ANY WARRANTY; without even the implied warranty of
11
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
GNU General Public License for more details.
14
You should have received a copy of the GNU General Public
15
License along with this program; if not, write to the Free
16
Software Foundation, Inc., 675 Massachusetts Ave, Cambridge,
19
Correspondence concerning ESO-MIDAS should be addressed as follows:
20
Internet e-mail: midas@eso.org
21
Postal address: European Southern Observatory
22
Data Management Division
23
Karl-Schwarzschild-Strasse 2
24
D 85748 Garching bei Muenchen
26
===========================================================================*/
28
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
32
.AUTHOR Carlos Guirao. IPG-ESO Garching
33
.CATEGORY Tests of host operating system interfaces.
34
.COMMENTS Automatic osx test.
35
This test exercises the functions of the module "osx".
37
.REMARKS A hanged condition has been detected for this test. It happens
38
when a procces is waiting for data to come from a socket, and
39
the connection is already stablished with a sender. If the
40
sender, for any reason, denies the data, the procces will hang
41
forever (or CTRL-C). SIGALRM does not work for this porpose
42
because the system call "read" ignores it when there is a
43
stablished connection.
46
.VERSION 1.1 05-Jul-1990 Implementation C. Guirao
49
------------------------------------------------------------*/
55
#include <sys/types.h>
59
#ifndef S_IFSOCK /* defined in stat.h if Unix domain sockets */
60
static char socket_name[] = "00"; /* for PC/SCO without local sockets */
61
# define OSX_OPEN_MODE NETW
63
static char socket_name[] = "/tmp/mtape";
64
# define OSX_OPEN_MODE LOCAL
77
#define PERROR { printf("\tERROR: %s\n",osmsg()); ospexit(1); }
78
#define PRINTF(string) { fflush(stdout); printf(string); fflush(stdout); sleep(1); }
87
printf("(TIMEOUT)\n");
91
static int readn(fd, ptr, nbytes)
92
/*++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
93
.PURPOSE Read "n" bytest from a descriptor. Use in place of
94
read() when fd is a stream socket
95
.RETURNS If succesful, the number of bytes actaully read is
96
returned. Otherwise, a -1 is returned.
97
.REMARKS System dependencies:
99
------------------------------------------------------------*/
110
if ( (st = osxinfo(fd,0,0)) == -1) PERROR
111
if (st == NODATA) break;
113
nread = osxread(fd, ptr, nleft);
114
if (nread < 0) /* error, return < 0 */
120
return(nbytes - nleft); /* return >= 0 */
129
char input[80], name[80], *pname;
132
static char *channame[2];
133
static struct stat statbuf;
137
channame[1]="localhost";
138
signal(SIGALRM,timeout);
140
printf("**********************************************\n");
141
printf("****** AUTOMATIC TEST FOR OSX INTERFACE ******\n");
142
printf("****** LOCAL MODE. USING SOCKETS. ******\n");
143
printf("**********************************************\n\n");
145
printf("Deleting socket files: %s if existing... ",
147
if (stat(socket_name,&statbuf) == 0 )
148
if (unlink(socket_name) < 0) PERROR
152
printf("Allocating memory for buffers...");
153
if ( (buf = (char *)malloc((size_t)(sizebuf+1))) == (char *)0) PERROR
156
/* First step: open & close mode*/
157
printf("\n************** Testing open & close\n");
159
printf("Opening a normal file.... ");
160
if ( (fdn = open(socket_name,O_CREAT,S_IREAD|S_IWRITE)) < 0) PERROR
163
printf("Opening a socket in READ mode, over a normal file.... ");
164
channame[0]=socket_name;
165
if ( (fd = osxopen(channame,LOCAL|IPC_READ)) != -1) {
166
printf("ERROR: Return code != -1\n");
172
printf("Opening a socket in WRITE mode, over a normal file.... ");
173
channame[0]=socket_name;
174
if ( (fd = osxopen(channame,LOCAL|IPC_WRITE)) != -1) {
175
printf("ERROR: Return code != -1\n");
181
printf("Closing and unlinking the normal file.... ");
182
if ( close(fdn) < 0) PERROR
183
if (unlink(socket_name) < 0) PERROR
186
printf("\nOpening a socket in READ mode.... ");
187
channame[0]=socket_name;
188
if ( (fd = osxopen(channame,OSX_OPEN_MODE|IPC_READ)) == -1) PERROR
191
printf("Reading status from socket.... ");
192
if ( (st = osxinfo(fd,0,0)) == -1) PERROR
193
if (st == NOCONN) { PRINTF("OK\n"); }
194
else printf("ERROR: Return code != %d\n",NOCONN);
196
printf("Closing the socket in READ mode.... ");
197
if ( osxclose(fd) == -1) PERROR
200
printf("\nTrying to open a socket in WRITE mode.... ");
201
channame[0]=socket_name;
202
if ( (fd = osxopen(channame,OSX_OPEN_MODE|IPC_WRITE)) != -1) {
203
printf("ERROR: Return code != -1\n");
209
printf("\nOpening a socket (once).... ");
210
channame[0]=socket_name;
211
if ( (fd = osxopen(channame,OSX_OPEN_MODE|IPC_READ)) == -1) PERROR
214
/* PC/SCO with NETW socket returns -1 Address already in use
215
printf("Trying to open the same socket mode (twice).... ");
216
channame[0]=socket_name;
217
if ( (fd = osxopen(channame,OSX_OPEN_MODE|IPC_READ)) == -1) PERROR
221
printf("Closing the socket (once).... ");
222
if ( osxclose(fd) == -1) PERROR
225
printf("Trying to close the same socket (twice).... ");
226
if ( osxclose(fd) != -1) {
227
PRINTF("ERROR: Return code != -1\n");
232
printf("\nOpening a socket in READ mode.... ");
233
channame[0]=socket_name;
234
if ( (fd = osxopen(channame,OSX_OPEN_MODE|IPC_READ)) == -1) PERROR
237
printf("Trying to read something... ");
240
if ( osxread(fd,buf,1) != -1) {
241
PRINTF("ERROR: Return code != -1\n");
247
printf("Trying to write something... ");
248
if ( osxwrite(fd,buf,1) != -1) {
249
PRINTF("ERROR: Return code != -1n");
254
printf("Closing the socket in READ mode.... ");
255
if ( osxclose(fd) == -1) PERROR
258
printf("\nDoing a fork for server & client \n");
260
if ( (childpid = fork()) < 0) PERROR
261
else if (childpid == 0) {
267
printf("**********************************************\n");
268
printf("****** END OF AUTOMATIC TEST FOR OSX ******\n");
269
printf("****** LOCAL MODE. USING SOCKETS. ******\n");
270
printf("**********************************************\n\n");
279
/* This is the father procces */
280
printf("I am the father.\n");
281
channame[0]=socket_name;
282
channame[1]="localhost";
284
printf("\n************** Using client ---> server transfer\n");
285
if ( (fd = osxopen(channame,OSX_OPEN_MODE|IPC_READ)) == -1) {
286
printf("Open socket in read mode... FAILED.\n");
289
else printf("Open socket in read mode... OK\n");
291
PRINTF("Waiting connection from client\n");
293
while( osxinfo(fd,0,0) == NOCONN ) ;
294
PRINTF("Connection from client... OK\n");
297
if ( (nbytes = testread(fd,buf,SIZEBUF,PATTERNA,LOWER)) != SIZEBUF) {
298
PRINTF("Reading pattern A in socket... FAILED.\n");
300
printf("\tError: Only %d bytes read !!!\n",nbytes);
303
else PRINTF("Reading pattern A in socket... OK\n");
305
if ( (nbytes = testread(fd,buf,SIZEBUF,PATTERNB,LOWER)) != SIZEBUF) {
306
PRINTF("Reading pattern B in socket... FAILED.\n");
308
printf("\tError: Only %d bytes read !!!\n",nbytes);
311
else PRINTF("Reading pattern B in socket... OK\n");
313
if ( (nbytes=testread(fd,buf,SIZEBUF/2,PATTERNC,LOWER)) != SIZEBUF/2) {
314
PRINTF("Reading (1/2) pattern C in socket... FAILED.\n");
316
printf("\tError: Only %d bytes read !!!\n",nbytes);
319
else PRINTF("Reading pattern C (1/2) in socket... OK\n");
321
if ( (nbytes=testread(fd,buf,SIZEBUF/2,PATTERNC,LOWER)) != SIZEBUF/2) {
322
PRINTF("Reading (2/2) pattern C in socket... FAILED.\n");
324
printf("\tError: Only %d bytes read !!!\n",nbytes);
327
else PRINTF("Reading pattern C (2/2) in socket... OK\n");
329
if ((nbytes=testwrite(fd,buf,SIZEBUF,PATTERNA,LOWER)) != SIZEBUF) {
330
printf("Writting pattern A in socket... FAILED.\n");
332
printf("\tError: only %d bytes written!!!\n",nbytes);
335
else printf("Writting pattern A in socket... OK\n");
338
if ( (nbytes = osxread(fd,buf,SIZEBUF)) != 0) {
339
PRINTF("Reading EOF in socket... FAILED.\n");
341
printf("%d bytes read !!!\n",nbytes);
345
else PRINTF("Reading EOF in socket... OK\n");
348
printf("\n************** Using server ---> client transfer\n");
350
PRINTF("Waiting connection from client\n");
352
while( osxinfo(fd,0,0) == NOCONN ) ;
353
PRINTF("Connection from client... OK\n");
356
if ((nbytes=testwrite(fd,buf,SIZEBUF/2,PATTERNA,LOWER)) != SIZEBUF/2) {
357
printf("Writting pattern A (1/2) in socket... FAILED.\n");
359
printf("\tError: only %d bytes written!!!\n",nbytes);
362
else printf("Writting pattern A (1/2) in socket... OK\n");
364
if ((nbytes=testwrite(fd,buf,SIZEBUF/2,PATTERNA,LOWER)) != SIZEBUF/2) {
365
printf("Writting pattern A (2/2) in socket... FAILED.\n");
367
printf("\tError: only %d bytes written!!!\n",nbytes);
370
else printf("Writting pattern A (2/2) in socket... OK\n");
372
if ((nbytes=testwrite(fd,buf,SIZEBUF,PATTERNB,LOWER)) != SIZEBUF) {
373
printf("Writting pattern B in socket... FAILED.\n");
375
printf("\tError: only %d bytes written!!!\n",nbytes);
378
else printf("Writting pattern B in socket... OK\n");
380
if ((nbytes=testwrite(fd,buf,SIZEBUF,PATTERNC,LOWER)) != SIZEBUF) {
381
printf("Writting pattern C in socket... FAILED.\n");
383
printf("\tError: only %d bytes written!!!\n",nbytes);
386
else printf("Writting pattern C in socket... OK\n");
389
if ( (nbytes = osxread(fd,buf,SIZEBUF)) != 0) {
390
PRINTF("Reading EOF in socket... FAILED.\n");
392
printf("%d bytes read !!!\n",nbytes);
396
else PRINTF("Reading EOF in socket... OK\n");
399
printf("\n************** Using server <--> client transfer\n");
401
PRINTF("Waiting connection from client\n");
403
while (osxinfo(fd,0,0) == NOCONN) ;
404
PRINTF("Connection from client... OK\n");
407
if ((nbytes=testwrite(fd,buf,SIZEBUF,PATTERNB,UPPER)) != SIZEBUF) {
408
printf("Writting pattern B in socket... FAILED.\n");
410
printf("\tError: only %d bytes written!!!\n",nbytes);
413
else printf("Writting pattern B in socket... OK\n");
415
if ( (nbytes=testread(fd,buf,SIZEBUF,PATTERNC,LOWER)) != SIZEBUF) {
416
PRINTF("Reading pattern C in socket... FAILED.\n");
418
printf("\tError: Only %d bytes read !!!\n",nbytes);
421
else PRINTF("Reading pattern C in socket... OK\n");
423
if ( (nbytes=testread(fd,buf,SIZEBUF,PATTERNA,LOWER)) != SIZEBUF) {
424
PRINTF("Reading pattern A in socket... FAILED.\n");
426
printf("\tError: Only %d bytes read !!!\n",nbytes);
429
else PRINTF("Reading pattern A in socket... OK\n");
431
if ((nbytes=testwrite(fd,buf,SIZEBUF,PATTERNC,UPPER)) != SIZEBUF) {
432
printf("Writting pattern C in socket... FAILED.\n");
434
printf("\tError: only %d bytes written!!!\n",nbytes);
437
else printf("Writting pattern C in socket... OK\n");
440
if ( (nbytes = osxread(fd,buf,SIZEBUF)) != 0) {
441
PRINTF("Reading EOF in socket... FAILED.\n");
443
printf("%d bytes read !!!\n",nbytes);
447
else PRINTF("Reading EOF in socket... OK\n");
450
if ( osxclose(fd) == -1) {
451
printf("Closing socket write mode... FAILED.\n");
454
printf("Closing socket write mode... OK\n\n");
457
/* Wait for child to finish */
458
printf("****** Waiting for child proccess to die\n");
460
printf("****** End of father proccess\n");
469
printf("I AM THE CHILD.\n");
470
channame[0]=socket_name;
471
channame[1]="localhost";
474
while ( (fd = osxopen(channame,OSX_OPEN_MODE|IPC_WRITE)) == -1) ;
476
printf("OPEN SOCKET IN WRITE MODE... OK\n");
478
if ( (nbytes = testwrite(fd,buf,SIZEBUF,PATTERNA,UPPER)) != SIZEBUF) {
479
printf("WRITTING PATTERN A IN SOCKET... FAILED.\n");
481
printf("\tERROR: ONLY %d BYTES WRITTEN!!!\n",nbytes);
484
else printf("WRITTING PATTERN A IN SOCKET... OK\n");
486
if ( (nbytes = testwrite(fd,buf,SIZEBUF,PATTERNB,UPPER)) != SIZEBUF) {
487
printf("WRITTING PATTERN B IN SOCKET... FAILED.\n");
489
printf("\tERROR: ONLY %d BYTES WRITTEN!!!\n",nbytes);
492
else printf("WRITTING PATTERN B IN SOCKET... OK\n");
494
if ( (nbytes = testwrite(fd,buf,SIZEBUF,PATTERNC,UPPER)) != SIZEBUF) {
495
printf("WRITTING PATTERN C IN SOCKET... FAILED.\n");
497
printf("\tERROR: ONLY %d BYTES WRITTEN!!!\n",nbytes);
500
else printf("WRITTING PATTERN C IN SOCKET... OK\n");
502
if ( (nbytes = testread(fd,buf,SIZEBUF,PATTERNA,UPPER)) != SIZEBUF) {
503
PRINTF("READING PATTERN A IN SOCKET... FAILED.\n");
505
printf("\tERROR: ONLY %d BYTES READ !!!\n",nbytes);
508
else PRINTF("READING PATTERN A IN SOCKET... OK\n");
510
if ( osxclose(fd) == -1) {
511
printf("CLOSING THE SOCKET WRITE MODE ... FAILED.\n");
514
printf("CLOSING THE SOCKET WRITE MODE... OK\n");
517
while ( (fd = osxopen(channame,OSX_OPEN_MODE|IPC_WRITE)) == -1) ;
518
printf("OPEN SOCKET IN WRITE MODE... OK\n");
521
if ( (nbytes = testread(fd,buf,SIZEBUF,PATTERNA,UPPER)) != SIZEBUF) {
522
PRINTF("READING PATTERN A IN SOCKET... FAILED.\n");
524
printf("\tERROR: ONLY %d BYTES READ !!!\n",nbytes);
527
else PRINTF("READING PATTERN A IN SOCKET... OK\n");
529
if ( (nbytes = testread(fd,buf,SIZEBUF,PATTERNB,UPPER)) != SIZEBUF) {
530
PRINTF("READING PATTERN B IN SOCKET... FAILED.\n");
532
printf("\tERROR: ONLY %d BYTES READ !!!\n",nbytes);
535
else PRINTF("READING PATTERN B IN SOCKET... OK\n");
537
if ( (nbytes = testread(fd,buf,SIZEBUF,PATTERNC,UPPER)) != SIZEBUF) {
538
PRINTF("READING PATTERN C IN SOCKET... FAILED.\n");
540
printf("\tERROR: ONLY %d BYTES READ !!!\n",nbytes);
543
else PRINTF("READING PATTERN C IN SOCKET... OK\n");
545
if ( osxclose(fd) == -1) {
546
printf("CLOSING THE SOCKET WRITE MODE ... FAILED.\n");
549
printf("CLOSING THE SOCKET WRITE MODE... OK\n");
553
PRINTF("WAITING DISCONNECTION FROM SERVER... ");
554
if ( (fd = osxopen(channame,OSX_OPEN_MODE|IPC_WRITE)) == -1) PERROR
555
while( osxread(fd,buf,1) != -1 ) ;
561
if ( (fd = osxopen(channame,OSX_OPEN_MODE|IPC_WRITE)) == -1) {
562
printf("OPEN SOCKET IN WRITE MODE... FAILED.\n");
565
else printf("OPEN SOCKET 0 IN WRITE MODE... OK\n");
567
if ( (nbytes = testread(fd,buf,SIZEBUF,PATTERNB,UPPER)) != SIZEBUF) {
568
PRINTF("READING PATTERN B IN SOCKET... FAILED.\n");
570
printf("\tERROR: ONLY %d BYTES READ !!!\n",nbytes);
573
else PRINTF("READING PATTERN B IN SOCKET... OK\n");
576
if ( (nbytes = testwrite(fd,buf,SIZEBUF,PATTERNC,UPPER)) != SIZEBUF) {
577
printf("WRITTING PATTERN C IN SOCKET... FAILED.\n");
579
printf("\tERROR: ONLY %d BYTES WRITTEN!!!\n",nbytes);
582
else printf("WRITTING PATTERN C IN SOCKET... OK\n");
584
if ( (nbytes = testwrite(fd,buf,SIZEBUF,PATTERNA,UPPER)) != SIZEBUF) {
585
printf("WRITTING PATTERN A IN SOCKET... FAILED.\n");
587
printf("\tERROR: ONLY %d BYTES WRITTEN!!!\n",nbytes);
590
else printf("WRITTING PATTERN A IN SOCKET... OK\n");
592
if ( (nbytes = testread(fd,buf,SIZEBUF,PATTERNC,UPPER)) != SIZEBUF) {
593
PRINTF("READING PATTERN C IN SOCKET... FAILED.\n");
595
printf("\tERROR: ONLY %d BYTES READ !!!\n",nbytes);
598
else PRINTF("READING PATTERN C IN SOCKET... OK\n");
600
if ( osxclose(fd) == -1) {
601
printf("CLOSING THE SOCKET WRITE MODE ... FAILED.\n");
604
printf("CLOSING THE SOCKET WRITE MODE... OK\n");
607
printf("****** END OF CHILD PROCCESS\n");
611
testread(fd,buffer,size,pattern,type)
619
for(i=0; i<size; i++) buffer[i] = 0;
621
if ( (nbytes = readn(fd,buffer,size)) < 0) {
623
printf("\tERROR READING A BUFFER: %s\n",osmsg());
624
else printf("\tError reading a buffer: %s\n",osmsg());
628
for(i=0; i<nbytes; i++) {
629
if ( buf[i] != pattern ) {
631
printf("\tPATTERN MISTMATCH: READ[%d]='%c'; EXPECTED '%c'\n", i, buf[i], pattern);
633
printf("\tPattern mistmatch: read[%d]='%c'; expected '%c'\n", i, buf[i], pattern);
641
testwrite(fd,buffer,size,pattern,type)
649
for(i=0; i<size; i++) buffer[i] = pattern;
651
if ( (nbytes = osxwrite(fd,buffer,size)) != size) {
653
printf("\tERROR WRITTING A BUFFER: %s\n",osmsg());
654
else printf("\tError writting a buffer: %s\n",osmsg());