Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(131)

Side by Side Diff: google_apis/gcm/engine/gcm_store_impl_unittest.cc

Issue 2380003002: GCM Store: Fix invalid argument errors (Closed)
Patch Set: Created 4 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/gcm_store_impl.h" 5 #include "google_apis/gcm/engine/gcm_store_impl.h"
6 6
7 #include <stdint.h> 7 #include <stdint.h>
8 8
9 #include <memory> 9 #include <memory>
10 #include <string> 10 #include <string>
11 #include <utility> 11 #include <utility>
12 #include <vector> 12 #include <vector>
13 13
14 #include "base/bind.h" 14 #include "base/bind.h"
15 #include "base/command_line.h" 15 #include "base/command_line.h"
16 #include "base/files/file_path.h" 16 #include "base/files/file_path.h"
17 #include "base/files/file_util.h"
17 #include "base/files/scoped_temp_dir.h" 18 #include "base/files/scoped_temp_dir.h"
18 #include "base/memory/ptr_util.h" 19 #include "base/memory/ptr_util.h"
19 #include "base/strings/string_number_conversions.h" 20 #include "base/strings/string_number_conversions.h"
20 #include "base/test/test_simple_task_runner.h" 21 #include "base/test/test_simple_task_runner.h"
21 #include "base/threading/thread_task_runner_handle.h" 22 #include "base/threading/thread_task_runner_handle.h"
22 #include "google_apis/gcm/base/fake_encryptor.h" 23 #include "google_apis/gcm/base/fake_encryptor.h"
23 #include "google_apis/gcm/base/mcs_message.h" 24 #include "google_apis/gcm/base/mcs_message.h"
24 #include "google_apis/gcm/base/mcs_util.h" 25 #include "google_apis/gcm/base/mcs_util.h"
25 #include "google_apis/gcm/protocol/mcs.pb.h" 26 #include "google_apis/gcm/protocol/mcs.pb.h"
26 #include "testing/gtest/include/gtest/gtest.h" 27 #include "testing/gtest/include/gtest/gtest.h"
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
62 std::unique_ptr<GCMStore::LoadResult> result); 63 std::unique_ptr<GCMStore::LoadResult> result);
63 void LoadWithoutCheckCallback( 64 void LoadWithoutCheckCallback(
64 std::unique_ptr<GCMStore::LoadResult>* result_dst, 65 std::unique_ptr<GCMStore::LoadResult>* result_dst,
65 std::unique_ptr<GCMStore::LoadResult> result); 66 std::unique_ptr<GCMStore::LoadResult> result);
66 void UpdateCallback(bool success); 67 void UpdateCallback(bool success);
67 68
68 protected: 69 protected:
69 scoped_refptr<base::TestSimpleTaskRunner> task_runner_; 70 scoped_refptr<base::TestSimpleTaskRunner> task_runner_;
70 base::ThreadTaskRunnerHandle task_runner_handle_; 71 base::ThreadTaskRunnerHandle task_runner_handle_;
71 base::ScopedTempDir temp_directory_; 72 base::ScopedTempDir temp_directory_;
73 base::FilePath store_path_;
72 bool expected_success_; 74 bool expected_success_;
73 uint64_t next_persistent_id_; 75 uint64_t next_persistent_id_;
74 }; 76 };
75 77
76 GCMStoreImplTest::GCMStoreImplTest() 78 GCMStoreImplTest::GCMStoreImplTest()
77 : task_runner_(new base::TestSimpleTaskRunner()), 79 : task_runner_(new base::TestSimpleTaskRunner()),
78 task_runner_handle_(task_runner_), 80 task_runner_handle_(task_runner_),
79 expected_success_(true), 81 expected_success_(true),
80 next_persistent_id_(base::Time::Now().ToInternalValue()) { 82 next_persistent_id_(base::Time::Now().ToInternalValue()) {
81 EXPECT_TRUE(temp_directory_.CreateUniqueTempDir()); 83 EXPECT_TRUE(temp_directory_.CreateUniqueTempDir());
82 } 84 }
83 85
84 GCMStoreImplTest::~GCMStoreImplTest() {} 86 GCMStoreImplTest::~GCMStoreImplTest() {}
85 87
86 std::unique_ptr<GCMStoreImpl> GCMStoreImplTest::BuildGCMStore() { 88 std::unique_ptr<GCMStoreImpl> GCMStoreImplTest::BuildGCMStore() {
89 // Pass an non-existent directory as store path to match the exact behavior in
90 // the production code. Currently GCMStoreImpl checks if the directory exists
Nicolas Zea 2016/09/29 16:49:57 should this comment be updated?
johnme 2016/09/29 17:08:29 Done.
91 // or not to determine the store existence.
92 store_path_ =
93 temp_directory_.GetPath().Append(FILE_PATH_LITERAL("GCM Store"));
87 return std::unique_ptr<GCMStoreImpl>(new GCMStoreImpl( 94 return std::unique_ptr<GCMStoreImpl>(new GCMStoreImpl(
88 // Pass an non-existent directory as store path to match the exact 95 store_path_, task_runner_,
89 // behavior in the production code. Currently GCMStoreImpl checks if 96 base::WrapUnique<Encryptor>(new FakeEncryptor)));
90 // the directory exist or not to determine the store existence.
91 temp_directory_.GetPath().Append(FILE_PATH_LITERAL("GCM Store")),
92 task_runner_, base::WrapUnique<Encryptor>(new FakeEncryptor)));
93 } 97 }
94 98
95 void GCMStoreImplTest::LoadGCMStore( 99 void GCMStoreImplTest::LoadGCMStore(
96 GCMStoreImpl* gcm_store, 100 GCMStoreImpl* gcm_store,
97 std::unique_ptr<GCMStore::LoadResult>* result_dst) { 101 std::unique_ptr<GCMStore::LoadResult>* result_dst) {
98 gcm_store->Load( 102 gcm_store->Load(
99 GCMStore::CREATE_IF_MISSING, 103 GCMStore::CREATE_IF_MISSING,
100 base::Bind(&GCMStoreImplTest::LoadCallback, 104 base::Bind(&GCMStoreImplTest::LoadCallback,
101 base::Unretained(this), 105 base::Unretained(this),
102 result_dst)); 106 result_dst));
(...skipping 30 matching lines...) Expand all
133 LoadGCMStore(gcm_store.get(), &load_result); 137 LoadGCMStore(gcm_store.get(), &load_result);
134 138
135 EXPECT_EQ(0U, load_result->device_android_id); 139 EXPECT_EQ(0U, load_result->device_android_id);
136 EXPECT_EQ(0U, load_result->device_security_token); 140 EXPECT_EQ(0U, load_result->device_security_token);
137 EXPECT_TRUE(load_result->incoming_messages.empty()); 141 EXPECT_TRUE(load_result->incoming_messages.empty());
138 EXPECT_TRUE(load_result->outgoing_messages.empty()); 142 EXPECT_TRUE(load_result->outgoing_messages.empty());
139 EXPECT_TRUE(load_result->gservices_settings.empty()); 143 EXPECT_TRUE(load_result->gservices_settings.empty());
140 EXPECT_EQ(base::Time::FromInternalValue(0LL), load_result->last_checkin_time); 144 EXPECT_EQ(base::Time::FromInternalValue(0LL), load_result->last_checkin_time);
141 } 145 }
142 146
143 // Verify new database is not created when DO_NOT_CREATE_NEW_STORE is passed. 147 // Verify new database is not created when DO_NOT_CREATE is passed.
144 TEST_F(GCMStoreImplTest, LoadWithoutCreatingNewStore) { 148 TEST_F(GCMStoreImplTest, LoadWithoutCreatingNewStore) {
145 std::unique_ptr<GCMStoreImpl> gcm_store(BuildGCMStore()); 149 std::unique_ptr<GCMStoreImpl> gcm_store(BuildGCMStore());
146 std::unique_ptr<GCMStore::LoadResult> load_result; 150 std::unique_ptr<GCMStore::LoadResult> load_result;
147 gcm_store->Load( 151 gcm_store->Load(
148 GCMStore::DO_NOT_CREATE, 152 GCMStore::DO_NOT_CREATE,
149 base::Bind(&GCMStoreImplTest::LoadWithoutCheckCallback, 153 base::Bind(&GCMStoreImplTest::LoadWithoutCheckCallback,
150 base::Unretained(this), 154 base::Unretained(this),
151 &load_result)); 155 &load_result));
152 PumpLoop(); 156 PumpLoop();
153 157
154 EXPECT_FALSE(load_result->success); 158 EXPECT_FALSE(load_result->success);
155 EXPECT_TRUE(load_result->store_does_not_exist); 159 EXPECT_TRUE(load_result->store_does_not_exist);
156 } 160 }
157 161
162 // Verifies that loads with DO_NOT_CREATE set store_does_not_exist to true when
163 // an empty directory was left behind after destroying the database.
164 TEST_F(GCMStoreImplTest, LoadWithEmptyDirectory) {
165 std::unique_ptr<GCMStoreImpl> gcm_store(BuildGCMStore());
166
167 // Create an empty directory at the store path, to simulate an empty directory
168 // being left behind after deleting a previous store.
169 ASSERT_TRUE(base::CreateDirectory(store_path_));
170
171 std::unique_ptr<GCMStore::LoadResult> load_result;
172 gcm_store->Load(
173 GCMStore::DO_NOT_CREATE,
174 base::Bind(&GCMStoreImplTest::LoadWithoutCheckCallback,
175 base::Unretained(this),
176 &load_result));
177 PumpLoop();
178
179 EXPECT_FALSE(load_result->success);
180 EXPECT_TRUE(load_result->store_does_not_exist);
181 }
182
158 TEST_F(GCMStoreImplTest, DeviceCredentials) { 183 TEST_F(GCMStoreImplTest, DeviceCredentials) {
159 std::unique_ptr<GCMStoreImpl> gcm_store(BuildGCMStore()); 184 std::unique_ptr<GCMStoreImpl> gcm_store(BuildGCMStore());
160 std::unique_ptr<GCMStore::LoadResult> load_result; 185 std::unique_ptr<GCMStore::LoadResult> load_result;
161 LoadGCMStore(gcm_store.get(), &load_result); 186 LoadGCMStore(gcm_store.get(), &load_result);
162 187
163 gcm_store->SetDeviceCredentials( 188 gcm_store->SetDeviceCredentials(
164 kDeviceId, 189 kDeviceId,
165 kDeviceToken, 190 kDeviceToken,
166 base::Bind(&GCMStoreImplTest::UpdateCallback, base::Unretained(this))); 191 base::Bind(&GCMStoreImplTest::UpdateCallback, base::Unretained(this)));
167 PumpLoop(); 192 PumpLoop();
(...skipping 565 matching lines...) Expand 10 before | Expand all | Expand 10 after
733 758
734 ASSERT_EQ(1u, load_result->instance_id_data.size()); 759 ASSERT_EQ(1u, load_result->instance_id_data.size());
735 ASSERT_TRUE(load_result->instance_id_data.find(kAppName2) != 760 ASSERT_TRUE(load_result->instance_id_data.find(kAppName2) !=
736 load_result->instance_id_data.end()); 761 load_result->instance_id_data.end());
737 EXPECT_EQ(instance_id_data2, load_result->instance_id_data[kAppName2]); 762 EXPECT_EQ(instance_id_data2, load_result->instance_id_data[kAppName2]);
738 } 763 }
739 764
740 } // namespace 765 } // namespace
741 766
742 } // namespace gcm 767 } // namespace gcm
OLDNEW
« google_apis/gcm/engine/gcm_store_impl.cc ('K') | « google_apis/gcm/engine/gcm_store_impl.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698