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

Side by Side Diff: sql/mojo/sql_test_base.cc

Issue 1176653002: mandoline filesystem: add a sqlite3 vfs to proxy filesystem usage. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Patch cleanup now that gn check passes. Created 5 years, 6 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
(Empty)
1 // Copyright 2015 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 "sql/mojo/sql_test_base.h"
6
7 #include "mojo/application/public/cpp/application_impl.h"
8 #include "mojo/util/capture_util.h"
9 #include "sql/mojo/enable_mojo_fs.h"
10 #include "sql/test/test_helpers.h"
11
12 using mojo::Capture;
13
14 namespace sql {
15
16 SQLTestBase::SQLTestBase() {
17 }
18
19 SQLTestBase::~SQLTestBase() {
20 }
21
22 base::FilePath SQLTestBase::db_path() {
23 return base::FilePath(FILE_PATH_LITERAL("SQLTest.db"));
24 }
25
26 sql::Connection& SQLTestBase::db() {
27 return db_;
28 }
29
30 bool SQLTestBase::Reopen() {
31 db_.Close();
32 return db_.Open(db_path());
33 }
34
35 bool SQLTestBase::GetPathExists(const base::FilePath& path) {
36 filesystem::FileError error = filesystem::FILE_ERROR_FAILED;
37 bool exists = false;
38 vfs_->GetDirectory()->Exists(path.value(), Capture(&error, &exists));
39 vfs_->GetDirectory().WaitForIncomingResponse();
40 if (error != filesystem::FILE_ERROR_OK)
41 return false;
42 return exists;
43 }
44
45 bool SQLTestBase::CorruptSizeInHeaderOfPath(const base::FilePath& path) {
Scott Hess - ex-Googler 2015/06/10 22:35:46 This duplication bugs me. WDYT about a helper to
Elliot Glaysher 2015/06/12 22:36:44 Created a corrupt the header function shared with
46 // See http://www.sqlite.org/fileformat.html#database_header
47 const size_t kHeaderSize = 100;
48 const size_t kPageSizeOffset = 16;
49 const size_t kFileChangeCountOffset = 24;
50 const size_t kPageCountOffset = 28;
51 const size_t kVersionValidForOffset = 92; // duplicate kFileChangeCountOffset
52
53 mojo::Array<uint8_t> header;
54
55 filesystem::FileError error = filesystem::FILE_ERROR_FAILED;
56 filesystem::FilePtr file_ptr;
57 vfs_->GetDirectory()->OpenFile(
58 mojo::String(path.value()), GetProxy(&file_ptr),
59 filesystem::kFlagRead | filesystem::kFlagWrite |
60 filesystem::kFlagOpenAlways,
61 Capture(&error));
62 vfs_->GetDirectory().WaitForIncomingResponse();
63 if (error != filesystem::FILE_ERROR_OK)
64 return false;
65
66 file_ptr->Read(kHeaderSize, 0, filesystem::WHENCE_FROM_BEGIN,
67 Capture(&error, &header));
68 file_ptr.WaitForIncomingResponse();
69 if (error != filesystem::FILE_ERROR_OK)
70 return false;
71
72 filesystem::FileInformationPtr info;
73 file_ptr->Stat(Capture(&error, &info));
74 file_ptr.WaitForIncomingResponse();
75 if (error != filesystem::FILE_ERROR_OK)
76 return false;
77 int64_t db_size = info->size;
78
79 const unsigned page_size =
80 test::ReadBigEndian(&header.front() + kPageSizeOffset, 2);
81
82 // One larger than the expected size.
83 const unsigned page_count =
84 static_cast<unsigned>((db_size + page_size) / page_size);
85 test::WriteBigEndian(page_count, &header.front() + kPageCountOffset, 4);
86
87 // Update change count so outstanding readers know the info changed.
88 // Both spots must match for the page count to be considered valid.
89 unsigned change_count =
90 test::ReadBigEndian(&header.front() + kFileChangeCountOffset, 4);
91 test::WriteBigEndian(change_count + 1,
92 &header.front() + kFileChangeCountOffset, 4);
93 test::WriteBigEndian(change_count + 1,
94 &header.front() + kVersionValidForOffset, 4);
95
96 uint32_t num_bytes_written = 0;
97 file_ptr->Write(header.Pass(), 0, filesystem::WHENCE_FROM_BEGIN,
98 Capture(&error, &num_bytes_written));
99 file_ptr.WaitForIncomingResponse();
100 if (error != filesystem::FILE_ERROR_OK)
101 return false;
102 if (num_bytes_written != kHeaderSize)
103 return false;
104
105 return true;
106 }
107
108 void SQLTestBase::WriteJunkToDatabase(bool truncate) {
109 uint32_t flags = 0;
110 if (truncate)
111 flags = filesystem::kFlagWrite | filesystem::kFlagCreate;
112 else
113 flags = filesystem::kFlagWrite | filesystem::kFlagOpen;
114
115 filesystem::FileError error = filesystem::FILE_ERROR_FAILED;
116 filesystem::FilePtr file_ptr;
117 vfs_->GetDirectory()->OpenFile(
118 mojo::String(db_path().value()), GetProxy(&file_ptr),
119 flags,
120 Capture(&error));
121 vfs_->GetDirectory().WaitForIncomingResponse();
122 if (error != filesystem::FILE_ERROR_OK)
123 return;
124
125 const char* kJunk = "Now is the winter of our discontent.";
126 mojo::Array<uint8_t> data(strlen(kJunk));
127 memcpy(&data.front(), kJunk, strlen(kJunk));
128
129 uint32_t num_bytes_written = 0;
130 file_ptr->Write(data.Pass(), 0, filesystem::WHENCE_FROM_BEGIN,
131 Capture(&error, &num_bytes_written));
132 file_ptr.WaitForIncomingResponse();
133 }
134
135 void SQLTestBase::TruncateDatabase() {
136 filesystem::FileError error = filesystem::FILE_ERROR_FAILED;
137 filesystem::FilePtr file_ptr;
138 vfs_->GetDirectory()->OpenFile(
139 mojo::String(db_path().value()), GetProxy(&file_ptr),
140 filesystem::kFlagWrite | filesystem::kFlagOpen,
141 Capture(&error));
142 vfs_->GetDirectory().WaitForIncomingResponse();
143 if (error != filesystem::FILE_ERROR_OK)
144 return;
145
146 file_ptr->Truncate(0, Capture(&error));
147 file_ptr.WaitForIncomingResponse();
148 ASSERT_EQ(filesystem::FILE_ERROR_OK, error);
149 }
150
151 void SQLTestBase::SetUp() {
152 ApplicationTestBase::SetUp();
153
154 mojo::URLRequestPtr request(mojo::URLRequest::New());
155 request->url = mojo::String::From("mojo:filesystem");
156 application_impl()->ConnectToService(request.Pass(), &files_);
157
158 filesystem::FileError error = filesystem::FILE_ERROR_FAILED;
159 filesystem::DirectoryPtr directory;
160 files()->OpenFileSystem("temp", GetProxy(&directory), Capture(&error));
161 ASSERT_TRUE(files().WaitForIncomingResponse());
162 ASSERT_EQ(filesystem::FILE_ERROR_OK, error);
163
164 vfs_.reset(new ScopedMojoFilesystemVFS(directory.Pass()));
165 ASSERT_TRUE(db_.Open(db_path()));
166 }
167
168 void SQLTestBase::TearDown() {
169 db_.Close();
170 vfs_.reset();
171
172 ApplicationTestBase::TearDown();
173 }
174
175 } // namespace sql
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698