• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2020 The Chromium Embedded Framework Authors. All rights
2 // reserved. Use of this source code is governed by a BSD-style license that
3 // can be found in the LICENSE file.
4 
5 #include "libcef/browser/devtools/devtools_util.h"
6 
7 #include "testing/gtest/include/gtest/gtest.h"
8 
9 using namespace devtools_util;
10 
TEST(DevToolsUtil,ProtocolParser_IsValidMessage)11 TEST(DevToolsUtil, ProtocolParser_IsValidMessage) {
12   // Empty dictionary is not valid.
13   EXPECT_FALSE(ProtocolParser::IsValidMessage(""));
14   EXPECT_FALSE(ProtocolParser::IsValidMessage("{}"));
15 
16   // Incorrectly formatted dictionary is not valid.
17   EXPECT_FALSE(ProtocolParser::IsValidMessage("{ ]"));
18 
19   // Everything else is valid (we don't verify JSON structure).
20   EXPECT_TRUE(ProtocolParser::IsValidMessage("{ }"));
21   EXPECT_TRUE(ProtocolParser::IsValidMessage("{blah blah}"));
22   EXPECT_TRUE(ProtocolParser::IsValidMessage("{method:\"foobar\"}"));
23 }
24 
TEST(DevToolsUtil,ProtocolParser_Initialize_IsFailure_Unknown)25 TEST(DevToolsUtil, ProtocolParser_Initialize_IsFailure_Unknown) {
26   ProtocolParser parser;
27   EXPECT_FALSE(parser.IsInitialized());
28 
29   // Empty message is invalid.
30   EXPECT_TRUE(parser.Initialize(""));
31   EXPECT_TRUE(parser.IsInitialized());
32   EXPECT_TRUE(parser.IsFailure());
33 
34   parser.Reset();
35   EXPECT_FALSE(parser.IsInitialized());
36 
37   // Empty dictionary is invalid.
38   EXPECT_TRUE(parser.Initialize("{}"));
39   EXPECT_TRUE(parser.IsInitialized());
40   EXPECT_TRUE(parser.IsFailure());
41 
42   parser.Reset();
43   EXPECT_FALSE(parser.IsInitialized());
44 
45   // Unrecognized dictionary type is invalid.
46   EXPECT_TRUE(parser.Initialize("{blah blah}"));
47   EXPECT_TRUE(parser.IsInitialized());
48   EXPECT_TRUE(parser.IsFailure());
49 }
50 
TEST(DevToolsUtil,ProtocolParser_Initialize_IsFailure_EventMalformed)51 TEST(DevToolsUtil, ProtocolParser_Initialize_IsFailure_EventMalformed) {
52   ProtocolParser parser;
53   EXPECT_FALSE(parser.IsInitialized());
54 
55   // Empty method is invalid.
56   EXPECT_TRUE(parser.Initialize("{\"method\":\"\"}"));
57   EXPECT_TRUE(parser.IsInitialized());
58   EXPECT_TRUE(parser.IsFailure());
59 
60   parser.Reset();
61   EXPECT_FALSE(parser.IsInitialized());
62 
63   // Unrecognized value is invalid.
64   EXPECT_TRUE(parser.Initialize("{\"method\":\"foo\",oops:false}"));
65   EXPECT_TRUE(parser.IsInitialized());
66   EXPECT_TRUE(parser.IsFailure());
67 
68   parser.Reset();
69   EXPECT_FALSE(parser.IsInitialized());
70 
71   // Params must be a dictionary.
72   EXPECT_TRUE(parser.Initialize("{\"method\":\",params:[]}"));
73   EXPECT_TRUE(parser.IsInitialized());
74   EXPECT_TRUE(parser.IsFailure());
75 }
76 
TEST(DevToolsUtil,ProtocolParser_Initialize_IsEvent)77 TEST(DevToolsUtil, ProtocolParser_Initialize_IsEvent) {
78   ProtocolParser parser;
79   EXPECT_FALSE(parser.IsInitialized());
80 
81   // Method without params is valid.
82   std::string message = "{\"method\":\"Test.myMethod\"}";
83   EXPECT_TRUE(parser.Initialize(message));
84   EXPECT_TRUE(parser.IsInitialized());
85   EXPECT_TRUE(parser.IsEvent());
86   EXPECT_STREQ("Test.myMethod", parser.method_.data());
87   EXPECT_TRUE(parser.params_.empty());
88 
89   parser.Reset();
90   EXPECT_FALSE(parser.IsInitialized());
91 
92   // Method with empty params dictionary is valid.
93   message = "{\"method\":\"Test.myMethod2\",\"params\":{}}";
94   EXPECT_TRUE(parser.Initialize(message));
95   EXPECT_TRUE(parser.IsInitialized());
96   EXPECT_TRUE(parser.IsEvent());
97   EXPECT_STREQ("Test.myMethod2", parser.method_.data());
98   EXPECT_STREQ("{}", parser.params_.data());
99 
100   parser.Reset();
101   EXPECT_FALSE(parser.IsInitialized());
102 
103   // Method with non-empty params dictionary is valid.
104   message = "{\"method\":\"Test.myMethod3\",\"params\":{\"foo\":\"bar\"}}";
105   EXPECT_TRUE(parser.Initialize(message));
106   EXPECT_TRUE(parser.IsInitialized());
107   EXPECT_TRUE(parser.IsEvent());
108   EXPECT_STREQ("Test.myMethod3", parser.method_.data());
109   EXPECT_STREQ("{\"foo\":\"bar\"}", parser.params_.data());
110 }
111 
TEST(DevToolsUtil,ProtocolParser_Initialize_IsFailure_ResultMalformed)112 TEST(DevToolsUtil, ProtocolParser_Initialize_IsFailure_ResultMalformed) {
113   ProtocolParser parser;
114   EXPECT_FALSE(parser.IsInitialized());
115 
116   // Empty ID is invalid.
117   EXPECT_TRUE(parser.Initialize("{\"id\":,result:{}}"));
118   EXPECT_TRUE(parser.IsInitialized());
119   EXPECT_TRUE(parser.IsFailure());
120 
121   parser.Reset();
122   EXPECT_FALSE(parser.IsInitialized());
123 
124   // Missing result or error value is invalid.
125   EXPECT_TRUE(parser.Initialize("{\"id\":1}"));
126   EXPECT_TRUE(parser.IsInitialized());
127   EXPECT_TRUE(parser.IsFailure());
128 
129   parser.Reset();
130   EXPECT_FALSE(parser.IsInitialized());
131 
132   // Unrecognized value is invalid.
133   EXPECT_TRUE(parser.Initialize("{\"id\":1,oops:false}"));
134   EXPECT_TRUE(parser.IsInitialized());
135   EXPECT_TRUE(parser.IsFailure());
136 
137   parser.Reset();
138   EXPECT_FALSE(parser.IsInitialized());
139 
140   // Result must be a dictionary.
141   EXPECT_TRUE(parser.Initialize("{\"id\":1,\"result\":[]}"));
142   EXPECT_TRUE(parser.IsInitialized());
143   EXPECT_TRUE(parser.IsFailure());
144 
145   parser.Reset();
146   EXPECT_FALSE(parser.IsInitialized());
147 
148   // Error must be a dictionary.
149   EXPECT_TRUE(parser.Initialize("{\"id\":1,\"error\":[]}"));
150   EXPECT_TRUE(parser.IsInitialized());
151   EXPECT_TRUE(parser.IsFailure());
152 }
153 
TEST(DevToolsUtil,ProtocolParser_Initialize_IsResult_Result)154 TEST(DevToolsUtil, ProtocolParser_Initialize_IsResult_Result) {
155   ProtocolParser parser;
156   EXPECT_FALSE(parser.IsInitialized());
157 
158   // Id with empty result dictionary is valid.
159   std::string message = "{\"id\":1,\"result\":{}}";
160   EXPECT_TRUE(parser.Initialize(message));
161   EXPECT_TRUE(parser.IsInitialized());
162   EXPECT_TRUE(parser.IsResult());
163   EXPECT_EQ(1, parser.message_id_);
164   EXPECT_TRUE(parser.success_);
165   EXPECT_STREQ("{}", parser.params_.data());
166 
167   parser.Reset();
168   EXPECT_FALSE(parser.IsInitialized());
169 
170   // Id with non-empty result dictionary is valid.
171   message = "{\"id\":2,\"result\":{\"foo\":\"bar\"}}";
172   EXPECT_TRUE(parser.Initialize(message));
173   EXPECT_TRUE(parser.IsInitialized());
174   EXPECT_TRUE(parser.IsResult());
175   EXPECT_EQ(2, parser.message_id_);
176   EXPECT_TRUE(parser.success_);
177   EXPECT_STREQ("{\"foo\":\"bar\"}", parser.params_.data());
178 }
179 
TEST(DevToolsUtil,ProtocolParser_Initialize_IsResult_Error)180 TEST(DevToolsUtil, ProtocolParser_Initialize_IsResult_Error) {
181   ProtocolParser parser;
182   EXPECT_FALSE(parser.IsInitialized());
183 
184   // Id with empty error dictionary is valid.
185   std::string message = "{\"id\":1,\"error\":{}}";
186   EXPECT_TRUE(parser.Initialize(message));
187   EXPECT_TRUE(parser.IsInitialized());
188   EXPECT_TRUE(parser.IsResult());
189   EXPECT_EQ(1, parser.message_id_);
190   EXPECT_FALSE(parser.success_);
191   EXPECT_STREQ("{}", parser.params_.data());
192 
193   parser.Reset();
194   EXPECT_FALSE(parser.IsInitialized());
195 
196   // Id with non-empty error dictionary is valid.
197   message = "{\"id\":2,\"error\":{\"foo\":\"bar\"}}";
198   EXPECT_TRUE(parser.Initialize(message));
199   EXPECT_TRUE(parser.IsInitialized());
200   EXPECT_TRUE(parser.IsResult());
201   EXPECT_EQ(2, parser.message_id_);
202   EXPECT_FALSE(parser.success_);
203   EXPECT_STREQ("{\"foo\":\"bar\"}", parser.params_.data());
204 }
205