[video] Fix the refresh of movies with additional versions or extras
[xbmc.git] / xbmc / test / TestUtils.cpp
blob1289c89ca2d85cd74fbb21ee23d99819391c9596
1 /*
2 * Copyright (C) 2005-2018 Team Kodi
3 * This file is part of Kodi - https://kodi.tv
5 * SPDX-License-Identifier: GPL-2.0-or-later
6 * See LICENSES/README.md for more information.
7 */
9 #include "TestUtils.h"
10 #include "Util.h"
11 #include "filesystem/File.h"
12 #include "filesystem/SpecialProtocol.h"
13 #include "platform/Filesystem.h"
14 #include "utils/StringUtils.h"
15 #include "utils/URIUtils.h"
17 #ifdef TARGET_WINDOWS
18 #include <windows.h>
19 #else
20 #include <cstdlib>
21 #include <climits>
22 #include <ctime>
23 #endif
25 #include <system_error>
27 namespace fs = KODI::PLATFORM::FILESYSTEM;
29 class CTempFile : public XFILE::CFile
31 public:
32 CTempFile() = default;
33 ~CTempFile()
35 Delete();
37 bool Create(const std::string &suffix)
39 std::error_code ec;
40 m_ptempFilePath = fs::temp_file_path(suffix, ec);
41 if (ec)
42 return false;
44 if (m_ptempFilePath.empty())
45 return false;
47 OpenForWrite(m_ptempFilePath, true);
48 return true;
50 bool Delete()
52 Close();
53 return CFile::Delete(m_ptempFilePath);
55 std::string getTempFilePath() const
57 return m_ptempFilePath;
59 std::string getTempFileDirectory() const
61 return URIUtils::GetDirectory(m_ptempFilePath);
63 private:
64 std::string m_ptempFilePath;
67 CXBMCTestUtils::CXBMCTestUtils()
69 probability = 0.01;
72 CXBMCTestUtils &CXBMCTestUtils::Instance()
74 static CXBMCTestUtils instance;
75 return instance;
78 std::string CXBMCTestUtils::ReferenceFilePath(const std::string& path)
80 return CSpecialProtocol::TranslatePath(URIUtils::AddFileToFolder("special://xbmc", path));
83 bool CXBMCTestUtils::SetReferenceFileBasePath()
85 std::string xbmcPath = CUtil::GetHomePath();
86 if (xbmcPath.empty())
87 return false;
89 /* Set xbmc, xbmcbin and home path */
90 CSpecialProtocol::SetXBMCPath(xbmcPath);
91 CSpecialProtocol::SetXBMCBinPath(xbmcPath);
92 CSpecialProtocol::SetHomePath(URIUtils::AddFileToFolder(xbmcPath, "portable_data"));
94 return true;
97 XFILE::CFile *CXBMCTestUtils::CreateTempFile(std::string const& suffix)
99 CTempFile *f = new CTempFile();
100 if (f->Create(suffix))
101 return f;
102 delete f;
103 return NULL;
106 bool CXBMCTestUtils::DeleteTempFile(XFILE::CFile *tempfile)
108 if (!tempfile)
109 return true;
110 CTempFile *f = static_cast<CTempFile*>(tempfile);
111 bool retval = f->Delete();
112 delete f;
113 return retval;
116 std::string CXBMCTestUtils::TempFilePath(XFILE::CFile const* const tempfile)
118 if (!tempfile)
119 return "";
120 CTempFile const* const f = static_cast<CTempFile const*>(tempfile);
121 return f->getTempFilePath();
124 std::string CXBMCTestUtils::TempFileDirectory(XFILE::CFile const* const tempfile)
126 if (!tempfile)
127 return "";
128 CTempFile const* const f = static_cast<CTempFile const*>(tempfile);
129 return f->getTempFileDirectory();
132 XFILE::CFile *CXBMCTestUtils::CreateCorruptedFile(std::string const& strFileName,
133 std::string const& suffix)
135 XFILE::CFile inputfile, *tmpfile = CreateTempFile(suffix);
136 unsigned char buf[20], tmpchar;
137 ssize_t size, i;
139 if (tmpfile && inputfile.Open(strFileName))
141 srand(time(NULL));
142 while ((size = inputfile.Read(buf, sizeof(buf))) > 0)
144 for (i = 0; i < size; i++)
146 if ((rand() % RAND_MAX) < (probability * RAND_MAX))
148 tmpchar = buf[i];
151 buf[i] = (rand() % 256);
152 } while (buf[i] == tmpchar);
155 if (tmpfile->Write(buf, size) < 0)
157 inputfile.Close();
158 tmpfile->Close();
159 DeleteTempFile(tmpfile);
160 return NULL;
163 inputfile.Close();
164 tmpfile->Close();
165 return tmpfile;
167 delete tmpfile;
168 return NULL;
172 std::vector<std::string> &CXBMCTestUtils::getTestFileFactoryReadUrls()
174 return TestFileFactoryReadUrls;
177 std::vector<std::string> &CXBMCTestUtils::getTestFileFactoryWriteUrls()
179 return TestFileFactoryWriteUrls;
182 std::string &CXBMCTestUtils::getTestFileFactoryWriteInputFile()
184 return TestFileFactoryWriteInputFile;
187 void CXBMCTestUtils::setTestFileFactoryWriteInputFile(std::string const& file)
189 TestFileFactoryWriteInputFile = file;
192 std::vector<std::string> &CXBMCTestUtils::getAdvancedSettingsFiles()
194 return AdvancedSettingsFiles;
197 std::vector<std::string> &CXBMCTestUtils::getGUISettingsFiles()
199 return GUISettingsFiles;
202 static const char usage[] =
203 "Kodi Test Suite\n"
204 "Usage: kodi-test [options]\n"
205 "\n"
206 "The following options are recognized by the kodi-test program.\n"
207 "\n"
208 " --add-testfilefactory-readurl [URL]\n"
209 " Add a url to be used int the TestFileFactory read tests.\n"
210 "\n"
211 " --add-testfilefactory-readurls [URLS]\n"
212 " Add multiple urls from a ',' delimited string of urls to be used\n"
213 " in the TestFileFactory read tests.\n"
214 "\n"
215 " --add-testfilefactory-writeurl [URL]\n"
216 " Add a url to be used int the TestFileFactory write tests.\n"
217 "\n"
218 " --add-testfilefactory-writeurls [URLS]\n"
219 " Add multiple urls from a ',' delimited string of urls to be used\n"
220 " in the TestFileFactory write tests.\n"
221 "\n"
222 " --set-testfilefactory-writeinputfile [FILE]\n"
223 " Set the path to the input file used in the TestFileFactory write tests.\n"
224 "\n"
225 " --add-advancedsettings-file [FILE]\n"
226 " Add an advanced settings file to be loaded in test cases that use them.\n"
227 "\n"
228 " --add-advancedsettings-files [FILES]\n"
229 " Add multiple advanced settings files from a ',' delimited string of\n"
230 " files to be loaded in test cases that use them.\n"
231 "\n"
232 " --add-guisettings-file [FILE]\n"
233 " Add a GUI settings file to be loaded in test cases that use them.\n"
234 "\n"
235 " --add-guisettings-files [FILES]\n"
236 " Add multiple GUI settings files from a ',' delimited string of\n"
237 " files to be loaded in test cases that use them.\n"
238 "\n"
239 " --set-probability [PROBABILITY]\n"
240 " Set the probability variable used by the file corrupting functions.\n"
241 " The variable should be a double type from 0.0 to 1.0. Values given\n"
242 " less than 0.0 are treated as 0.0. Values greater than 1.0 are treated\n"
243 " as 1.0. The default probability is 0.01.\n"
246 void CXBMCTestUtils::ParseArgs(int argc, char **argv)
248 int i;
249 std::string arg;
250 for (i = 1; i < argc; i++)
252 arg = argv[i];
253 if (arg == "--add-testfilefactory-readurl")
255 TestFileFactoryReadUrls.emplace_back(argv[++i]);
257 else if (arg == "--add-testfilefactory-readurls")
259 arg = argv[++i];
260 std::vector<std::string> urls = StringUtils::Split(arg, ",");
261 for (const auto& it : urls)
262 TestFileFactoryReadUrls.push_back(it);
264 else if (arg == "--add-testfilefactory-writeurl")
266 TestFileFactoryWriteUrls.emplace_back(argv[++i]);
268 else if (arg == "--add-testfilefactory-writeurls")
270 arg = argv[++i];
271 std::vector<std::string> urls = StringUtils::Split(arg, ",");
272 for (const auto& it : urls)
273 TestFileFactoryWriteUrls.push_back(it);
275 else if (arg == "--set-testfilefactory-writeinputfile")
277 TestFileFactoryWriteInputFile = argv[++i];
279 else if (arg == "--add-advancedsettings-file")
281 AdvancedSettingsFiles.emplace_back(argv[++i]);
283 else if (arg == "--add-advancedsettings-files")
285 arg = argv[++i];
286 std::vector<std::string> urls = StringUtils::Split(arg, ",");
287 for (const auto& it : urls)
288 AdvancedSettingsFiles.push_back(it);
290 else if (arg == "--add-guisettings-file")
292 GUISettingsFiles.emplace_back(argv[++i]);
294 else if (arg == "--add-guisettings-files")
296 arg = argv[++i];
297 std::vector<std::string> urls = StringUtils::Split(arg, ",");
298 for (const auto& it : urls)
299 GUISettingsFiles.push_back(it);
301 else if (arg == "--set-probability")
303 probability = atof(argv[++i]);
304 if (probability < 0.0)
305 probability = 0.0;
306 else if (probability > 1.0)
307 probability = 1.0;
309 else
311 std::cerr << usage;
312 exit(EXIT_FAILURE);
317 std::string CXBMCTestUtils::getNewLineCharacters() const
319 #ifdef TARGET_WINDOWS
320 return "\r\n";
321 #else
322 return "\n";
323 #endif