| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 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/engine/account_mapping.h" | 5 #include "google_apis/gcm/engine/account_mapping.h" |
| 6 | 6 |
| 7 #include <string> | 7 #include <string> |
| 8 | 8 |
| 9 #include "testing/gtest/include/gtest/gtest.h" | 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 | 10 |
| 11 namespace gcm { | 11 namespace gcm { |
| 12 | 12 |
| 13 namespace { | 13 namespace { |
| 14 | 14 |
| 15 TEST(AccountMappingTest, SerializeAccountMapping) { | 15 TEST(AccountMappingTest, SerializeAccountMapping) { |
| 16 AccountMapping account_mapping; | 16 AccountMapping account_mapping; |
| 17 account_mapping.account_id = "acc_id"; | 17 account_mapping.account_id = "acc_id"; |
| 18 account_mapping.email = "test@example.com"; | 18 account_mapping.email = "test@example.com"; |
| 19 account_mapping.access_token = "access_token"; | 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(); |
| 23 |
| 24 EXPECT_EQ("test@example.com&new&0", account_mapping.SerializeAsString()); |
| 25 |
| 20 account_mapping.status = AccountMapping::ADDING; | 26 account_mapping.status = AccountMapping::ADDING; |
| 21 account_mapping.status_change_timestamp = base::Time(); | 27 account_mapping.status_change_timestamp = |
| 28 base::Time::FromInternalValue(1305797421259977LL); |
| 22 account_mapping.last_message_id = "last_message_id_1"; | 29 account_mapping.last_message_id = "last_message_id_1"; |
| 23 account_mapping.last_message_type = AccountMapping::MSG_ADD; | |
| 24 | 30 |
| 25 EXPECT_EQ("test@example.com&0&add&last_message_id_1", | 31 EXPECT_EQ("test@example.com&adding&1305797421259977&last_message_id_1", |
| 32 account_mapping.SerializeAsString()); |
| 33 |
| 34 account_mapping.status = AccountMapping::MAPPED; |
| 35 |
| 36 EXPECT_EQ("test@example.com&mapped&1305797421259977&last_message_id_1", |
| 37 account_mapping.SerializeAsString()); |
| 38 |
| 39 account_mapping.last_message_id.clear(); |
| 40 |
| 41 EXPECT_EQ("test@example.com&mapped&1305797421259977", |
| 26 account_mapping.SerializeAsString()); | 42 account_mapping.SerializeAsString()); |
| 27 | 43 |
| 28 account_mapping.account_id = "acc_id2"; | 44 account_mapping.account_id = "acc_id2"; |
| 29 account_mapping.email = "test@gmail.com"; | 45 account_mapping.email = "test@gmail.com"; |
| 30 account_mapping.access_token = "access_token"; // should be ignored. | 46 account_mapping.access_token = "access_token"; // should be ignored. |
| 31 account_mapping.status = AccountMapping::MAPPED; // should be ignored. | 47 account_mapping.status = AccountMapping::REMOVING; |
| 32 account_mapping.status_change_timestamp = | |
| 33 base::Time::FromInternalValue(1305797421259977LL); | |
| 34 account_mapping.last_message_id = "last_message_id_2"; | 48 account_mapping.last_message_id = "last_message_id_2"; |
| 35 account_mapping.last_message_type = AccountMapping::MSG_REMOVE; | |
| 36 | 49 |
| 37 EXPECT_EQ("test@gmail.com&1305797421259977&remove&last_message_id_2", | 50 EXPECT_EQ("test@gmail.com&removing&1305797421259977&last_message_id_2", |
| 38 account_mapping.SerializeAsString()); | |
| 39 | |
| 40 account_mapping.last_message_type = AccountMapping::MSG_NONE; | |
| 41 | |
| 42 EXPECT_EQ("test@gmail.com&1305797421259977&none", | |
| 43 account_mapping.SerializeAsString()); | 51 account_mapping.SerializeAsString()); |
| 44 } | 52 } |
| 45 | 53 |
| 46 TEST(AccountMappingTest, DeserializeAccountMapping) { | 54 TEST(AccountMappingTest, DeserializeAccountMapping) { |
| 47 AccountMapping account_mapping; | 55 AccountMapping account_mapping; |
| 48 account_mapping.account_id = "acc_id"; | 56 account_mapping.account_id = "acc_id"; |
| 49 EXPECT_TRUE(account_mapping.ParseFromString( | 57 |
| 50 "test@example.com&0&add&last_message_id_1")); | 58 EXPECT_TRUE(account_mapping.ParseFromString("test@example.com&new&0")); |
| 51 EXPECT_EQ("acc_id", account_mapping.account_id); | 59 EXPECT_EQ("acc_id", account_mapping.account_id); |
| 52 EXPECT_EQ("test@example.com", account_mapping.email); | 60 EXPECT_EQ("test@example.com", account_mapping.email); |
| 53 EXPECT_TRUE(account_mapping.access_token.empty()); | 61 EXPECT_TRUE(account_mapping.access_token.empty()); |
| 62 EXPECT_EQ(AccountMapping::NEW, account_mapping.status); |
| 63 EXPECT_EQ(base::Time(), account_mapping.status_change_timestamp); |
| 64 EXPECT_TRUE(account_mapping.last_message_id.empty()); |
| 65 |
| 66 EXPECT_TRUE(account_mapping.ParseFromString( |
| 67 "test@gmail.com&adding&1305797421259977&last_message_id_1")); |
| 68 EXPECT_EQ("acc_id", account_mapping.account_id); |
| 69 EXPECT_EQ("test@gmail.com", account_mapping.email); |
| 70 EXPECT_TRUE(account_mapping.access_token.empty()); |
| 54 EXPECT_EQ(AccountMapping::ADDING, account_mapping.status); | 71 EXPECT_EQ(AccountMapping::ADDING, account_mapping.status); |
| 55 EXPECT_EQ(base::Time(), account_mapping.status_change_timestamp); | 72 EXPECT_EQ(base::Time::FromInternalValue(1305797421259977LL), |
| 56 EXPECT_EQ(AccountMapping::MSG_ADD, account_mapping.last_message_type); | 73 account_mapping.status_change_timestamp); |
| 57 EXPECT_EQ("last_message_id_1", account_mapping.last_message_id); | 74 EXPECT_EQ("last_message_id_1", account_mapping.last_message_id); |
| 58 | 75 |
| 59 EXPECT_TRUE(account_mapping.ParseFromString( | 76 EXPECT_TRUE(account_mapping.ParseFromString( |
| 60 "test@gmail.com&1305797421259977&remove&last_message_id_2")); | 77 "test@example.com&mapped&1305797421259977")); |
| 78 EXPECT_EQ("acc_id", account_mapping.account_id); |
| 79 EXPECT_EQ("test@example.com", account_mapping.email); |
| 80 EXPECT_TRUE(account_mapping.access_token.empty()); |
| 81 EXPECT_EQ(AccountMapping::MAPPED, account_mapping.status); |
| 82 EXPECT_EQ(base::Time::FromInternalValue(1305797421259977LL), |
| 83 account_mapping.status_change_timestamp); |
| 84 EXPECT_TRUE(account_mapping.last_message_id.empty()); |
| 85 |
| 86 EXPECT_TRUE(account_mapping.ParseFromString( |
| 87 "test@gmail.com&mapped&1305797421259977&last_message_id_1")); |
| 88 EXPECT_EQ("acc_id", account_mapping.account_id); |
| 89 EXPECT_EQ("test@gmail.com", account_mapping.email); |
| 90 EXPECT_TRUE(account_mapping.access_token.empty()); |
| 91 EXPECT_EQ(AccountMapping::MAPPED, account_mapping.status); |
| 92 EXPECT_EQ(base::Time::FromInternalValue(1305797421259977LL), |
| 93 account_mapping.status_change_timestamp); |
| 94 EXPECT_EQ("last_message_id_1", account_mapping.last_message_id); |
| 95 |
| 96 EXPECT_TRUE(account_mapping.ParseFromString( |
| 97 "test@gmail.com&removing&1305797421259977&last_message_id_2")); |
| 61 EXPECT_EQ("acc_id", account_mapping.account_id); | 98 EXPECT_EQ("acc_id", account_mapping.account_id); |
| 62 EXPECT_EQ("test@gmail.com", account_mapping.email); | 99 EXPECT_EQ("test@gmail.com", account_mapping.email); |
| 63 EXPECT_TRUE(account_mapping.access_token.empty()); | 100 EXPECT_TRUE(account_mapping.access_token.empty()); |
| 64 EXPECT_EQ(AccountMapping::REMOVING, account_mapping.status); | 101 EXPECT_EQ(AccountMapping::REMOVING, account_mapping.status); |
| 65 EXPECT_EQ(base::Time::FromInternalValue(1305797421259977LL), | 102 EXPECT_EQ(base::Time::FromInternalValue(1305797421259977LL), |
| 66 account_mapping.status_change_timestamp); | 103 account_mapping.status_change_timestamp); |
| 67 EXPECT_EQ(AccountMapping::MSG_REMOVE, account_mapping.last_message_type); | |
| 68 EXPECT_EQ("last_message_id_2", account_mapping.last_message_id); | 104 EXPECT_EQ("last_message_id_2", account_mapping.last_message_id); |
| 69 | |
| 70 EXPECT_TRUE(account_mapping.ParseFromString( | |
| 71 "test@gmail.com&1305797421259977&none")); | |
| 72 EXPECT_EQ("acc_id", account_mapping.account_id); | |
| 73 EXPECT_EQ("test@gmail.com", account_mapping.email); | |
| 74 EXPECT_TRUE(account_mapping.access_token.empty()); | |
| 75 EXPECT_EQ(AccountMapping::MAPPED, account_mapping.status); | |
| 76 EXPECT_EQ(base::Time::FromInternalValue(1305797421259977LL), | |
| 77 account_mapping.status_change_timestamp); | |
| 78 EXPECT_EQ(AccountMapping::MSG_NONE, account_mapping.last_message_type); | |
| 79 EXPECT_EQ("", account_mapping.last_message_id); | |
| 80 } | 105 } |
| 81 | 106 |
| 82 TEST(AccountMappingTest, DeserializeAccountMappingInvalidInput) { | 107 TEST(AccountMappingTest, DeserializeAccountMappingInvalidInput) { |
| 83 AccountMapping account_mapping; | 108 AccountMapping account_mapping; |
| 84 account_mapping.account_id = "acc_id"; | 109 account_mapping.account_id = "acc_id"; |
| 85 // Too many agruments. | 110 // Too many agruments. |
| 86 EXPECT_FALSE(account_mapping.ParseFromString( | 111 EXPECT_FALSE(account_mapping.ParseFromString( |
| 87 "test@example.com&1305797421259935" | 112 "test@example.com&adding&1305797421259935&last_message_id_1&stuff_here")); |
| 88 "&add&last_message_id_1&stuff_here")); | |
| 89 // Too few arguments. | 113 // Too few arguments. |
| 90 EXPECT_FALSE(account_mapping.ParseFromString( | 114 EXPECT_FALSE(account_mapping.ParseFromString( |
| 91 "test@example.com&1305797421259935&remove")); | 115 "test@example.com&removing&1305797421259935")); |
| 92 // Too few arguments. | 116 // Too few arguments. |
| 93 EXPECT_FALSE(account_mapping.ParseFromString( | 117 EXPECT_FALSE(account_mapping.ParseFromString( |
| 94 "test@example.com&1305797421259935")); | 118 "test@example.com&adding&1305797421259935")); |
| 119 // Too few arguments. |
| 120 EXPECT_FALSE(account_mapping.ParseFromString( |
| 121 "test@example.com&new")); |
| 122 // Too few arguments. |
| 123 EXPECT_FALSE(account_mapping.ParseFromString( |
| 124 "test@example.com&mapped")); |
| 95 // Missing email. | 125 // Missing email. |
| 96 EXPECT_FALSE(account_mapping.ParseFromString( | 126 EXPECT_FALSE(account_mapping.ParseFromString( |
| 97 "&1305797421259935&remove&last_message_id_2")); | 127 "&remove&1305797421259935&last_message_id_2")); |
| 128 // Missing mapping status. |
| 129 EXPECT_FALSE(account_mapping.ParseFromString( |
| 130 "test@example.com&&1305797421259935&last_message_id_2")); |
| 131 // Unkown mapping status. |
| 132 EXPECT_FALSE(account_mapping.ParseFromString( |
| 133 "test@example.com&random&1305797421259935&last_message_id_2")); |
| 98 // Missing mapping status change timestamp. | 134 // Missing mapping status change timestamp. |
| 99 EXPECT_FALSE(account_mapping.ParseFromString( | 135 EXPECT_FALSE(account_mapping.ParseFromString( |
| 100 "test@gmail.com&&remove&last_message_id_2")); | 136 "test@gmail.com&removing&&last_message_id_2")); |
| 101 // Last mapping status change timestamp not parseable. | 137 // Last mapping status change timestamp not parseable. |
| 102 EXPECT_FALSE(account_mapping.ParseFromString( | 138 EXPECT_FALSE(account_mapping.ParseFromString( |
| 103 "test@gmail.com&remove&asdfjkl&last_message_id_2")); | 139 "test@gmail.com&removing&asdfjkl&last_message_id_2")); |
| 104 // Missing message type. | |
| 105 EXPECT_FALSE(account_mapping.ParseFromString( | |
| 106 "test@example.com&1305797421259935&&last_message_id_2")); | |
| 107 // Unkown message type. | |
| 108 EXPECT_FALSE(account_mapping.ParseFromString( | |
| 109 "test@example.com&1305797421259935&random&last_message_id_2")); | |
| 110 // Message type is none when message details specified. | |
| 111 EXPECT_FALSE(account_mapping.ParseFromString( | |
| 112 "test@example.com&1305797421259935&none&last_message_id_2")); | |
| 113 // Message type is messed up. | |
| 114 EXPECT_FALSE(account_mapping.ParseFromString( | |
| 115 "test@example.com&1305797421259935&random")); | |
| 116 // Missing last message ID. | 140 // Missing last message ID. |
| 117 EXPECT_FALSE(account_mapping.ParseFromString( | 141 EXPECT_FALSE(account_mapping.ParseFromString( |
| 118 "test@example.com&1305797421259935&remove&")); | 142 "test@example.com&removing&1305797421259935&")); |
| 119 } | 143 } |
| 120 | 144 |
| 121 } // namespace | 145 } // namespace |
| 122 } // namespace gcm | 146 } // namespace gcm |
| OLD | NEW |