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.
5 #include "sql/mojo/sql_test_base.h"
7 #include "mojo/application/public/cpp/application_impl.h"
8 #include "mojo/util/capture_util.h"
9 #include "sql/mojo/mojo_vfs.h"
10 #include "sql/test/test_helpers.h"
16 SQLTestBase::SQLTestBase()
20 SQLTestBase::~SQLTestBase() {
23 base::FilePath
SQLTestBase::db_path() {
24 return base::FilePath(FILE_PATH_LITERAL("SQLTest.db"));
27 sql::Connection
& SQLTestBase::db() {
31 bool SQLTestBase::Reopen() {
33 return db_
.Open(db_path());
36 bool SQLTestBase::GetPathExists(const base::FilePath
& path
) {
37 filesystem::FileError error
= filesystem::FILE_ERROR_FAILED
;
39 vfs_
->GetDirectory()->Exists(path
.AsUTF8Unsafe(), Capture(&error
, &exists
));
40 vfs_
->GetDirectory().WaitForIncomingResponse();
41 if (error
!= filesystem::FILE_ERROR_OK
)
46 bool SQLTestBase::CorruptSizeInHeaderOfDB() {
47 // See http://www.sqlite.org/fileformat.html#database_header
48 const size_t kHeaderSize
= 100;
50 mojo::Array
<uint8_t> header
;
52 filesystem::FileError error
= filesystem::FILE_ERROR_FAILED
;
53 filesystem::FilePtr file_ptr
;
54 vfs_
->GetDirectory()->OpenFile(
55 mojo::String(db_path().AsUTF8Unsafe()), GetProxy(&file_ptr
),
56 filesystem::kFlagRead
| filesystem::kFlagWrite
|
57 filesystem::kFlagOpenAlways
,
59 vfs_
->GetDirectory().WaitForIncomingResponse();
60 if (error
!= filesystem::FILE_ERROR_OK
)
63 file_ptr
->Read(kHeaderSize
, 0, filesystem::WHENCE_FROM_BEGIN
,
64 Capture(&error
, &header
));
65 file_ptr
.WaitForIncomingResponse();
66 if (error
!= filesystem::FILE_ERROR_OK
)
69 filesystem::FileInformationPtr info
;
70 file_ptr
->Stat(Capture(&error
, &info
));
71 file_ptr
.WaitForIncomingResponse();
72 if (error
!= filesystem::FILE_ERROR_OK
)
74 int64_t db_size
= info
->size
;
76 test::CorruptSizeInHeaderMemory(&header
.front(), db_size
);
78 uint32_t num_bytes_written
= 0;
79 file_ptr
->Write(header
.Pass(), 0, filesystem::WHENCE_FROM_BEGIN
,
80 Capture(&error
, &num_bytes_written
));
81 file_ptr
.WaitForIncomingResponse();
82 if (error
!= filesystem::FILE_ERROR_OK
)
84 if (num_bytes_written
!= kHeaderSize
)
90 void SQLTestBase::WriteJunkToDatabase(WriteJunkType type
) {
92 if (type
== TYPE_OVERWRITE_AND_TRUNCATE
)
93 flags
= filesystem::kFlagWrite
| filesystem::kFlagCreate
;
95 flags
= filesystem::kFlagWrite
| filesystem::kFlagOpen
;
97 filesystem::FileError error
= filesystem::FILE_ERROR_FAILED
;
98 filesystem::FilePtr file_ptr
;
99 vfs_
->GetDirectory()->OpenFile(
100 mojo::String(db_path().AsUTF8Unsafe()), GetProxy(&file_ptr
),
103 vfs_
->GetDirectory().WaitForIncomingResponse();
104 if (error
!= filesystem::FILE_ERROR_OK
)
107 const char* kJunk
= "Now is the winter of our discontent.";
108 mojo::Array
<uint8_t> data(strlen(kJunk
));
109 memcpy(&data
.front(), kJunk
, strlen(kJunk
));
111 uint32_t num_bytes_written
= 0;
112 file_ptr
->Write(data
.Pass(), 0, filesystem::WHENCE_FROM_BEGIN
,
113 Capture(&error
, &num_bytes_written
));
114 file_ptr
.WaitForIncomingResponse();
117 void SQLTestBase::TruncateDatabase() {
118 filesystem::FileError error
= filesystem::FILE_ERROR_FAILED
;
119 filesystem::FilePtr file_ptr
;
120 vfs_
->GetDirectory()->OpenFile(
121 mojo::String(db_path().AsUTF8Unsafe()), GetProxy(&file_ptr
),
122 filesystem::kFlagWrite
| filesystem::kFlagOpen
,
124 vfs_
->GetDirectory().WaitForIncomingResponse();
125 if (error
!= filesystem::FILE_ERROR_OK
)
128 file_ptr
->Truncate(0, Capture(&error
));
129 file_ptr
.WaitForIncomingResponse();
130 ASSERT_EQ(filesystem::FILE_ERROR_OK
, error
);
133 void SQLTestBase::SetUp() {
134 ApplicationTestBase::SetUp();
136 mojo::URLRequestPtr
request(mojo::URLRequest::New());
137 request
->url
= mojo::String::From("mojo:filesystem");
138 application_impl()->ConnectToService(request
.Pass(), &files_
);
140 filesystem::FileSystemClientPtr client
;
141 binding_
.Bind(GetProxy(&client
));
143 filesystem::FileError error
= filesystem::FILE_ERROR_FAILED
;
144 filesystem::DirectoryPtr directory
;
145 files()->OpenFileSystem("temp", GetProxy(&directory
), client
.Pass(),
147 ASSERT_TRUE(files().WaitForIncomingResponse());
148 ASSERT_EQ(filesystem::FILE_ERROR_OK
, error
);
150 vfs_
.reset(new ScopedMojoFilesystemVFS(directory
.Pass()));
151 ASSERT_TRUE(db_
.Open(db_path()));
154 void SQLTestBase::TearDown() {
158 ApplicationTestBase::TearDown();
161 void SQLTestBase::OnFileSystemShutdown() {