xref: /6.0.3/couchstore/tests/test_fileops.h (revision c2c458ff)
1/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2/*
3 *     Copyright 2016 Couchbase, Inc
4 *
5 *   Licensed under the Apache License, Version 2.0 (the "License");
6 *   you may not use this file except in compliance with the License.
7 *   You may obtain a copy of the License at
8 *
9 *       http://www.apache.org/licenses/LICENSE-2.0
10 *
11 *   Unless required by applicable law or agreed to in writing, software
12 *   distributed under the License is distributed on an "AS IS" BASIS,
13 *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *   See the License for the specific language governing permissions and
15 *   limitations under the License.
16 */
17
18#pragma once
19
20#include "config.h"
21#include <libcouchstore/couch_db.h>
22#include <map>
23#include <memory>
24#include <string>
25#include <vector>
26
27#include "gmock/gmock.h"
28
29/**
30 * This is a 'Mock' implementation of a FileOpsInterface. It usually
31 * delegates calls to its methods to the FileOpsInterface instance
32 * `wrapped_ops`.
33 *
34 * It can be driven, if required, to return errors etc. by using the
35 * Google Mock API.
36 */
37class MockOps : public FileOpsInterface {
38public:
39    MockOps(FileOpsInterface* ops);
40    ~MockOps();
41
42    MOCK_METHOD0(version, uint64_t());
43    MOCK_METHOD1(constructor,
44                 couch_file_handle(couchstore_error_info_t* errinfo));
45    MOCK_METHOD4(open, couchstore_error_t(couchstore_error_info_t* errinfo,
46                                          couch_file_handle* handle,
47                                          const char* path, int oflag));
48    MOCK_METHOD2(close, couchstore_error_t(couchstore_error_info_t* errinfo,
49                                           couch_file_handle handle));
50    MOCK_METHOD2(set_periodic_sync,
51                 couchstore_error_t(couch_file_handle handle,
52                                    uint64_t period_bytes));
53    MOCK_METHOD5(pread, ssize_t(couchstore_error_info_t* errinfo,
54                                couch_file_handle handle, void* buf,
55                                size_t nbytes, cs_off_t offset));
56    MOCK_METHOD5(pwrite, ssize_t(couchstore_error_info_t* errinfo,
57                                 couch_file_handle handle, const void* buf,
58                                 size_t nbytes, cs_off_t offset));
59    MOCK_METHOD2(goto_eof, cs_off_t(couchstore_error_info_t* errinfo,
60                                    couch_file_handle handle));
61    MOCK_METHOD2(sync, couchstore_error_t(couchstore_error_info_t* errinfo,
62                                          couch_file_handle handle));
63    MOCK_METHOD5(advise, couchstore_error_t(couchstore_error_info_t* errinfo,
64                                            couch_file_handle handle,
65                                            cs_off_t offset, cs_off_t len,
66                                            couchstore_file_advice_t advice));
67    MOCK_METHOD1(destructor, void(couch_file_handle handle));
68    MOCK_METHOD1(set_tracing_enabled,
69                 couchstore_error_t(couch_file_handle handle));
70    MOCK_METHOD1(set_write_validation_enabled,
71                 couchstore_error_t(couch_file_handle handle));
72    MOCK_METHOD1(set_mprotect_enabled,
73                 couchstore_error_t(couch_file_handle handle));
74
75    void DelegateToFake();
76
77    FileOpsInterface* get_wrapped();
78
79protected:
80    std::unique_ptr<FileOpsInterface> wrapped_ops;
81};
82
83/**
84 * This is a FileOpsInterface implementation that always delegates calls
85 * to the `wrapped_ops` instance. It will also log trace information about
86 * the calls which are being made to stderr.
87 */
88class LogOps : public FileOpsInterface {
89public:
90    LogOps(FileOpsInterface* ops);
91    ~LogOps();
92
93    couch_file_handle constructor(couchstore_error_info_t* errinfo) override ;
94    couchstore_error_t open(couchstore_error_info_t* errinfo,
95                            couch_file_handle* handle, const char* path,
96                            int oflag) override;
97    couchstore_error_t close(couchstore_error_info_t* errinfo,
98                             couch_file_handle handle) override;
99    ssize_t pread(couchstore_error_info_t* errinfo,
100                  couch_file_handle handle, void* buf, size_t nbytes,
101                  cs_off_t offset) override;
102    ssize_t pwrite(couchstore_error_info_t* errinfo,
103                   couch_file_handle handle, const void* buf,
104                   size_t nbytes, cs_off_t offset) override;
105    cs_off_t goto_eof(couchstore_error_info_t* errinfo,
106                      couch_file_handle handle) override;
107    couchstore_error_t sync(couchstore_error_info_t* errinfo,
108                            couch_file_handle handle) override;
109    couchstore_error_t advise(couchstore_error_info_t* errinfo,
110                              couch_file_handle handle, cs_off_t offset,
111                              cs_off_t len,
112                              couchstore_file_advice_t advice) override;
113    couchstore_error_t set_tracing_enabled(couch_file_handle handle) override;
114    couchstore_error_t set_write_validation_enabled(
115            couch_file_handle handle) override;
116    couchstore_error_t set_mprotect_enabled(couch_file_handle handle) override;
117    void destructor(couch_file_handle handle) override;
118protected:
119    std::unique_ptr<FileOpsInterface> wrapped_ops;
120};
121