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.
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"
25 #include <system_error>
27 namespace fs
= KODI::PLATFORM::FILESYSTEM
;
29 class CTempFile
: public XFILE::CFile
32 CTempFile() = default;
37 bool Create(const std::string
&suffix
)
40 m_ptempFilePath
= fs::temp_file_path(suffix
, ec
);
44 if (m_ptempFilePath
.empty())
47 OpenForWrite(m_ptempFilePath
, true);
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
);
61 std::string m_ptempFilePath
;
64 CXBMCTestUtils::CXBMCTestUtils()
69 CXBMCTestUtils
&CXBMCTestUtils::Instance()
71 static CXBMCTestUtils 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();
86 /* Set xbmc, xbmcbin and home path */
87 CSpecialProtocol::SetXBMCPath(xbmcPath
);
88 CSpecialProtocol::SetXBMCBinPath(xbmcPath
);
89 CSpecialProtocol::SetHomePath(URIUtils::AddFileToFolder(xbmcPath
, "portable_data"));
94 XFILE::CFile
*CXBMCTestUtils::CreateTempFile(std::string
const& suffix
)
96 CTempFile
*f
= new CTempFile();
97 if (f
->Create(suffix
))
103 bool CXBMCTestUtils::DeleteTempFile(XFILE::CFile
*tempfile
)
107 CTempFile
*f
= static_cast<CTempFile
*>(tempfile
);
108 bool retval
= f
->Delete();
113 std::string
CXBMCTestUtils::TempFilePath(XFILE::CFile
const* const tempfile
)
117 CTempFile
const* const f
= static_cast<CTempFile
const*>(tempfile
);
118 return f
->getTempFilePath();
121 std::string
CXBMCTestUtils::TempFileDirectory(XFILE::CFile
const* const tempfile
)
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
;
136 if (tmpfile
&& inputfile
.Open(strFileName
))
139 while ((size
= inputfile
.Read(buf
, sizeof(buf
))) > 0)
141 for (i
= 0; i
< size
; i
++)
143 if ((rand() % RAND_MAX
) < (probability
* RAND_MAX
))
148 buf
[i
] = (rand() % 256);
149 } while (buf
[i
] == tmpchar
);
152 if (tmpfile
->Write(buf
, size
) < 0)
156 DeleteTempFile(tmpfile
);
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
[] =
201 "Usage: kodi-test [options]\n"
203 "The following options are recognized by the kodi-test program.\n"
205 " --add-testfilefactory-readurl [URL]\n"
206 " Add a url to be used int the TestFileFactory read tests.\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"
212 " --add-testfilefactory-writeurl [URL]\n"
213 " Add a url to be used int the TestFileFactory write tests.\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"
219 " --set-testfilefactory-writeinputfile [FILE]\n"
220 " Set the path to the input file used in the TestFileFactory write tests.\n"
222 " --add-advancedsettings-file [FILE]\n"
223 " Add an advanced settings file to be loaded in test cases that use them.\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"
229 " --add-guisettings-file [FILE]\n"
230 " Add a GUI settings file to be loaded in test cases that use them.\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"
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
)
247 for (i
= 1; i
< argc
; i
++)
250 if (arg
== "--add-testfilefactory-readurl")
252 TestFileFactoryReadUrls
.emplace_back(argv
[++i
]);
254 else if (arg
== "--add-testfilefactory-readurls")
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")
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")
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")
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)
303 else if (probability
> 1.0)
314 std::string
CXBMCTestUtils::getNewLineCharacters() const
316 #ifdef TARGET_WINDOWS