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

Side by Side Diff: chromeos/components/tether/message_wrapper_unittest.cc

Issue 2700873002: [CrOS Tether] Create MessageWrapper, a wrapper class for proto messages sent for tethering. This cl… (Closed)
Patch Set: Created 3 years, 10 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
OLDNEW
(Empty)
1 // Copyright 2017 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "chromeos/components/tether/message_wrapper.h"
6
7 #include <sstream>
8
9 #include "base/base64url.h"
10 #include "base/macros.h"
11 #include "chromeos/components/tether/proto/tether.pb.h"
12 #include "testing/gmock/include/gmock/gmock.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace chromeos {
16
17 namespace tether {
18
19 namespace {
20
21 DeviceStatus CreateFakeDeviceStatus() {
22 WifiStatus wifi_status;
23 wifi_status.set_status_code(
24 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED);
25 wifi_status.set_ssid("Google A");
26
27 DeviceStatus device_status;
28 device_status.set_battery_percentage(75);
29 device_status.set_cell_provider("Google Fi");
30 device_status.set_connection_strength(4);
31 device_status.mutable_wifi_status()->CopyFrom(wifi_status);
32
33 return device_status;
34 }
35
36 void VerifyProtoConversion(const google::protobuf::MessageLite* proto,
37 const MessageWrapper& wrapper,
38 const MessageType& expected_message_type) {
39 std::string raw_message = wrapper.ToRawMessage();
40 EXPECT_TRUE(raw_message.length() > 0);
41
42 std::unique_ptr<MessageWrapper> wrapper_from_raw_message =
43 MessageWrapper::FromRawMessage(raw_message);
44 EXPECT_TRUE(wrapper_from_raw_message);
45 EXPECT_EQ(expected_message_type, wrapper_from_raw_message->GetMessageType());
46 EXPECT_EQ(proto->SerializeAsString(),
47 wrapper_from_raw_message->GetProto()->SerializeAsString());
48 }
49
50 } // namespace
51
52 class MessageWrapperTest : public testing::Test {
53 protected:
54 MessageWrapperTest() {}
55
56 private:
57 DISALLOW_COPY_AND_ASSIGN(MessageWrapperTest);
58 };
59
60 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_ConnectTetheringRequest) {
61 ConnectTetheringRequest request;
62
63 MessageWrapper wrapper(request);
Ryan Hansberry 2017/02/17 02:29:07 Constructing a MessageWrapper is common to all of
Kyle Horimoto 2017/02/17 20:05:04 It's not a good idea to do that. MessageWrapper's
64 VerifyProtoConversion(&request, wrapper,
65 MessageType::CONNECT_TETHERING_REQUEST);
66 }
67
68 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_ConnectTetheringResponse) {
69 ConnectTetheringResponse response;
70 response.set_ssid("Instant Tethering 123456");
71 response.set_password("password");
72 response.set_response_code(ConnectTetheringResponse_ResponseCode::
73 ConnectTetheringResponse_ResponseCode_SUCCESS);
74 response.mutable_device_status()->CopyFrom(CreateFakeDeviceStatus());
75
76 MessageWrapper wrapper(response);
77 VerifyProtoConversion(&response, wrapper,
78 MessageType::CONNECT_TETHERING_RESPONSE);
79 }
80
81 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_DisconnectTetheringRequest) {
82 DisconnectTetheringRequest request;
83
84 MessageWrapper wrapper(request);
85 VerifyProtoConversion(&request, wrapper,
86 MessageType::DISCONNECT_TETHERING_REQUEST);
87 }
88
89 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_KeepAliveTickle) {
90 KeepAliveTickle tickle;
91
92 MessageWrapper wrapper(tickle);
93 VerifyProtoConversion(&tickle, wrapper, MessageType::KEEP_ALIVE_TICKLE);
94 }
95
96 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_TetherAvailabilityRequest) {
97 TetherAvailabilityRequest request;
98
99 MessageWrapper wrapper(request);
100 VerifyProtoConversion(&request, wrapper,
101 MessageType::TETHER_AVAILABILITY_REQUEST);
102 }
103
104 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_TetherAvailabilityResponse) {
105 TetherAvailabilityResponse response;
106 response.set_response_code(
107 TetherAvailabilityResponse_ResponseCode::
108 TetherAvailabilityResponse_ResponseCode_TETHER_AVAILABLE);
109 response.mutable_device_status()->CopyFrom(CreateFakeDeviceStatus());
110
111 MessageWrapper wrapper(response);
112 VerifyProtoConversion(&response, wrapper,
113 MessageType::TETHER_AVAILABILITY_RESPONSE);
114 }
115
116 TEST_F(MessageWrapperTest, TestHandleInvalidJson) {
117 EXPECT_FALSE(MessageWrapper::FromRawMessage("not JSON"));
118 }
119
120 TEST_F(MessageWrapperTest, TestHandleJsonWithoutType) {
121 ConnectTetheringRequest request;
122 std::string encoded_message;
123 base::Base64UrlEncode(request.SerializeAsString(),
124 base::Base64UrlEncodePolicy::INCLUDE_PADDING,
125 &encoded_message);
126
127 std::stringstream ss;
128 ss << "{data: \"" << encoded_message << "\"}";
Ryan Hansberry 2017/02/17 02:29:07 I think JSON keys are wrapped in quotes too -- ple
Kyle Horimoto 2017/02/17 20:05:04 Done.
129
130 EXPECT_FALSE(MessageWrapper::FromRawMessage(ss.str()));
131 }
132
133 TEST_F(MessageWrapperTest, TestHandleJsonWithoutData) {
134 EXPECT_FALSE(MessageWrapper::FromRawMessage("{type: 1}"));
135 }
136
137 TEST_F(MessageWrapperTest, TestHandleJsonWithoutTypeOrData) {
138 EXPECT_FALSE(MessageWrapper::FromRawMessage("{}"));
139 }
140
141 TEST_F(MessageWrapperTest, TestHandleDataNotEncodedWithBase64) {
142 ConnectTetheringRequest request;
143
144 std::stringstream ss;
145 ss << "{type: " << static_cast<int>(MessageType::CONNECT_TETHERING_REQUEST)
146 << ", data: \""
147 << request.SerializeAsString() // Do not convert to base-64.
148 << "\"}";
149
150 EXPECT_FALSE(MessageWrapper::FromRawMessage(ss.str()));
151 }
152
Ryan Hansberry 2017/02/17 02:29:07 It's a PITA, but do you see any value adding a san
Kyle Horimoto 2017/02/17 20:05:04 Done.
153 } // namespace tether
Ryan Hansberry 2017/02/17 02:29:07 Please add test that makes sure this works when th
Kyle Horimoto 2017/02/17 20:05:04 Done.
154
155 } // namespace cryptauth
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698