1 /* Written by Anthony Liguori <aliguori@us.ibm.com> */
2 
3 #include <stdio.h>
4 #include <stdlib.h>
5 #include <time.h>
6 #include <termios.h>
7 #include <unistd.h>
8 
canonicalize(char * buffer)9 static void canonicalize(char *buffer)
10 {
11 	char *reader, *writer;
12 
13 	reader = writer = buffer;
14 
15 	while (*reader) {
16 		*writer = *reader;
17 		if (*reader != '\r') writer++;
18 		reader++;
19 	}
20 	*writer = *reader;
21 }
22 
main(int argc,char ** argv)23 int main(int argc, char **argv)
24 {
25 	char buffer[4096];
26 	char *line;
27 	unsigned int seed;
28 	size_t size;
29 	int i;
30 	int runs;
31 	struct termios term;
32 
33 	tcgetattr(STDIN_FILENO, &term);
34 	cfmakeraw(&term);
35 	tcsetattr(STDIN_FILENO, TCSAFLUSH, &term);
36 
37 	tcgetattr(STDOUT_FILENO, &term);
38 	cfmakeraw(&term);
39 	tcsetattr(STDOUT_FILENO, TCSAFLUSH, &term);
40 
41 	printf("!!!XEN Test Begin!!!\n"); fflush(stdout);
42 	line = fgets(buffer, sizeof(buffer), stdin);
43 	if (line == NULL) {
44 		printf("Failure\n"); fflush(stdout);
45 		return 1;
46 	}
47 
48 	canonicalize(line);
49 	seed = strtoul(line, 0, 0);
50 
51 	printf("Seed Okay.\n"); fflush(stdout);
52 
53 	srandom(seed);
54 
55 	for (runs = (random() % 100000) + 4096; runs > 0; runs--) {
56 		size = random() % 4096;
57 
58 		for (i = 0; i < size; i++) {
59 			int ch;
60 			int exp;
61 
62 			ch = fgetc(stdin);
63 			exp = random() & 0xFF;
64 			if (ch != exp) {
65 				printf("Expected %d got %d\n",
66 				       exp, ch);
67 				fflush(stdout);
68 			}
69 			printf("Got %d/%d good bytes\n", i, size);
70 		}
71 
72 		printf("Okay.\n"); fflush(stdout);
73 	}
74 
75 	return 0;
76 }
77