You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

270 lines
8.5 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #ifndef TALK_BASE_FILEUTILS_MOCK_H_
  28. #define TALK_BASE_FILEUTILS_MOCK_H_
  29. #include <string>
  30. #include <utility>
  31. #include <vector>
  32. #include "talk/base/fileutils.h"
  33. #include "talk/base/gunit.h"
  34. #include "talk/base/pathutils.h"
  35. #include "talk/base/stream.h"
  36. namespace talk_base {
  37. class FakeFileStream : public FileStream {
  38. public:
  39. explicit FakeFileStream(const std::string & contents) :
  40. string_stream_(contents)
  41. {}
  42. virtual StreamResult Read(void* buffer, size_t buffer_len,
  43. size_t* read, int* error) {
  44. return string_stream_.Read(buffer, buffer_len, read, error);
  45. }
  46. virtual void Close() {
  47. return string_stream_.Close();
  48. }
  49. virtual bool GetSize(size_t* size) const {
  50. return string_stream_.GetSize(size);
  51. }
  52. private:
  53. StringStream string_stream_;
  54. };
  55. class FakeDirectoryIterator : public DirectoryIterator {
  56. public:
  57. typedef std::pair<std::string, std::string> File;
  58. /*
  59. * files should be sorted by directory
  60. * put '/' at the end of file if you want it to be a directory
  61. *
  62. * Sample list:
  63. * /var/dir/file1
  64. * /var/dir/file2
  65. * /var/dir/subdir1/
  66. * /var/dir/subdir2/
  67. * /var/dir2/file2
  68. * /var/dir3/
  69. *
  70. * you can call Iterate for any path: /var, /var/dir, /var/dir2
  71. * unrelated files will be ignored
  72. */
  73. explicit FakeDirectoryIterator(const std::vector<File>& all_files) :
  74. all_files_(all_files) {}
  75. virtual bool Iterate(const Pathname& path) {
  76. path_iterator_ = all_files_.begin();
  77. path_ = path.pathname();
  78. // make sure path ends end with '/'
  79. if (path_.rfind(Pathname::DefaultFolderDelimiter()) != path_.size() - 1)
  80. path_ += Pathname::DefaultFolderDelimiter();
  81. return FakeDirectoryIterator::Search(std::string(""));
  82. }
  83. virtual bool Next() {
  84. std::string current_name = Name();
  85. path_iterator_++;
  86. return FakeDirectoryIterator::Search(current_name);
  87. }
  88. bool Search(const std::string& current_name) {
  89. for (; path_iterator_ != all_files_.end(); path_iterator_++) {
  90. if (path_iterator_->first.find(path_) == 0
  91. && Name().compare(current_name) != 0) {
  92. return true;
  93. }
  94. }
  95. return false;
  96. }
  97. virtual bool IsDirectory() const {
  98. std::string sub_path = path_iterator_->first;
  99. return std::string::npos !=
  100. sub_path.find(Pathname::DefaultFolderDelimiter(), path_.size());
  101. }
  102. virtual std::string Name() const {
  103. std::string sub_path = path_iterator_->first;
  104. // path - top level path (ex. /var/lib)
  105. // sub_path - subpath under top level path (ex. /var/lib/dir/dir/file )
  106. // find shortest non-trivial common path. (ex. /var/lib/dir)
  107. size_t start = path_.size();
  108. size_t end = sub_path.find(Pathname::DefaultFolderDelimiter(), start);
  109. if (end != std::string::npos) {
  110. return sub_path.substr(start, end - start);
  111. } else {
  112. return sub_path.substr(start);
  113. }
  114. }
  115. private:
  116. const std::vector<File> all_files_;
  117. std::string path_;
  118. std::vector<File>::const_iterator path_iterator_;
  119. };
  120. class FakeFileSystem : public FilesystemInterface {
  121. public:
  122. typedef std::pair<std::string, std::string> File;
  123. explicit FakeFileSystem(const std::vector<File>& all_files) :
  124. all_files_(all_files) {}
  125. virtual DirectoryIterator *IterateDirectory() {
  126. return new FakeDirectoryIterator(all_files_);
  127. }
  128. virtual FileStream * OpenFile(
  129. const Pathname &filename,
  130. const std::string &mode) {
  131. std::vector<File>::const_iterator i_files = all_files_.begin();
  132. std::string path = filename.pathname();
  133. for (; i_files != all_files_.end(); i_files++) {
  134. if (i_files->first.compare(path) == 0) {
  135. return new FakeFileStream(i_files->second);
  136. }
  137. }
  138. return NULL;
  139. }
  140. bool CreatePrivateFile(const Pathname &filename) {
  141. EXPECT_TRUE(false) << "Unsupported operation";
  142. return false;
  143. }
  144. bool DeleteFile(const Pathname &filename) {
  145. EXPECT_TRUE(false) << "Unsupported operation";
  146. return false;
  147. }
  148. bool DeleteEmptyFolder(const Pathname &folder) {
  149. EXPECT_TRUE(false) << "Unsupported operation";
  150. return false;
  151. }
  152. bool DeleteFolderContents(const Pathname &folder) {
  153. EXPECT_TRUE(false) << "Unsupported operation";
  154. return false;
  155. }
  156. bool DeleteFolderAndContents(const Pathname &folder) {
  157. EXPECT_TRUE(false) << "Unsupported operation";
  158. return false;
  159. }
  160. bool CreateFolder(const Pathname &pathname) {
  161. EXPECT_TRUE(false) << "Unsupported operation";
  162. return false;
  163. }
  164. bool MoveFolder(const Pathname &old_path, const Pathname &new_path) {
  165. EXPECT_TRUE(false) << "Unsupported operation";
  166. return false;
  167. }
  168. bool MoveFile(const Pathname &old_path, const Pathname &new_path) {
  169. EXPECT_TRUE(false) << "Unsupported operation";
  170. return false;
  171. }
  172. bool CopyFile(const Pathname &old_path, const Pathname &new_path) {
  173. EXPECT_TRUE(false) << "Unsupported operation";
  174. return false;
  175. }
  176. bool IsFolder(const Pathname &pathname) {
  177. EXPECT_TRUE(false) << "Unsupported operation";
  178. return false;
  179. }
  180. bool IsFile(const Pathname &pathname) {
  181. EXPECT_TRUE(false) << "Unsupported operation";
  182. return false;
  183. }
  184. bool IsAbsent(const Pathname &pathname) {
  185. EXPECT_TRUE(false) << "Unsupported operation";
  186. return false;
  187. }
  188. bool IsTemporaryPath(const Pathname &pathname) {
  189. EXPECT_TRUE(false) << "Unsupported operation";
  190. return false;
  191. }
  192. bool GetTemporaryFolder(Pathname &path, bool create,
  193. const std::string *append) {
  194. EXPECT_TRUE(false) << "Unsupported operation";
  195. return false;
  196. }
  197. std::string TempFilename(const Pathname &dir, const std::string &prefix) {
  198. EXPECT_TRUE(false) << "Unsupported operation";
  199. return std::string();
  200. }
  201. bool GetFileSize(const Pathname &path, size_t *size) {
  202. EXPECT_TRUE(false) << "Unsupported operation";
  203. return false;
  204. }
  205. bool GetFileTime(const Pathname &path, FileTimeType which,
  206. time_t* time) {
  207. EXPECT_TRUE(false) << "Unsupported operation";
  208. return false;
  209. }
  210. bool GetAppPathname(Pathname *path) {
  211. EXPECT_TRUE(false) << "Unsupported operation";
  212. return false;
  213. }
  214. bool GetAppDataFolder(Pathname *path, bool per_user) {
  215. EXPECT_TRUE(per_user) << "Unsupported operation";
  216. #ifdef WIN32
  217. path->SetPathname("c:\\Users\\test_user", "");
  218. #else
  219. path->SetPathname("/home/user/test_user", "");
  220. #endif
  221. return true;
  222. }
  223. bool GetAppTempFolder(Pathname *path) {
  224. EXPECT_TRUE(false) << "Unsupported operation";
  225. return false;
  226. }
  227. bool GetDiskFreeSpace(const Pathname &path, int64 *freebytes) {
  228. EXPECT_TRUE(false) << "Unsupported operation";
  229. return false;
  230. }
  231. Pathname GetCurrentDirectory() {
  232. return Pathname();
  233. }
  234. private:
  235. const std::vector<File> all_files_;
  236. };
  237. } // namespace talk_base
  238. #endif // TALK_BASE_FILEUTILS_MOCK_H_