import of upstream version 1.17
[mw/micromonitor-lm32.git] / umon_main / host / src / ttftp / tftptest.c
1 #define WIN32_LEAN_AND_MEAN
2
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <string.h>
6 #include <sys/types.h>
7 #include <fcntl.h>
8 #include <sys/stat.h>
9 #include <signal.h>
10 #include <errno.h>
11
12 #ifdef BUILD_WITH_VCC
13 #include <io.h>
14 #include <winsock2.h>
15 typedef unsigned short ushort;
16 #define sleep(n) Sleep(n*1000)
17 #else
18 #include <unistd.h>
19 #include <netdb.h>
20 #include <sys/socket.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #endif
24
25 #include "ttftp.h"
26
27
28 int             test_Opcode = 0, test_OpcodePassCount = 0;
29 int             test_CorruptByte, test_OpcodePass, test_Type, test_SleepTime;
30 int             test_OpcodePassCount;
31
32 /* testTftp():
33         This function is called at the top of each opcode reception to
34         support the ability to insert various errors into the protocol
35         and/or data.
36 */
37 void
38 testTftp(int opcode,char *msg,int len)
39 {
40         int     i;
41
42         if (opcode != test_Opcode)
43                 return;
44
45         if (++test_OpcodePassCount != test_OpcodePass)
46                 return;
47
48         if (test_Type == TFTPTEST_QUIT) {
49                 exit(1);
50         }
51         else if (test_Type == TFTPTEST_SLEEP) {
52                 fprintf(stderr,"TEST: sleeping...");
53                 for(i=0;i<test_SleepTime;i++) {
54                         sleep(1);
55                         fprintf(stderr,".");
56                 }
57                 fprintf(stderr,"\n");
58         }
59         else if (test_Type == TFTPTEST_CORRUPT) {
60                 fprintf(stderr,"TEST: corruption...");
61                 msg[test_CorruptByte] = 0;
62         }
63         else if (test_Type == TFTPTEST_BBNO) {
64                 fprintf(stderr,"TEST: blockno err...");
65                 msg[3] += 3;
66         }
67 }
68
69 /* testSetup():
70         Called at startup to setup tests that are carried out by testTftp().
71         The incoming line is formatted...
72                 OPCODE,OPCODE_PASS,TESTTYPE,ARG1,ARG2
73         where...
74                 OPCODE is RRQ, WRQ, ACK, DAT or ERR;
75                 OPCODE_PASS indicates what opcode to apply this to, for example,
76                         if OPCODE_PASS is 3 and OPCODE is DAT, the apply this test to
77                         the third TFTP_DAT;
78                 TESTTYPE is SLEEP or CORRUPT
79                         if SLEEP, then ARG1 is sleep time, ARG2 is not used;
80                         if CORRUPT, then ARG1 is byte number, ARG2 is not used; 
81
82 */
83 int
84 testSetup(char *setupline)
85 {
86         int      ccnt;
87         char *commas[5], *cp;
88
89         /* Determin the opcode: */
90         if (!strncmp(setupline,"RRQ,",4))
91                 test_Opcode = TFTP_RRQ;
92         else if (!strncmp(setupline,"WRQ,",4))
93                 test_Opcode = TFTP_WRQ;
94         else if (!strncmp(setupline,"ACK,",4))
95                 test_Opcode = TFTP_ACK;
96         else if (!strncmp(setupline,"DAT,",4))
97                 test_Opcode = TFTP_DAT;
98         else if (!strncmp(setupline,"ERR,",4))
99                 test_Opcode = TFTP_ERR;
100         else {
101                 fprintf(stderr,"Bad opcode.\n");
102                 return(-1);
103         }
104
105         ccnt = 0;
106         cp = setupline;
107         while(*cp) {
108                 if (*cp == ',')
109                         commas[ccnt++] = cp;
110                 cp++;
111         }
112         if ((ccnt < 2) || (ccnt > 4)) {
113                 fprintf(stderr,"Bad comma count.\n");
114                 return(-1);
115         }
116         
117         test_OpcodePass = atoi(commas[0] + 1);
118         if (test_OpcodePass <= 0) {
119                 fprintf(stderr,"Bad opcode pass (out of range).\n");
120                 return(-1);
121         }
122         if (((test_Opcode == TFTP_RRQ) || (test_Opcode == TFTP_WRQ)) &&
123                 (test_OpcodePass != 1)) {
124                 fprintf(stderr,"Bad opcode pass (NA for opcode).\n");
125                 return(-1);
126         }
127
128         if (!strncmp(commas[1]+1,"SLEEP,",6))  {
129                 test_Type = TFTPTEST_SLEEP;
130                 test_SleepTime = atoi(commas[2]+1);
131                 if (test_SleepTime <= 0) {
132                         fprintf(stderr,"Bad sleep time.\n");
133                         return(-1);
134                 }
135         }
136         else if (!strncmp(commas[1]+1,"BBNO",4))  {
137                 test_Type = TFTPTEST_BBNO;
138         }
139         else if (!strncmp(commas[1]+1,"QUIT",4))  {
140                 test_Type = TFTPTEST_QUIT;
141         }
142         else if (!strncmp(commas[1]+1,"CORRUPT,",8)) {
143                 test_Type = TFTPTEST_CORRUPT;
144                 test_CorruptByte = atoi(commas[2]+1);
145                 if (test_CorruptByte <= 0) {
146                         fprintf(stderr,"Bad byte number.\n");
147                         return(-1);
148                 }
149         }
150         else {
151                 fprintf(stderr,"Bad testtype.\n");
152                 return(-1);
153         }
154         
155         return(0);
156 }