[WASAPI] fix stream types and frequencies enumeration
[xbmc.git] / xbmc / test / TestUtils.cpp
blob5face64555a4b8afc6a35f1727bbf12862b1ddb8
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 const std::string& getTempFilePath() const { return m_ptempFilePath; }
56 std::string getTempFileDirectory() const
58 return URIUtils::GetDirectory(m_ptempFilePath);
60 private:
61 std::string m_ptempFilePath;
64 CXBMCTestUtils::CXBMCTestUtils()
66 probability = 0.01;
69 CXBMCTestUtils &CXBMCTestUtils::Instance()
71 static CXBMCTestUtils instance;
72 return instance;
75 std::string CXBMCTestUtils::ReferenceFilePath(const std::string& path)
77 return CSpecialProtocol::TranslatePath(URIUtils::AddFileToFolder("special://xbmc", path));
80 bool CXBMCTestUtils::SetReferenceFileBasePath()
82 std::string xbmcPath = CUtil::GetHomePath();
83 if (xbmcPath.empty())
84 return false;
86 /* Set xbmc, xbmcbin and home path */
87 CSpecialProtocol::SetXBMCPath(xbmcPath);
88 CSpecialProtocol::SetXBMCBinPath(xbmcPath);
89 CSpecialProtocol::SetHomePath(URIUtils::AddFileToFolder(xbmcPath, "portable_data"));
91 return true;
94 XFILE::CFile *CXBMCTestUtils::CreateTempFile(std::string const& suffix)
96 CTempFile *f = new CTempFile();
97 if (f->Create(suffix))
98 return f;
99 delete f;
100 return NULL;
103 bool CXBMCTestUtils::DeleteTempFile(XFILE::CFile *tempfile)
105 if (!tempfile)
106 return true;
107 CTempFile *f = static_cast<CTempFile*>(tempfile);
108 bool retval = f->Delete();
109 delete f;
110 return retval;
113 std::string CXBMCTestUtils::TempFilePath(XFILE::CFile const* const tempfile)
115 if (!tempfile)
116 return "";
117 CTempFile const* const f = static_cast<CTempFile const*>(tempfile);
118 return f->getTempFilePath();
121 std::string CXBMCTestUtils::TempFileDirectory(XFILE::CFile const* const tempfile)
123 if (!tempfile)
124 return "";
125 CTempFile const* const f = static_cast<CTempFile const*>(tempfile);
126 return f->getTempFileDirectory();
129 XFILE::CFile *CXBMCTestUtils::CreateCorruptedFile(std::string const& strFileName,
130 std::string const& suffix)
132 XFILE::CFile inputfile, *tmpfile = CreateTempFile(suffix);
133 unsigned char buf[20], tmpchar;
134 ssize_t size, i;
136 if (tmpfile && inputfile.Open(strFileName))
138 srand(time(NULL));
139 while ((size = inputfile.Read(buf, sizeof(buf))) > 0)
141 for (i = 0; i < size; i++)
143 if ((rand() % RAND_MAX) < (probability * RAND_MAX))
145 tmpchar = buf[i];
148 buf[i] = (rand() % 256);
149 } while (buf[i] == tmpchar);
152 if (tmpfile->Write(buf, size) < 0)
154 inputfile.Close();
155 tmpfile->Close();
156 DeleteTempFile(tmpfile);
157 return NULL;
160 inputfile.Close();
161 tmpfile->Close();
162 return tmpfile;
164 delete tmpfile;
165 return NULL;
169 std::vector<std::string> &CXBMCTestUtils::getTestFileFactoryReadUrls()
171 return TestFileFactoryReadUrls;
174 std::vector<std::string> &CXBMCTestUtils::getTestFileFactoryWriteUrls()
176 return TestFileFactoryWriteUrls;
179 std::string &CXBMCTestUtils::getTestFileFactoryWriteInputFile()
181 return TestFileFactoryWriteInputFile;
184 void CXBMCTestUtils::setTestFileFactoryWriteInputFile(std::string const& file)
186 TestFileFactoryWriteInputFile = file;
189 std::vector<std::string> &CXBMCTestUtils::getAdvancedSettingsFiles()
191 return AdvancedSettingsFiles;
194 std::vector<std::string> &CXBMCTestUtils::getGUISettingsFiles()
196 return GUISettingsFiles;
199 static const char usage[] =
200 "Kodi Test Suite\n"
201 "Usage: kodi-test [options]\n"
202 "\n"
203 "The following options are recognized by the kodi-test program.\n"
204 "\n"
205 " --add-testfilefactory-readurl [URL]\n"
206 " Add a url to be used int the TestFileFactory read tests.\n"
207 "\n"
208 " --add-testfilefactory-readurls [URLS]\n"
209 " Add multiple urls from a ',' delimited string of urls to be used\n"
210 " in the TestFileFactory read tests.\n"
211 "\n"
212 " --add-testfilefactory-writeurl [URL]\n"
213 " Add a url to be used int the TestFileFactory write tests.\n"
214 "\n"
215 " --add-testfilefactory-writeurls [URLS]\n"
216 " Add multiple urls from a ',' delimited string of urls to be used\n"
217 " in the TestFileFactory write tests.\n"
218 "\n"
219 " --set-testfilefactory-writeinputfile [FILE]\n"
220 " Set the path to the input file used in the TestFileFactory write tests.\n"
221 "\n"
222 " --add-advancedsettings-file [FILE]\n"
223 " Add an advanced settings file to be loaded in test cases that use them.\n"
224 "\n"
225 " --add-advancedsettings-files [FILES]\n"
226 " Add multiple advanced settings files from a ',' delimited string of\n"
227 " files to be loaded in test cases that use them.\n"
228 "\n"
229 " --add-guisettings-file [FILE]\n"
230 " Add a GUI settings file to be loaded in test cases that use them.\n"
231 "\n"
232 " --add-guisettings-files [FILES]\n"
233 " Add multiple GUI settings files from a ',' delimited string of\n"
234 " files to be loaded in test cases that use them.\n"
235 "\n"
236 " --set-probability [PROBABILITY]\n"
237 " Set the probability variable used by the file corrupting functions.\n"
238 " The variable should be a double type from 0.0 to 1.0. Values given\n"
239 " less than 0.0 are treated as 0.0. Values greater than 1.0 are treated\n"
240 " as 1.0. The default probability is 0.01.\n"
243 void CXBMCTestUtils::ParseArgs(int argc, char **argv)
245 int i;
246 std::string arg;
247 for (i = 1; i < argc; i++)
249 arg = argv[i];
250 if (arg == "--add-testfilefactory-readurl")
252 TestFileFactoryReadUrls.emplace_back(argv[++i]);
254 else if (arg == "--add-testfilefactory-readurls")
256 arg = argv[++i];
257 std::vector<std::string> urls = StringUtils::Split(arg, ",");
258 for (const auto& it : urls)
259 TestFileFactoryReadUrls.push_back(it);
261 else if (arg == "--add-testfilefactory-writeurl")
263 TestFileFactoryWriteUrls.emplace_back(argv[++i]);
265 else if (arg == "--add-testfilefactory-writeurls")
267 arg = argv[++i];
268 std::vector<std::string> urls = StringUtils::Split(arg, ",");
269 for (const auto& it : urls)
270 TestFileFactoryWriteUrls.push_back(it);
272 else if (arg == "--set-testfilefactory-writeinputfile")
274 TestFileFactoryWriteInputFile = argv[++i];
276 else if (arg == "--add-advancedsettings-file")
278 AdvancedSettingsFiles.emplace_back(argv[++i]);
280 else if (arg == "--add-advancedsettings-files")
282 arg = argv[++i];
283 std::vector<std::string> urls = StringUtils::Split(arg, ",");
284 for (const auto& it : urls)
285 AdvancedSettingsFiles.push_back(it);
287 else if (arg == "--add-guisettings-file")
289 GUISettingsFiles.emplace_back(argv[++i]);
291 else if (arg == "--add-guisettings-files")
293 arg = argv[++i];
294 std::vector<std::string> urls = StringUtils::Split(arg, ",");
295 for (const auto& it : urls)
296 GUISettingsFiles.push_back(it);
298 else if (arg == "--set-probability")
300 probability = atof(argv[++i]);
301 if (probability < 0.0)
302 probability = 0.0;
303 else if (probability > 1.0)
304 probability = 1.0;
306 else
308 std::cerr << usage;
309 exit(EXIT_FAILURE);
314 std::string CXBMCTestUtils::getNewLineCharacters() const
316 #ifdef TARGET_WINDOWS
317 return "\r\n";
318 #else
319 return "\n";
320 #endif