From 3f902aad5b427a8162bf860a758878b55b13e775 Mon Sep 17 00:00:00 2001 From: David Pursell Date: Tue, 1 Mar 2016 08:58:26 -0800 Subject: adb: relax serial matching rules. Currently targeting a device by serial requires matching the serial number exactly. This CL relaxes the matching rules for local transports to ignore protocol prefixes and make the port optional: [tcp:|udp:][:port] The purpose of this is to allow a user to set ANDROID_SERIAL to something like "tcp:100.100.100.100" and have it work for both fastboot and adb (assuming the device comes up at 100.100.100.100 in both modes). This CL also adds some unit tests for the modified functions to make sure they work as expected. Bug: 27340240 Change-Id: I006e0c70c84331ab44d05d0a0f462d06592eb879 --- adb/socket_test.cpp | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'adb/socket_test.cpp') diff --git a/adb/socket_test.cpp b/adb/socket_test.cpp index 471ca09e4..5cbef6dcf 100644 --- a/adb/socket_test.cpp +++ b/adb/socket_test.cpp @@ -270,3 +270,49 @@ TEST_F(LocalSocketTest, close_socket_in_CLOSE_WAIT_state) { } #endif // defined(__linux__) + +#if ADB_HOST + +// Checks that skip_host_serial(serial) returns a pointer to the part of |serial| which matches +// |expected|, otherwise logs the failure to gtest. +void VerifySkipHostSerial(const std::string& serial, const char* expected) { + const char* result = internal::skip_host_serial(serial.c_str()); + if (expected == nullptr) { + EXPECT_EQ(nullptr, result); + } else { + EXPECT_STREQ(expected, result); + } +} + +// Check [tcp:|udp:][:]: format. +TEST(socket_test, test_skip_host_serial) { + for (const std::string& protocol : {"", "tcp:", "udp:"}) { + VerifySkipHostSerial(protocol, nullptr); + VerifySkipHostSerial(protocol + "foo", nullptr); + + VerifySkipHostSerial(protocol + "foo:bar", ":bar"); + VerifySkipHostSerial(protocol + "foo:bar:baz", ":bar:baz"); + + VerifySkipHostSerial(protocol + "foo:123:bar", ":bar"); + VerifySkipHostSerial(protocol + "foo:123:456", ":456"); + VerifySkipHostSerial(protocol + "foo:123:bar:baz", ":bar:baz"); + + // Don't register a port unless it's all numbers and ends with ':'. + VerifySkipHostSerial(protocol + "foo:123", ":123"); + VerifySkipHostSerial(protocol + "foo:123bar:baz", ":123bar:baz"); + } +} + +// Check :: format. +TEST(socket_test, test_skip_host_serial_prefix) { + for (const std::string& prefix : {"usb:", "product:", "model:", "device:"}) { + VerifySkipHostSerial(prefix, nullptr); + VerifySkipHostSerial(prefix + "foo", nullptr); + + VerifySkipHostSerial(prefix + "foo:bar", ":bar"); + VerifySkipHostSerial(prefix + "foo:bar:baz", ":bar:baz"); + VerifySkipHostSerial(prefix + "foo:123:bar", ":123:bar"); + } +} + +#endif // ADB_HOST -- cgit v1.2.3