1 #include "gtest/gtest.h"
4 #include "nsISocketTransport.h"
6 #include "nsComponentManagerUtils.h"
7 #include "../../base/nsSocketTransportService2.h"
8 #include "nsServiceManagerUtils.h"
9 #include "nsThreadUtils.h"
14 TEST(TestSocketTransportService
, PortRemappingPreferenceReading
)
16 nsCOMPtr
<nsISocketTransportService
> service
=
17 do_GetService("@mozilla.org/network/socket-transport-service;1");
20 auto* sts
= gSocketTransportService
;
23 NS_DispatchAndSpinEventLoopUntilComplete(
24 "test"_ns
, sts
, NS_NewRunnableFunction("test", [&]() {
25 auto CheckPortRemap
= [&](uint16_t input
, uint16_t output
) -> bool {
26 sts
->ApplyPortRemap(&input
);
27 return input
== output
;
31 ASSERT_FALSE(sts
->UpdatePortRemapPreference(";"_ns
));
32 ASSERT_FALSE(sts
->UpdatePortRemapPreference(" ;"_ns
));
33 ASSERT_FALSE(sts
->UpdatePortRemapPreference("; "_ns
));
34 ASSERT_FALSE(sts
->UpdatePortRemapPreference("foo"_ns
));
35 ASSERT_FALSE(sts
->UpdatePortRemapPreference(" foo"_ns
));
36 ASSERT_FALSE(sts
->UpdatePortRemapPreference(" foo "_ns
));
37 ASSERT_FALSE(sts
->UpdatePortRemapPreference("10=20;"_ns
));
39 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1"_ns
));
40 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1="_ns
));
41 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1,="_ns
));
42 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1-="_ns
));
43 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1-,="_ns
));
45 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1=2,"_ns
));
46 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1=2-3"_ns
));
47 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1-2,=3"_ns
));
48 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1-2,3"_ns
));
49 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1-2,3-4"_ns
));
50 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1-2,3-4,"_ns
));
51 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1-2,3-4="_ns
));
52 ASSERT_FALSE(sts
->UpdatePortRemapPreference("10000000=10"_ns
));
54 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1=2;3"_ns
));
55 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1-4=2;3"_ns
));
56 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1-4=2;3="_ns
));
57 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1-foo=2;3=15"_ns
));
58 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1-4=foo;3=15"_ns
));
59 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1-4=2;foo=15"_ns
));
60 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1-4=2;3=foo"_ns
));
61 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1-4=2x3=15"_ns
));
62 ASSERT_FALSE(sts
->UpdatePortRemapPreference("1+4=2;3=15"_ns
));
65 ASSERT_TRUE(sts
->UpdatePortRemapPreference("1=2"_ns
));
66 ASSERT_TRUE(CheckPortRemap(1, 2));
67 ASSERT_TRUE(CheckPortRemap(2, 2));
68 ASSERT_TRUE(CheckPortRemap(3, 3));
70 ASSERT_TRUE(sts
->UpdatePortRemapPreference("10=20"_ns
));
71 ASSERT_TRUE(CheckPortRemap(1, 1));
72 ASSERT_TRUE(CheckPortRemap(2, 2));
73 ASSERT_TRUE(CheckPortRemap(3, 3));
74 ASSERT_TRUE(CheckPortRemap(10, 20));
76 ASSERT_TRUE(sts
->UpdatePortRemapPreference("100-200=1000"_ns
));
77 ASSERT_TRUE(CheckPortRemap(10, 10));
78 ASSERT_TRUE(CheckPortRemap(99, 99));
79 ASSERT_TRUE(CheckPortRemap(100, 1000));
80 ASSERT_TRUE(CheckPortRemap(101, 1000));
81 ASSERT_TRUE(CheckPortRemap(200, 1000));
82 ASSERT_TRUE(CheckPortRemap(201, 201));
84 ASSERT_TRUE(sts
->UpdatePortRemapPreference("100-200,500=1000"_ns
));
85 ASSERT_TRUE(CheckPortRemap(10, 10));
86 ASSERT_TRUE(CheckPortRemap(99, 99));
87 ASSERT_TRUE(CheckPortRemap(100, 1000));
88 ASSERT_TRUE(CheckPortRemap(101, 1000));
89 ASSERT_TRUE(CheckPortRemap(200, 1000));
90 ASSERT_TRUE(CheckPortRemap(201, 201));
91 ASSERT_TRUE(CheckPortRemap(499, 499));
92 ASSERT_TRUE(CheckPortRemap(500, 1000));
93 ASSERT_TRUE(CheckPortRemap(501, 501));
95 ASSERT_TRUE(sts
->UpdatePortRemapPreference("1-3=10;5-8,12=20"_ns
));
96 ASSERT_TRUE(CheckPortRemap(1, 10));
97 ASSERT_TRUE(CheckPortRemap(2, 10));
98 ASSERT_TRUE(CheckPortRemap(3, 10));
99 ASSERT_TRUE(CheckPortRemap(4, 4));
100 ASSERT_TRUE(CheckPortRemap(5, 20));
101 ASSERT_TRUE(CheckPortRemap(8, 20));
102 ASSERT_TRUE(CheckPortRemap(11, 11));
103 ASSERT_TRUE(CheckPortRemap(12, 20));
105 ASSERT_TRUE(sts
->UpdatePortRemapPreference("80=8080;443=8080"_ns
));
106 ASSERT_TRUE(CheckPortRemap(80, 8080));
107 ASSERT_TRUE(CheckPortRemap(443, 8080));
109 // Later rules rewrite earlier rules
110 ASSERT_TRUE(sts
->UpdatePortRemapPreference("10=100;10=200"_ns
));
111 ASSERT_TRUE(CheckPortRemap(10, 200));
113 ASSERT_TRUE(sts
->UpdatePortRemapPreference("10-20=100;10-20=200"_ns
));
114 ASSERT_TRUE(CheckPortRemap(10, 200));
115 ASSERT_TRUE(CheckPortRemap(20, 200));
117 ASSERT_TRUE(sts
->UpdatePortRemapPreference("10-20=100;15=200"_ns
));
118 ASSERT_TRUE(CheckPortRemap(10, 100));
119 ASSERT_TRUE(CheckPortRemap(15, 200));
120 ASSERT_TRUE(CheckPortRemap(20, 100));
122 ASSERT_TRUE(sts
->UpdatePortRemapPreference(
123 " 100 - 200 = 1000 ; 150 = 2000 "_ns
));
124 ASSERT_TRUE(CheckPortRemap(100, 1000));
125 ASSERT_TRUE(CheckPortRemap(150, 2000));
126 ASSERT_TRUE(CheckPortRemap(200, 1000));
128 // Turn off any mapping
129 ASSERT_TRUE(sts
->UpdatePortRemapPreference(""_ns
));
130 for (uint32_t port
= 0; port
< 65536; ++port
) {
131 ASSERT_TRUE(CheckPortRemap((uint16_t)port
, (uint16_t)port
));
136 TEST(TestSocketTransportService
, StatusValues
)
138 static_assert(static_cast<nsresult
>(nsISocketTransport::STATUS_RESOLVING
) ==
139 NS_NET_STATUS_RESOLVING_HOST
);
140 static_assert(static_cast<nsresult
>(nsISocketTransport::STATUS_RESOLVED
) ==
141 NS_NET_STATUS_RESOLVED_HOST
);
143 static_cast<nsresult
>(nsISocketTransport::STATUS_CONNECTING_TO
) ==
144 NS_NET_STATUS_CONNECTING_TO
);
146 static_cast<nsresult
>(nsISocketTransport::STATUS_CONNECTED_TO
) ==
147 NS_NET_STATUS_CONNECTED_TO
);
148 static_assert(static_cast<nsresult
>(nsISocketTransport::STATUS_SENDING_TO
) ==
149 NS_NET_STATUS_SENDING_TO
);
150 static_assert(static_cast<nsresult
>(nsISocketTransport::STATUS_WAITING_FOR
) ==
151 NS_NET_STATUS_WAITING_FOR
);
153 static_cast<nsresult
>(nsISocketTransport::STATUS_RECEIVING_FROM
) ==
154 NS_NET_STATUS_RECEIVING_FROM
);
155 static_assert(static_cast<nsresult
>(
156 nsISocketTransport::STATUS_TLS_HANDSHAKE_STARTING
) ==
157 NS_NET_STATUS_TLS_HANDSHAKE_STARTING
);
159 static_cast<nsresult
>(nsISocketTransport::STATUS_TLS_HANDSHAKE_ENDED
) ==
160 NS_NET_STATUS_TLS_HANDSHAKE_ENDED
);
164 } // namespace mozilla