2021-11-25 21:11:59 +00:00
|
|
|
#include "common.h"
|
|
|
|
|
|
|
|
#include <assert.h>
|
|
|
|
|
2022-02-05 21:32:40 +00:00
|
|
|
#include "adb/adb_device.h"
|
2022-02-03 21:46:24 +00:00
|
|
|
#include "adb/adb_parser.h"
|
2021-11-25 21:11:59 +00:00
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_adb_devices(void) {
|
2022-02-05 21:32:40 +00:00
|
|
|
char output[] =
|
|
|
|
"List of devices attached\n"
|
|
|
|
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
|
|
|
|
"device:MyDevice transport_id:1\n"
|
|
|
|
"192.168.1.1:5555 device product:MyWifiProduct model:MyWifiModel "
|
|
|
|
"device:MyWifiDevice trandport_id:2\n";
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
|
|
|
bool ok = sc_adb_parse_devices(output, &vec);
|
|
|
|
assert(ok);
|
|
|
|
assert(vec.size == 2);
|
2022-02-05 21:32:40 +00:00
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
struct sc_adb_device *device = &vec.data[0];
|
2022-02-05 21:32:40 +00:00
|
|
|
assert(!strcmp("0123456789abcdef", device->serial));
|
|
|
|
assert(!strcmp("device", device->state));
|
|
|
|
assert(!strcmp("MyModel", device->model));
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
device = &vec.data[1];
|
2022-02-05 21:32:40 +00:00
|
|
|
assert(!strcmp("192.168.1.1:5555", device->serial));
|
|
|
|
assert(!strcmp("device", device->state));
|
|
|
|
assert(!strcmp("MyWifiModel", device->model));
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
sc_adb_devices_destroy(&vec);
|
2022-02-05 21:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_adb_devices_cr(void) {
|
2022-02-05 21:32:40 +00:00
|
|
|
char output[] =
|
|
|
|
"List of devices attached\r\n"
|
|
|
|
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
|
|
|
|
"device:MyDevice transport_id:1\r\n"
|
|
|
|
"192.168.1.1:5555 device product:MyWifiProduct model:MyWifiModel "
|
|
|
|
"device:MyWifiDevice trandport_id:2\r\n";
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
|
|
|
bool ok = sc_adb_parse_devices(output, &vec);
|
|
|
|
assert(ok);
|
|
|
|
assert(vec.size == 2);
|
2022-02-05 21:32:40 +00:00
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
struct sc_adb_device *device = &vec.data[0];
|
2022-02-05 21:32:40 +00:00
|
|
|
assert(!strcmp("0123456789abcdef", device->serial));
|
|
|
|
assert(!strcmp("device", device->state));
|
|
|
|
assert(!strcmp("MyModel", device->model));
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
device = &vec.data[1];
|
2022-02-05 21:32:40 +00:00
|
|
|
assert(!strcmp("192.168.1.1:5555", device->serial));
|
|
|
|
assert(!strcmp("device", device->state));
|
|
|
|
assert(!strcmp("MyWifiModel", device->model));
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
sc_adb_devices_destroy(&vec);
|
2022-02-05 21:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_adb_devices_daemon_start(void) {
|
2022-02-05 21:32:40 +00:00
|
|
|
char output[] =
|
|
|
|
"* daemon not running; starting now at tcp:5037\n"
|
|
|
|
"* daemon started successfully\n"
|
|
|
|
"List of devices attached\n"
|
|
|
|
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
|
|
|
|
"device:MyDevice transport_id:1\n";
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
|
|
|
bool ok = sc_adb_parse_devices(output, &vec);
|
|
|
|
assert(ok);
|
|
|
|
assert(vec.size == 1);
|
2022-02-05 21:32:40 +00:00
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
struct sc_adb_device *device = &vec.data[0];
|
2022-02-05 21:32:40 +00:00
|
|
|
assert(!strcmp("0123456789abcdef", device->serial));
|
|
|
|
assert(!strcmp("device", device->state));
|
|
|
|
assert(!strcmp("MyModel", device->model));
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
sc_adb_devices_destroy(&vec);
|
2022-02-05 21:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_adb_devices_daemon_start_mixed(void) {
|
2022-02-05 21:32:40 +00:00
|
|
|
char output[] =
|
|
|
|
"List of devices attached\n"
|
|
|
|
"adb server version (41) doesn't match this client (39); killing...\n"
|
|
|
|
"* daemon started successfully *\n"
|
|
|
|
"0123456789abcdef unauthorized usb:1-1\n"
|
|
|
|
"87654321 device usb:2-1 product:MyProduct model:MyModel "
|
|
|
|
"device:MyDevice\n";
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
|
|
|
bool ok = sc_adb_parse_devices(output, &vec);
|
|
|
|
assert(ok);
|
|
|
|
assert(vec.size == 2);
|
2022-02-05 21:32:40 +00:00
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
struct sc_adb_device *device = &vec.data[0];
|
2022-02-05 21:32:40 +00:00
|
|
|
assert(!strcmp("0123456789abcdef", device->serial));
|
|
|
|
assert(!strcmp("unauthorized", device->state));
|
|
|
|
assert(!device->model);
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
device = &vec.data[1];
|
2022-02-05 21:32:40 +00:00
|
|
|
assert(!strcmp("87654321", device->serial));
|
|
|
|
assert(!strcmp("device", device->state));
|
|
|
|
assert(!strcmp("MyModel", device->model));
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
sc_adb_devices_destroy(&vec);
|
2022-02-05 21:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_adb_devices_without_eol(void) {
|
2022-02-05 21:32:40 +00:00
|
|
|
char output[] =
|
|
|
|
"List of devices attached\n"
|
|
|
|
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
|
|
|
|
"device:MyDevice transport_id:1";
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
|
|
|
bool ok = sc_adb_parse_devices(output, &vec);
|
|
|
|
assert(ok);
|
|
|
|
assert(vec.size == 1);
|
|
|
|
|
|
|
|
struct sc_adb_device *device = &vec.data[0];
|
2022-02-05 21:32:40 +00:00
|
|
|
assert(!strcmp("0123456789abcdef", device->serial));
|
|
|
|
assert(!strcmp("device", device->state));
|
|
|
|
assert(!strcmp("MyModel", device->model));
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
sc_adb_devices_destroy(&vec);
|
2022-02-05 21:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_adb_devices_without_header(void) {
|
2022-02-05 21:32:40 +00:00
|
|
|
char output[] =
|
|
|
|
"0123456789abcdef device usb:2-1 product:MyProduct model:MyModel "
|
|
|
|
"device:MyDevice transport_id:1\n";
|
2022-02-18 20:16:53 +00:00
|
|
|
|
|
|
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
|
|
|
bool ok = sc_adb_parse_devices(output, &vec);
|
|
|
|
assert(!ok);
|
2022-02-05 21:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_adb_devices_corrupted(void) {
|
2022-02-05 21:32:40 +00:00
|
|
|
char output[] =
|
|
|
|
"List of devices attached\n"
|
|
|
|
"corrupted_garbage\n";
|
2022-02-18 20:16:53 +00:00
|
|
|
|
|
|
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
|
|
|
bool ok = sc_adb_parse_devices(output, &vec);
|
|
|
|
assert(ok);
|
|
|
|
assert(vec.size == 0);
|
2022-02-05 21:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_adb_devices_spaces(void) {
|
2022-02-05 21:32:40 +00:00
|
|
|
char output[] =
|
|
|
|
"List of devices attached\n"
|
|
|
|
"0123456789abcdef unauthorized usb:1-4 transport_id:3\n";
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
struct sc_vec_adb_devices vec = SC_VECTOR_INITIALIZER;
|
|
|
|
bool ok = sc_adb_parse_devices(output, &vec);
|
|
|
|
assert(ok);
|
|
|
|
assert(vec.size == 1);
|
2022-02-05 21:32:40 +00:00
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
struct sc_adb_device *device = &vec.data[0];
|
2022-02-05 21:32:40 +00:00
|
|
|
assert(!strcmp("0123456789abcdef", device->serial));
|
|
|
|
assert(!strcmp("unauthorized", device->state));
|
|
|
|
assert(!device->model);
|
|
|
|
|
2022-02-18 20:16:53 +00:00
|
|
|
sc_adb_devices_destroy(&vec);
|
2022-02-05 21:32:40 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_get_ip_single_line(void) {
|
2021-11-25 21:11:59 +00:00
|
|
|
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
|
|
|
"192.168.12.34\r\r\n";
|
|
|
|
|
2022-02-06 09:52:55 +00:00
|
|
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
2021-11-25 21:11:59 +00:00
|
|
|
assert(ip);
|
|
|
|
assert(!strcmp(ip, "192.168.12.34"));
|
2021-12-31 09:49:22 +00:00
|
|
|
free(ip);
|
2021-11-25 21:11:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_get_ip_single_line_without_eol(void) {
|
2021-11-25 21:11:59 +00:00
|
|
|
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
|
|
|
"192.168.12.34";
|
|
|
|
|
2022-02-06 09:52:55 +00:00
|
|
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
2021-11-25 21:11:59 +00:00
|
|
|
assert(ip);
|
|
|
|
assert(!strcmp(ip, "192.168.12.34"));
|
2021-12-31 09:49:22 +00:00
|
|
|
free(ip);
|
2021-11-25 21:11:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_get_ip_single_line_with_trailing_space(void) {
|
2021-11-25 21:11:59 +00:00
|
|
|
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
|
|
|
"192.168.12.34 \n";
|
|
|
|
|
2022-02-06 09:52:55 +00:00
|
|
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
2021-11-25 21:11:59 +00:00
|
|
|
assert(ip);
|
|
|
|
assert(!strcmp(ip, "192.168.12.34"));
|
2021-12-31 09:49:22 +00:00
|
|
|
free(ip);
|
2021-11-25 21:11:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_get_ip_multiline_first_ok(void) {
|
2021-11-25 21:11:59 +00:00
|
|
|
char ip_route[] = "192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
|
|
|
"192.168.1.2\r\n"
|
|
|
|
"10.0.0.0/24 dev rmnet proto kernel scope link src "
|
|
|
|
"10.0.0.2\r\n";
|
|
|
|
|
2022-02-06 09:52:55 +00:00
|
|
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
2021-11-25 21:11:59 +00:00
|
|
|
assert(ip);
|
|
|
|
assert(!strcmp(ip, "192.168.1.2"));
|
2021-12-31 09:49:22 +00:00
|
|
|
free(ip);
|
2021-11-25 21:11:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_get_ip_multiline_second_ok(void) {
|
2021-11-25 21:11:59 +00:00
|
|
|
char ip_route[] = "10.0.0.0/24 dev rmnet proto kernel scope link src "
|
|
|
|
"10.0.0.3\r\n"
|
|
|
|
"192.168.1.0/24 dev wlan0 proto kernel scope link src "
|
|
|
|
"192.168.1.3\r\n";
|
|
|
|
|
2022-02-06 09:52:55 +00:00
|
|
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
2021-11-25 21:11:59 +00:00
|
|
|
assert(ip);
|
|
|
|
assert(!strcmp(ip, "192.168.1.3"));
|
2021-12-31 09:49:22 +00:00
|
|
|
free(ip);
|
2021-11-25 21:11:59 +00:00
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_get_ip_no_wlan(void) {
|
2021-11-25 21:11:59 +00:00
|
|
|
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
|
|
|
"192.168.12.34\r\r\n";
|
|
|
|
|
2022-02-06 09:52:55 +00:00
|
|
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
|
|
|
assert(!ip);
|
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_get_ip_no_wlan_without_eol(void) {
|
2022-02-06 09:52:55 +00:00
|
|
|
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
|
|
|
"192.168.12.34";
|
|
|
|
|
|
|
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
2021-11-25 21:11:59 +00:00
|
|
|
assert(!ip);
|
|
|
|
}
|
|
|
|
|
2022-05-24 19:03:42 +00:00
|
|
|
static void test_get_ip_truncated(void) {
|
2021-11-25 21:11:59 +00:00
|
|
|
char ip_route[] = "192.168.1.0/24 dev rmnet proto kernel scope link src "
|
|
|
|
"\n";
|
|
|
|
|
2022-02-06 09:52:55 +00:00
|
|
|
char *ip = sc_adb_parse_device_ip_from_output(ip_route);
|
2021-11-25 21:11:59 +00:00
|
|
|
assert(!ip);
|
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char *argv[]) {
|
|
|
|
(void) argc;
|
|
|
|
(void) argv;
|
|
|
|
|
2022-02-05 21:32:40 +00:00
|
|
|
test_adb_devices();
|
|
|
|
test_adb_devices_cr();
|
|
|
|
test_adb_devices_daemon_start();
|
|
|
|
test_adb_devices_daemon_start_mixed();
|
|
|
|
test_adb_devices_without_eol();
|
|
|
|
test_adb_devices_without_header();
|
|
|
|
test_adb_devices_corrupted();
|
|
|
|
test_adb_devices_spaces();
|
|
|
|
|
2021-11-25 21:11:59 +00:00
|
|
|
test_get_ip_single_line();
|
|
|
|
test_get_ip_single_line_without_eol();
|
|
|
|
test_get_ip_single_line_with_trailing_space();
|
|
|
|
test_get_ip_multiline_first_ok();
|
|
|
|
test_get_ip_multiline_second_ok();
|
|
|
|
test_get_ip_no_wlan();
|
2022-02-06 09:52:55 +00:00
|
|
|
test_get_ip_no_wlan_without_eol();
|
2021-11-25 21:11:59 +00:00
|
|
|
test_get_ip_truncated();
|
2022-05-24 19:04:10 +00:00
|
|
|
|
|
|
|
return 0;
|
2021-11-25 21:11:59 +00:00
|
|
|
}
|