diff options
author | David Pursell <dpursell@google.com> | 2016-03-01 08:58:26 -0800 |
---|---|---|
committer | David Pursell <dpursell@google.com> | 2016-03-02 12:54:58 -0800 |
commit | 3f902aad5b427a8162bf860a758878b55b13e775 (patch) | |
tree | 978843a3e8a6f1d1681b65ffa1c80c48c38020a6 /adb/socket_test.cpp | |
parent | d1ec9c450d8dc9d24b5f0254111449ecfcbeaea6 (diff) | |
download | system_core-3f902aad5b427a8162bf860a758878b55b13e775.tar.gz system_core-3f902aad5b427a8162bf860a758878b55b13e775.tar.bz2 system_core-3f902aad5b427a8162bf860a758878b55b13e775.zip |
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:]<hostname>[: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
Diffstat (limited to 'adb/socket_test.cpp')
-rw-r--r-- | adb/socket_test.cpp | 46 |
1 files changed, 46 insertions, 0 deletions
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:]<serial>[:<port>]:<command> 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 <prefix>:<serial>:<command> 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 |