/proc/self/cwd/test/mocks/filesystem/mocks.h
Line | Count | Source (jump to first uncovered line) |
1 | | #pragma once |
2 | | |
3 | | #include <cstdint> |
4 | | #include <string> |
5 | | |
6 | | #include "envoy/filesystem/filesystem.h" |
7 | | #include "envoy/filesystem/watcher.h" |
8 | | |
9 | | #include "source/common/common/thread.h" |
10 | | |
11 | | #include "gmock/gmock.h" |
12 | | |
13 | | namespace Envoy { |
14 | | namespace Filesystem { |
15 | | |
16 | | class MockFile : public File { |
17 | | public: |
18 | | MockFile(); |
19 | | ~MockFile() override; |
20 | | |
21 | | // Filesystem::File |
22 | | Api::IoCallBoolResult open(FlagSet flag) override; |
23 | | Api::IoCallSizeResult write(absl::string_view buffer) override; |
24 | | Api::IoCallBoolResult close() override; |
25 | | Api::IoCallSizeResult pread(void* buf, uint64_t count, uint64_t offset) override; |
26 | | Api::IoCallSizeResult pwrite(const void* buf, uint64_t count, uint64_t offset) override; |
27 | 0 | bool isOpen() const override { return is_open_; }; |
28 | | MOCK_METHOD(std::string, path, (), (const)); |
29 | | MOCK_METHOD(DestinationType, destinationType, (), (const)); |
30 | | MOCK_METHOD(Api::IoCallResult<FileInfo>, info, ()); |
31 | | |
32 | | // The first parameter here must be `const FlagSet&` otherwise it doesn't compile with libstdc++ |
33 | | MOCK_METHOD(Api::IoCallBoolResult, open_, (const FlagSet& flag)); |
34 | | MOCK_METHOD(Api::IoCallSizeResult, write_, (absl::string_view buffer)); |
35 | | MOCK_METHOD(Api::IoCallBoolResult, close_, ()); |
36 | | MOCK_METHOD(Api::IoCallSizeResult, pread_, (void* buf, uint64_t count, uint64_t offset)); |
37 | | MOCK_METHOD(Api::IoCallSizeResult, pwrite_, (const void* buf, uint64_t count, uint64_t offset)); |
38 | | |
39 | | absl::Mutex mutex_; |
40 | | uint64_t num_opens_{0}; |
41 | | uint64_t num_writes_{0}; |
42 | | uint64_t num_preads_{0}; |
43 | | uint64_t num_pwrites_{0}; |
44 | | |
45 | 0 | bool waitForEventCount(uint64_t& event_counter, uint64_t expected) { |
46 | 0 | auto func = [&]() { return event_counter == expected; }; |
47 | 0 | bool result = mutex_.LockWhenWithTimeout(absl::Condition(&func), absl::Seconds(15)); |
48 | 0 | mutex_.Unlock(); |
49 | 0 | return result; |
50 | 0 | } |
51 | | |
52 | | private: |
53 | | bool is_open_{false}; |
54 | | }; |
55 | | |
56 | | class MockInstance : public Instance { |
57 | | public: |
58 | | MockInstance(); |
59 | | ~MockInstance() override; |
60 | | |
61 | | // Filesystem::Instance |
62 | | MOCK_METHOD(FilePtr, createFile, (const FilePathAndType&)); |
63 | | MOCK_METHOD(bool, fileExists, (const std::string&)); |
64 | | MOCK_METHOD(bool, directoryExists, (const std::string&)); |
65 | | MOCK_METHOD(ssize_t, fileSize, (const std::string&)); |
66 | | MOCK_METHOD(absl::StatusOr<std::string>, fileReadToEnd, (const std::string&)); |
67 | | MOCK_METHOD(absl::StatusOr<PathSplitResult>, splitPathFromFilename, (absl::string_view)); |
68 | | MOCK_METHOD(bool, illegalPath, (const std::string&)); |
69 | | MOCK_METHOD(Api::IoCallResult<FileInfo>, stat, (absl::string_view)); |
70 | | MOCK_METHOD(Api::IoCallBoolResult, createPath, (absl::string_view)); |
71 | | }; |
72 | | |
73 | | class MockWatcher : public Watcher { |
74 | | public: |
75 | | MockWatcher(); |
76 | | ~MockWatcher() override; |
77 | | |
78 | | MOCK_METHOD(absl::Status, addWatch, (absl::string_view, uint32_t, OnChangedCb)); |
79 | | }; |
80 | | |
81 | | } // namespace Filesystem |
82 | | } // namespace Envoy |