TYPE3
[iec.git] / src / type3_AndroidCloud / anbox-master / external / cpu_features / test / filesystem_for_testing.cc
1 // 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 #include "filesystem_for_testing.h"
16
17 #include <cassert>
18 #include <climits>
19 #include <cstdio>
20 #include <cstring>
21 #include <utility>
22
23 namespace cpu_features {
24
25 FakeFile::FakeFile(int file_descriptor, const char* content)
26     : file_descriptor_(file_descriptor), content_(content) {}
27
28 FakeFile::~FakeFile() { assert(!opened_); }
29
30 void FakeFile::Open() {
31   assert(!opened_);
32   opened_ = true;
33 }
34
35 void FakeFile::Close() {
36   assert(opened_);
37   opened_ = false;
38 }
39
40 int FakeFile::Read(int fd, void* buf, size_t count) {
41   assert(count < INT_MAX);
42   assert(fd == file_descriptor_);
43   const size_t remainder = content_.size() - head_index_;
44   const size_t read = count > remainder ? remainder : count;
45   memcpy(buf, content_.data() + head_index_, read);
46   head_index_ += read;
47   assert(read < INT_MAX);
48   return read;
49 }
50
51 void FakeFilesystem::Reset() { files_.clear(); }
52
53 FakeFile* FakeFilesystem::CreateFile(const std::string& filename,
54                                      const char* content) {
55   auto& file = files_[filename];
56   file =
57       std::unique_ptr<FakeFile>(new FakeFile(next_file_descriptor_++, content));
58   return file.get();
59 }
60
61 FakeFile* FakeFilesystem::FindFileOrNull(const std::string& filename) const {
62   const auto itr = files_.find(filename);
63   return itr == files_.end() ? nullptr : itr->second.get();
64 }
65
66 FakeFile* FakeFilesystem::FindFileOrDie(const int file_descriptor) const {
67   for (const auto& filename_file_pair : files_) {
68     FakeFile* const file_ptr = filename_file_pair.second.get();
69     if (file_ptr->GetFileDescriptor() == file_descriptor) {
70       return file_ptr;
71     }
72   }
73   assert(false);
74   return nullptr;
75 }
76
77 static FakeFilesystem* kFilesystem = new FakeFilesystem();
78
79 FakeFilesystem& GetEmptyFilesystem() {
80   kFilesystem->Reset();
81   return *kFilesystem;
82 }
83
84 extern "C" int CpuFeatures_OpenFile(const char* filename) {
85   auto* const file = kFilesystem->FindFileOrNull(filename);
86   if (file) {
87     file->Open();
88     return file->GetFileDescriptor();
89   }
90   return -1;
91 }
92
93 extern "C" void CpuFeatures_CloseFile(int file_descriptor) {
94   kFilesystem->FindFileOrDie(file_descriptor)->Close();
95 }
96
97 extern "C" int CpuFeatures_ReadFile(int file_descriptor, void* buffer,
98                                     size_t buffer_size) {
99   return kFilesystem->FindFileOrDie(file_descriptor)
100       ->Read(file_descriptor, buffer, buffer_size);
101 }
102
103 }  // namespace cpu_features