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