summaryrefslogtreecommitdiffstats
path: root/netwerk/test/gtest/TestSocketTransportService.cpp
blob: 89adad3740e348589806eda17a3d7e6108b8cf4c (plain)
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include "gtest/gtest.h"

#include "nsCOMPtr.h"
#include "nsISocketTransport.h"
#include "nsString.h"
#include "nsComponentManagerUtils.h"
#include "../../base/nsSocketTransportService2.h"
#include "nsServiceManagerUtils.h"
#include "nsThreadUtils.h"

namespace mozilla {
namespace net {

TEST(TestSocketTransportService, PortRemappingPreferenceReading)
{
  nsCOMPtr<nsISocketTransportService> service =
      do_GetService("@mozilla.org/network/socket-transport-service;1");
  ASSERT_TRUE(service);

  auto* sts = gSocketTransportService;
  ASSERT_TRUE(sts);

  NS_DispatchAndSpinEventLoopUntilComplete(
      "test"_ns, sts, NS_NewRunnableFunction("test", [&]() {
        auto CheckPortRemap = [&](uint16_t input, uint16_t output) -> bool {
          sts->ApplyPortRemap(&input);
          return input == output;
        };

        // Ill-formed prefs
        ASSERT_FALSE(sts->UpdatePortRemapPreference(";"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference(" ;"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("; "_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("foo"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference(" foo"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference(" foo "_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("10=20;"_ns));

        ASSERT_FALSE(sts->UpdatePortRemapPreference("1"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1="_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1,="_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1-="_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1-,="_ns));

        ASSERT_FALSE(sts->UpdatePortRemapPreference("1=2,"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1=2-3"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1-2,=3"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1-2,3"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1-2,3-4"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1-2,3-4,"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1-2,3-4="_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("10000000=10"_ns));

        ASSERT_FALSE(sts->UpdatePortRemapPreference("1=2;3"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1-4=2;3"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1-4=2;3="_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1-foo=2;3=15"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1-4=foo;3=15"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1-4=2;foo=15"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1-4=2;3=foo"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1-4=2x3=15"_ns));
        ASSERT_FALSE(sts->UpdatePortRemapPreference("1+4=2;3=15"_ns));

        // Well-formed prefs
        ASSERT_TRUE(sts->UpdatePortRemapPreference("1=2"_ns));
        ASSERT_TRUE(CheckPortRemap(1, 2));
        ASSERT_TRUE(CheckPortRemap(2, 2));
        ASSERT_TRUE(CheckPortRemap(3, 3));

        ASSERT_TRUE(sts->UpdatePortRemapPreference("10=20"_ns));
        ASSERT_TRUE(CheckPortRemap(1, 1));
        ASSERT_TRUE(CheckPortRemap(2, 2));
        ASSERT_TRUE(CheckPortRemap(3, 3));
        ASSERT_TRUE(CheckPortRemap(10, 20));

        ASSERT_TRUE(sts->UpdatePortRemapPreference("100-200=1000"_ns));
        ASSERT_TRUE(CheckPortRemap(10, 10));
        ASSERT_TRUE(CheckPortRemap(99, 99));
        ASSERT_TRUE(CheckPortRemap(100, 1000));
        ASSERT_TRUE(CheckPortRemap(101, 1000));
        ASSERT_TRUE(CheckPortRemap(200, 1000));
        ASSERT_TRUE(CheckPortRemap(201, 201));

        ASSERT_TRUE(sts->UpdatePortRemapPreference("100-200,500=1000"_ns));
        ASSERT_TRUE(CheckPortRemap(10, 10));
        ASSERT_TRUE(CheckPortRemap(99, 99));
        ASSERT_TRUE(CheckPortRemap(100, 1000));
        ASSERT_TRUE(CheckPortRemap(101, 1000));
        ASSERT_TRUE(CheckPortRemap(200, 1000));
        ASSERT_TRUE(CheckPortRemap(201, 201));
        ASSERT_TRUE(CheckPortRemap(499, 499));
        ASSERT_TRUE(CheckPortRemap(500, 1000));
        ASSERT_TRUE(CheckPortRemap(501, 501));

        ASSERT_TRUE(sts->UpdatePortRemapPreference("1-3=10;5-8,12=20"_ns));
        ASSERT_TRUE(CheckPortRemap(1, 10));
        ASSERT_TRUE(CheckPortRemap(2, 10));
        ASSERT_TRUE(CheckPortRemap(3, 10));
        ASSERT_TRUE(CheckPortRemap(4, 4));
        ASSERT_TRUE(CheckPortRemap(5, 20));
        ASSERT_TRUE(CheckPortRemap(8, 20));
        ASSERT_TRUE(CheckPortRemap(11, 11));
        ASSERT_TRUE(CheckPortRemap(12, 20));

        ASSERT_TRUE(sts->UpdatePortRemapPreference("80=8080;443=8080"_ns));
        ASSERT_TRUE(CheckPortRemap(80, 8080));
        ASSERT_TRUE(CheckPortRemap(443, 8080));

        // Later rules rewrite earlier rules
        ASSERT_TRUE(sts->UpdatePortRemapPreference("10=100;10=200"_ns));
        ASSERT_TRUE(CheckPortRemap(10, 200));

        ASSERT_TRUE(sts->UpdatePortRemapPreference("10-20=100;10-20=200"_ns));
        ASSERT_TRUE(CheckPortRemap(10, 200));
        ASSERT_TRUE(CheckPortRemap(20, 200));

        ASSERT_TRUE(sts->UpdatePortRemapPreference("10-20=100;15=200"_ns));
        ASSERT_TRUE(CheckPortRemap(10, 100));
        ASSERT_TRUE(CheckPortRemap(15, 200));
        ASSERT_TRUE(CheckPortRemap(20, 100));

        ASSERT_TRUE(sts->UpdatePortRemapPreference(
            " 100 - 200 = 1000 ; 150 = 2000 "_ns));
        ASSERT_TRUE(CheckPortRemap(100, 1000));
        ASSERT_TRUE(CheckPortRemap(150, 2000));
        ASSERT_TRUE(CheckPortRemap(200, 1000));

        // Turn off any mapping
        ASSERT_TRUE(sts->UpdatePortRemapPreference(""_ns));
        for (uint32_t port = 0; port < 65536; ++port) {
          ASSERT_TRUE(CheckPortRemap((uint16_t)port, (uint16_t)port));
        }
      }));
}

TEST(TestSocketTransportService, StatusValues)
{
  static_assert(static_cast<nsresult>(nsISocketTransport::STATUS_RESOLVING) ==
                NS_NET_STATUS_RESOLVING_HOST);
  static_assert(static_cast<nsresult>(nsISocketTransport::STATUS_RESOLVED) ==
                NS_NET_STATUS_RESOLVED_HOST);
  static_assert(
      static_cast<nsresult>(nsISocketTransport::STATUS_CONNECTING_TO) ==
      NS_NET_STATUS_CONNECTING_TO);
  static_assert(
      static_cast<nsresult>(nsISocketTransport::STATUS_CONNECTED_TO) ==
      NS_NET_STATUS_CONNECTED_TO);
  static_assert(static_cast<nsresult>(nsISocketTransport::STATUS_SENDING_TO) ==
                NS_NET_STATUS_SENDING_TO);
  static_assert(static_cast<nsresult>(nsISocketTransport::STATUS_WAITING_FOR) ==
                NS_NET_STATUS_WAITING_FOR);
  static_assert(
      static_cast<nsresult>(nsISocketTransport::STATUS_RECEIVING_FROM) ==
      NS_NET_STATUS_RECEIVING_FROM);
  static_assert(static_cast<nsresult>(
                    nsISocketTransport::STATUS_TLS_HANDSHAKE_STARTING) ==
                NS_NET_STATUS_TLS_HANDSHAKE_STARTING);
  static_assert(
      static_cast<nsresult>(nsISocketTransport::STATUS_TLS_HANDSHAKE_ENDED) ==
      NS_NET_STATUS_TLS_HANDSHAKE_ENDED);
}

}  // namespace net
}  // namespace mozilla