1 // Copyright 2014 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/engine/account_mapping.h"
9 #include "testing/gtest/include/gtest/gtest.h"
15 TEST(AccountMappingTest
, SerializeAccountMapping
) {
16 AccountMapping account_mapping
;
17 account_mapping
.account_id
= "acc_id";
18 account_mapping
.email
= "test@example.com";
19 account_mapping
.access_token
= "access_token";
20 account_mapping
.status
= AccountMapping::NEW
;
21 account_mapping
.status_change_timestamp
= base::Time();
22 account_mapping
.last_message_id
.clear();
24 EXPECT_EQ("test@example.com&new&0", account_mapping
.SerializeAsString());
26 account_mapping
.status
= AccountMapping::ADDING
;
27 account_mapping
.status_change_timestamp
=
28 base::Time::FromInternalValue(1305797421259977LL);
29 account_mapping
.last_message_id
= "last_message_id_1";
31 EXPECT_EQ("test@example.com&adding&1305797421259977&last_message_id_1",
32 account_mapping
.SerializeAsString());
34 account_mapping
.status
= AccountMapping::MAPPED
;
36 EXPECT_EQ("test@example.com&mapped&1305797421259977&last_message_id_1",
37 account_mapping
.SerializeAsString());
39 account_mapping
.last_message_id
.clear();
41 EXPECT_EQ("test@example.com&mapped&1305797421259977",
42 account_mapping
.SerializeAsString());
44 account_mapping
.account_id
= "acc_id2";
45 account_mapping
.email
= "test@gmail.com";
46 account_mapping
.access_token
= "access_token"; // should be ignored.
47 account_mapping
.status
= AccountMapping::REMOVING
;
49 // Serialize removing message without a message_id.
50 EXPECT_EQ("test@gmail.com&removing&1305797421259977",
51 account_mapping
.SerializeAsString());
53 // Add a message ID and serialize again.
54 account_mapping
.last_message_id
= "last_message_id_2";
56 EXPECT_EQ("test@gmail.com&removing&1305797421259977&last_message_id_2",
57 account_mapping
.SerializeAsString());
60 TEST(AccountMappingTest
, DeserializeAccountMapping
) {
61 AccountMapping account_mapping
;
62 account_mapping
.account_id
= "acc_id";
64 EXPECT_TRUE(account_mapping
.ParseFromString("test@example.com&new&0"));
65 EXPECT_EQ("acc_id", account_mapping
.account_id
);
66 EXPECT_EQ("test@example.com", account_mapping
.email
);
67 EXPECT_TRUE(account_mapping
.access_token
.empty());
68 EXPECT_EQ(AccountMapping::NEW
, account_mapping
.status
);
69 EXPECT_EQ(base::Time(), account_mapping
.status_change_timestamp
);
70 EXPECT_TRUE(account_mapping
.last_message_id
.empty());
72 EXPECT_TRUE(account_mapping
.ParseFromString(
73 "test@gmail.com&adding&1305797421259977&last_message_id_1"));
74 EXPECT_EQ("acc_id", account_mapping
.account_id
);
75 EXPECT_EQ("test@gmail.com", account_mapping
.email
);
76 EXPECT_TRUE(account_mapping
.access_token
.empty());
77 EXPECT_EQ(AccountMapping::ADDING
, account_mapping
.status
);
78 EXPECT_EQ(base::Time::FromInternalValue(1305797421259977LL),
79 account_mapping
.status_change_timestamp
);
80 EXPECT_EQ("last_message_id_1", account_mapping
.last_message_id
);
82 EXPECT_TRUE(account_mapping
.ParseFromString(
83 "test@example.com&mapped&1305797421259977"));
84 EXPECT_EQ("acc_id", account_mapping
.account_id
);
85 EXPECT_EQ("test@example.com", account_mapping
.email
);
86 EXPECT_TRUE(account_mapping
.access_token
.empty());
87 EXPECT_EQ(AccountMapping::MAPPED
, account_mapping
.status
);
88 EXPECT_EQ(base::Time::FromInternalValue(1305797421259977LL),
89 account_mapping
.status_change_timestamp
);
90 EXPECT_TRUE(account_mapping
.last_message_id
.empty());
92 EXPECT_TRUE(account_mapping
.ParseFromString(
93 "test@gmail.com&mapped&1305797421259977&last_message_id_1"));
94 EXPECT_EQ("acc_id", account_mapping
.account_id
);
95 EXPECT_EQ("test@gmail.com", account_mapping
.email
);
96 EXPECT_TRUE(account_mapping
.access_token
.empty());
97 EXPECT_EQ(AccountMapping::MAPPED
, account_mapping
.status
);
98 EXPECT_EQ(base::Time::FromInternalValue(1305797421259977LL),
99 account_mapping
.status_change_timestamp
);
100 EXPECT_EQ("last_message_id_1", account_mapping
.last_message_id
);
102 EXPECT_TRUE(account_mapping
.ParseFromString(
103 "test@gmail.com&removing&1305797421259977&last_message_id_2"));
104 EXPECT_EQ("acc_id", account_mapping
.account_id
);
105 EXPECT_EQ("test@gmail.com", account_mapping
.email
);
106 EXPECT_TRUE(account_mapping
.access_token
.empty());
107 EXPECT_EQ(AccountMapping::REMOVING
, account_mapping
.status
);
108 EXPECT_EQ(base::Time::FromInternalValue(1305797421259977LL),
109 account_mapping
.status_change_timestamp
);
110 EXPECT_EQ("last_message_id_2", account_mapping
.last_message_id
);
112 EXPECT_TRUE(account_mapping
.ParseFromString(
113 "test@gmail.com&removing&1305797421259935"));
114 EXPECT_EQ("acc_id", account_mapping
.account_id
);
115 EXPECT_EQ("test@gmail.com", account_mapping
.email
);
116 EXPECT_TRUE(account_mapping
.access_token
.empty());
117 EXPECT_EQ(AccountMapping::REMOVING
, account_mapping
.status
);
118 EXPECT_EQ(base::Time::FromInternalValue(1305797421259935LL),
119 account_mapping
.status_change_timestamp
);
120 EXPECT_TRUE(account_mapping
.last_message_id
.empty());
123 TEST(AccountMappingTest
, DeserializeAccountMappingInvalidInput
) {
124 AccountMapping account_mapping
;
125 account_mapping
.account_id
= "acc_id";
126 // Too many agruments.
127 EXPECT_FALSE(account_mapping
.ParseFromString(
128 "test@example.com&adding&1305797421259935&last_message_id_1&stuff_here"));
129 // Too few arguments.
130 EXPECT_FALSE(account_mapping
.ParseFromString(
131 "test@example.com&adding&1305797421259935"));
132 // Too few arguments.
133 EXPECT_FALSE(account_mapping
.ParseFromString(
134 "test@example.com&new"));
135 // Too few arguments.
136 EXPECT_FALSE(account_mapping
.ParseFromString(
137 "test@example.com&mapped"));
139 EXPECT_FALSE(account_mapping
.ParseFromString(
140 "&remove&1305797421259935&last_message_id_2"));
141 // Missing mapping status.
142 EXPECT_FALSE(account_mapping
.ParseFromString(
143 "test@example.com&&1305797421259935&last_message_id_2"));
144 // Unkown mapping status.
145 EXPECT_FALSE(account_mapping
.ParseFromString(
146 "test@example.com&random&1305797421259935&last_message_id_2"));
147 // Missing mapping status change timestamp.
148 EXPECT_FALSE(account_mapping
.ParseFromString(
149 "test@gmail.com&removing&&last_message_id_2"));
150 // Last mapping status change timestamp not parseable.
151 EXPECT_FALSE(account_mapping
.ParseFromString(
152 "test@gmail.com&removing&asdfjkl&last_message_id_2"));