OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/extensions/api/cast_channel/cast_channel_api.h" | 5 #include "chrome/browser/extensions/api/cast_channel/cast_channel_api.h" |
6 | 6 |
7 #include "base/json/json_reader.h" | |
8 #include "base/logging.h" | |
9 #include "base/memory/ref_counted.h" | |
10 #include "base/memory/scoped_ptr.h" | 7 #include "base/memory/scoped_ptr.h" |
11 #include "base/values.h" | |
12 #include "net/base/ip_endpoint.h" | 8 #include "net/base/ip_endpoint.h" |
13 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
14 #include "url/gurl.h" | 10 #include "url/gurl.h" |
15 | 11 |
16 namespace { | |
17 | |
18 base::ListValue* parseList(const std::string& json) { | |
19 base::ListValue* result = NULL; | |
20 base::Value* parsed = base::JSONReader::Read(json); | |
21 CHECK(parsed); | |
22 CHECK(parsed->GetAsList(&result)); | |
23 CHECK(result); | |
24 return result; | |
25 } | |
26 | |
27 } // namespace | |
28 | |
29 namespace extensions { | 12 namespace extensions { |
| 13 namespace api { |
| 14 namespace cast_channel { |
30 | 15 |
31 // Tests URL parsing and validation. | 16 // Tests URL parsing and validation. |
32 TEST(CastChannelOpenFunctionTest, TestParseChannelUrl) { | 17 TEST(CastChannelOpenFunctionTest, TestParseChannelUrl) { |
33 typedef CastChannelOpenFunction ccof; | 18 typedef CastChannelOpenFunction ccof; |
34 cast_channel::ConnectInfo connect_info; | 19 ConnectInfo connect_info; |
35 | 20 |
36 EXPECT_TRUE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1:8009"), | 21 EXPECT_TRUE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1:8009"), |
37 &connect_info)); | 22 &connect_info)); |
38 EXPECT_EQ(connect_info.ip_address, "192.0.0.1"); | 23 EXPECT_EQ(connect_info.ip_address, "192.0.0.1"); |
39 EXPECT_EQ(connect_info.port, 8009); | 24 EXPECT_EQ(connect_info.port, 8009); |
40 EXPECT_EQ(connect_info.auth, cast_channel::CHANNEL_AUTH_TYPE_SSL); | 25 EXPECT_EQ(connect_info.auth, CHANNEL_AUTH_TYPE_SSL); |
41 | 26 |
42 EXPECT_TRUE(ccof::ParseChannelUrl(GURL("casts://192.0.0.1:12345"), | 27 EXPECT_TRUE(ccof::ParseChannelUrl(GURL("casts://192.0.0.1:12345"), |
43 &connect_info)); | 28 &connect_info)); |
44 EXPECT_EQ(connect_info.ip_address, "192.0.0.1"); | 29 EXPECT_EQ(connect_info.ip_address, "192.0.0.1"); |
45 EXPECT_EQ(connect_info.port, 12345); | 30 EXPECT_EQ(connect_info.port, 12345); |
46 EXPECT_EQ(connect_info.auth, cast_channel::CHANNEL_AUTH_TYPE_SSL_VERIFIED); | 31 EXPECT_EQ(connect_info.auth, CHANNEL_AUTH_TYPE_SSL_VERIFIED); |
47 | 32 |
48 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("http://192.0.0.1:12345"), | 33 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("http://192.0.0.1:12345"), |
49 &connect_info)); | 34 &connect_info)); |
50 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast:192.0.0.1:12345"), | 35 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast:192.0.0.1:12345"), |
51 &connect_info)); | 36 &connect_info)); |
52 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://:12345"), &connect_info)); | 37 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://:12345"), &connect_info)); |
53 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1:abcd"), | 38 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1:abcd"), |
54 &connect_info)); | 39 &connect_info)); |
55 EXPECT_FALSE(ccof::ParseChannelUrl(GURL(""), &connect_info)); | 40 EXPECT_FALSE(ccof::ParseChannelUrl(GURL(""), &connect_info)); |
56 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("foo"), &connect_info)); | 41 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("foo"), &connect_info)); |
57 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast:"), &connect_info)); | 42 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast:"), &connect_info)); |
58 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast::"), &connect_info)); | 43 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast::"), &connect_info)); |
59 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1"), &connect_info)); | 44 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1"), &connect_info)); |
60 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://:"), &connect_info)); | 45 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://:"), &connect_info)); |
61 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1:"), &connect_info)); | 46 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1:"), &connect_info)); |
62 } | 47 } |
63 | 48 |
64 // Tests validation of ConnectInfo. | 49 // Tests validation of ConnectInfo. |
65 TEST(CastChannelOpenFunctionTest, TestParseConnectInfo) { | 50 TEST(CastChannelOpenFunctionTest, TestParseConnectInfo) { |
66 typedef CastChannelOpenFunction ccof; | 51 typedef CastChannelOpenFunction ccof; |
67 scoped_ptr<net::IPEndPoint> ip_endpoint; | 52 scoped_ptr<net::IPEndPoint> ip_endpoint; |
68 | 53 |
69 // Valid ConnectInfo | 54 // Valid ConnectInfo |
70 cast_channel::ConnectInfo connect_info; | 55 ConnectInfo connect_info; |
71 connect_info.ip_address = "192.0.0.1"; | 56 connect_info.ip_address = "192.0.0.1"; |
72 connect_info.port = 8009; | 57 connect_info.port = 8009; |
73 connect_info.auth = cast_channel::CHANNEL_AUTH_TYPE_SSL; | 58 connect_info.auth = CHANNEL_AUTH_TYPE_SSL; |
74 | 59 |
75 ip_endpoint.reset(ccof::ParseConnectInfo(connect_info)); | 60 ip_endpoint.reset(ccof::ParseConnectInfo(connect_info)); |
76 EXPECT_TRUE(ip_endpoint.get() != NULL); | 61 EXPECT_TRUE(ip_endpoint.get() != NULL); |
77 EXPECT_EQ(ip_endpoint->ToString(), "192.0.0.1:8009"); | 62 EXPECT_EQ(ip_endpoint->ToString(), "192.0.0.1:8009"); |
78 | 63 |
79 // Invalid IP | 64 // Invalid IP |
80 cast_channel::ConnectInfo invalid_ip_connect_info; | 65 ConnectInfo invalid_ip_connect_info; |
81 invalid_ip_connect_info.ip_address = "blargh"; | 66 invalid_ip_connect_info.ip_address = "blargh"; |
82 invalid_ip_connect_info.port = 8009; | 67 invalid_ip_connect_info.port = 8009; |
83 invalid_ip_connect_info.auth = cast_channel::CHANNEL_AUTH_TYPE_SSL; | 68 invalid_ip_connect_info.auth = CHANNEL_AUTH_TYPE_SSL; |
84 ip_endpoint.reset(ccof::ParseConnectInfo(invalid_ip_connect_info)); | 69 ip_endpoint.reset(ccof::ParseConnectInfo(invalid_ip_connect_info)); |
85 EXPECT_TRUE(ip_endpoint.get() == NULL); | 70 EXPECT_TRUE(ip_endpoint.get() == NULL); |
86 | 71 |
87 // Invalid port | 72 // Invalid port |
88 cast_channel::ConnectInfo invalid_port_connect_info; | 73 ConnectInfo invalid_port_connect_info; |
89 invalid_port_connect_info.ip_address = "192.0.0.1"; | 74 invalid_port_connect_info.ip_address = "192.0.0.1"; |
90 invalid_port_connect_info.port = -1; | 75 invalid_port_connect_info.port = -1; |
91 invalid_port_connect_info.auth = cast_channel::CHANNEL_AUTH_TYPE_SSL; | 76 invalid_port_connect_info.auth = CHANNEL_AUTH_TYPE_SSL; |
92 ip_endpoint.reset(ccof::ParseConnectInfo(invalid_port_connect_info)); | 77 ip_endpoint.reset(ccof::ParseConnectInfo(invalid_port_connect_info)); |
93 EXPECT_TRUE(ip_endpoint.get() == NULL); | 78 EXPECT_TRUE(ip_endpoint.get() == NULL); |
94 | 79 |
95 // Invalid auth | 80 // Invalid auth |
96 cast_channel::ConnectInfo invalid_auth_connect_info; | 81 ConnectInfo invalid_auth_connect_info; |
97 invalid_auth_connect_info.ip_address = "192.0.0.1"; | 82 invalid_auth_connect_info.ip_address = "192.0.0.1"; |
98 invalid_auth_connect_info.port = 8009; | 83 invalid_auth_connect_info.port = 8009; |
99 invalid_auth_connect_info.auth = cast_channel::CHANNEL_AUTH_TYPE_NONE; | 84 invalid_auth_connect_info.auth = CHANNEL_AUTH_TYPE_NONE; |
100 ip_endpoint.reset(ccof::ParseConnectInfo(invalid_auth_connect_info)); | 85 ip_endpoint.reset(ccof::ParseConnectInfo(invalid_auth_connect_info)); |
101 EXPECT_TRUE(ip_endpoint.get() == NULL); | 86 EXPECT_TRUE(ip_endpoint.get() == NULL); |
102 } | 87 } |
103 | 88 |
104 TEST(CastChannelOpenFunctionTest, TestPrepareValidUrl) { | 89 } // namespace cast_channel |
105 scoped_refptr<CastChannelOpenFunction> ccof(new CastChannelOpenFunction); | 90 } // namespace api |
106 scoped_ptr<base::ListValue> params_json( | |
107 parseList("[\"casts://127.0.0.1:8009\"]")); | |
108 | |
109 ccof->SetArgs(params_json.get()); | |
110 EXPECT_TRUE(ccof->Prepare()); | |
111 EXPECT_TRUE(ccof->GetError().empty()); | |
112 } | |
113 | |
114 TEST(CastChannelOpenFunctionTest, TestPrepareInvalidUrl) { | |
115 scoped_refptr<CastChannelOpenFunction> ccof(new CastChannelOpenFunction); | |
116 scoped_ptr<base::ListValue> params_json(parseList("[\"blargh\"]")); | |
117 | |
118 ccof->SetArgs(params_json.get()); | |
119 EXPECT_FALSE(ccof->Prepare()); | |
120 EXPECT_EQ(ccof->GetError(), "Invalid Cast URL blargh"); | |
121 } | |
122 | |
123 TEST(CastChannelOpenFunctionTest, TestPrepareValidConnectInfo) { | |
124 scoped_refptr<CastChannelOpenFunction> ccof(new CastChannelOpenFunction); | |
125 scoped_ptr<base::ListValue> params_json( | |
126 parseList("[{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " | |
127 "\"auth\": \"ssl\"}]")); | |
128 | |
129 ccof->SetArgs(params_json.get()); | |
130 EXPECT_TRUE(ccof->Prepare()); | |
131 EXPECT_TRUE(ccof->GetError().empty()); | |
132 } | |
133 | |
134 TEST(CastChannelOpenFunctionTest, TestPrepareInvalidConnectInfo) { | |
135 scoped_refptr<CastChannelOpenFunction> ccof(new CastChannelOpenFunction); | |
136 scoped_ptr<base::ListValue> params_json(parseList("[12345]")); | |
137 | |
138 ccof->SetArgs(params_json.get()); | |
139 EXPECT_FALSE(ccof->Prepare()); | |
140 EXPECT_EQ(ccof->GetError(), "Invalid connect_info"); | |
141 } | |
142 | |
143 TEST(CastChannelSendFunctionTest, TestPrepareValidMessageInfo) { | |
144 scoped_refptr<CastChannelSendFunction> ccsf(new CastChannelSendFunction); | |
145 scoped_ptr<base::ListValue> params_json( | |
146 parseList("[{\"channelId\": 1, \"url\": \"cast://127.0.0.1:8009\", " | |
147 "\"connectInfo\": " | |
148 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " | |
149 "\"auth\": \"ssl\"}, \"readyState\": \"open\"}, " | |
150 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", " | |
151 "\"destinationId\": \"bar\", \"data\": \"string\"}]")); | |
152 | |
153 ccsf->SetArgs(params_json.get()); | |
154 EXPECT_TRUE(ccsf->Prepare()); | |
155 EXPECT_TRUE(ccsf->GetError().empty()); | |
156 } | |
157 | |
158 TEST(CastChannelSendFunctionTest, TestPrepareInvalidMessageInfo) { | |
159 scoped_refptr<CastChannelSendFunction> ccsf(new CastChannelSendFunction); | |
160 scoped_ptr<base::ListValue> params_json( | |
161 parseList("[{\"channelId\": 1, \"url\": \"cast://127.0.0.1:8009\", " | |
162 "\"connectInfo\": " | |
163 "{\"ipAddress\": \"127.0.0.1\", \"port\": 8009, " | |
164 "\"auth\": \"ssl\"}, \"readyState\": \"open\"}, " | |
165 "{\"namespace_\": \"foo\", \"sourceId\": \"src\", " | |
166 "\"destinationId\": \"bar\", \"data\": 1235}]")); | |
167 | |
168 ccsf->SetArgs(params_json.get()); | |
169 EXPECT_FALSE(ccsf->Prepare()); | |
170 EXPECT_EQ(ccsf->GetError(), "Invalid type of message_info.data"); | |
171 } | |
172 | |
173 } // namespace extensions | 91 } // namespace extensions |
OLD | NEW |