1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "third_party/blink/renderer/platform/weborigin/known_ports.h"
6 #include "third_party/blink/renderer/platform/weborigin/kurl.h"
7 
8 #include "testing/gtest/include/gtest/gtest.h"
9 
10 namespace blink {
11 
TEST(KnownPortsTest,IsDefaultPortForProtocol)12 TEST(KnownPortsTest, IsDefaultPortForProtocol) {
13   struct TestCase {
14     const uint16_t port;
15     const char* protocol;
16     const bool is_known;
17   } inputs[] = {
18       // Known ones.
19       {80, "http", true},
20       {443, "https", true},
21       {80, "ws", true},
22       {443, "wss", true},
23       {21, "ftp", true},
24 
25       // Unknown ones.
26       {5, "foo", false},
27       {80, "http:", false},
28       {443, "http", false},
29       {21, "ftps", false},
30       {990, "ftps", false},
31       {990, "ftp", false},
32 
33       // With upper cases.
34       {80, "HTTP", false},
35       {443, "Https", false},
36   };
37 
38   for (const TestCase& test : inputs) {
39     bool result = IsDefaultPortForProtocol(test.port, test.protocol);
40     EXPECT_EQ(test.is_known, result);
41   }
42 }
43 
TEST(KnownPortsTest,DefaultPortForProtocol)44 TEST(KnownPortsTest, DefaultPortForProtocol) {
45   struct TestCase {
46     const uint16_t port;
47     const char* protocol;
48   } inputs[] = {
49       // Known ones.
50       {80, "http"},
51       {443, "https"},
52       {80, "ws"},
53       {443, "wss"},
54       {21, "ftp"},
55 
56       // Unknown ones.
57       {0, "foo"},
58       {0, "http:"},
59       {0, "HTTP"},
60       {0, "Https"},
61       {0, "ftps"},
62   };
63 
64   for (const TestCase& test : inputs)
65     EXPECT_EQ(test.port, DefaultPortForProtocol(test.protocol));
66 }
67 
TEST(KnownPortsTest,IsPortAllowedForScheme)68 TEST(KnownPortsTest, IsPortAllowedForScheme) {
69   struct TestCase {
70     const char* url;
71     const bool is_allowed;
72   } inputs[] = {
73       // Allowed ones.
74       {"http://example.com", true},
75       {"file://example.com", true},
76       {"file://example.com:87", true},
77       {"ftp://example.com:21", true},
78       {"http://example.com:80", true},
79       {"http://example.com:8889", true},
80 
81       // Disallowed ones.
82       {"ftp://example.com:87", false},
83       {"ws://example.com:21", false},
84   };
85 
86   for (const TestCase& test : inputs)
87     EXPECT_EQ(test.is_allowed, IsPortAllowedForScheme(KURL(test.url)));
88 }
89 
90 }  // namespace blink
91