port_range_unittest.cc 2.17 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73
// Copyright 2015 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "remoting/protocol/port_range.h"

#include <sstream>

#include "testing/gmock/include/gmock/gmock.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace remoting {

TEST(PortRange, ParseEmpty) {
  PortRange port_range;

  EXPECT_TRUE(PortRange::Parse("", &port_range));
  EXPECT_TRUE(port_range.is_null());
}

TEST(PortRange, ParseValid) {
  PortRange port_range;

  EXPECT_TRUE(PortRange::Parse("1-65535", &port_range));
  EXPECT_FALSE(port_range.is_null());
  EXPECT_EQ(1u, port_range.min_port);
  EXPECT_EQ(65535u, port_range.max_port);

  EXPECT_TRUE(PortRange::Parse(" 1 - 65535 ", &port_range));
  EXPECT_FALSE(port_range.is_null());
  EXPECT_EQ(1u, port_range.min_port);
  EXPECT_EQ(65535u, port_range.max_port);

  EXPECT_TRUE(PortRange::Parse("12400-12400", &port_range));
  EXPECT_FALSE(port_range.is_null());
  EXPECT_EQ(12400u, port_range.min_port);
  EXPECT_EQ(12400u, port_range.max_port);
}

TEST(PortRange, ParseInvalid) {
  PortRange port_range;
  port_range.min_port = 123;
  port_range.max_port = 456;

  EXPECT_FALSE(PortRange::Parse("-65535", &port_range));
  EXPECT_FALSE(PortRange::Parse("1-", &port_range));
  EXPECT_FALSE(PortRange::Parse("-", &port_range));
  EXPECT_FALSE(PortRange::Parse("-1-65535", &port_range));
  EXPECT_FALSE(PortRange::Parse("1--65535", &port_range));
  EXPECT_FALSE(PortRange::Parse("1-65535-", &port_range));
  EXPECT_FALSE(PortRange::Parse("0-65535", &port_range));
  EXPECT_FALSE(PortRange::Parse("1-65536", &port_range));
  EXPECT_FALSE(PortRange::Parse("1-4294967295", &port_range));
  EXPECT_FALSE(PortRange::Parse("10-1", &port_range));
  EXPECT_FALSE(PortRange::Parse("1foo-2bar", &port_range));

  // Unsuccessful parses should NOT modify their output.
  EXPECT_EQ(123, port_range.min_port);
  EXPECT_EQ(456, port_range.max_port);
}

TEST(PortRange, Output) {
  PortRange port_range;
  port_range.min_port = 123;
  port_range.max_port = 456;

  std::ostringstream str;
  str << port_range;

  EXPECT_THAT(str.str(), testing::MatchesRegex(".*123.*456.*"));
}

}  // namespace remoting