Chromium Code Reviews| Index: sql/mojo/sql_test_base.cc |
| diff --git a/sql/mojo/sql_test_base.cc b/sql/mojo/sql_test_base.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..a7dc0994e779d7eb5cf4694e792e2f9c880521ff |
| --- /dev/null |
| +++ b/sql/mojo/sql_test_base.cc |
| @@ -0,0 +1,175 @@ |
| +// Copyright 2015 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#include "sql/mojo/sql_test_base.h" |
| + |
| +#include "mojo/application/public/cpp/application_impl.h" |
| +#include "mojo/util/capture_util.h" |
| +#include "sql/mojo/enable_mojo_fs.h" |
| +#include "sql/test/test_helpers.h" |
| + |
| +using mojo::Capture; |
| + |
| +namespace sql { |
| + |
| +SQLTestBase::SQLTestBase() { |
| +} |
| + |
| +SQLTestBase::~SQLTestBase() { |
| +} |
| + |
| +base::FilePath SQLTestBase::db_path() { |
| + return base::FilePath(FILE_PATH_LITERAL("SQLTest.db")); |
| +} |
| + |
| +sql::Connection& SQLTestBase::db() { |
| + return db_; |
| +} |
| + |
| +bool SQLTestBase::Reopen() { |
| + db_.Close(); |
| + return db_.Open(db_path()); |
| +} |
| + |
| +bool SQLTestBase::GetPathExists(const base::FilePath& path) { |
| + filesystem::FileError error = filesystem::FILE_ERROR_FAILED; |
| + bool exists = false; |
| + vfs_->GetDirectory()->Exists(path.value(), Capture(&error, &exists)); |
| + vfs_->GetDirectory().WaitForIncomingResponse(); |
| + if (error != filesystem::FILE_ERROR_OK) |
| + return false; |
| + return exists; |
| +} |
| + |
| +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
|
| + // See http://www.sqlite.org/fileformat.html#database_header |
| + const size_t kHeaderSize = 100; |
| + const size_t kPageSizeOffset = 16; |
| + const size_t kFileChangeCountOffset = 24; |
| + const size_t kPageCountOffset = 28; |
| + const size_t kVersionValidForOffset = 92; // duplicate kFileChangeCountOffset |
| + |
| + mojo::Array<uint8_t> header; |
| + |
| + filesystem::FileError error = filesystem::FILE_ERROR_FAILED; |
| + filesystem::FilePtr file_ptr; |
| + vfs_->GetDirectory()->OpenFile( |
| + mojo::String(path.value()), GetProxy(&file_ptr), |
| + filesystem::kFlagRead | filesystem::kFlagWrite | |
| + filesystem::kFlagOpenAlways, |
| + Capture(&error)); |
| + vfs_->GetDirectory().WaitForIncomingResponse(); |
| + if (error != filesystem::FILE_ERROR_OK) |
| + return false; |
| + |
| + file_ptr->Read(kHeaderSize, 0, filesystem::WHENCE_FROM_BEGIN, |
| + Capture(&error, &header)); |
| + file_ptr.WaitForIncomingResponse(); |
| + if (error != filesystem::FILE_ERROR_OK) |
| + return false; |
| + |
| + filesystem::FileInformationPtr info; |
| + file_ptr->Stat(Capture(&error, &info)); |
| + file_ptr.WaitForIncomingResponse(); |
| + if (error != filesystem::FILE_ERROR_OK) |
| + return false; |
| + int64_t db_size = info->size; |
| + |
| + const unsigned page_size = |
| + test::ReadBigEndian(&header.front() + kPageSizeOffset, 2); |
| + |
| + // One larger than the expected size. |
| + const unsigned page_count = |
| + static_cast<unsigned>((db_size + page_size) / page_size); |
| + test::WriteBigEndian(page_count, &header.front() + kPageCountOffset, 4); |
| + |
| + // Update change count so outstanding readers know the info changed. |
| + // Both spots must match for the page count to be considered valid. |
| + unsigned change_count = |
| + test::ReadBigEndian(&header.front() + kFileChangeCountOffset, 4); |
| + test::WriteBigEndian(change_count + 1, |
| + &header.front() + kFileChangeCountOffset, 4); |
| + test::WriteBigEndian(change_count + 1, |
| + &header.front() + kVersionValidForOffset, 4); |
| + |
| + uint32_t num_bytes_written = 0; |
| + file_ptr->Write(header.Pass(), 0, filesystem::WHENCE_FROM_BEGIN, |
| + Capture(&error, &num_bytes_written)); |
| + file_ptr.WaitForIncomingResponse(); |
| + if (error != filesystem::FILE_ERROR_OK) |
| + return false; |
| + if (num_bytes_written != kHeaderSize) |
| + return false; |
| + |
| + return true; |
| +} |
| + |
| +void SQLTestBase::WriteJunkToDatabase(bool truncate) { |
| + uint32_t flags = 0; |
| + if (truncate) |
| + flags = filesystem::kFlagWrite | filesystem::kFlagCreate; |
| + else |
| + flags = filesystem::kFlagWrite | filesystem::kFlagOpen; |
| + |
| + filesystem::FileError error = filesystem::FILE_ERROR_FAILED; |
| + filesystem::FilePtr file_ptr; |
| + vfs_->GetDirectory()->OpenFile( |
| + mojo::String(db_path().value()), GetProxy(&file_ptr), |
| + flags, |
| + Capture(&error)); |
| + vfs_->GetDirectory().WaitForIncomingResponse(); |
| + if (error != filesystem::FILE_ERROR_OK) |
| + return; |
| + |
| + const char* kJunk = "Now is the winter of our discontent."; |
| + mojo::Array<uint8_t> data(strlen(kJunk)); |
| + memcpy(&data.front(), kJunk, strlen(kJunk)); |
| + |
| + uint32_t num_bytes_written = 0; |
| + file_ptr->Write(data.Pass(), 0, filesystem::WHENCE_FROM_BEGIN, |
| + Capture(&error, &num_bytes_written)); |
| + file_ptr.WaitForIncomingResponse(); |
| +} |
| + |
| +void SQLTestBase::TruncateDatabase() { |
| + filesystem::FileError error = filesystem::FILE_ERROR_FAILED; |
| + filesystem::FilePtr file_ptr; |
| + vfs_->GetDirectory()->OpenFile( |
| + mojo::String(db_path().value()), GetProxy(&file_ptr), |
| + filesystem::kFlagWrite | filesystem::kFlagOpen, |
| + Capture(&error)); |
| + vfs_->GetDirectory().WaitForIncomingResponse(); |
| + if (error != filesystem::FILE_ERROR_OK) |
| + return; |
| + |
| + file_ptr->Truncate(0, Capture(&error)); |
| + file_ptr.WaitForIncomingResponse(); |
| + ASSERT_EQ(filesystem::FILE_ERROR_OK, error); |
| +} |
| + |
| +void SQLTestBase::SetUp() { |
| + ApplicationTestBase::SetUp(); |
| + |
| + mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| + request->url = mojo::String::From("mojo:filesystem"); |
| + application_impl()->ConnectToService(request.Pass(), &files_); |
| + |
| + filesystem::FileError error = filesystem::FILE_ERROR_FAILED; |
| + filesystem::DirectoryPtr directory; |
| + files()->OpenFileSystem("temp", GetProxy(&directory), Capture(&error)); |
| + ASSERT_TRUE(files().WaitForIncomingResponse()); |
| + ASSERT_EQ(filesystem::FILE_ERROR_OK, error); |
| + |
| + vfs_.reset(new ScopedMojoFilesystemVFS(directory.Pass())); |
| + ASSERT_TRUE(db_.Open(db_path())); |
| +} |
| + |
| +void SQLTestBase::TearDown() { |
| + db_.Close(); |
| + vfs_.reset(); |
| + |
| + ApplicationTestBase::TearDown(); |
| +} |
| + |
| +} // namespace sql |