OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 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 "google_apis/gcm/base/mcs_util.h" | 5 #include "google_apis/gcm/base/mcs_util.h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
| 10 #include <memory> |
| 11 |
10 #include "base/bind.h" | 12 #include "base/bind.h" |
11 #include "base/macros.h" | 13 #include "base/macros.h" |
12 #include "base/memory/scoped_ptr.h" | |
13 #include "base/run_loop.h" | 14 #include "base/run_loop.h" |
14 #include "base/strings/string_number_conversions.h" | 15 #include "base/strings/string_number_conversions.h" |
15 #include "testing/gtest/include/gtest/gtest.h" | 16 #include "testing/gtest/include/gtest/gtest.h" |
16 | 17 |
17 namespace gcm { | 18 namespace gcm { |
18 namespace { | 19 namespace { |
19 | 20 |
20 const uint64_t kAuthId = 4421448356646222460; | 21 const uint64_t kAuthId = 4421448356646222460; |
21 const uint64_t kAuthToken = 12345; | 22 const uint64_t kAuthToken = 12345; |
22 | 23 |
23 // Build a login request protobuf. | 24 // Build a login request protobuf. |
24 TEST(MCSUtilTest, BuildLoginRequest) { | 25 TEST(MCSUtilTest, BuildLoginRequest) { |
25 scoped_ptr<mcs_proto::LoginRequest> login_request = | 26 std::unique_ptr<mcs_proto::LoginRequest> login_request = |
26 BuildLoginRequest(kAuthId, kAuthToken, "1.0"); | 27 BuildLoginRequest(kAuthId, kAuthToken, "1.0"); |
27 ASSERT_EQ("chrome-1.0", login_request->id()); | 28 ASSERT_EQ("chrome-1.0", login_request->id()); |
28 ASSERT_EQ(base::Uint64ToString(kAuthToken), login_request->auth_token()); | 29 ASSERT_EQ(base::Uint64ToString(kAuthToken), login_request->auth_token()); |
29 ASSERT_EQ(base::Uint64ToString(kAuthId), login_request->user()); | 30 ASSERT_EQ(base::Uint64ToString(kAuthId), login_request->user()); |
30 ASSERT_EQ("android-3d5c23dac2a1fa7c", login_request->device_id()); | 31 ASSERT_EQ("android-3d5c23dac2a1fa7c", login_request->device_id()); |
31 ASSERT_EQ("new_vc", login_request->setting(0).name()); | 32 ASSERT_EQ("new_vc", login_request->setting(0).name()); |
32 ASSERT_EQ("1", login_request->setting(0).value()); | 33 ASSERT_EQ("1", login_request->setting(0).value()); |
33 // TODO(zea): test the other fields once they have valid values. | 34 // TODO(zea): test the other fields once they have valid values. |
34 } | 35 } |
35 | 36 |
36 // Test building a protobuf and extracting the tag from a protobuf. | 37 // Test building a protobuf and extracting the tag from a protobuf. |
37 TEST(MCSUtilTest, ProtobufToTag) { | 38 TEST(MCSUtilTest, ProtobufToTag) { |
38 for (uint8_t i = 0; i < kNumProtoTypes; ++i) { | 39 for (uint8_t i = 0; i < kNumProtoTypes; ++i) { |
39 scoped_ptr<google::protobuf::MessageLite> protobuf = | 40 std::unique_ptr<google::protobuf::MessageLite> protobuf = |
40 BuildProtobufFromTag(i); | 41 BuildProtobufFromTag(i); |
41 if (!protobuf.get()) // Not all tags have protobuf definitions. | 42 if (!protobuf.get()) // Not all tags have protobuf definitions. |
42 continue; | 43 continue; |
43 ASSERT_EQ(i, GetMCSProtoTag(*protobuf)) << "Type " << i; | 44 ASSERT_EQ(i, GetMCSProtoTag(*protobuf)) << "Type " << i; |
44 } | 45 } |
45 } | 46 } |
46 | 47 |
47 // Test getting and setting persistent ids. | 48 // Test getting and setting persistent ids. |
48 TEST(MCSUtilTest, PersistentIds) { | 49 TEST(MCSUtilTest, PersistentIds) { |
49 static_assert(kNumProtoTypes == 16U, "Update Persistent Ids"); | 50 static_assert(kNumProtoTypes == 16U, "Update Persistent Ids"); |
50 const int kTagsWithPersistentIds[] = { | 51 const int kTagsWithPersistentIds[] = { |
51 kIqStanzaTag, | 52 kIqStanzaTag, |
52 kDataMessageStanzaTag | 53 kDataMessageStanzaTag |
53 }; | 54 }; |
54 for (size_t i = 0; i < arraysize(kTagsWithPersistentIds); ++i) { | 55 for (size_t i = 0; i < arraysize(kTagsWithPersistentIds); ++i) { |
55 int tag = kTagsWithPersistentIds[i]; | 56 int tag = kTagsWithPersistentIds[i]; |
56 scoped_ptr<google::protobuf::MessageLite> protobuf = | 57 std::unique_ptr<google::protobuf::MessageLite> protobuf = |
57 BuildProtobufFromTag(tag); | 58 BuildProtobufFromTag(tag); |
58 ASSERT_TRUE(protobuf.get()); | 59 ASSERT_TRUE(protobuf.get()); |
59 SetPersistentId(base::IntToString(tag), protobuf.get()); | 60 SetPersistentId(base::IntToString(tag), protobuf.get()); |
60 int get_val = 0; | 61 int get_val = 0; |
61 base::StringToInt(GetPersistentId(*protobuf), &get_val); | 62 base::StringToInt(GetPersistentId(*protobuf), &get_val); |
62 ASSERT_EQ(tag, get_val); | 63 ASSERT_EQ(tag, get_val); |
63 } | 64 } |
64 } | 65 } |
65 | 66 |
66 // Test getting and setting stream ids. | 67 // Test getting and setting stream ids. |
67 TEST(MCSUtilTest, StreamIds) { | 68 TEST(MCSUtilTest, StreamIds) { |
68 static_assert(kNumProtoTypes == 16U, "Update Stream Ids"); | 69 static_assert(kNumProtoTypes == 16U, "Update Stream Ids"); |
69 const int kTagsWithStreamIds[] = { | 70 const int kTagsWithStreamIds[] = { |
70 kIqStanzaTag, | 71 kIqStanzaTag, |
71 kDataMessageStanzaTag, | 72 kDataMessageStanzaTag, |
72 kHeartbeatPingTag, | 73 kHeartbeatPingTag, |
73 kHeartbeatAckTag, | 74 kHeartbeatAckTag, |
74 kLoginResponseTag, | 75 kLoginResponseTag, |
75 }; | 76 }; |
76 for (size_t i = 0; i < arraysize(kTagsWithStreamIds); ++i) { | 77 for (size_t i = 0; i < arraysize(kTagsWithStreamIds); ++i) { |
77 int tag = kTagsWithStreamIds[i]; | 78 int tag = kTagsWithStreamIds[i]; |
78 scoped_ptr<google::protobuf::MessageLite> protobuf = | 79 std::unique_ptr<google::protobuf::MessageLite> protobuf = |
79 BuildProtobufFromTag(tag); | 80 BuildProtobufFromTag(tag); |
80 ASSERT_TRUE(protobuf.get()); | 81 ASSERT_TRUE(protobuf.get()); |
81 SetLastStreamIdReceived(tag, protobuf.get()); | 82 SetLastStreamIdReceived(tag, protobuf.get()); |
82 int get_id = GetLastStreamIdReceived(*protobuf); | 83 int get_id = GetLastStreamIdReceived(*protobuf); |
83 ASSERT_EQ(tag, get_id); | 84 ASSERT_EQ(tag, get_id); |
84 } | 85 } |
85 } | 86 } |
86 | 87 |
87 } // namespace | 88 } // namespace |
88 } // namespace gcm | 89 } // namespace gcm |
OLD | NEW |