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