summaryrefslogtreecommitdiffstats
path: root/sql/test/sql_test_base.cc
blob: bdd427fe7c105a0840bf43fb50c4d95573c4e58f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
// 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/test/sql_test_base.h"

#include "base/files/file_util.h"
#include "sql/test/test_helpers.h"

namespace sql {

SQLTestBase::SQLTestBase() {
}

SQLTestBase::~SQLTestBase() {
}

base::FilePath SQLTestBase::db_path() {
  return temp_dir_.path().AppendASCII("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) {
  return base::PathExists(path);
}

bool SQLTestBase::CorruptSizeInHeaderOfDB() {
  return sql::test::CorruptSizeInHeader(db_path());
}

void SQLTestBase::WriteJunkToDatabase(WriteJunkType type) {
  base::ScopedFILE file(base::OpenFile(
      db_path(),
      type == TYPE_OVERWRITE_AND_TRUNCATE ? "wb" : "rb+"));
  ASSERT_TRUE(file.get() != NULL);
  ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET));

  const char* kJunk = "Now is the winter of our discontent.";
  fputs(kJunk, file.get());
}

void SQLTestBase::TruncateDatabase() {
  base::ScopedFILE file(base::OpenFile(db_path(), "rb+"));
  ASSERT_TRUE(file.get() != NULL);
  ASSERT_EQ(0, fseek(file.get(), 0, SEEK_SET));
  ASSERT_TRUE(base::TruncateFile(file.get()));
}

void SQLTestBase::SetUp() {
  ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
  ASSERT_TRUE(db_.Open(db_path()));
}

void SQLTestBase::TearDown() {
  db_.Close();
}

}  // namespace sql