1/*
2 *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3 *
4 *  Use of this source code is governed by a BSD-style license
5 *  that can be found in the LICENSE file in the root of the source
6 *  tree. An additional intellectual property rights grant can be found
7 *  in the file PATENTS.  All contributing project authors may
8 *  be found in the AUTHORS file in the root of the source tree.
9 */
10
11#ifndef WEBRTC_BASE_FILEUTILS_MOCK_H_
12#define WEBRTC_BASE_FILEUTILS_MOCK_H_
13
14#include <string>
15#include <utility>
16#include <vector>
17
18#include "webrtc/base/fileutils.h"
19#include "webrtc/base/gunit.h"
20#include "webrtc/base/pathutils.h"
21#include "webrtc/base/stream.h"
22
23namespace rtc {
24
25class FakeFileStream : public FileStream {
26  public:
27    explicit FakeFileStream(const std::string & contents) :
28      string_stream_(contents)
29    {}
30
31    virtual StreamResult Read(void* buffer, size_t buffer_len,
32                              size_t* read, int* error) {
33      return string_stream_.Read(buffer, buffer_len, read, error);
34    }
35
36    virtual void Close() {
37      return string_stream_.Close();
38    }
39    virtual bool GetSize(size_t* size) const {
40      return string_stream_.GetSize(size);
41    }
42
43  private:
44    StringStream string_stream_;
45};
46
47class FakeDirectoryIterator : public DirectoryIterator {
48  public:
49    typedef std::pair<std::string, std::string> File;
50
51    /*
52     * files should be sorted by directory
53     * put '/' at the end of file if you want it to be a directory
54     *
55     * Sample list:
56     *  /var/dir/file1
57     *  /var/dir/file2
58     *  /var/dir/subdir1/
59     *  /var/dir/subdir2/
60     *  /var/dir2/file2
61     *  /var/dir3/
62     *
63     *  you can call Iterate for any path: /var, /var/dir, /var/dir2
64     *  unrelated files will be ignored
65     */
66    explicit FakeDirectoryIterator(const std::vector<File>& all_files) :
67      all_files_(all_files) {}
68
69    virtual bool Iterate(const Pathname& path) {
70      path_iterator_ = all_files_.begin();
71      path_ = path.pathname();
72
73      // make sure path ends end with '/'
74      if (path_.rfind(Pathname::DefaultFolderDelimiter()) != path_.size() - 1)
75        path_ += Pathname::DefaultFolderDelimiter();
76
77      return  FakeDirectoryIterator::Search(std::string(""));
78    }
79
80    virtual bool Next() {
81      std::string current_name = Name();
82      path_iterator_++;
83      return FakeDirectoryIterator::Search(current_name);
84    }
85
86    bool Search(const std::string& current_name) {
87      for (; path_iterator_ != all_files_.end(); path_iterator_++) {
88        if (path_iterator_->first.find(path_) == 0
89            && Name().compare(current_name) != 0) {
90          return true;
91        }
92      }
93
94      return false;
95    }
96
97    virtual bool IsDirectory() const {
98      std::string sub_path = path_iterator_->first;
99
100      return std::string::npos !=
101        sub_path.find(Pathname::DefaultFolderDelimiter(), path_.size());
102    }
103
104    virtual std::string Name() const {
105      std::string sub_path = path_iterator_->first;
106
107      // path     - top level path  (ex. /var/lib)
108      // sub_path - subpath under top level path (ex. /var/lib/dir/dir/file )
109      // find shortest non-trivial common path. (ex. /var/lib/dir)
110      size_t start  = path_.size();
111      size_t end    = sub_path.find(Pathname::DefaultFolderDelimiter(), start);
112
113      if (end != std::string::npos) {
114        return sub_path.substr(start, end - start);
115      } else {
116        return sub_path.substr(start);
117      }
118    }
119
120  private:
121    const std::vector<File> all_files_;
122
123    std::string path_;
124    std::vector<File>::const_iterator path_iterator_;
125};
126
127class FakeFileSystem : public FilesystemInterface {
128  public:
129    typedef std::pair<std::string, std::string> File;
130
131    explicit FakeFileSystem(const std::vector<File>& all_files) :
132     all_files_(all_files) {}
133
134    virtual DirectoryIterator *IterateDirectory() {
135     return new FakeDirectoryIterator(all_files_);
136    }
137
138    virtual FileStream * OpenFile(
139       const Pathname &filename,
140       const std::string &mode) {
141     std::vector<File>::const_iterator i_files = all_files_.begin();
142     std::string path = filename.pathname();
143
144     for (; i_files != all_files_.end(); i_files++) {
145       if (i_files->first.compare(path) == 0) {
146         return new FakeFileStream(i_files->second);
147       }
148     }
149
150     return NULL;
151    }
152
153    bool CreatePrivateFile(const Pathname &filename) {
154      EXPECT_TRUE(false) << "Unsupported operation";
155      return false;
156    }
157    bool DeleteFile(const Pathname &filename) {
158      EXPECT_TRUE(false) << "Unsupported operation";
159      return false;
160    }
161    bool DeleteEmptyFolder(const Pathname &folder) {
162      EXPECT_TRUE(false) << "Unsupported operation";
163      return false;
164    }
165    bool DeleteFolderContents(const Pathname &folder) {
166      EXPECT_TRUE(false) << "Unsupported operation";
167      return false;
168    }
169    bool DeleteFolderAndContents(const Pathname &folder) {
170      EXPECT_TRUE(false) << "Unsupported operation";
171      return false;
172    }
173    bool CreateFolder(const Pathname &pathname) {
174      EXPECT_TRUE(false) << "Unsupported operation";
175      return false;
176    }
177    bool MoveFolder(const Pathname &old_path, const Pathname &new_path) {
178      EXPECT_TRUE(false) << "Unsupported operation";
179      return false;
180    }
181    bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
182      EXPECT_TRUE(false) << "Unsupported operation";
183      return false;
184    }
185    bool CopyFile(const Pathname &old_path, const Pathname &new_path) {
186      EXPECT_TRUE(false) << "Unsupported operation";
187      return false;
188    }
189    bool IsFolder(const Pathname &pathname) {
190      EXPECT_TRUE(false) << "Unsupported operation";
191      return false;
192    }
193    bool IsFile(const Pathname &pathname) {
194      EXPECT_TRUE(false) << "Unsupported operation";
195      return false;
196    }
197    bool IsAbsent(const Pathname &pathname) {
198      EXPECT_TRUE(false) << "Unsupported operation";
199      return false;
200    }
201    bool IsTemporaryPath(const Pathname &pathname) {
202      EXPECT_TRUE(false) << "Unsupported operation";
203      return false;
204    }
205    bool GetTemporaryFolder(Pathname &path, bool create,
206                            const std::string *append) {
207      EXPECT_TRUE(false) << "Unsupported operation";
208      return false;
209    }
210    std::string TempFilename(const Pathname &dir, const std::string &prefix) {
211      EXPECT_TRUE(false) << "Unsupported operation";
212      return std::string();
213    }
214    bool GetFileSize(const Pathname &path, size_t *size) {
215      EXPECT_TRUE(false) << "Unsupported operation";
216      return false;
217    }
218    bool GetFileTime(const Pathname &path, FileTimeType which,
219                     time_t* time) {
220      EXPECT_TRUE(false) << "Unsupported operation";
221      return false;
222    }
223    bool GetAppPathname(Pathname *path) {
224      EXPECT_TRUE(false) << "Unsupported operation";
225      return false;
226    }
227    bool GetAppDataFolder(Pathname *path, bool per_user) {
228      EXPECT_TRUE(per_user) << "Unsupported operation";
229#if defined(WEBRTC_WIN)
230      path->SetPathname("c:\\Users\\test_user", "");
231#else
232      path->SetPathname("/home/user/test_user", "");
233#endif
234      return true;
235    }
236    bool GetAppTempFolder(Pathname *path) {
237      EXPECT_TRUE(false) << "Unsupported operation";
238      return false;
239    }
240    bool GetDiskFreeSpace(const Pathname &path, int64 *freebytes) {
241      EXPECT_TRUE(false) << "Unsupported operation";
242      return false;
243    }
244    Pathname GetCurrentDirectory() {
245      return Pathname();
246    }
247
248  private:
249    const std::vector<File> all_files_;
250};
251}  // namespace rtc
252
253#endif  // WEBRTC_BASE_FILEUTILS_MOCK_H_
254