aboutsummaryrefslogtreecommitdiff
path: root/webrtc/base/fileutils_mock.h
blob: 428d444e1242a7d56d2d6e5d17583a181acd383f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/*
 *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
 *
 *  Use of this source code is governed by a BSD-style license
 *  that can be found in the LICENSE file in the root of the source
 *  tree. An additional intellectual property rights grant can be found
 *  in the file PATENTS.  All contributing project authors may
 *  be found in the AUTHORS file in the root of the source tree.
 */

#ifndef WEBRTC_BASE_FILEUTILS_MOCK_H_
#define WEBRTC_BASE_FILEUTILS_MOCK_H_

#include <string>
#include <utility>
#include <vector>

#include "webrtc/base/fileutils.h"
#include "webrtc/base/gunit.h"
#include "webrtc/base/pathutils.h"
#include "webrtc/base/stream.h"

namespace rtc {

class FakeFileStream : public FileStream {
  public:
    explicit FakeFileStream(const std::string & contents) :
      string_stream_(contents)
    {}

    virtual StreamResult Read(void* buffer, size_t buffer_len,
                              size_t* read, int* error) {
      return string_stream_.Read(buffer, buffer_len, read, error);
    }

    virtual void Close() {
      return string_stream_.Close();
    }
    virtual bool GetSize(size_t* size) const {
      return string_stream_.GetSize(size);
    }

  private:
    StringStream string_stream_;
};

class FakeDirectoryIterator : public DirectoryIterator {
  public:
    typedef std::pair<std::string, std::string> File;

    /*
     * files should be sorted by directory
     * put '/' at the end of file if you want it to be a directory
     *
     * Sample list:
     *  /var/dir/file1
     *  /var/dir/file2
     *  /var/dir/subdir1/
     *  /var/dir/subdir2/
     *  /var/dir2/file2
     *  /var/dir3/
     *
     *  you can call Iterate for any path: /var, /var/dir, /var/dir2
     *  unrelated files will be ignored
     */
    explicit FakeDirectoryIterator(const std::vector<File>& all_files) :
      all_files_(all_files) {}

    virtual bool Iterate(const Pathname& path) {
      path_iterator_ = all_files_.begin();
      path_ = path.pathname();

      // make sure path ends end with '/'
      if (path_.rfind(Pathname::DefaultFolderDelimiter()) != path_.size() - 1)
        path_ += Pathname::DefaultFolderDelimiter();

      return  FakeDirectoryIterator::Search(std::string(""));
    }

    virtual bool Next() {
      std::string current_name = Name();
      path_iterator_++;
      return FakeDirectoryIterator::Search(current_name);
    }

    bool Search(const std::string& current_name) {
      for (; path_iterator_ != all_files_.end(); path_iterator_++) {
        if (path_iterator_->first.find(path_) == 0
            && Name().compare(current_name) != 0) {
          return true;
        }
      }

      return false;
    }

    virtual bool IsDirectory() const {
      std::string sub_path = path_iterator_->first;

      return std::string::npos !=
        sub_path.find(Pathname::DefaultFolderDelimiter(), path_.size());
    }

    virtual std::string Name() const {
      std::string sub_path = path_iterator_->first;

      // path     - top level path  (ex. /var/lib)
      // sub_path - subpath under top level path (ex. /var/lib/dir/dir/file )
      // find shortest non-trivial common path. (ex. /var/lib/dir)
      size_t start  = path_.size();
      size_t end    = sub_path.find(Pathname::DefaultFolderDelimiter(), start);

      if (end != std::string::npos) {
        return sub_path.substr(start, end - start);
      } else {
        return sub_path.substr(start);
      }
    }

  private:
    const std::vector<File> all_files_;

    std::string path_;
    std::vector<File>::const_iterator path_iterator_;
};

class FakeFileSystem : public FilesystemInterface {
  public:
    typedef std::pair<std::string, std::string> File;

    explicit FakeFileSystem(const std::vector<File>& all_files) :
     all_files_(all_files) {}

    virtual DirectoryIterator *IterateDirectory() {
     return new FakeDirectoryIterator(all_files_);
    }

    virtual FileStream * OpenFile(
       const Pathname &filename,
       const std::string &mode) {
     std::vector<File>::const_iterator i_files = all_files_.begin();
     std::string path = filename.pathname();

     for (; i_files != all_files_.end(); i_files++) {
       if (i_files->first.compare(path) == 0) {
         return new FakeFileStream(i_files->second);
       }
     }

     return NULL;
    }

    bool CreatePrivateFile(const Pathname &filename) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool DeleteFile(const Pathname &filename) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool DeleteEmptyFolder(const Pathname &folder) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool DeleteFolderContents(const Pathname &folder) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool DeleteFolderAndContents(const Pathname &folder) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool CreateFolder(const Pathname &pathname) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool MoveFolder(const Pathname &old_path, const Pathname &new_path) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool CopyFile(const Pathname &old_path, const Pathname &new_path) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool IsFolder(const Pathname &pathname) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool IsFile(const Pathname &pathname) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool IsAbsent(const Pathname &pathname) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool IsTemporaryPath(const Pathname &pathname) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool GetTemporaryFolder(Pathname &path, bool create,
                            const std::string *append) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    std::string TempFilename(const Pathname &dir, const std::string &prefix) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return std::string();
    }
    bool GetFileSize(const Pathname &path, size_t *size) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool GetFileTime(const Pathname &path, FileTimeType which,
                     time_t* time) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool GetAppPathname(Pathname *path) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool GetAppDataFolder(Pathname *path, bool per_user) {
      EXPECT_TRUE(per_user) << "Unsupported operation";
#if defined(WEBRTC_WIN)
      path->SetPathname("c:\\Users\\test_user", "");
#else
      path->SetPathname("/home/user/test_user", "");
#endif
      return true;
    }
    bool GetAppTempFolder(Pathname *path) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    bool GetDiskFreeSpace(const Pathname& path, int64_t* freebytes) {
      EXPECT_TRUE(false) << "Unsupported operation";
      return false;
    }
    Pathname GetCurrentDirectory() {
      return Pathname();
    }

  private:
    const std::vector<File> all_files_;
};
}  // namespace rtc

#endif  // WEBRTC_BASE_FILEUTILS_MOCK_H_