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() {
19 SQLTestBase::~SQLTestBase() {
22 base::FilePath
SQLTestBase::db_path() {
23 return base::FilePath(FILE_PATH_LITERAL("SQLTest.db"));
26 sql::Connection
& SQLTestBase::db() {
30 bool SQLTestBase::Reopen() {
32 return db_
.Open(db_path());
35 bool SQLTestBase::GetPathExists(const base::FilePath
& path
) {
36 filesystem::FileError error
= filesystem::FILE_ERROR_FAILED
;
38 vfs_
->GetDirectory()->Exists(path
.AsUTF8Unsafe(), Capture(&error
, &exists
));
39 vfs_
->GetDirectory().WaitForIncomingResponse();
40 if (error
!= filesystem::FILE_ERROR_OK
)
45 bool SQLTestBase::CorruptSizeInHeaderOfDB() {
46 // See http://www.sqlite.org/fileformat.html#database_header
47 const size_t kHeaderSize
= 100;
49 mojo::Array
<uint8_t> header
;
51 filesystem::FileError error
= filesystem::FILE_ERROR_FAILED
;
52 filesystem::FilePtr file_ptr
;
53 vfs_
->GetDirectory()->OpenFile(
54 mojo::String(db_path().AsUTF8Unsafe()), GetProxy(&file_ptr
),
55 filesystem::kFlagRead
| filesystem::kFlagWrite
|
56 filesystem::kFlagOpenAlways
,
58 vfs_
->GetDirectory().WaitForIncomingResponse();
59 if (error
!= filesystem::FILE_ERROR_OK
)
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
)
68 filesystem::FileInformationPtr info
;
69 file_ptr
->Stat(Capture(&error
, &info
));
70 file_ptr
.WaitForIncomingResponse();
71 if (error
!= filesystem::FILE_ERROR_OK
)
73 int64_t db_size
= info
->size
;
75 test::CorruptSizeInHeaderMemory(&header
.front(), db_size
);
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
)
83 if (num_bytes_written
!= kHeaderSize
)
89 void SQLTestBase::WriteJunkToDatabase(WriteJunkType type
) {
91 if (type
== TYPE_OVERWRITE_AND_TRUNCATE
)
92 flags
= filesystem::kFlagWrite
| filesystem::kFlagCreate
;
94 flags
= filesystem::kFlagWrite
| filesystem::kFlagOpen
;
96 filesystem::FileError error
= filesystem::FILE_ERROR_FAILED
;
97 filesystem::FilePtr file_ptr
;
98 vfs_
->GetDirectory()->OpenFile(
99 mojo::String(db_path().AsUTF8Unsafe()), GetProxy(&file_ptr
),
102 vfs_
->GetDirectory().WaitForIncomingResponse();
103 if (error
!= filesystem::FILE_ERROR_OK
)
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
));
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();
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().AsUTF8Unsafe()), GetProxy(&file_ptr
),
121 filesystem::kFlagWrite
| filesystem::kFlagOpen
,
123 vfs_
->GetDirectory().WaitForIncomingResponse();
124 if (error
!= filesystem::FILE_ERROR_OK
)
127 file_ptr
->Truncate(0, Capture(&error
));
128 file_ptr
.WaitForIncomingResponse();
129 ASSERT_EQ(filesystem::FILE_ERROR_OK
, error
);
132 void SQLTestBase::SetUp() {
133 ApplicationTestBase::SetUp();
135 mojo::URLRequestPtr
request(mojo::URLRequest::New());
136 request
->url
= mojo::String::From("mojo:filesystem");
137 application_impl()->ConnectToService(request
.Pass(), &files_
);
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
);
145 vfs_
.reset(new ScopedMojoFilesystemVFS(directory
.Pass()));
146 ASSERT_TRUE(db_
.Open(db_path()));
149 void SQLTestBase::TearDown() {
153 ApplicationTestBase::TearDown();