Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2016 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 "android-base/parsenetaddress.h"
     18 
     19 #include <gtest/gtest.h>
     20 
     21 using android::base::ParseNetAddress;
     22 
     23 TEST(ParseNetAddressTest, TestUrl) {
     24   std::string canonical, host, error;
     25   int port = 123;
     26 
     27   EXPECT_TRUE(
     28       ParseNetAddress("www.google.com", &host, &port, &canonical, &error));
     29   EXPECT_EQ("www.google.com:123", canonical);
     30   EXPECT_EQ("www.google.com", host);
     31   EXPECT_EQ(123, port);
     32 
     33   EXPECT_TRUE(
     34       ParseNetAddress("www.google.com:666", &host, &port, &canonical, &error));
     35   EXPECT_EQ("www.google.com:666", canonical);
     36   EXPECT_EQ("www.google.com", host);
     37   EXPECT_EQ(666, port);
     38 }
     39 
     40 TEST(ParseNetAddressTest, TestIpv4) {
     41   std::string canonical, host, error;
     42   int port = 123;
     43 
     44   EXPECT_TRUE(ParseNetAddress("1.2.3.4", &host, &port, &canonical, &error));
     45   EXPECT_EQ("1.2.3.4:123", canonical);
     46   EXPECT_EQ("1.2.3.4", host);
     47   EXPECT_EQ(123, port);
     48 
     49   EXPECT_TRUE(ParseNetAddress("1.2.3.4:666", &host, &port, &canonical, &error));
     50   EXPECT_EQ("1.2.3.4:666", canonical);
     51   EXPECT_EQ("1.2.3.4", host);
     52   EXPECT_EQ(666, port);
     53 }
     54 
     55 TEST(ParseNetAddressTest, TestIpv6) {
     56   std::string canonical, host, error;
     57   int port = 123;
     58 
     59   EXPECT_TRUE(ParseNetAddress("::1", &host, &port, &canonical, &error));
     60   EXPECT_EQ("[::1]:123", canonical);
     61   EXPECT_EQ("::1", host);
     62   EXPECT_EQ(123, port);
     63 
     64   EXPECT_TRUE(ParseNetAddress("fe80::200:5aee:feaa:20a2", &host, &port,
     65                               &canonical, &error));
     66   EXPECT_EQ("[fe80::200:5aee:feaa:20a2]:123", canonical);
     67   EXPECT_EQ("fe80::200:5aee:feaa:20a2", host);
     68   EXPECT_EQ(123, port);
     69 
     70   EXPECT_TRUE(ParseNetAddress("[::1]:666", &host, &port, &canonical, &error));
     71   EXPECT_EQ("[::1]:666", canonical);
     72   EXPECT_EQ("::1", host);
     73   EXPECT_EQ(666, port);
     74 
     75   EXPECT_TRUE(ParseNetAddress("[fe80::200:5aee:feaa:20a2]:666", &host, &port,
     76                               &canonical, &error));
     77   EXPECT_EQ("[fe80::200:5aee:feaa:20a2]:666", canonical);
     78   EXPECT_EQ("fe80::200:5aee:feaa:20a2", host);
     79   EXPECT_EQ(666, port);
     80 }
     81 
     82 TEST(ParseNetAddressTest, TestInvalidAddress) {
     83   std::string canonical, host;
     84   int port;
     85 
     86   std::string failure_cases[] = {
     87       // Invalid IPv4.
     88       "1.2.3.4:",
     89       "1.2.3.4::",
     90       ":123",
     91 
     92       // Invalid IPv6.
     93       ":1",
     94       "::::::::1",
     95       "[::1",
     96       "[::1]",
     97       "[::1]:",
     98       "[::1]::",
     99 
    100       // Invalid port.
    101       "1.2.3.4:-1",
    102       "1.2.3.4:0",
    103       "1.2.3.4:65536"
    104       "1.2.3.4:hello",
    105       "[::1]:-1",
    106       "[::1]:0",
    107       "[::1]:65536",
    108       "[::1]:hello",
    109   };
    110 
    111   for (const auto& address : failure_cases) {
    112     // Failure should give some non-empty error string.
    113     std::string error;
    114     EXPECT_FALSE(ParseNetAddress(address, &host, &port, &canonical, &error));
    115     EXPECT_NE("", error);
    116   }
    117 }
    118 
    119 // Null canonical address argument.
    120 TEST(ParseNetAddressTest, TestNullCanonicalAddress) {
    121   std::string host, error;
    122   int port = 42;
    123 
    124   EXPECT_TRUE(ParseNetAddress("www.google.com", &host, &port, nullptr, &error));
    125   EXPECT_TRUE(ParseNetAddress("1.2.3.4", &host, &port, nullptr, &error));
    126   EXPECT_TRUE(ParseNetAddress("::1", &host, &port, nullptr, &error));
    127 }
    128