blob: 7474b5f165827892b5c182d6124386f5848403c7 [file] [log] [blame]
Guillaume Chatelet439d3712018-02-01 10:03:09 +01001// Copyright 2017 Google Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15// Implements a fake filesystem, useful for tests.
Guillaume Chatelet8e58ef02018-02-01 10:38:48 +010016#ifndef CPU_FEATURES_TEST_FILESYSTEM_FOR_TESTING_H_
17#define CPU_FEATURES_TEST_FILESYSTEM_FOR_TESTING_H_
Guillaume Chatelet439d3712018-02-01 10:03:09 +010018
19#include <memory>
20#include <string>
21#include <unordered_map>
22
23#include "internal/filesystem.h"
24
25namespace cpu_features {
26
27class FakeFile {
28 public:
29 explicit FakeFile(int file_descriptor, const char* content);
30 ~FakeFile();
31
32 void Open();
33 void Close();
34 int Read(int fd, void* buf, size_t count);
35
36 int GetFileDescriptor() const { return file_descriptor_; }
37
38 private:
39 const int file_descriptor_;
40 const std::string content_;
41 bool opened_ = false;
42 size_t head_index_ = 0;
43};
44
45class FakeFilesystem {
46 public:
47 void Reset();
48 FakeFile* CreateFile(const std::string& filename, const char* content);
49 FakeFile* FindFileOrDie(const int file_descriptor) const;
50 FakeFile* FindFileOrNull(const std::string& filename) const;
51
52 private:
Leonard Mosescubdb36d92019-07-03 05:57:19 -070053 int next_file_descriptor_ = 0;
Guillaume Chatelet439d3712018-02-01 10:03:09 +010054 std::unordered_map<std::string, std::unique_ptr<FakeFile>> files_;
55};
56
57FakeFilesystem& GetEmptyFilesystem();
58
59} // namespace cpu_features
60
Guillaume Chatelet8e58ef02018-02-01 10:38:48 +010061#endif // CPU_FEATURES_TEST_FILESYSTEM_FOR_TESTING_H_