TEST(TestFile, Write) { XFILE::CFile *file; const char str[] = "TestFile.Write test string\n"; char buf[30]; memset(&buf, 0, sizeof(buf)); ASSERT_TRUE((file = XBMC_CREATETEMPFILE("")) != NULL); file->Close(); ASSERT_TRUE(file->OpenForWrite(XBMC_TEMPFILEPATH(file), true)); EXPECT_EQ((int)sizeof(str), file->Write(str, sizeof(str))); file->Flush(); EXPECT_EQ(0, file->GetPosition()); file->Close(); ASSERT_TRUE(file->Open(XBMC_TEMPFILEPATH(file))); EXPECT_EQ(0, file->GetPosition()); EXPECT_EQ((int64_t)sizeof(str), file->Seek(0, SEEK_END)); EXPECT_EQ(0, file->Seek(0, SEEK_SET)); EXPECT_EQ((int64_t)sizeof(str), file->GetLength()); EXPECT_EQ(sizeof(str), file->Read(buf, sizeof(buf))); file->Flush(); EXPECT_EQ((int64_t)sizeof(str), file->GetPosition()); EXPECT_TRUE(!memcmp(str, buf, sizeof(str))); file->Close(); EXPECT_TRUE(XBMC_DELETETEMPFILE(file)); }
TEST(TestAliasShortcutUtils, IsAliasShortcut) { XFILE::CFile *tmpFile = XBMC_CREATETEMPFILE("noaliastest"); std::string noalias = XBMC_TEMPFILEPATH(tmpFile); #if defined(TARGET_DARWIN_OSX) XFILE::CFile *aliasDestFile = XBMC_CREATETEMPFILE("aliastest"); std::string alias = XBMC_TEMPFILEPATH(aliasDestFile); //we only need the path here so delete the alias file //which will be recreated as shortcut later: XBMC_DELETETEMPFILE(aliasDestFile); // create alias from a pointing to /Volumes CDarwinUtils::CreateAliasShortcut(alias, "/Volumes"); EXPECT_TRUE(IsAliasShortcut(alias, true)); XFILE::CFile::Delete(alias); // volumes is not a shortcut but a dir EXPECT_FALSE(IsAliasShortcut("/Volumes", true)); #endif // a regular file is not a shortcut EXPECT_FALSE(IsAliasShortcut(noalias, false)); XBMC_DELETETEMPFILE(tmpFile); // empty string is not an alias std::string emptyString; EXPECT_FALSE(IsAliasShortcut(emptyString, false)); // non-existent file is no alias std::string nonExistingFile="/IDontExistsNormally/somefile.txt"; EXPECT_FALSE(IsAliasShortcut(nonExistingFile, false)); }
// This test will fail until ActionDeleteFolder has a proper implementation TEST(TestFileOperationJob, ActionDeleteFolder) { XFILE::CFile *tmpfile; CStdString tmpfilepath, destpath; CFileItemList items; CFileOperationJob job; ASSERT_TRUE((tmpfile = XBMC_CREATETEMPFILE(""))); tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); tmpfile->Close(); destpath = tmpfilepath; destpath += ".deletefolder"; ASSERT_FALSE(XFILE::CFile::Exists(destpath)); CFileItemPtr item(new CFileItem(destpath)); item->SetPath(destpath); item->m_bIsFolder = true; item->Select(true); items.Add(item); job.SetFileOperation(CFileOperationJob::ActionCreateFolder, items, destpath); EXPECT_EQ(CFileOperationJob::ActionCreateFolder, job.GetAction()); EXPECT_TRUE(job.DoWork()); EXPECT_TRUE(XFILE::CDirectory::Exists(destpath)); job.SetFileOperation(CFileOperationJob::ActionDeleteFolder, items, destpath); EXPECT_EQ(CFileOperationJob::ActionDeleteFolder, job.GetAction()); EXPECT_TRUE(job.DoWork()); EXPECT_FALSE(XFILE::CDirectory::Exists(destpath)); EXPECT_TRUE(XBMC_DELETETEMPFILE(tmpfile)); }
TEST(TestFileOperationJob, ActionMove) { XFILE::CFile *tmpfile; CStdString tmpfilepath, destpath, destfile; CFileItemList items; CFileOperationJob job; ASSERT_TRUE((tmpfile = XBMC_CREATETEMPFILE(""))); tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); tmpfile->Close(); CFileItemPtr item(new CFileItem(tmpfilepath)); item->SetPath(tmpfilepath); item->m_bIsFolder = false; item->Select(true); items.Add(item); URIUtils::GetDirectory(tmpfilepath, destpath); destpath = URIUtils::AddFileToFolder(destpath, "move"); destfile = URIUtils::AddFileToFolder(destpath, URIUtils::GetFileName(tmpfilepath)); ASSERT_FALSE(XFILE::CFile::Exists(destfile)); ASSERT_TRUE(XFILE::CDirectory::Create(destpath)); job.SetFileOperation(CFileOperationJob::ActionMove, items, destpath); EXPECT_EQ(CFileOperationJob::ActionMove, job.GetAction()); EXPECT_TRUE(job.DoWork()); EXPECT_FALSE(XFILE::CFile::Exists(tmpfilepath)); EXPECT_TRUE(XFILE::CFile::Exists(destfile)); EXPECT_TRUE(XFILE::CFile::Delete(destfile)); EXPECT_TRUE(XFILE::CDirectory::Remove(destpath)); }
TEST(TestFileOperationJob, ActionMove) { XFILE::CFile *tmpfile; CStdString tmpfilepath, destpath; CFileItemList items; CFileOperationJob job; ASSERT_TRUE((tmpfile = XBMC_CREATETEMPFILE(""))); tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); CFileItemPtr item(new CFileItem(tmpfilepath)); item->SetPath(tmpfilepath); item->m_bIsFolder = false; item->Select(true); items.Add(item); destpath = tmpfilepath; destpath += ".move"; ASSERT_FALSE(XFILE::CFile::Exists(destpath)); job.SetFileOperation(CFileOperationJob::ActionMove, items, destpath); EXPECT_EQ(CFileOperationJob::ActionMove, job.GetAction()); EXPECT_TRUE(job.DoWork()); EXPECT_FALSE(XFILE::CFile::Exists(tmpfilepath)); EXPECT_TRUE(XFILE::CFile::Exists(destpath)); EXPECT_TRUE(XFILE::CFile::Delete(destpath)); }
TEST(TestFileUtils, DeleteItem_CStdString) { XFILE::CFile *tmpfile; ASSERT_TRUE((tmpfile = XBMC_CREATETEMPFILE(""))); EXPECT_TRUE(CFileUtils::DeleteItem(XBMC_TEMPFILEPATH(tmpfile))); }
TEST(TestFile, SetHidden) { XFILE::CFile *file; ASSERT_TRUE((file = XBMC_CREATETEMPFILE("")) != NULL); file->Close(); EXPECT_TRUE(XFILE::CFile::Exists(XBMC_TEMPFILEPATH(file))); bool result = XFILE::CFile::SetHidden(XBMC_TEMPFILEPATH(file), true); #ifdef TARGET_WINDOWS EXPECT_TRUE(result); #else EXPECT_FALSE(result); #endif EXPECT_TRUE(XFILE::CFile::Exists(XBMC_TEMPFILEPATH(file))); EXPECT_TRUE(XBMC_DELETETEMPFILE(file)); }
TEST(TestFileUtils, DeleteItemString) { XFILE::CFile *tmpfile; ASSERT_NE(nullptr, (tmpfile = XBMC_CREATETEMPFILE(""))); tmpfile->Close(); //Close tmpfile before we try to delete it EXPECT_TRUE(CFileUtils::DeleteItem(XBMC_TEMPFILEPATH(tmpfile))); }
TEST(TestFile, Exists) { XFILE::CFile *file; ASSERT_TRUE((file = XBMC_CREATETEMPFILE("")) != NULL); file->Close(); EXPECT_TRUE(XFILE::CFile::Exists(XBMC_TEMPFILEPATH(file))); EXPECT_FALSE(XFILE::CFile::Exists("")); EXPECT_TRUE(XBMC_DELETETEMPFILE(file)); }
TEST(TestFile, Rename) { XFILE::CFile *file; CStdString path1, path2; ASSERT_TRUE((file = XBMC_CREATETEMPFILE("")) != NULL); file->Close(); path1 = XBMC_TEMPFILEPATH(file); ASSERT_TRUE((file = XBMC_CREATETEMPFILE("")) != NULL); file->Close(); path2 = XBMC_TEMPFILEPATH(file); EXPECT_TRUE(XFILE::CFile::Delete(path1)); EXPECT_FALSE(XFILE::CFile::Exists(path1)); EXPECT_TRUE(XFILE::CFile::Exists(path2)); EXPECT_TRUE(XFILE::CFile::Rename(path2, path1)); EXPECT_TRUE(XFILE::CFile::Exists(path1)); EXPECT_FALSE(XFILE::CFile::Exists(path2)); EXPECT_TRUE(XFILE::CFile::Delete(path1)); }
TEST(TestFile, Copy) { XFILE::CFile *file; std::string path1, path2; ASSERT_NE(nullptr, file = XBMC_CREATETEMPFILE("")); file->Close(); path1 = XBMC_TEMPFILEPATH(file); ASSERT_NE(nullptr, file = XBMC_CREATETEMPFILE("")); file->Close(); path2 = XBMC_TEMPFILEPATH(file); EXPECT_TRUE(XFILE::CFile::Delete(path1)); EXPECT_FALSE(XFILE::CFile::Exists(path1)); EXPECT_TRUE(XFILE::CFile::Exists(path2)); EXPECT_TRUE(XFILE::CFile::Copy(path2, path1)); EXPECT_TRUE(XFILE::CFile::Exists(path1)); EXPECT_TRUE(XFILE::CFile::Exists(path2)); EXPECT_TRUE(XFILE::CFile::Delete(path1)); EXPECT_TRUE(XFILE::CFile::Delete(path2)); }
TEST(TestFile, Delete) { XFILE::CFile *file; CStdString path; ASSERT_TRUE((file = XBMC_CREATETEMPFILE("")) != NULL); file->Close(); path = XBMC_TEMPFILEPATH(file); EXPECT_TRUE(XFILE::CFile::Exists(path)); EXPECT_TRUE(XFILE::CFile::Delete(path)); EXPECT_FALSE(XFILE::CFile::Exists(path)); }
TEST(TestAsyncFileCopy, General) { CAsyncFileCopy c; XFILE::CFile *f1, *f2; char vardata[sizeof(refdata)]; ASSERT_TRUE((f1 = XBMC_CREATETEMPFILE(""))); ASSERT_TRUE((f2 = XBMC_CREATETEMPFILE(".copy"))); EXPECT_EQ((int)sizeof(refdata), f1->Write(refdata, sizeof(refdata))); f1->Close(); f2->Close(); EXPECT_TRUE(c.Copy(XBMC_TEMPFILEPATH(f1), XBMC_TEMPFILEPATH(f2), "")); EXPECT_TRUE(f2->Open(XBMC_TEMPFILEPATH(f2))); EXPECT_EQ(sizeof(refdata), f2->Read(vardata, sizeof(refdata))); f2->Close(); EXPECT_TRUE(!memcmp(vardata, refdata, sizeof(refdata))); EXPECT_TRUE(XBMC_DELETETEMPFILE(f1)); EXPECT_TRUE(XBMC_DELETETEMPFILE(f2)); }
TEST(TestAliasShortcutUtils, TranslateAliasShortcut) { XFILE::CFile *tmpFile = XBMC_CREATETEMPFILE("noaliastest"); std::string noalias = XBMC_TEMPFILEPATH(tmpFile); std::string noaliastemp = noalias; #if defined(TARGET_DARWIN_OSX) XFILE::CFile *aliasDestFile = XBMC_CREATETEMPFILE("aliastest"); std::string alias = XBMC_TEMPFILEPATH(aliasDestFile); //we only need the path here so delete the alias file //which will be recreated as shortcut later: XBMC_DELETETEMPFILE(aliasDestFile); // create alias from a pointing to /Volumes CDarwinUtils::CreateAliasShortcut(alias, "/Volumes"); // resolve the shortcut TranslateAliasShortcut(alias); EXPECT_STREQ("/Volumes", alias.c_str()); XFILE::CFile::Delete(alias); #endif // translating a non-shortcut url should result in no change... TranslateAliasShortcut(noaliastemp); EXPECT_STREQ(noaliastemp.c_str(), noalias.c_str()); XBMC_DELETETEMPFILE(tmpFile); //translate empty should stay empty std::string emptyString; TranslateAliasShortcut(emptyString); EXPECT_STREQ("", emptyString.c_str()); // translate non-existent file should result in no change... std::string nonExistingFile="/IDontExistsNormally/somefile.txt"; std::string resolvedNonExistingFile=nonExistingFile; TranslateAliasShortcut(resolvedNonExistingFile); EXPECT_STREQ(resolvedNonExistingFile.c_str(), nonExistingFile.c_str()); }
TEST(TestFileUtils, DeleteItem_CFileItemPtr) { XFILE::CFile *tmpfile; std::string tmpfilepath; ASSERT_NE(nullptr, (tmpfile = XBMC_CREATETEMPFILE(""))); tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); CFileItemPtr item(new CFileItem(tmpfilepath)); item->SetPath(tmpfilepath); item->m_bIsFolder = false; item->Select(true); tmpfile->Close(); //Close tmpfile before we try to delete it EXPECT_TRUE(CFileUtils::DeleteItem(item)); }
TEST(TestFileUtils, DeleteItem_CFileItemPtr) { XFILE::CFile *tmpfile; CStdString tmpfilepath; ASSERT_TRUE((tmpfile = XBMC_CREATETEMPFILE(""))); tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); CFileItemPtr item(new CFileItem(tmpfilepath)); item->SetPath(tmpfilepath); item->m_bIsFolder = false; item->Select(true); EXPECT_TRUE(CFileUtils::DeleteItem(item)); }
TEST(TestFileOperationJob, GetFunctions) { XFILE::CFile *tmpfile; CStdString tmpfilepath, destpath, destfile; CFileItemList items; CFileOperationJob job; ASSERT_TRUE((tmpfile = XBMC_CREATETEMPFILE(""))); tmpfilepath = XBMC_TEMPFILEPATH(tmpfile); tmpfile->Close(); CFileItemPtr item(new CFileItem(tmpfilepath)); item->SetPath(tmpfilepath); item->m_bIsFolder = false; item->Select(true); items.Add(item); URIUtils::GetDirectory(tmpfilepath, destpath); destpath = URIUtils::AddFileToFolder(destpath, "getfunctions"); destfile = URIUtils::AddFileToFolder(destpath, URIUtils::GetFileName(tmpfilepath)); ASSERT_FALSE(XFILE::CFile::Exists(destfile)); job.SetFileOperation(CFileOperationJob::ActionCopy, items, destpath); EXPECT_EQ(CFileOperationJob::ActionCopy, job.GetAction()); EXPECT_TRUE(job.DoWork()); EXPECT_TRUE(XFILE::CFile::Exists(tmpfilepath)); EXPECT_TRUE(XFILE::CFile::Exists(destfile)); std::cout << "GetAverageSpeed(): " << job.GetAverageSpeed() << std::endl; std::cout << "GetCurrentOperation(): " << job.GetCurrentOperation() << std::endl; std::cout << "GetCurrentFile(): " << job.GetCurrentFile() << std::endl; EXPECT_FALSE(job.GetItems().IsEmpty()); EXPECT_TRUE(XBMC_DELETETEMPFILE(tmpfile)); EXPECT_TRUE(XFILE::CFile::Delete(destfile)); EXPECT_TRUE(XFILE::CDirectory::Remove(destpath)); }
/* Test case to test for graceful handling of corrupted input. * NOTE: The test case is considered a "success" as long as the corrupted * file was successfully generated and the test case runs without a segfault. */ TEST(TestRarFile, CorruptedFile) { XFILE::CFile *file; char buf[16]; memset(&buf, 0, sizeof(buf)); CStdString reffilepath, strrarpath, strpathinrar, str; CFileItemList itemlist; unsigned int size, i; int64_t count = 0; reffilepath = XBMC_REF_FILE_PATH("xbmc/filesystem/test/reffile.txt.rar"); ASSERT_TRUE((file = XBMC_CREATECORRUPTEDFILE(reffilepath, ".rar")) != NULL); std::cout << "Reference file generated at '" << XBMC_TEMPFILEPATH(file) << "'\n"; URIUtils::CreateArchivePath(strrarpath, "rar", XBMC_TEMPFILEPATH(file), ""); if (!XFILE::CDirectory::GetDirectory(strrarpath, itemlist, "", XFILE::DIR_FLAG_NO_FILE_DIRS)) { XBMC_DELETETEMPFILE(file); SUCCEED(); return; } if (itemlist.IsEmpty()) { XBMC_DELETETEMPFILE(file); SUCCEED(); return; } strpathinrar = itemlist[0]->GetPath(); if (!file->Open(strpathinrar)) { XBMC_DELETETEMPFILE(file); SUCCEED(); return; } std::cout << "file->GetLength(): " << testing::PrintToString(file->GetLength()) << "\n"; std::cout << "file->Seek(file->GetLength() / 2, SEEK_CUR) return value: " << testing::PrintToString(file->Seek(file->GetLength() / 2, SEEK_CUR)) << "\n"; std::cout << "file->Seek(0, SEEK_END) return value: " << testing::PrintToString(file->Seek(0, SEEK_END)) << "\n"; std::cout << "file->Seek(0, SEEK_SET) return value: " << testing::PrintToString(file->Seek(0, SEEK_SET)) << "\n"; std::cout << "File contents:\n"; while ((size = file->Read(buf, sizeof(buf))) > 0) { str.Format(" %08X", count); std::cout << str << " "; count += size; for (i = 0; i < size; i++) { str.Format("%02X ", buf[i]); std::cout << str; } while (i++ < sizeof(buf)) std::cout << " "; std::cout << " ["; for (i = 0; i < size; i++) { if (buf[i] >= ' ' && buf[i] <= '~') std::cout << buf[i]; else std::cout << "."; } std::cout << "]\n"; } file->Close(); XBMC_DELETETEMPFILE(file); }