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

Side by Side Diff: sql/test/test_helpers.cc

Issue 196073002: Move ScopedFILE to base namespace and scoped_file.h (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 9 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 | Annotate | Revision Log
« no previous file with comments | « sql/connection_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "sql/test/test_helpers.h" 5 #include "sql/test/test_helpers.h"
6 6
7 #include <string> 7 #include <string>
8 8
9 #include "base/file_util.h" 9 #include "base/file_util.h"
10 #include "base/files/scoped_file.h"
10 #include "sql/connection.h" 11 #include "sql/connection.h"
11 #include "sql/statement.h" 12 #include "sql/statement.h"
12 #include "testing/gtest/include/gtest/gtest.h" 13 #include "testing/gtest/include/gtest/gtest.h"
13 14
14 namespace { 15 namespace {
15 16
16 size_t CountSQLItemsOfType(sql::Connection* db, const char* type) { 17 size_t CountSQLItemsOfType(sql::Connection* db, const char* type) {
17 const char kTypeSQL[] = "SELECT COUNT(*) FROM sqlite_master WHERE type = ?"; 18 const char kTypeSQL[] = "SELECT COUNT(*) FROM sqlite_master WHERE type = ?";
18 sql::Statement s(db->GetUniqueStatement(kTypeSQL)); 19 sql::Statement s(db->GetUniqueStatement(kTypeSQL));
19 s.BindCString(0, type); 20 s.BindCString(0, type);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
68 bool CorruptSizeInHeader(const base::FilePath& db_path) { 69 bool CorruptSizeInHeader(const base::FilePath& db_path) {
69 // See http://www.sqlite.org/fileformat.html#database_header 70 // See http://www.sqlite.org/fileformat.html#database_header
70 const size_t kHeaderSize = 100; 71 const size_t kHeaderSize = 100;
71 const size_t kPageSizeOffset = 16; 72 const size_t kPageSizeOffset = 16;
72 const size_t kFileChangeCountOffset = 24; 73 const size_t kFileChangeCountOffset = 24;
73 const size_t kPageCountOffset = 28; 74 const size_t kPageCountOffset = 28;
74 const size_t kVersionValidForOffset = 92; // duplicate kFileChangeCountOffset 75 const size_t kVersionValidForOffset = 92; // duplicate kFileChangeCountOffset
75 76
76 unsigned char header[kHeaderSize]; 77 unsigned char header[kHeaderSize];
77 78
78 file_util::ScopedFILE file(base::OpenFile(db_path, "rb+")); 79 base::ScopedFILE file(base::OpenFile(db_path, "rb+"));
79 if (!file.get()) 80 if (!file.get())
80 return false; 81 return false;
81 82
82 if (0 != fseek(file.get(), 0, SEEK_SET)) 83 if (0 != fseek(file.get(), 0, SEEK_SET))
83 return false; 84 return false;
84 if (1u != fread(header, sizeof(header), 1, file.get())) 85 if (1u != fread(header, sizeof(header), 1, file.get()))
85 return false; 86 return false;
86 87
87 int64 db_size = 0; 88 int64 db_size = 0;
88 if (!base::GetFileSize(db_path, &db_size)) 89 if (!base::GetFileSize(db_path, &db_size))
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
121 122
122 int page_number = 0; 123 int page_number = 0;
123 if (!GetRootPage(&db, tree_name, &page_number)) 124 if (!GetRootPage(&db, tree_name, &page_number))
124 return false; 125 return false;
125 126
126 // SQLite uses 1-based page numbering. 127 // SQLite uses 1-based page numbering.
127 const long int page_ofs = (page_number - 1) * page_size; 128 const long int page_ofs = (page_number - 1) * page_size;
128 scoped_ptr<char[]> page_buf(new char[page_size]); 129 scoped_ptr<char[]> page_buf(new char[page_size]);
129 130
130 // Get the page into page_buf. 131 // Get the page into page_buf.
131 file_util::ScopedFILE file(base::OpenFile(db_path, "rb+")); 132 base::ScopedFILE file(base::OpenFile(db_path, "rb+"));
132 if (!file.get()) 133 if (!file.get())
133 return false; 134 return false;
134 if (0 != fseek(file.get(), page_ofs, SEEK_SET)) 135 if (0 != fseek(file.get(), page_ofs, SEEK_SET))
135 return false; 136 return false;
136 if (1u != fread(page_buf.get(), page_size, 1, file.get())) 137 if (1u != fread(page_buf.get(), page_size, 1, file.get()))
137 return false; 138 return false;
138 139
139 // Require the page to be a leaf node. A multilevel tree would be 140 // Require the page to be a leaf node. A multilevel tree would be
140 // very hard to restore correctly. 141 // very hard to restore correctly.
141 if (page_buf[0] != 0xD && page_buf[0] != 0xA) 142 if (page_buf[0] != 0xD && page_buf[0] != 0xA)
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after
241 sql::Statement statement(db->GetUniqueStatement("PRAGMA integrity_check")); 242 sql::Statement statement(db->GetUniqueStatement("PRAGMA integrity_check"));
242 243
243 // SQLite should always return a row of data. 244 // SQLite should always return a row of data.
244 EXPECT_TRUE(statement.Step()); 245 EXPECT_TRUE(statement.Step());
245 246
246 return statement.ColumnString(0); 247 return statement.ColumnString(0);
247 } 248 }
248 249
249 } // namespace test 250 } // namespace test
250 } // namespace sql 251 } // namespace sql
OLDNEW
« no previous file with comments | « sql/connection_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698