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)
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
)
31 memset(buf
, 0, count
);
33 while ((unsigned long) (p
- buf
) < buf_len
&& !strchr(buf
, '%')) {
34 bytes
= read(fd
, p
, count
);
45 START_TEST(test_created_devs
)
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
,
62 static void setup(void) {
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
;
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
));
93 static void teardown(void)
95 tcsetattr(global_fd
, TCSANOW
, &original
);
100 START_TEST(test_wrong_cmd
)
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
);
118 START_TEST(test_a_cmd
)
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
);
136 START_TEST(test_ab_cmd
)
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
);
154 START_TEST(test_foo_cmd
)
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
);
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
);
192 int main(int argc
, char *argv
[])
201 fprintf(stderr
, "You most specify the number of devices\n");
205 fake_devices_number
= strtol(argv
[1], NULL
, 10);
206 if (fake_devices_number
== LONG_MIN
||
207 fake_devices_number
== LONG_MAX
) {
212 s
= fake_device_suite();
213 sr
= srunner_create(s
);
214 srunner_run_all(sr
, CK_NORMAL
);
215 nf
= srunner_ntests_failed(sr
);
218 return (nf
== 0) ? EXIT_SUCCESS
: EXIT_FAILURE
;