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.

148 lines
5.2 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. #include "talk/base/fileutils.h"
  28. #include "talk/base/gunit.h"
  29. #include "talk/base/pathutils.h"
  30. #include "talk/base/stream.h"
  31. namespace talk_base {
  32. // Make sure we can get a temp folder for the later tests.
  33. TEST(FilesystemTest, GetTemporaryFolder) {
  34. Pathname path;
  35. EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL));
  36. }
  37. // Test creating a temp file, reading it back in, and deleting it.
  38. TEST(FilesystemTest, TestOpenFile) {
  39. Pathname path;
  40. EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL));
  41. path.SetPathname(Filesystem::TempFilename(path, "ut"));
  42. FileStream* fs;
  43. char buf[256];
  44. size_t bytes;
  45. fs = Filesystem::OpenFile(path, "wb");
  46. ASSERT_TRUE(fs != NULL);
  47. EXPECT_EQ(SR_SUCCESS, fs->Write("test", 4, &bytes, NULL));
  48. EXPECT_EQ(4U, bytes);
  49. delete fs;
  50. EXPECT_TRUE(Filesystem::IsFile(path));
  51. fs = Filesystem::OpenFile(path, "rb");
  52. ASSERT_TRUE(fs != NULL);
  53. EXPECT_EQ(SR_SUCCESS, fs->Read(buf, sizeof(buf), &bytes, NULL));
  54. EXPECT_EQ(4U, bytes);
  55. delete fs;
  56. EXPECT_TRUE(Filesystem::DeleteFile(path));
  57. EXPECT_FALSE(Filesystem::IsFile(path));
  58. }
  59. // Test opening a non-existent file.
  60. TEST(FilesystemTest, TestOpenBadFile) {
  61. Pathname path;
  62. EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL));
  63. path.SetFilename("not an actual file");
  64. EXPECT_FALSE(Filesystem::IsFile(path));
  65. FileStream* fs = Filesystem::OpenFile(path, "rb");
  66. EXPECT_FALSE(fs != NULL);
  67. }
  68. // Test that CreatePrivateFile fails for existing files and succeeds for
  69. // non-existent ones.
  70. TEST(FilesystemTest, TestCreatePrivateFile) {
  71. Pathname path;
  72. EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL));
  73. path.SetFilename("private_file_test");
  74. // First call should succeed because the file doesn't exist yet.
  75. EXPECT_TRUE(Filesystem::CreatePrivateFile(path));
  76. // Next call should fail, because now it exists.
  77. EXPECT_FALSE(Filesystem::CreatePrivateFile(path));
  78. // Verify that we have permission to open the file for reading and writing.
  79. scoped_ptr<FileStream> fs(Filesystem::OpenFile(path, "wb"));
  80. EXPECT_TRUE(fs.get() != NULL);
  81. // Have to close the file on Windows before it will let us delete it.
  82. fs.reset();
  83. // Verify that we have permission to delete the file.
  84. EXPECT_TRUE(Filesystem::DeleteFile(path));
  85. }
  86. // Test checking for free disk space.
  87. TEST(FilesystemTest, TestGetDiskFreeSpace) {
  88. // Note that we should avoid picking any file/folder which could be located
  89. // at the remotely mounted drive/device.
  90. Pathname path;
  91. ASSERT_TRUE(Filesystem::GetAppDataFolder(&path, true));
  92. int64 free1 = 0;
  93. EXPECT_TRUE(Filesystem::IsFolder(path));
  94. EXPECT_FALSE(Filesystem::IsFile(path));
  95. EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free1));
  96. EXPECT_GT(free1, 0);
  97. int64 free2 = 0;
  98. path.AppendFolder("this_folder_doesnt_exist");
  99. EXPECT_FALSE(Filesystem::IsFolder(path));
  100. EXPECT_TRUE(Filesystem::IsAbsent(path));
  101. EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free2));
  102. // These should be the same disk, and disk free space should not have changed
  103. // by more than 1% between the two calls.
  104. EXPECT_LT(static_cast<int64>(free1 * .9), free2);
  105. EXPECT_LT(free2, static_cast<int64>(free1 * 1.1));
  106. int64 free3 = 0;
  107. path.clear();
  108. EXPECT_TRUE(path.empty());
  109. EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free3));
  110. // Current working directory may not be where exe is.
  111. // EXPECT_LT(static_cast<int64>(free1 * .9), free3);
  112. // EXPECT_LT(free3, static_cast<int64>(free1 * 1.1));
  113. EXPECT_GT(free3, 0);
  114. }
  115. // Tests that GetCurrentDirectory() returns something.
  116. TEST(FilesystemTest, TestGetCurrentDirectory) {
  117. EXPECT_FALSE(Filesystem::GetCurrentDirectory().empty());
  118. }
  119. // Tests that GetAppPathname returns something.
  120. TEST(FilesystemTest, TestGetAppPathname) {
  121. Pathname path;
  122. EXPECT_TRUE(Filesystem::GetAppPathname(&path));
  123. EXPECT_FALSE(path.empty());
  124. }
  125. } // namespace talk_base