Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "adb_utils.h" |
| 18 | |
| 19 | #include <gtest/gtest.h> |
| 20 | |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 21 | #include <stdlib.h> |
| 22 | #include <string.h> |
| 23 | |
| 24 | #include "sysdeps.h" |
| 25 | |
| 26 | #include <base/test_utils.h> |
| 27 | |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 28 | TEST(adb_utils, directory_exists) { |
| 29 | ASSERT_TRUE(directory_exists("/proc")); |
| 30 | ASSERT_FALSE(directory_exists("/proc/self")); // Symbolic link. |
| 31 | ASSERT_FALSE(directory_exists("/proc/does-not-exist")); |
| 32 | } |
| 33 | |
| 34 | TEST(adb_utils, escape_arg) { |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 35 | ASSERT_EQ(R"('')", escape_arg("")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 36 | |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 37 | ASSERT_EQ(R"('abc')", escape_arg("abc")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 38 | |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 39 | ASSERT_EQ(R"(' abc')", escape_arg(" abc")); |
Elliott Hughes | de52ce2 | 2015-05-15 12:06:00 -0700 | [diff] [blame] | 40 | ASSERT_EQ(R"(''\''abc')", escape_arg("'abc")); |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 41 | ASSERT_EQ(R"('"abc')", escape_arg("\"abc")); |
| 42 | ASSERT_EQ(R"('\abc')", escape_arg("\\abc")); |
| 43 | ASSERT_EQ(R"('(abc')", escape_arg("(abc")); |
| 44 | ASSERT_EQ(R"(')abc')", escape_arg(")abc")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 45 | |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 46 | ASSERT_EQ(R"('abc abc')", escape_arg("abc abc")); |
Elliott Hughes | de52ce2 | 2015-05-15 12:06:00 -0700 | [diff] [blame] | 47 | ASSERT_EQ(R"('abc'\''abc')", escape_arg("abc'abc")); |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 48 | ASSERT_EQ(R"('abc"abc')", escape_arg("abc\"abc")); |
| 49 | ASSERT_EQ(R"('abc\abc')", escape_arg("abc\\abc")); |
| 50 | ASSERT_EQ(R"('abc(abc')", escape_arg("abc(abc")); |
| 51 | ASSERT_EQ(R"('abc)abc')", escape_arg("abc)abc")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 52 | |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 53 | ASSERT_EQ(R"('abc ')", escape_arg("abc ")); |
Elliott Hughes | de52ce2 | 2015-05-15 12:06:00 -0700 | [diff] [blame] | 54 | ASSERT_EQ(R"('abc'\''')", escape_arg("abc'")); |
Elliott Hughes | 48db87f | 2015-04-17 20:50:11 -0700 | [diff] [blame] | 55 | ASSERT_EQ(R"('abc"')", escape_arg("abc\"")); |
| 56 | ASSERT_EQ(R"('abc\')", escape_arg("abc\\")); |
| 57 | ASSERT_EQ(R"('abc(')", escape_arg("abc(")); |
| 58 | ASSERT_EQ(R"('abc)')", escape_arg("abc)")); |
Elliott Hughes | 3810445 | 2015-04-17 13:57:15 -0700 | [diff] [blame] | 59 | } |
Elliott Hughes | 09ccf1f | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 60 | |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame^] | 61 | TEST(adb_utils, adb_basename) { |
| 62 | EXPECT_EQ("sh", adb_basename("/system/bin/sh")); |
| 63 | EXPECT_EQ("sh", adb_basename("sh")); |
| 64 | } |
| 65 | |
Elliott Hughes | 09ccf1f | 2015-07-18 12:21:30 -0700 | [diff] [blame] | 66 | TEST(adb_utils, parse_host_and_port) { |
| 67 | std::string canonical_address; |
| 68 | std::string host; |
| 69 | int port; |
| 70 | std::string error; |
| 71 | |
| 72 | // Name, default port. |
| 73 | port = 123; |
| 74 | ASSERT_TRUE(parse_host_and_port("www.google.com", &canonical_address, &host, &port, &error)); |
| 75 | ASSERT_EQ("www.google.com:123", canonical_address); |
| 76 | ASSERT_EQ("www.google.com", host); |
| 77 | ASSERT_EQ(123, port); |
| 78 | |
| 79 | // Name, explicit port. |
| 80 | ASSERT_TRUE(parse_host_and_port("www.google.com:666", &canonical_address, &host, &port, &error)); |
| 81 | ASSERT_EQ("www.google.com:666", canonical_address); |
| 82 | ASSERT_EQ("www.google.com", host); |
| 83 | ASSERT_EQ(666, port); |
| 84 | |
| 85 | // IPv4, default port. |
| 86 | port = 123; |
| 87 | ASSERT_TRUE(parse_host_and_port("1.2.3.4", &canonical_address, &host, &port, &error)); |
| 88 | ASSERT_EQ("1.2.3.4:123", canonical_address); |
| 89 | ASSERT_EQ("1.2.3.4", host); |
| 90 | ASSERT_EQ(123, port); |
| 91 | |
| 92 | // IPv4, explicit port. |
| 93 | ASSERT_TRUE(parse_host_and_port("1.2.3.4:666", &canonical_address, &host, &port, &error)); |
| 94 | ASSERT_EQ("1.2.3.4:666", canonical_address); |
| 95 | ASSERT_EQ("1.2.3.4", host); |
| 96 | ASSERT_EQ(666, port); |
| 97 | |
| 98 | // Simple IPv6, default port. |
| 99 | port = 123; |
| 100 | ASSERT_TRUE(parse_host_and_port("::1", &canonical_address, &host, &port, &error)); |
| 101 | ASSERT_EQ("[::1]:123", canonical_address); |
| 102 | ASSERT_EQ("::1", host); |
| 103 | ASSERT_EQ(123, port); |
| 104 | |
| 105 | // Simple IPv6, explicit port. |
| 106 | ASSERT_TRUE(parse_host_and_port("[::1]:666", &canonical_address, &host, &port, &error)); |
| 107 | ASSERT_EQ("[::1]:666", canonical_address); |
| 108 | ASSERT_EQ("::1", host); |
| 109 | ASSERT_EQ(666, port); |
| 110 | |
| 111 | // Hairy IPv6, default port. |
| 112 | port = 123; |
| 113 | ASSERT_TRUE(parse_host_and_port("fe80::200:5aee:feaa:20a2", &canonical_address, &host, &port, &error)); |
| 114 | ASSERT_EQ("[fe80::200:5aee:feaa:20a2]:123", canonical_address); |
| 115 | ASSERT_EQ("fe80::200:5aee:feaa:20a2", host); |
| 116 | ASSERT_EQ(123, port); |
| 117 | |
| 118 | // Simple IPv6, explicit port. |
| 119 | ASSERT_TRUE(parse_host_and_port("[fe80::200:5aee:feaa:20a2]:666", &canonical_address, &host, &port, &error)); |
| 120 | ASSERT_EQ("[fe80::200:5aee:feaa:20a2]:666", canonical_address); |
| 121 | ASSERT_EQ("fe80::200:5aee:feaa:20a2", host); |
| 122 | ASSERT_EQ(666, port); |
| 123 | |
| 124 | // Invalid IPv4. |
| 125 | EXPECT_FALSE(parse_host_and_port("1.2.3.4:", &canonical_address, &host, &port, &error)); |
| 126 | EXPECT_FALSE(parse_host_and_port("1.2.3.4::", &canonical_address, &host, &port, &error)); |
| 127 | EXPECT_FALSE(parse_host_and_port("1.2.3.4:hello", &canonical_address, &host, &port, &error)); |
| 128 | EXPECT_FALSE(parse_host_and_port(":123", &canonical_address, &host, &port, &error)); |
| 129 | |
| 130 | // Invalid IPv6. |
| 131 | EXPECT_FALSE(parse_host_and_port(":1", &canonical_address, &host, &port, &error)); |
| 132 | EXPECT_FALSE(parse_host_and_port("::::::::1", &canonical_address, &host, &port, &error)); |
| 133 | EXPECT_FALSE(parse_host_and_port("[::1", &canonical_address, &host, &port, &error)); |
| 134 | EXPECT_FALSE(parse_host_and_port("[::1]", &canonical_address, &host, &port, &error)); |
| 135 | EXPECT_FALSE(parse_host_and_port("[::1]:", &canonical_address, &host, &port, &error)); |
| 136 | EXPECT_FALSE(parse_host_and_port("[::1]::", &canonical_address, &host, &port, &error)); |
| 137 | EXPECT_FALSE(parse_host_and_port("[::1]:hello", &canonical_address, &host, &port, &error)); |
| 138 | |
| 139 | // Invalid ports. |
| 140 | EXPECT_FALSE(parse_host_and_port("[::1]:-1", &canonical_address, &host, &port, &error)); |
| 141 | EXPECT_FALSE(parse_host_and_port("[::1]:0", &canonical_address, &host, &port, &error)); |
| 142 | EXPECT_FALSE(parse_host_and_port("[::1]:65536", &canonical_address, &host, &port, &error)); |
| 143 | EXPECT_FALSE(parse_host_and_port("1.2.3.4:-1", &canonical_address, &host, &port, &error)); |
| 144 | EXPECT_FALSE(parse_host_and_port("1.2.3.4:0", &canonical_address, &host, &port, &error)); |
| 145 | EXPECT_FALSE(parse_host_and_port("1.2.3.4:65536", &canonical_address, &host, &port, &error)); |
| 146 | } |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 147 | |
| 148 | TEST(adb_utils, mkdirs) { |
| 149 | TemporaryDir td; |
Elliott Hughes | d189cfb | 2015-07-30 17:42:01 -0700 | [diff] [blame^] | 150 | std::string path = std::string(td.path) + "/dir/subdir/file"; |
| 151 | EXPECT_TRUE(mkdirs(path)); |
| 152 | EXPECT_NE(-1, adb_creat(path.c_str(), 0600)); |
| 153 | EXPECT_FALSE(mkdirs(path + "/subdir/")); |
Alex Vallée | 28d1f8d | 2015-05-06 16:26:00 -0400 | [diff] [blame] | 154 | } |