Introduce old redir program
[lcapit-junk-code.git] / linux-kernel / fake-serial / user-land / test-suite.c
blobb19dd14c77f6b5c8c1ed1cad2cf02f0daea83b4c
1 #include <stdio.h>
2 #include <check.h>
3 #include <string.h>
4 #include <errno.h>
5 #include <sys/types.h>
6 #include <sys/stat.h>
7 #include <fcntl.h>
8 #include <termios.h>
9 #include <unistd.h>
10 #include <stdlib.h>
11 #include <limits.h>
13 #define FAKE_TESTS_BUF_MAX 64
14 #define FAKE_TESTS_DEF_TIMEOUT 10
15 #define FAKE_TESTS_DFL_PORT "/dev/ttyFAKEDEVICE0"
17 #define NOT_USED(x) (x = x)
19 static int global_fd;
20 static struct termios original;
21 static long fake_devices_number;
23 static int read_cmd_response(int fd, char *buf, size_t buf_len)
25 char *p;
26 size_t count;
27 ssize_t bytes;
29 p = buf;
30 count = buf_len;
31 memset(buf, 0, count);
33 while ((unsigned long) (p - buf) < buf_len && !strchr(buf, '%')) {
34 bytes = read(fd, p, count);
35 if (bytes < 0)
36 return -1;
38 p += bytes;
39 count -= bytes;
42 return 0;
45 START_TEST(test_created_devs)
47 int fd;
48 long i;
49 char port[PATH_MAX+1];
51 for (i = 0; i < fake_devices_number; i++) {
52 memset(port, 0, sizeof(port));
53 sprintf(port, "/dev/ttyFAKEDEVICE%lu", i);
54 fd = open(port, O_RDWR);
55 fail_unless(fd > 0, "open() %lu failed: %s", i,
56 strerror(errno));
57 close(fd);
60 END_TEST
62 static void setup(void) {
63 int fd, ret;
64 struct termios tio;
66 fd = open(FAKE_TESTS_DFL_PORT, O_RDWR | O_NOCTTY);
67 fail_unless(fd > 0, "open(): %s\n", strerror(errno));
69 ret = tcgetattr(fd, &original);
70 fail_unless(ret == 0, "tcgetattr(): %s\b", strerror(errno));
72 memset(&tio, 0, sizeof(struct termios));
73 tio.c_cflag = CS8 | CREAD | CRTSCTS | CLOCAL | HUPCL;
74 tio.c_iflag = IGNPAR | IGNBRK;
75 tio.c_oflag = 0;
76 tio.c_lflag = 0;
77 tio.c_cc[VTIME] = 1;
78 tio.c_cc[VMIN] = 0;
80 /* If it fails, we will use the default settings */
81 cfsetispeed(&tio, B115200);
82 cfsetospeed(&tio, B115200);
84 ret = tcflush(fd, TCIFLUSH);
85 fail_unless(ret == 0, "tcflush(): %s\b", strerror(errno));
87 ret = tcsetattr(fd, TCSANOW, &tio);
88 fail_unless(ret == 0, "tcsetattr(): %s\b", strerror(errno));
90 global_fd = fd;
93 static void teardown(void)
95 tcsetattr(global_fd, TCSANOW, &original);
96 close(global_fd);
97 global_fd = -1;
100 START_TEST(test_wrong_cmd)
102 int ret;
103 ssize_t bytes;
104 char buf[FAKE_TESTS_BUF_MAX];
105 const char wcmd[] = "AAAA%";
106 const size_t wcmd_len = sizeof(wcmd);
107 const char *error_cmd = "ERROR: Sua mula%";
109 bytes = write(global_fd, wcmd, wcmd_len);
110 fail_unless((unsigned long) bytes == wcmd_len, "Written %d\n", (int) bytes);
112 ret = read_cmd_response(global_fd, buf, sizeof(buf));
113 fail_unless(ret == 0, "read_cmd_response(): %s\n", strerror(errno));
114 fail_unless(!strcmp(buf, error_cmd), "Got: %s\n", buf);
116 END_TEST
118 START_TEST(test_a_cmd)
120 int ret;
121 ssize_t bytes;
122 char buf[FAKE_TESTS_BUF_MAX];
123 const char wcmd[] = "AT+A%";
124 const size_t wcmd_len = sizeof(wcmd);
125 const char *resp = "AT+A: aaaaaa%";
127 bytes = write(global_fd, wcmd, wcmd_len);
128 fail_unless((unsigned long) bytes == wcmd_len, "Written %d\n", (int) bytes);
130 ret = read_cmd_response(global_fd, buf, sizeof(buf));
131 fail_unless(ret == 0, "read_cmd_response(): %s\n", strerror(errno));
132 fail_unless(!strcmp(buf, resp), "Got: %s\n", buf);
134 END_TEST
136 START_TEST(test_ab_cmd)
138 int ret;
139 ssize_t bytes;
140 char buf[FAKE_TESTS_BUF_MAX];
141 const char wcmd[] = "AT+AB%";
142 const size_t wcmd_len = sizeof(wcmd);
143 const char *resp = "AT+AB: seu puto%";
145 bytes = write(global_fd, wcmd, wcmd_len);
146 fail_unless((unsigned long) bytes == wcmd_len, "Written %d\n", (int) bytes);
148 ret = read_cmd_response(global_fd, buf, sizeof(buf));
149 fail_unless(ret == 0, "read_cmd_response(): %s\n", strerror(errno));
150 fail_unless(!strcmp(buf, resp), "Got: %s\n", buf);
152 END_TEST
154 START_TEST(test_foo_cmd)
156 int ret;
157 ssize_t bytes;
158 char buf[FAKE_TESTS_BUF_MAX];
159 const char wcmd[] = "AT+FOO%";
160 const size_t wcmd_len = sizeof(wcmd);
161 const char *resp = "AT+FOO: canalha!%";
163 bytes = write(global_fd, wcmd, wcmd_len);
164 fail_unless((unsigned long) bytes == wcmd_len, "Written %d\n", (int) bytes);
166 ret = read_cmd_response(global_fd, buf, sizeof(buf));
167 fail_unless(ret == 0, "read_cmd_response(): %s\n", strerror(errno));
168 fail_unless(!strcmp(buf, resp), "Got: %s\n", buf);
170 END_TEST
172 static Suite *fake_device_suite(void)
174 Suite *s = suite_create("Fake device basic");
175 TCase *tc_basic = tcase_create("Basic");
176 TCase *tc_func = tcase_create("Functionality");
178 suite_add_tcase(s, tc_basic);
179 tcase_add_test(tc_basic, test_created_devs);
181 suite_add_tcase(s, tc_func);
182 tcase_add_test(tc_func, test_wrong_cmd);
183 tcase_add_test(tc_func, test_a_cmd);
184 tcase_add_test(tc_func, test_ab_cmd);
185 tcase_add_test(tc_func, test_foo_cmd);
186 tcase_add_checked_fixture(tc_func, setup, teardown);
187 tcase_set_timeout(tc_func, FAKE_TESTS_DEF_TIMEOUT);
189 return s;
192 int main(int argc, char *argv[])
194 int nf;
195 Suite *s;
196 SRunner *sr;
198 NOT_USED(argc);
200 if (!argv[1]) {
201 fprintf(stderr, "You most specify the number of devices\n");
202 exit(1);
205 fake_devices_number = strtol(argv[1], NULL, 10);
206 if (fake_devices_number == LONG_MIN ||
207 fake_devices_number == LONG_MAX) {
208 perror("strtol()");
209 exit(1);
212 s = fake_device_suite();
213 sr = srunner_create(s);
214 srunner_run_all(sr, CK_NORMAL);
215 nf = srunner_ntests_failed(sr);
216 srunner_free(sr);
218 return (nf == 0) ? EXIT_SUCCESS : EXIT_FAILURE;