OLD | NEW |
(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/logging.h" |
| 11 #include "base/macros.h" |
| 12 #include "chromeos/components/tether/proto/tether.pb.h" |
| 13 #include "testing/gmock/include/gmock/gmock.h" |
| 14 #include "testing/gtest/include/gtest/gtest.h" |
| 15 |
| 16 namespace chromeos { |
| 17 |
| 18 namespace tether { |
| 19 |
| 20 namespace { |
| 21 |
| 22 DeviceStatus CreateFakeDeviceStatus() { |
| 23 WifiStatus wifi_status; |
| 24 wifi_status.set_status_code( |
| 25 WifiStatus_StatusCode::WifiStatus_StatusCode_CONNECTED); |
| 26 wifi_status.set_ssid("Google A"); |
| 27 |
| 28 DeviceStatus device_status; |
| 29 device_status.set_battery_percentage(75); |
| 30 device_status.set_cell_provider("Google Fi"); |
| 31 device_status.set_connection_strength(4); |
| 32 device_status.mutable_wifi_status()->CopyFrom(wifi_status); |
| 33 |
| 34 return device_status; |
| 35 } |
| 36 |
| 37 TetherAvailabilityResponse CreateTetherAvailabilityResponse() { |
| 38 TetherAvailabilityResponse response; |
| 39 response.set_response_code( |
| 40 TetherAvailabilityResponse_ResponseCode:: |
| 41 TetherAvailabilityResponse_ResponseCode_TETHER_AVAILABLE); |
| 42 response.mutable_device_status()->CopyFrom(CreateFakeDeviceStatus()); |
| 43 return response; |
| 44 } |
| 45 |
| 46 void VerifyProtoConversion(const google::protobuf::MessageLite* proto, |
| 47 const MessageWrapper& wrapper, |
| 48 const MessageType& expected_message_type) { |
| 49 std::string raw_message = wrapper.ToRawMessage(); |
| 50 EXPECT_TRUE(raw_message.length() > 0); |
| 51 |
| 52 std::unique_ptr<MessageWrapper> wrapper_from_raw_message = |
| 53 MessageWrapper::FromRawMessage(raw_message); |
| 54 EXPECT_TRUE(wrapper_from_raw_message); |
| 55 EXPECT_EQ(expected_message_type, wrapper_from_raw_message->GetMessageType()); |
| 56 EXPECT_EQ(proto->SerializeAsString(), |
| 57 wrapper_from_raw_message->GetProto()->SerializeAsString()); |
| 58 } |
| 59 |
| 60 } // namespace |
| 61 |
| 62 class MessageWrapperTest : public testing::Test { |
| 63 protected: |
| 64 MessageWrapperTest() {} |
| 65 |
| 66 private: |
| 67 DISALLOW_COPY_AND_ASSIGN(MessageWrapperTest); |
| 68 }; |
| 69 |
| 70 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_ConnectTetheringRequest) { |
| 71 ConnectTetheringRequest request; |
| 72 |
| 73 MessageWrapper wrapper(request); |
| 74 VerifyProtoConversion(&request, wrapper, |
| 75 MessageType::CONNECT_TETHERING_REQUEST); |
| 76 } |
| 77 |
| 78 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_ConnectTetheringResponse) { |
| 79 ConnectTetheringResponse response; |
| 80 response.set_ssid("Instant Tethering 123456"); |
| 81 response.set_password("password"); |
| 82 response.set_response_code(ConnectTetheringResponse_ResponseCode:: |
| 83 ConnectTetheringResponse_ResponseCode_SUCCESS); |
| 84 response.mutable_device_status()->CopyFrom(CreateFakeDeviceStatus()); |
| 85 |
| 86 MessageWrapper wrapper(response); |
| 87 VerifyProtoConversion(&response, wrapper, |
| 88 MessageType::CONNECT_TETHERING_RESPONSE); |
| 89 } |
| 90 |
| 91 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_DisconnectTetheringRequest) { |
| 92 DisconnectTetheringRequest request; |
| 93 |
| 94 MessageWrapper wrapper(request); |
| 95 VerifyProtoConversion(&request, wrapper, |
| 96 MessageType::DISCONNECT_TETHERING_REQUEST); |
| 97 } |
| 98 |
| 99 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_KeepAliveTickle) { |
| 100 KeepAliveTickle tickle; |
| 101 |
| 102 MessageWrapper wrapper(tickle); |
| 103 VerifyProtoConversion(&tickle, wrapper, MessageType::KEEP_ALIVE_TICKLE); |
| 104 } |
| 105 |
| 106 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_TetherAvailabilityRequest) { |
| 107 TetherAvailabilityRequest request; |
| 108 |
| 109 MessageWrapper wrapper(request); |
| 110 VerifyProtoConversion(&request, wrapper, |
| 111 MessageType::TETHER_AVAILABILITY_REQUEST); |
| 112 } |
| 113 |
| 114 TEST_F(MessageWrapperTest, TestToAndFromRawMessage_TetherAvailabilityResponse) { |
| 115 TetherAvailabilityResponse response = CreateTetherAvailabilityResponse(); |
| 116 |
| 117 MessageWrapper wrapper(response); |
| 118 VerifyProtoConversion(&response, wrapper, |
| 119 MessageType::TETHER_AVAILABILITY_RESPONSE); |
| 120 } |
| 121 |
| 122 TEST_F(MessageWrapperTest, TestHandleInvalidJson) { |
| 123 EXPECT_FALSE(MessageWrapper::FromRawMessage("not JSON")); |
| 124 } |
| 125 |
| 126 TEST_F(MessageWrapperTest, TestHandleJsonWithoutType) { |
| 127 ConnectTetheringRequest request; |
| 128 std::string encoded_message; |
| 129 base::Base64UrlEncode(request.SerializeAsString(), |
| 130 base::Base64UrlEncodePolicy::INCLUDE_PADDING, |
| 131 &encoded_message); |
| 132 |
| 133 std::stringstream ss; |
| 134 ss << "{\"data\": \"" << encoded_message << "\"}"; |
| 135 |
| 136 EXPECT_FALSE(MessageWrapper::FromRawMessage(ss.str())); |
| 137 } |
| 138 |
| 139 TEST_F(MessageWrapperTest, TestHandleJsonWithoutData) { |
| 140 EXPECT_FALSE(MessageWrapper::FromRawMessage("{\"type\":1}")); |
| 141 } |
| 142 |
| 143 TEST_F(MessageWrapperTest, TestHandleJsonWithoutTypeOrData) { |
| 144 EXPECT_FALSE(MessageWrapper::FromRawMessage("{}")); |
| 145 } |
| 146 |
| 147 TEST_F(MessageWrapperTest, TestHandleDataNotEncodedWithBase64) { |
| 148 TetherAvailabilityResponse response = CreateTetherAvailabilityResponse(); |
| 149 |
| 150 std::stringstream ss; |
| 151 ss << "{\"type\":" << static_cast<int>(MessageType::CONNECT_TETHERING_REQUEST) |
| 152 << ",\"data\":\"" |
| 153 << response.SerializeAsString() // Do not convert to base-64. |
| 154 << "\"}"; |
| 155 |
| 156 EXPECT_FALSE(MessageWrapper::FromRawMessage(ss.str())); |
| 157 } |
| 158 |
| 159 TEST_F(MessageWrapperTest, TestFromRawMessage_StringLiteral) { |
| 160 // Type 2 is TETHER_AVAILABILITY_RESPONSE, and the data supplied is |
| 161 // CreateTetherAvailabilityResponse() encoded in base-64. |
| 162 std::string raw_message = |
| 163 "{\"type\":2,\"data\":\"CAESHQhLEglHb29nbGUgRmkYBCIMCAESCEdvb2dsZSBB\"}"; |
| 164 |
| 165 std::unique_ptr<MessageWrapper> wrapper = |
| 166 MessageWrapper::FromRawMessage(raw_message); |
| 167 EXPECT_TRUE(wrapper); |
| 168 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, |
| 169 wrapper->GetMessageType()); |
| 170 EXPECT_EQ(CreateTetherAvailabilityResponse().SerializeAsString(), |
| 171 wrapper->GetProto()->SerializeAsString()); |
| 172 } |
| 173 |
| 174 TEST_F(MessageWrapperTest, TestFromRawMessage_ExtraJsonKeyValuePair) { |
| 175 TetherAvailabilityResponse response = CreateTetherAvailabilityResponse(); |
| 176 std::string response_string_base64; |
| 177 base::Base64UrlEncode(response.SerializeAsString(), |
| 178 base::Base64UrlEncodePolicy::INCLUDE_PADDING, |
| 179 &response_string_base64); |
| 180 |
| 181 std::stringstream ss; |
| 182 ss << "{\"type\":" |
| 183 << static_cast<int>(MessageType::TETHER_AVAILABILITY_RESPONSE) |
| 184 << ",\"data\":\"" << response_string_base64 << "\"" |
| 185 << ",\"extraKey\":\"extraValue\"}"; |
| 186 |
| 187 std::unique_ptr<MessageWrapper> wrapper = |
| 188 MessageWrapper::FromRawMessage(ss.str()); |
| 189 EXPECT_TRUE(wrapper); |
| 190 EXPECT_EQ(MessageType::TETHER_AVAILABILITY_RESPONSE, |
| 191 wrapper->GetMessageType()); |
| 192 EXPECT_EQ(response.SerializeAsString(), |
| 193 wrapper->GetProto()->SerializeAsString()); |
| 194 } |
| 195 |
| 196 } // namespace tether |
| 197 |
| 198 } // namespace cryptauth |
OLD | NEW |