15976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org/* 25976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * libjingle 35976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Copyright 2004--2011, Google Inc. 45976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * 55976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * Redistribution and use in source and binary forms, with or without 65976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * modification, are permitted provided that the following conditions are met: 75976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * 85976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * 1. Redistributions of source code must retain the above copyright notice, 95976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * this list of conditions and the following disclaimer. 105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * 2. Redistributions in binary form must reproduce the above copyright notice, 115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * this list of conditions and the following disclaimer in the documentation 125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * and/or other materials provided with the distribution. 135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * 3. The name of the author may not be used to endorse or promote products 145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * derived from this software without specific prior written permission. 155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * 165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org */ 275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/fileutils.h" 295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/gunit.h" 305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/pathutils.h" 315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org#include "talk/base/stream.h" 325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgnamespace talk_base { 345976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Make sure we can get a temp folder for the later tests. 365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgTEST(FilesystemTest, GetTemporaryFolder) { 375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org Pathname path; 385976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL)); 395976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org} 405976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 415976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Test creating a temp file, reading it back in, and deleting it. 425976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgTEST(FilesystemTest, TestOpenFile) { 435976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org Pathname path; 445976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL)); 455976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org path.SetPathname(Filesystem::TempFilename(path, "ut")); 465976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 475976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org FileStream* fs; 485976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org char buf[256]; 495976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org size_t bytes; 505976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 515976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org fs = Filesystem::OpenFile(path, "wb"); 525976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org ASSERT_TRUE(fs != NULL); 535976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_EQ(SR_SUCCESS, fs->Write("test", 4, &bytes, NULL)); 545976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_EQ(4U, bytes); 555976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org delete fs; 565976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 575976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(Filesystem::IsFile(path)); 585976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 595976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org fs = Filesystem::OpenFile(path, "rb"); 605976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org ASSERT_TRUE(fs != NULL); 615976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_EQ(SR_SUCCESS, fs->Read(buf, sizeof(buf), &bytes, NULL)); 625976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_EQ(4U, bytes); 635976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org delete fs; 645976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 655976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(Filesystem::DeleteFile(path)); 665976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_FALSE(Filesystem::IsFile(path)); 675976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org} 685976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 695976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Test opening a non-existent file. 705976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgTEST(FilesystemTest, TestOpenBadFile) { 715976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org Pathname path; 725976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL)); 735976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org path.SetFilename("not an actual file"); 745976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 755976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_FALSE(Filesystem::IsFile(path)); 765976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 775976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org FileStream* fs = Filesystem::OpenFile(path, "rb"); 785976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_FALSE(fs != NULL); 795976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org} 805976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 815976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Test that CreatePrivateFile fails for existing files and succeeds for 825976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// non-existent ones. 835976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgTEST(FilesystemTest, TestCreatePrivateFile) { 845976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org Pathname path; 855976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(Filesystem::GetTemporaryFolder(path, true, NULL)); 865976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org path.SetFilename("private_file_test"); 875976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 885976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // First call should succeed because the file doesn't exist yet. 895976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(Filesystem::CreatePrivateFile(path)); 905976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Next call should fail, because now it exists. 915976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_FALSE(Filesystem::CreatePrivateFile(path)); 925976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 935976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Verify that we have permission to open the file for reading and writing. 945976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org scoped_ptr<FileStream> fs(Filesystem::OpenFile(path, "wb")); 955976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(fs.get() != NULL); 965976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Have to close the file on Windows before it will let us delete it. 975976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org fs.reset(); 985976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 995976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Verify that we have permission to delete the file. 1005976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(Filesystem::DeleteFile(path)); 1015976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org} 1025976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1035976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Test checking for free disk space. 1045976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgTEST(FilesystemTest, TestGetDiskFreeSpace) { 1055976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Note that we should avoid picking any file/folder which could be located 1065976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // at the remotely mounted drive/device. 1075976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org Pathname path; 1085976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org ASSERT_TRUE(Filesystem::GetAppDataFolder(&path, true)); 1095976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1105976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org int64 free1 = 0; 1115976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(Filesystem::IsFolder(path)); 1125976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_FALSE(Filesystem::IsFile(path)); 1135976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free1)); 1145976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_GT(free1, 0); 1155976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1165976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org int64 free2 = 0; 1175976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org path.AppendFolder("this_folder_doesnt_exist"); 1185976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_FALSE(Filesystem::IsFolder(path)); 1195976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(Filesystem::IsAbsent(path)); 1205976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free2)); 1215976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // These should be the same disk, and disk free space should not have changed 1225976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // by more than 1% between the two calls. 1235976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_LT(static_cast<int64>(free1 * .9), free2); 1245976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_LT(free2, static_cast<int64>(free1 * 1.1)); 1255976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1265976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org int64 free3 = 0; 1275976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org path.clear(); 1285976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(path.empty()); 1295976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(Filesystem::GetDiskFreeSpace(path, &free3)); 1305976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // Current working directory may not be where exe is. 1315976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // EXPECT_LT(static_cast<int64>(free1 * .9), free3); 1325976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org // EXPECT_LT(free3, static_cast<int64>(free1 * 1.1)); 1335976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_GT(free3, 0); 1345976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org} 1355976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1365976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Tests that GetCurrentDirectory() returns something. 1375976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgTEST(FilesystemTest, TestGetCurrentDirectory) { 1385976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_FALSE(Filesystem::GetCurrentDirectory().empty()); 1395976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org} 1405976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1415976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org// Tests that GetAppPathname returns something. 1425976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.orgTEST(FilesystemTest, TestGetAppPathname) { 1435976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org Pathname path; 1445976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_TRUE(Filesystem::GetAppPathname(&path)); 1455976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org EXPECT_FALSE(path.empty()); 1465976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org} 1475976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org 1485976650443d68ccfadf1dea24999ee459dd2819mflodman@webrtc.org} // namespace talk_base 149