xref: /6.6.0/couchstore/tests/test_fileops.cc (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#include "test_fileops.h"
19
20MockOps::MockOps(FileOpsInterface* ops) : wrapped_ops(ops) {
21    DelegateToFake();
22}
23
24MockOps::~MockOps() {
25}
26
27using ::testing::Invoke;
28using ::testing::_;
29
30FileOpsInterface* MockOps::get_wrapped() {
31    return wrapped_ops.get();
32}
33
34void MockOps::DelegateToFake() {
35    ON_CALL(*this, pwrite(_, _, _, _, _))
36            .WillByDefault(Invoke(wrapped_ops.get(), &FileOpsInterface::pwrite));
37    ON_CALL(*this, pread(_, _, _, _, _))
38            .WillByDefault(Invoke(wrapped_ops.get(), &FileOpsInterface::pread));
39    ON_CALL(*this, sync(_, _))
40            .WillByDefault(Invoke(wrapped_ops.get(), &FileOpsInterface::sync));
41    ON_CALL(*this, constructor(_))
42            .WillByDefault(Invoke(wrapped_ops.get(), &FileOpsInterface::constructor));
43    ON_CALL(*this, destructor(_))
44            .WillByDefault(Invoke(wrapped_ops.get(), &FileOpsInterface::destructor));
45    ON_CALL(*this, open(_, _, _, _))
46            .WillByDefault(Invoke(wrapped_ops.get(), &FileOpsInterface::open));
47    ON_CALL(*this, close(_, _))
48            .WillByDefault(Invoke(wrapped_ops.get(), &FileOpsInterface::close));
49    ON_CALL(*this, set_periodic_sync(_, _))
50            .WillByDefault(Invoke(wrapped_ops.get(),
51                                  &FileOpsInterface::set_periodic_sync));
52    ON_CALL(*this, goto_eof(_, _))
53            .WillByDefault(Invoke(wrapped_ops.get(), &FileOpsInterface::goto_eof));
54    ON_CALL(*this, advise(_, _, _, _, _))
55        .WillByDefault(Invoke(wrapped_ops.get(), &FileOpsInterface::advise));
56    ON_CALL(*this, set_tracing_enabled(_))
57            .WillByDefault(Invoke(wrapped_ops.get(),
58                                  &FileOpsInterface::set_tracing_enabled));
59    ON_CALL(*this, set_write_validation_enabled(_))
60            .WillByDefault(
61                    Invoke(wrapped_ops.get(),
62                           &FileOpsInterface::set_write_validation_enabled));
63    ON_CALL(*this, set_mprotect_enabled(_))
64            .WillByDefault(Invoke(wrapped_ops.get(),
65                                  &FileOpsInterface::set_mprotect_enabled));
66}
67
68LogOps::LogOps(FileOpsInterface* ops) : wrapped_ops(ops) {}
69
70LogOps::~LogOps() {}
71
72couch_file_handle LogOps::constructor(couchstore_error_info_t* errinfo) {
73    auto rv = wrapped_ops->constructor(errinfo);
74    std::cerr << "@constructor(" << errinfo << ") -> " << rv << std::endl;
75    return rv;
76}
77
78couchstore_error_t LogOps::open(couchstore_error_info_t* errinfo,
79                                couch_file_handle* handle, const char* path,
80                                int oflag) {
81    auto rv = wrapped_ops->open(errinfo, handle, path, oflag);
82    std::cerr << "@open(" << errinfo << ", " << handle << ", " << path << ", "
83              << oflag << ") -> " << rv << std::endl;
84    return rv;
85}
86
87couchstore_error_t LogOps::close(couchstore_error_info_t* errinfo,
88                                 couch_file_handle handle) {
89    auto rv = wrapped_ops->close(errinfo, handle);
90    std::cerr << "@close(" << errinfo << ", " << handle << ") -> "
91              << rv << std::endl;
92    return rv;
93}
94
95ssize_t LogOps::pread(couchstore_error_info_t* errinfo,
96                      couch_file_handle handle, void* buf, size_t nbytes,
97                      cs_off_t offset) {
98    auto rv = wrapped_ops->pread(errinfo, handle, buf, nbytes, offset);
99    std::cerr << "@pread(" << errinfo << ", " << handle << ", " << buf << ", "
100              << nbytes << ", " << offset << ") -> " << rv << std::endl;
101    return rv;
102}
103
104ssize_t LogOps::pwrite(couchstore_error_info_t* errinfo,
105                       couch_file_handle handle, const void* buf, size_t nbytes,
106                       cs_off_t offset) {
107    auto rv = wrapped_ops->pwrite(errinfo, handle, buf, nbytes, offset);
108    std::cerr << "@pwrite(" << errinfo << ", " << handle << ", " << buf << ", "
109              << nbytes << ", " << offset << ") -> " << rv << std::endl;
110    return rv;
111}
112
113cs_off_t LogOps::goto_eof(couchstore_error_info_t* errinfo,
114                          couch_file_handle handle) {
115    auto rv = wrapped_ops->goto_eof(errinfo, handle);
116    std::cerr << "@goto_eof(" << errinfo << ", " << handle << ") -> "
117              << rv << std::endl;
118    return rv;
119}
120
121couchstore_error_t LogOps::sync(couchstore_error_info_t* errinfo,
122                                couch_file_handle handle) {
123    auto rv = wrapped_ops->sync(errinfo, handle);
124    std::cerr << "@sync(" << errinfo << ", " << handle << ") -> "
125              << rv << std::endl;
126    return rv;
127}
128
129couchstore_error_t LogOps::advise(couchstore_error_info_t* errinfo,
130                                  couch_file_handle handle, cs_off_t offset,
131                                  cs_off_t len,
132                                  couchstore_file_advice_t advice) {
133    auto rv = wrapped_ops->advise(errinfo, handle, offset, len, advice);
134    std::cerr << "@advise(" << errinfo << ", " << handle << ", " << offset
135              << ", " << len << ", " << advice << ") -> " << rv << std::endl;
136    return rv;
137}
138
139couchstore_error_t LogOps::set_tracing_enabled(couch_file_handle handle) {
140    auto rv = wrapped_ops->set_tracing_enabled(handle);
141    std::cerr << "@set_tracing_enabled(" << handle << ") -> " << rv
142              << std::endl;
143    return rv;
144}
145
146couchstore_error_t LogOps::set_write_validation_enabled(
147        couch_file_handle handle) {
148    auto rv = wrapped_ops->set_write_validation_enabled(handle);
149    std::cerr << "@set_write_validation_enabled(" << handle << ") -> " << rv
150              << std::endl;
151    return rv;
152}
153
154couchstore_error_t LogOps::set_mprotect_enabled(couch_file_handle handle) {
155    auto rv = wrapped_ops->set_mprotect_enabled(handle);
156    std::cerr << "@set_mprotect_enabled(" << handle << ") -> " << rv
157              << std::endl;
158    return rv;
159}
160
161void LogOps::destructor(couch_file_handle handle) {
162    std::cerr << "@destructor(" << handle << ")" << std::endl;
163    wrapped_ops->destructor(handle);
164}
165