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