2 * Copyright (C) 2005-2013 Team XBMC
5 * This Program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2, or (at your option)
10 * This Program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with XBMC; see the file COPYING. If not, see
17 * <http://www.gnu.org/licenses/>.
21 #include "TestUtils.h"
23 #include "filesystem/File.h"
24 #include "filesystem/SpecialProtocol.h"
25 #include "utils/StringUtils.h"
35 class CTempFile : public XFILE::CFile
43 bool Create(const CStdString &suffix)
48 m_ptempFileDirectory = CSpecialProtocol::TranslatePath("special://temp/");
49 m_ptempFilePath = m_ptempFileDirectory + "xbmctempfileXXXXXX";
50 m_ptempFilePath += suffix;
51 if (m_ptempFilePath.length() >= MAX_PATH)
56 strcpy(tmp, m_ptempFilePath.c_str());
59 if (!GetTempFileName(CSpecialProtocol::TranslatePath("special://temp/"),
60 "xbmctempfile", 0, tmp))
65 m_ptempFilePath = tmp;
67 if ((fd = mkstemps(tmp, suffix.length())) < 0)
73 m_ptempFilePath = tmp;
76 OpenForWrite(m_ptempFilePath.c_str(), true);
82 return CFile::Delete(m_ptempFilePath);
84 CStdString getTempFilePath() const
86 return m_ptempFilePath;
88 CStdString getTempFileDirectory() const
90 return m_ptempFileDirectory;
93 CStdString m_ptempFilePath;
94 CStdString m_ptempFileDirectory;
97 CXBMCTestUtils::CXBMCTestUtils()
102 CXBMCTestUtils &CXBMCTestUtils::Instance()
104 static CXBMCTestUtils instance;
108 CStdString CXBMCTestUtils::ReferenceFilePath(CStdString const& path)
110 return CSpecialProtocol::TranslatePath("special://xbmc") + path;
113 bool CXBMCTestUtils::SetReferenceFileBasePath()
116 CUtil::GetHomePath(xbmcPath);
117 if (xbmcPath.empty())
120 /* Set xbmc path and xbmcbin path */
121 CSpecialProtocol::SetXBMCPath(xbmcPath);
122 CSpecialProtocol::SetXBMCBinPath(xbmcPath);
127 XFILE::CFile *CXBMCTestUtils::CreateTempFile(CStdString const& suffix)
129 CTempFile *f = new CTempFile();
130 if (f->Create(suffix))
136 bool CXBMCTestUtils::DeleteTempFile(XFILE::CFile *tempfile)
140 CTempFile *f = static_cast<CTempFile*>(tempfile);
141 bool retval = f->Delete();
146 CStdString CXBMCTestUtils::TempFilePath(XFILE::CFile const* const tempfile)
150 CTempFile const* const f = static_cast<CTempFile const* const>(tempfile);
151 return f->getTempFilePath();
154 CStdString CXBMCTestUtils::TempFileDirectory(XFILE::CFile const* const tempfile)
158 CTempFile const* const f = static_cast<CTempFile const* const>(tempfile);
159 return f->getTempFileDirectory();
162 XFILE::CFile *CXBMCTestUtils::CreateCorruptedFile(CStdString const& strFileName,
163 CStdString const& suffix)
165 XFILE::CFile inputfile, *tmpfile = CreateTempFile(suffix);
166 unsigned char buf[20], tmpchar;
167 unsigned int size, i;
169 if (tmpfile && inputfile.Open(strFileName))
172 while ((size = inputfile.Read(buf, sizeof(buf))) > 0)
174 for (i = 0; i < size; i++)
176 if ((rand() % RAND_MAX) < (probability * RAND_MAX))
181 buf[i] = (rand() % 256);
182 } while (buf[i] == tmpchar);
185 if (tmpfile->Write(buf, size) < 0)
189 DeleteTempFile(tmpfile);
202 std::vector<CStdString> &CXBMCTestUtils::getTestFileFactoryReadUrls()
204 return TestFileFactoryReadUrls;
207 std::vector<CStdString> &CXBMCTestUtils::getTestFileFactoryWriteUrls()
209 return TestFileFactoryWriteUrls;
212 CStdString &CXBMCTestUtils::getTestFileFactoryWriteInputFile()
214 return TestFileFactoryWriteInputFile;
217 void CXBMCTestUtils::setTestFileFactoryWriteInputFile(CStdString const& file)
219 TestFileFactoryWriteInputFile = file;
222 std::vector<CStdString> &CXBMCTestUtils::getAdvancedSettingsFiles()
224 return AdvancedSettingsFiles;
227 std::vector<CStdString> &CXBMCTestUtils::getGUISettingsFiles()
229 return GUISettingsFiles;
232 static const char usage[] =
234 "Usage: xbmc-test [options]\n"
236 "The following options are recognized by the xbmc-test program.\n"
238 " --add-testfilefactory-readurl [URL]\n"
239 " Add a url to be used int the TestFileFactory read tests.\n"
241 " --add-testfilefactory-readurls [URLS]\n"
242 " Add multiple urls from a ',' delimited string of urls to be used\n"
243 " in the TestFileFactory read tests.\n"
245 " --add-testfilefactory-writeurl [URL]\n"
246 " Add a url to be used int the TestFileFactory write tests.\n"
248 " --add-testfilefactory-writeurls [URLS]\n"
249 " Add multiple urls from a ',' delimited string of urls to be used\n"
250 " in the TestFileFactory write tests.\n"
252 " --set-testfilefactory-writeinputfile [FILE]\n"
253 " Set the path to the input file used in the TestFileFactory write tests.\n"
255 " --add-advancedsettings-file [FILE]\n"
256 " Add an advanced settings file to be loaded in test cases that use them.\n"
258 " --add-advancedsettings-files [FILES]\n"
259 " Add multiple advanced settings files from a ',' delimited string of\n"
260 " files to be loaded in test cases that use them.\n"
262 " --add-guisettings-file [FILE]\n"
263 " Add a GUI settings file to be loaded in test cases that use them.\n"
265 " --add-guisettings-files [FILES]\n"
266 " Add multiple GUI settings files from a ',' delimited string of\n"
267 " files to be loaded in test cases that use them.\n"
269 " --set-probability [PROBABILITY]\n"
270 " Set the probability variable used by the file corrupting functions.\n"
271 " The variable should be a double type from 0.0 to 1.0. Values given\n"
272 " less than 0.0 are treated as 0.0. Values greater than 1.0 are treated\n"
273 " as 1.0. The default probability is 0.01.\n"
276 void CXBMCTestUtils::ParseArgs(int argc, char **argv)
280 for (i = 1; i < argc; i++)
283 if (arg == "--add-testfilefactory-readurl")
285 TestFileFactoryReadUrls.push_back(argv[++i]);
287 else if (arg == "--add-testfilefactory-readurls")
290 std::vector<std::string> urls = StringUtils::Split(arg, ",");
291 std::vector<std::string>::iterator it;
292 for (it = urls.begin(); it < urls.end(); it++)
293 TestFileFactoryReadUrls.push_back(*it);
295 else if (arg == "--add-testfilefactory-writeurl")
297 TestFileFactoryWriteUrls.push_back(argv[++i]);
299 else if (arg == "--add-testfilefactory-writeurls")
302 std::vector<std::string> urls = StringUtils::Split(arg, ",");
303 std::vector<std::string>::iterator it;
304 for (it = urls.begin(); it < urls.end(); it++)
305 TestFileFactoryWriteUrls.push_back(*it);
307 else if (arg == "--set-testfilefactory-writeinputfile")
309 TestFileFactoryWriteInputFile = argv[++i];
311 else if (arg == "--add-advancedsettings-file")
313 AdvancedSettingsFiles.push_back(argv[++i]);
315 else if (arg == "--add-advancedsettings-files")
318 std::vector<std::string> urls = StringUtils::Split(arg, ",");
319 std::vector<std::string>::iterator it;
320 for (it = urls.begin(); it < urls.end(); it++)
321 AdvancedSettingsFiles.push_back(*it);
323 else if (arg == "--add-guisettings-file")
325 GUISettingsFiles.push_back(argv[++i]);
327 else if (arg == "--add-guisettings-files")
330 std::vector<std::string> urls = StringUtils::Split(arg, ",");
331 std::vector<std::string>::iterator it;
332 for (it = urls.begin(); it < urls.end(); it++)
333 GUISettingsFiles.push_back(*it);
335 else if (arg == "--set-probability")
337 probability = atof(argv[++i]);
338 if (probability < 0.0)
340 else if (probability > 1.0)
351 std::string CXBMCTestUtils::getNewLineCharacters() const
353 #ifdef TARGET_WINDOWS