1 // Copyright 2013 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.
5 #include "google_apis/gcm/base/mcs_message.h"
7 #include "base/logging.h"
8 #include "base/message_loop/message_loop.h"
9 #include "google_apis/gcm/base/mcs_util.h"
10 #include "google_apis/gcm/protocol/mcs.pb.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 const uint64 kAndroidId
= 12345;
16 const uint64 kSecret
= 54321;
18 class MCSMessageTest
: public testing::Test
{
21 ~MCSMessageTest() override
;
24 base::MessageLoop message_loop_
;
27 MCSMessageTest::MCSMessageTest() {
30 MCSMessageTest::~MCSMessageTest() {
33 TEST_F(MCSMessageTest
, Invalid
) {
35 EXPECT_FALSE(message
.IsValid());
38 TEST_F(MCSMessageTest
, InitInferTag
) {
39 scoped_ptr
<mcs_proto::LoginRequest
> login_request(
40 BuildLoginRequest(kAndroidId
, kSecret
, ""));
41 scoped_ptr
<google::protobuf::MessageLite
> login_copy(
42 new mcs_proto::LoginRequest(*login_request
));
43 MCSMessage
message(*login_copy
);
45 ASSERT_TRUE(message
.IsValid());
46 EXPECT_EQ(kLoginRequestTag
, message
.tag());
47 EXPECT_EQ(login_request
->ByteSize(), message
.size());
48 EXPECT_EQ(login_request
->SerializeAsString(), message
.SerializeAsString());
49 EXPECT_EQ(login_request
->SerializeAsString(),
50 message
.GetProtobuf().SerializeAsString());
51 login_copy
= message
.CloneProtobuf();
52 EXPECT_EQ(login_request
->SerializeAsString(),
53 login_copy
->SerializeAsString());
56 TEST_F(MCSMessageTest
, InitWithTag
) {
57 scoped_ptr
<mcs_proto::LoginRequest
> login_request(
58 BuildLoginRequest(kAndroidId
, kSecret
, ""));
59 scoped_ptr
<google::protobuf::MessageLite
> login_copy(
60 new mcs_proto::LoginRequest(*login_request
));
61 MCSMessage
message(kLoginRequestTag
, *login_copy
);
63 ASSERT_TRUE(message
.IsValid());
64 EXPECT_EQ(kLoginRequestTag
, message
.tag());
65 EXPECT_EQ(login_request
->ByteSize(), message
.size());
66 EXPECT_EQ(login_request
->SerializeAsString(), message
.SerializeAsString());
67 EXPECT_EQ(login_request
->SerializeAsString(),
68 message
.GetProtobuf().SerializeAsString());
69 login_copy
= message
.CloneProtobuf();
70 EXPECT_EQ(login_request
->SerializeAsString(),
71 login_copy
->SerializeAsString());
74 TEST_F(MCSMessageTest
, InitPassOwnership
) {
75 scoped_ptr
<mcs_proto::LoginRequest
> login_request(
76 BuildLoginRequest(kAndroidId
, kSecret
, ""));
77 scoped_ptr
<google::protobuf::MessageLite
> login_copy(
78 new mcs_proto::LoginRequest(*login_request
));
79 MCSMessage
message(kLoginRequestTag
, login_copy
.Pass());
80 EXPECT_FALSE(login_copy
.get());
81 ASSERT_TRUE(message
.IsValid());
82 EXPECT_EQ(kLoginRequestTag
, message
.tag());
83 EXPECT_EQ(login_request
->ByteSize(), message
.size());
84 EXPECT_EQ(login_request
->SerializeAsString(), message
.SerializeAsString());
85 EXPECT_EQ(login_request
->SerializeAsString(),
86 message
.GetProtobuf().SerializeAsString());
87 login_copy
= message
.CloneProtobuf();
88 EXPECT_EQ(login_request
->SerializeAsString(),
89 login_copy
->SerializeAsString());