summaryrefslogtreecommitdiffstats
path: root/sync/test/directory_backing_store_corruption_testing.cc
blob: 7418465b0015ac757aa08cb106896435ccd3be66 (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
// 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 "sync/test/directory_backing_store_corruption_testing.h"

#include <string>

#include "base/files/file_util.h"
#include "base/files/scoped_file.h"

namespace syncer {
namespace syncable {
namespace corruption_testing {

// This value needs to be large enough to force the underlying DB to be read
// from disk before writing, but small enough so that tests don't take too long
// and timeout. The value depend on the underlying DB page size as well as the
// DB's cache_size PRAGMA. If test fails, you either increase
// kNumEntriesRequiredForCorruption, or increase the size of each entry.
const int kNumEntriesRequiredForCorruption = 2000;

bool CorruptDatabase(const base::FilePath& backing_file_path) {
  // Corrupt the DB by write a bunch of zeros at the beginning.
  //
  // Because the file may already open for writing, it's important that we open
  // it in a sharing-compatible way for platforms that have the concept of
  // shared/exclusive file access (e.g. Windows).
  base::ScopedFILE db_file(base::OpenFile(backing_file_path, "wb"));
  if (!db_file.get())
    return false;
  const std::string zeros(4096, '\0');
  const int num_written = fwrite(zeros.data(), zeros.size(), 1, db_file.get());
  return num_written == 1U;
}

}  // namespace corruption_testing
}  // namespace syncable
}  // namespace syncer