Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(26)

Side by Side Diff: chrome/browser/extensions/api/cast_channel/cast_channel_api_unittest.cc

Issue 255443002: Implement argument validation for chrome.cast.channel.{open,send} (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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"
7 #include "base/memory/scoped_ptr.h" 10 #include "base/memory/scoped_ptr.h"
11 #include "base/values.h"
8 #include "net/base/ip_endpoint.h" 12 #include "net/base/ip_endpoint.h"
9 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
10 #include "url/gurl.h" 14 #include "url/gurl.h"
11 15
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
12 namespace extensions { 29 namespace extensions {
13 namespace api {
14 namespace cast_channel {
15 30
16 // Tests URL parsing and validation. 31 // Tests URL parsing and validation.
17 TEST(CastChannelOpenFunctionTest, TestParseChannelUrl) { 32 TEST(CastChannelOpenFunctionTest, TestParseChannelUrl) {
18 typedef CastChannelOpenFunction ccof; 33 typedef CastChannelOpenFunction ccof;
19 ConnectInfo connect_info; 34 cast_channel::ConnectInfo connect_info;
20 35
21 EXPECT_TRUE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1:8009"), 36 EXPECT_TRUE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1:8009"),
22 &connect_info)); 37 &connect_info));
23 EXPECT_EQ(connect_info.ip_address, "192.0.0.1"); 38 EXPECT_EQ(connect_info.ip_address, "192.0.0.1");
24 EXPECT_EQ(connect_info.port, 8009); 39 EXPECT_EQ(connect_info.port, 8009);
25 EXPECT_EQ(connect_info.auth, CHANNEL_AUTH_TYPE_SSL); 40 EXPECT_EQ(connect_info.auth, cast_channel::CHANNEL_AUTH_TYPE_SSL);
26 41
27 EXPECT_TRUE(ccof::ParseChannelUrl(GURL("casts://192.0.0.1:12345"), 42 EXPECT_TRUE(ccof::ParseChannelUrl(GURL("casts://192.0.0.1:12345"),
28 &connect_info)); 43 &connect_info));
29 EXPECT_EQ(connect_info.ip_address, "192.0.0.1"); 44 EXPECT_EQ(connect_info.ip_address, "192.0.0.1");
30 EXPECT_EQ(connect_info.port, 12345); 45 EXPECT_EQ(connect_info.port, 12345);
31 EXPECT_EQ(connect_info.auth, CHANNEL_AUTH_TYPE_SSL_VERIFIED); 46 EXPECT_EQ(connect_info.auth, cast_channel::CHANNEL_AUTH_TYPE_SSL_VERIFIED);
32 47
33 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("http://192.0.0.1:12345"), 48 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("http://192.0.0.1:12345"),
34 &connect_info)); 49 &connect_info));
35 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast:192.0.0.1:12345"), 50 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast:192.0.0.1:12345"),
36 &connect_info)); 51 &connect_info));
37 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://:12345"), &connect_info)); 52 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://:12345"), &connect_info));
38 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1:abcd"), 53 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1:abcd"),
39 &connect_info)); 54 &connect_info));
40 EXPECT_FALSE(ccof::ParseChannelUrl(GURL(""), &connect_info)); 55 EXPECT_FALSE(ccof::ParseChannelUrl(GURL(""), &connect_info));
41 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("foo"), &connect_info)); 56 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("foo"), &connect_info));
42 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast:"), &connect_info)); 57 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast:"), &connect_info));
43 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast::"), &connect_info)); 58 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast::"), &connect_info));
44 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1"), &connect_info)); 59 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1"), &connect_info));
45 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://:"), &connect_info)); 60 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://:"), &connect_info));
46 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1:"), &connect_info)); 61 EXPECT_FALSE(ccof::ParseChannelUrl(GURL("cast://192.0.0.1:"), &connect_info));
47 } 62 }
48 63
49 // Tests validation of ConnectInfo. 64 // Tests validation of ConnectInfo.
50 TEST(CastChannelOpenFunctionTest, TestParseConnectInfo) { 65 TEST(CastChannelOpenFunctionTest, TestParseConnectInfo) {
51 typedef CastChannelOpenFunction ccof; 66 typedef CastChannelOpenFunction ccof;
52 scoped_ptr<net::IPEndPoint> ip_endpoint; 67 scoped_ptr<net::IPEndPoint> ip_endpoint;
53 68
54 // Valid ConnectInfo 69 // Valid ConnectInfo
55 ConnectInfo connect_info; 70 cast_channel::ConnectInfo connect_info;
56 connect_info.ip_address = "192.0.0.1"; 71 connect_info.ip_address = "192.0.0.1";
57 connect_info.port = 8009; 72 connect_info.port = 8009;
58 connect_info.auth = CHANNEL_AUTH_TYPE_SSL; 73 connect_info.auth = cast_channel::CHANNEL_AUTH_TYPE_SSL;
59 74
60 ip_endpoint.reset(ccof::ParseConnectInfo(connect_info)); 75 ip_endpoint.reset(ccof::ParseConnectInfo(connect_info));
61 EXPECT_TRUE(ip_endpoint.get() != NULL); 76 EXPECT_TRUE(ip_endpoint.get() != NULL);
62 EXPECT_EQ(ip_endpoint->ToString(), "192.0.0.1:8009"); 77 EXPECT_EQ(ip_endpoint->ToString(), "192.0.0.1:8009");
63 78
64 // Invalid IP 79 // Invalid IP
65 ConnectInfo invalid_ip_connect_info; 80 cast_channel::ConnectInfo invalid_ip_connect_info;
66 invalid_ip_connect_info.ip_address = "blargh"; 81 invalid_ip_connect_info.ip_address = "blargh";
67 invalid_ip_connect_info.port = 8009; 82 invalid_ip_connect_info.port = 8009;
68 invalid_ip_connect_info.auth = CHANNEL_AUTH_TYPE_SSL; 83 invalid_ip_connect_info.auth = cast_channel::CHANNEL_AUTH_TYPE_SSL;
69 ip_endpoint.reset(ccof::ParseConnectInfo(invalid_ip_connect_info)); 84 ip_endpoint.reset(ccof::ParseConnectInfo(invalid_ip_connect_info));
70 EXPECT_TRUE(ip_endpoint.get() == NULL); 85 EXPECT_TRUE(ip_endpoint.get() == NULL);
71 86
72 // Invalid port 87 // Invalid port
73 ConnectInfo invalid_port_connect_info; 88 cast_channel::ConnectInfo invalid_port_connect_info;
74 invalid_port_connect_info.ip_address = "192.0.0.1"; 89 invalid_port_connect_info.ip_address = "192.0.0.1";
75 invalid_port_connect_info.port = -1; 90 invalid_port_connect_info.port = -1;
76 invalid_port_connect_info.auth = CHANNEL_AUTH_TYPE_SSL; 91 invalid_port_connect_info.auth = cast_channel::CHANNEL_AUTH_TYPE_SSL;
77 ip_endpoint.reset(ccof::ParseConnectInfo(invalid_port_connect_info)); 92 ip_endpoint.reset(ccof::ParseConnectInfo(invalid_port_connect_info));
78 EXPECT_TRUE(ip_endpoint.get() == NULL); 93 EXPECT_TRUE(ip_endpoint.get() == NULL);
79 94
80 // Invalid auth 95 // Invalid auth
81 ConnectInfo invalid_auth_connect_info; 96 cast_channel::ConnectInfo invalid_auth_connect_info;
82 invalid_auth_connect_info.ip_address = "192.0.0.1"; 97 invalid_auth_connect_info.ip_address = "192.0.0.1";
83 invalid_auth_connect_info.port = 8009; 98 invalid_auth_connect_info.port = 8009;
84 invalid_auth_connect_info.auth = CHANNEL_AUTH_TYPE_NONE; 99 invalid_auth_connect_info.auth = cast_channel::CHANNEL_AUTH_TYPE_NONE;
85 ip_endpoint.reset(ccof::ParseConnectInfo(invalid_auth_connect_info)); 100 ip_endpoint.reset(ccof::ParseConnectInfo(invalid_auth_connect_info));
86 EXPECT_TRUE(ip_endpoint.get() == NULL); 101 EXPECT_TRUE(ip_endpoint.get() == NULL);
87 } 102 }
88 103
89 } // namespace cast_channel 104 TEST(CastChannelOpenFunctionTest, TestPrepareValidUrl) {
90 } // namespace api 105 scoped_refptr<CastChannelOpenFunction> ccof(new CastChannelOpenFunction);
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
91 } // namespace extensions 173 } // namespace extensions
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698