| Joshua Duong | 81f2db4 | 2020-04-16 15:58:19 -0700 | [diff] [blame] | 1 | /* | 
 | 2 |  * Copyright (C) 2020 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 <gtest/gtest.h> | 
 | 18 |  | 
 | 19 | #include "adb_mdns.h" | 
 | 20 |  | 
 | 21 | static bool isValidMdnsServiceName(std::string_view name) { | 
 | 22 |     // The rules for Service Names [RFC6335] state that they may be no more | 
 | 23 |     // than fifteen characters long (not counting the mandatory underscore), | 
 | 24 |     // consisting of only letters, digits, and hyphens, must begin and end | 
 | 25 |     // with a letter or digit, must not contain consecutive hyphens, and | 
 | 26 |     // must contain at least one letter. | 
 | 27 |  | 
 | 28 |     // No more than 15 characters long | 
 | 29 |     if (name.empty() || name.size() > 15) { | 
 | 30 |         return false; | 
 | 31 |     } | 
 | 32 |  | 
 | 33 |     bool hasAtLeastOneLetter = false; | 
 | 34 |     bool sawHyphen = false; | 
 | 35 |     for (size_t i = 0; i < name.size(); ++i) { | 
 | 36 |         // Must contain at least one letter | 
 | 37 |         // Only contains letters, digits and hyphens | 
 | 38 |         if (name[i] == '-') { | 
 | 39 |             // Cannot be at beginning or end | 
 | 40 |             if (i == 0 || i == name.size() - 1) { | 
 | 41 |                 return false; | 
 | 42 |             } | 
 | 43 |             if (sawHyphen) { | 
 | 44 |                 // Consecutive hyphen found | 
 | 45 |                 return false; | 
 | 46 |             } | 
 | 47 |             sawHyphen = true; | 
 | 48 |             continue; | 
 | 49 |         } | 
 | 50 |  | 
 | 51 |         sawHyphen = false; | 
 | 52 |         if ((name[i] >= 'a' && name[i] <= 'z') || (name[i] >= 'A' && name[i] <= 'Z')) { | 
 | 53 |             hasAtLeastOneLetter = true; | 
 | 54 |             continue; | 
 | 55 |         } | 
 | 56 |  | 
 | 57 |         if (name[i] >= '0' && name[i] <= '9') { | 
 | 58 |             continue; | 
 | 59 |         } | 
 | 60 |  | 
 | 61 |         // Invalid character | 
 | 62 |         return false; | 
 | 63 |     } | 
 | 64 |  | 
 | 65 |     return hasAtLeastOneLetter; | 
 | 66 | } | 
 | 67 |  | 
 | 68 | TEST(mdns, test_isValidMdnsServiceName) { | 
 | 69 |     // Longer than 15 characters | 
 | 70 |     EXPECT_FALSE(isValidMdnsServiceName("abcd1234abcd1234")); | 
 | 71 |  | 
 | 72 |     // Contains invalid characters | 
 | 73 |     EXPECT_FALSE(isValidMdnsServiceName("a*a")); | 
 | 74 |     EXPECT_FALSE(isValidMdnsServiceName("a_a")); | 
 | 75 |     EXPECT_FALSE(isValidMdnsServiceName("_a")); | 
 | 76 |  | 
 | 77 |     // Does not begin or end with letter or digit | 
 | 78 |     EXPECT_FALSE(isValidMdnsServiceName("")); | 
 | 79 |     EXPECT_FALSE(isValidMdnsServiceName("-")); | 
 | 80 |     EXPECT_FALSE(isValidMdnsServiceName("-a")); | 
 | 81 |     EXPECT_FALSE(isValidMdnsServiceName("-1")); | 
 | 82 |     EXPECT_FALSE(isValidMdnsServiceName("a-")); | 
 | 83 |     EXPECT_FALSE(isValidMdnsServiceName("1-")); | 
 | 84 |  | 
 | 85 |     // Contains consecutive hyphens | 
 | 86 |     EXPECT_FALSE(isValidMdnsServiceName("a--a")); | 
 | 87 |  | 
 | 88 |     // Does not contain at least one letter | 
 | 89 |     EXPECT_FALSE(isValidMdnsServiceName("1")); | 
 | 90 |     EXPECT_FALSE(isValidMdnsServiceName("12")); | 
 | 91 |     EXPECT_FALSE(isValidMdnsServiceName("1-2")); | 
 | 92 |  | 
 | 93 |     // Some valid names | 
 | 94 |     EXPECT_TRUE(isValidMdnsServiceName("a")); | 
 | 95 |     EXPECT_TRUE(isValidMdnsServiceName("a1")); | 
 | 96 |     EXPECT_TRUE(isValidMdnsServiceName("1A")); | 
 | 97 |     EXPECT_TRUE(isValidMdnsServiceName("aZ")); | 
 | 98 |     EXPECT_TRUE(isValidMdnsServiceName("a-Z")); | 
 | 99 |     EXPECT_TRUE(isValidMdnsServiceName("a-b-Z")); | 
 | 100 |     EXPECT_TRUE(isValidMdnsServiceName("abc-def-123-456")); | 
 | 101 | } | 
 | 102 |  | 
 | 103 | TEST(mdns, ServiceName_RFC6335) { | 
 | 104 |     EXPECT_TRUE(isValidMdnsServiceName(ADB_MDNS_SERVICE_TYPE)); | 
 | 105 |     EXPECT_TRUE(isValidMdnsServiceName(ADB_MDNS_TLS_PAIRING_TYPE)); | 
 | 106 |     EXPECT_TRUE(isValidMdnsServiceName(ADB_MDNS_TLS_CONNECT_TYPE)); | 
 | 107 | } |