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 std::string
getTempFilePath() const
57 return m_ptempFilePath
;
59 std::string
getTempFileDirectory() const
61 return URIUtils::GetDirectory(m_ptempFilePath
);
64 std::string m_ptempFilePath
;
67 CXBMCTestUtils::CXBMCTestUtils()
72 CXBMCTestUtils
&CXBMCTestUtils::Instance()
74 static CXBMCTestUtils 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();
89 /* Set xbmc, xbmcbin and home path */
90 CSpecialProtocol::SetXBMCPath(xbmcPath
);
91 CSpecialProtocol::SetXBMCBinPath(xbmcPath
);
92 CSpecialProtocol::SetHomePath(URIUtils::AddFileToFolder(xbmcPath
, "portable_data"));
97 XFILE::CFile
*CXBMCTestUtils::CreateTempFile(std::string
const& suffix
)
99 CTempFile
*f
= new CTempFile();
100 if (f
->Create(suffix
))
106 bool CXBMCTestUtils::DeleteTempFile(XFILE::CFile
*tempfile
)
110 CTempFile
*f
= static_cast<CTempFile
*>(tempfile
);
111 bool retval
= f
->Delete();
116 std::string
CXBMCTestUtils::TempFilePath(XFILE::CFile
const* const tempfile
)
120 CTempFile
const* const f
= static_cast<CTempFile
const*>(tempfile
);
121 return f
->getTempFilePath();
124 std::string
CXBMCTestUtils::TempFileDirectory(XFILE::CFile
const* const tempfile
)
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
;
139 if (tmpfile
&& inputfile
.Open(strFileName
))
142 while ((size
= inputfile
.Read(buf
, sizeof(buf
))) > 0)
144 for (i
= 0; i
< size
; i
++)
146 if ((rand() % RAND_MAX
) < (probability
* RAND_MAX
))
151 buf
[i
] = (rand() % 256);
152 } while (buf
[i
] == tmpchar
);
155 if (tmpfile
->Write(buf
, size
) < 0)
159 DeleteTempFile(tmpfile
);
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
[] =
204 "Usage: kodi-test [options]\n"
206 "The following options are recognized by the kodi-test program.\n"
208 " --add-testfilefactory-readurl [URL]\n"
209 " Add a url to be used int the TestFileFactory read tests.\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"
215 " --add-testfilefactory-writeurl [URL]\n"
216 " Add a url to be used int the TestFileFactory write tests.\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"
222 " --set-testfilefactory-writeinputfile [FILE]\n"
223 " Set the path to the input file used in the TestFileFactory write tests.\n"
225 " --add-advancedsettings-file [FILE]\n"
226 " Add an advanced settings file to be loaded in test cases that use them.\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"
232 " --add-guisettings-file [FILE]\n"
233 " Add a GUI settings file to be loaded in test cases that use them.\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"
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
)
250 for (i
= 1; i
< argc
; i
++)
253 if (arg
== "--add-testfilefactory-readurl")
255 TestFileFactoryReadUrls
.emplace_back(argv
[++i
]);
257 else if (arg
== "--add-testfilefactory-readurls")
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")
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")
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")
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)
306 else if (probability
> 1.0)
317 std::string
CXBMCTestUtils::getNewLineCharacters() const
319 #ifdef TARGET_WINDOWS