OLD | NEW |
(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) { |
| 46 // See http://www.sqlite.org/fileformat.html#database_header |
| 47 const size_t kHeaderSize = 100; |
| 48 |
| 49 mojo::Array<uint8_t> header; |
| 50 |
| 51 filesystem::FileError error = filesystem::FILE_ERROR_FAILED; |
| 52 filesystem::FilePtr file_ptr; |
| 53 vfs_->GetDirectory()->OpenFile( |
| 54 mojo::String(path.value()), GetProxy(&file_ptr), |
| 55 filesystem::kFlagRead | filesystem::kFlagWrite | |
| 56 filesystem::kFlagOpenAlways, |
| 57 Capture(&error)); |
| 58 vfs_->GetDirectory().WaitForIncomingResponse(); |
| 59 if (error != filesystem::FILE_ERROR_OK) |
| 60 return false; |
| 61 |
| 62 file_ptr->Read(kHeaderSize, 0, filesystem::WHENCE_FROM_BEGIN, |
| 63 Capture(&error, &header)); |
| 64 file_ptr.WaitForIncomingResponse(); |
| 65 if (error != filesystem::FILE_ERROR_OK) |
| 66 return false; |
| 67 |
| 68 filesystem::FileInformationPtr info; |
| 69 file_ptr->Stat(Capture(&error, &info)); |
| 70 file_ptr.WaitForIncomingResponse(); |
| 71 if (error != filesystem::FILE_ERROR_OK) |
| 72 return false; |
| 73 int64_t db_size = info->size; |
| 74 |
| 75 test::CorruptSizeInHeaderMemory(&header.front(), db_size); |
| 76 |
| 77 uint32_t num_bytes_written = 0; |
| 78 file_ptr->Write(header.Pass(), 0, filesystem::WHENCE_FROM_BEGIN, |
| 79 Capture(&error, &num_bytes_written)); |
| 80 file_ptr.WaitForIncomingResponse(); |
| 81 if (error != filesystem::FILE_ERROR_OK) |
| 82 return false; |
| 83 if (num_bytes_written != kHeaderSize) |
| 84 return false; |
| 85 |
| 86 return true; |
| 87 } |
| 88 |
| 89 void SQLTestBase::WriteJunkToDatabase(WriteJunkType type) { |
| 90 uint32_t flags = 0; |
| 91 if (type == TYPE_TRUNCATE_AND_CREATE) |
| 92 flags = filesystem::kFlagWrite | filesystem::kFlagCreate; |
| 93 else |
| 94 flags = filesystem::kFlagWrite | filesystem::kFlagOpen; |
| 95 |
| 96 filesystem::FileError error = filesystem::FILE_ERROR_FAILED; |
| 97 filesystem::FilePtr file_ptr; |
| 98 vfs_->GetDirectory()->OpenFile( |
| 99 mojo::String(db_path().value()), GetProxy(&file_ptr), |
| 100 flags, |
| 101 Capture(&error)); |
| 102 vfs_->GetDirectory().WaitForIncomingResponse(); |
| 103 if (error != filesystem::FILE_ERROR_OK) |
| 104 return; |
| 105 |
| 106 const char* kJunk = "Now is the winter of our discontent."; |
| 107 mojo::Array<uint8_t> data(strlen(kJunk)); |
| 108 memcpy(&data.front(), kJunk, strlen(kJunk)); |
| 109 |
| 110 uint32_t num_bytes_written = 0; |
| 111 file_ptr->Write(data.Pass(), 0, filesystem::WHENCE_FROM_BEGIN, |
| 112 Capture(&error, &num_bytes_written)); |
| 113 file_ptr.WaitForIncomingResponse(); |
| 114 } |
| 115 |
| 116 void SQLTestBase::TruncateDatabase() { |
| 117 filesystem::FileError error = filesystem::FILE_ERROR_FAILED; |
| 118 filesystem::FilePtr file_ptr; |
| 119 vfs_->GetDirectory()->OpenFile( |
| 120 mojo::String(db_path().value()), GetProxy(&file_ptr), |
| 121 filesystem::kFlagWrite | filesystem::kFlagOpen, |
| 122 Capture(&error)); |
| 123 vfs_->GetDirectory().WaitForIncomingResponse(); |
| 124 if (error != filesystem::FILE_ERROR_OK) |
| 125 return; |
| 126 |
| 127 file_ptr->Truncate(0, Capture(&error)); |
| 128 file_ptr.WaitForIncomingResponse(); |
| 129 ASSERT_EQ(filesystem::FILE_ERROR_OK, error); |
| 130 } |
| 131 |
| 132 void SQLTestBase::SetUp() { |
| 133 ApplicationTestBase::SetUp(); |
| 134 |
| 135 mojo::URLRequestPtr request(mojo::URLRequest::New()); |
| 136 request->url = mojo::String::From("mojo:filesystem"); |
| 137 application_impl()->ConnectToService(request.Pass(), &files_); |
| 138 |
| 139 filesystem::FileError error = filesystem::FILE_ERROR_FAILED; |
| 140 filesystem::DirectoryPtr directory; |
| 141 files()->OpenFileSystem("temp", GetProxy(&directory), Capture(&error)); |
| 142 ASSERT_TRUE(files().WaitForIncomingResponse()); |
| 143 ASSERT_EQ(filesystem::FILE_ERROR_OK, error); |
| 144 |
| 145 vfs_.reset(new ScopedMojoFilesystemVFS(directory.Pass())); |
| 146 ASSERT_TRUE(db_.Open(db_path())); |
| 147 } |
| 148 |
| 149 void SQLTestBase::TearDown() { |
| 150 db_.Close(); |
| 151 vfs_.reset(); |
| 152 |
| 153 ApplicationTestBase::TearDown(); |
| 154 } |
| 155 |
| 156 } // namespace sql |
OLD | NEW |