ArchiveTests.cpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include <AzCore/UnitTest/TestTypes.h>
  9. #include <AzCore/Asset/AssetManagerBus.h>
  10. #include <AzCore/Math/Uuid.h>
  11. #include <AzCore/Memory/Memory.h>
  12. #include <AzCore/std/smart_ptr/unique_ptr.h>
  13. #include <AzCore/UserSettings/UserSettingsComponent.h>
  14. #include <AzCore/IO/FileIO.h>
  15. #include <AZTestShared/Utils/Utils.h>
  16. #include <AzToolsFramework/Archive/ArchiveAPI.h>
  17. #include <AzFramework/StringFunc/StringFunc.h>
  18. #include <AzToolsFramework/Archive/ArchiveAPI.h>
  19. #include <AzToolsFramework/AssetBundle/AssetBundleAPI.h>
  20. #include <AzToolsFramework/UnitTest/ToolsTestApplication.h>
  21. #include <QString>
  22. #include <QDir>
  23. #include <QFileInfo>
  24. #include <QStandardPaths>
  25. #include <QTemporaryDir>
  26. #include <QTextStream>
  27. namespace UnitTest
  28. {
  29. namespace
  30. {
  31. bool CreateDummyFile(const QString& fullPathToFile, const QString& tempStr = {})
  32. {
  33. QFileInfo fi(fullPathToFile);
  34. QDir fp(fi.path());
  35. fp.mkpath(".");
  36. QFile writer(fullPathToFile);
  37. if (!writer.open(QFile::ReadWrite))
  38. {
  39. return false;
  40. }
  41. {
  42. QTextStream stream(&writer);
  43. stream << tempStr << Qt::endl;
  44. }
  45. writer.close();
  46. return true;
  47. }
  48. class ArchiveComponentTest :
  49. public UnitTest::LeakDetectionFixture
  50. {
  51. public:
  52. QStringList CreateArchiveFileList()
  53. {
  54. QStringList returnList;
  55. returnList.append("basicfile.txt");
  56. returnList.append("basicfile2.txt");
  57. returnList.append("testfolder/folderfile.txt");
  58. returnList.append("testfolder2/sharedfolderfile.txt");
  59. returnList.append("testfolder2/sharedfolderfile2.txt");
  60. returnList.append("testfolder3/testfolder4/depthfile.bat");
  61. return returnList;
  62. }
  63. QString GetArchiveFolderName()
  64. {
  65. return "archive";
  66. }
  67. QString GetExtractFolderName()
  68. {
  69. return "extracted";
  70. }
  71. void CreateArchiveFolder(QString archiveFolderName, QStringList fileList)
  72. {
  73. QDir tempPath = QDir(m_tempDir.GetDirectory()).filePath(archiveFolderName);
  74. for (const auto& thisFile : fileList)
  75. {
  76. QString absoluteTestFilePath = tempPath.absoluteFilePath(thisFile);
  77. EXPECT_TRUE(CreateDummyFile(absoluteTestFilePath));
  78. }
  79. }
  80. QString CreateArchiveListTextFile()
  81. {
  82. QString listFilePath = QDir(m_tempDir.GetDirectory()).absoluteFilePath("filelist.txt");
  83. QString textContent = CreateArchiveFileList().join("\n");
  84. EXPECT_TRUE(CreateDummyFile(listFilePath, textContent));
  85. return listFilePath;
  86. }
  87. void CreateArchiveFolder()
  88. {
  89. CreateArchiveFolder(GetArchiveFolderName(), CreateArchiveFileList());
  90. }
  91. QString GetArchivePath()
  92. {
  93. return QDir(m_tempDir.GetDirectory()).filePath("TestArchive.pak");
  94. }
  95. QString GetArchiveFolder()
  96. {
  97. return QDir(m_tempDir.GetDirectory()).filePath(GetArchiveFolderName());
  98. }
  99. QString GetExtractFolder()
  100. {
  101. return QDir(m_tempDir.GetDirectory()).filePath(GetExtractFolderName());
  102. }
  103. bool CreateArchive()
  104. {
  105. std::future<bool> createResult;
  106. AzToolsFramework::ArchiveCommandsBus::BroadcastResult(createResult,
  107. &AzToolsFramework::ArchiveCommandsBus::Events::CreateArchive,
  108. GetArchivePath().toUtf8().constData(), GetArchiveFolder().toUtf8().constData());
  109. bool result = createResult.get();
  110. return result;
  111. }
  112. void SetUp() override
  113. {
  114. m_app.reset(aznew ToolsTestApplication("ArchiveComponentTest"));
  115. AZ::ComponentApplication::StartupParameters startupParameters;
  116. startupParameters.m_loadSettingsRegistry = false;
  117. m_app->Start(AzFramework::Application::Descriptor(), startupParameters);
  118. // Without this, the user settings component would attempt to save on finalize/shutdown. Since the file is
  119. // shared across the whole engine, if multiple tests are run in parallel, the saving could cause a crash
  120. // in the unit tests.
  121. AZ::UserSettingsComponentRequestBus::Broadcast(&AZ::UserSettingsComponentRequests::DisableSaveOnFinalize);
  122. if (auto fileIoBase = AZ::IO::FileIOBase::GetInstance(); fileIoBase != nullptr)
  123. {
  124. QDir cacheFolder(m_tempDir.GetDirectory());
  125. // set the product tree folder to somewhere besides the root temp dir.
  126. // This is to avoid error spam - if you try to write to the Cache folder or a subfolder,
  127. // AZ::IO will issue an error, since the cache is supposed to be read-only.
  128. // here we set it to (tempFolder)/Cache subfolder so that if you want a folder in your test to act like
  129. // the read-only cache folder, you can use that folder, but otherwise, all other folders are fair game to
  130. // use for your tests without triggering the "you cannot write to the cache" error.
  131. fileIoBase->SetAlias("@products@", cacheFolder.absoluteFilePath("Cache").toUtf8().constData());
  132. }
  133. }
  134. void TearDown() override
  135. {
  136. m_app->Stop();
  137. m_app.reset();
  138. }
  139. AZStd::unique_ptr<ToolsTestApplication> m_app;
  140. AZ::Test::ScopedAutoTempDirectory m_tempDir;
  141. };
  142. TEST_F(ArchiveComponentTest, CreateArchive_FilesAtThreeDepths_ArchiveCreated)
  143. {
  144. CreateArchiveFolder();
  145. bool createResult = CreateArchive();
  146. EXPECT_TRUE(createResult);
  147. }
  148. TEST_F(ArchiveComponentTest, ListFilesInArchive_FilesAtThreeDepths_FilesFound)
  149. {
  150. CreateArchiveFolder();
  151. EXPECT_EQ(CreateArchive(), true);
  152. AZStd::vector<AZStd::string> fileList;
  153. bool listResult{ false };
  154. AzToolsFramework::ArchiveCommandsBus::BroadcastResult(listResult,
  155. &AzToolsFramework::ArchiveCommandsBus::Events::ListFilesInArchive,
  156. GetArchivePath().toUtf8().constData(), fileList);
  157. EXPECT_TRUE(listResult);
  158. EXPECT_EQ(fileList.size(), 6);
  159. }
  160. TEST_F(ArchiveComponentTest, CreateDeltaCatalog_AssetsNotRegistered_Failure)
  161. {
  162. QStringList fileList = CreateArchiveFileList();
  163. CreateArchiveFolder(GetArchiveFolderName(), fileList);
  164. bool createResult = CreateArchive();
  165. EXPECT_EQ(createResult, true);
  166. bool catalogCreated{ true };
  167. AZ::Test::AssertAbsorber assertAbsorber;
  168. AzToolsFramework::AssetBundleCommandsBus::BroadcastResult(catalogCreated,
  169. &AzToolsFramework::AssetBundleCommandsBus::Events::CreateDeltaCatalog, GetArchivePath().toUtf8().constData(), true);
  170. EXPECT_EQ(catalogCreated, false);
  171. }
  172. TEST_F(ArchiveComponentTest, AddFilesToArchive_FromListFile_Success)
  173. {
  174. QString listFile = CreateArchiveListTextFile();
  175. CreateArchiveFolder(GetArchiveFolderName(), CreateArchiveFileList());
  176. std::future<bool> addResult;
  177. AzToolsFramework::ArchiveCommandsBus::BroadcastResult(
  178. addResult, &AzToolsFramework::ArchiveCommandsBus::Events::AddFilesToArchive, GetArchivePath().toUtf8().constData(),
  179. GetArchiveFolder().toUtf8().constData(), listFile.toUtf8().constData());
  180. bool result = addResult.get();
  181. EXPECT_TRUE(result);
  182. }
  183. TEST_F(ArchiveComponentTest, ExtractArchive_AllFiles_Success)
  184. {
  185. CreateArchiveFolder();
  186. bool createResult = CreateArchive();
  187. EXPECT_TRUE(createResult);
  188. std::future<bool> extractResult;
  189. AzToolsFramework::ArchiveCommandsBus::BroadcastResult(
  190. extractResult, &AzToolsFramework::ArchiveCommandsBus::Events::ExtractArchive, GetArchivePath().toUtf8().constData(),
  191. GetExtractFolder().toUtf8().constData());
  192. bool result = extractResult.get();
  193. EXPECT_TRUE(result);
  194. QStringList archiveFiles = CreateArchiveFileList();
  195. for (const auto& file : archiveFiles)
  196. {
  197. QString fullFilePath = QDir(GetExtractFolder()).absoluteFilePath(file);
  198. QFileInfo fi(fullFilePath);
  199. EXPECT_TRUE(fi.exists());
  200. }
  201. }
  202. TEST_F(ArchiveComponentTest, CreateDeltaCatalog_ArchiveWithoutCatalogAssetsRegistered_Success)
  203. {
  204. QStringList fileList = CreateArchiveFileList();
  205. CreateArchiveFolder(GetArchiveFolderName(), fileList);
  206. bool createResult = CreateArchive();
  207. EXPECT_EQ(createResult, true);
  208. for (const auto& thisPath : fileList)
  209. {
  210. AZ::Data::AssetInfo newInfo;
  211. newInfo.m_relativePath = thisPath.toUtf8().constData();
  212. newInfo.m_assetType = AZ::Uuid::CreateRandom();
  213. newInfo.m_sizeBytes = 100; // Arbitrary
  214. AZ::Data::AssetId generatedID(AZ::Uuid::CreateRandom());
  215. newInfo.m_assetId = generatedID;
  216. AZ::Data::AssetCatalogRequestBus::Broadcast(&AZ::Data::AssetCatalogRequestBus::Events::RegisterAsset, generatedID, newInfo);
  217. }
  218. AZ_TEST_START_TRACE_SUPPRESSION;
  219. bool catalogCreated{ false };
  220. AzToolsFramework::AssetBundleCommandsBus::BroadcastResult(catalogCreated, &AzToolsFramework::AssetBundleCommandsBus::Events::CreateDeltaCatalog, GetArchivePath().toUtf8().constData(), true);
  221. AZ_TEST_STOP_TRACE_SUPPRESSION_NO_COUNT; // the above raises at least one complaint, but is os specific, since it creates a file in the cache (and then deletes it)
  222. EXPECT_EQ(catalogCreated, true);
  223. }
  224. TEST_F(ArchiveComponentTest, SUITE_periodic_ArchiveAsyncMemoryCorruptionTest)
  225. {
  226. // simulate the way the Asset Processor might create many archives asynchronously, overlapping.
  227. // The general pattern the AP uses is that NCPUs threads are created, and each thread could be creating an archive
  228. // at the same time. Each thread is operating on its own temp directory, and calls two APIs:
  229. // CreateArchive (every time), and then AddFileToArchive (some of the time).
  230. // There is always a file in the archive, but not always one in the extra API call.
  231. // to simulate this, we're going to start 8 threads
  232. // those 8 threads will continuously create files in a folder, then archive them, then add additional files to that archive.
  233. const int numThreads = 8;
  234. const int numIterationsPerThread = 100; // takes about 20sec in debug on good HW with ASAN, much faster in profile.
  235. auto threadFn =
  236. [this](int threadIndex, int iterations)
  237. {
  238. const int numDummyFiles = 5;
  239. for (int iteration = 0; iteration < iterations; ++iteration)
  240. {
  241. // create a temp folder and then 5 dummy files in that folder to represent the files that will be archived
  242. // tempfolder/archive_n_n = folder containing files to archive initially, in the "CreateArchive" API call.
  243. // tempfolder/extra_n_n = folder containing files to add to archive afterwards, in the "AddFilesToArchive" API call.
  244. // tempfolder/TestArchive_n_n.zip = archive output file.
  245. // tempfolder/extra_n_n/filelist.txt = list of files to add to archive in the "AddFilesToArchive" call.
  246. // we do not attempt to read the archive back, this is just a thrash test.
  247. QString folderName = QString("archive%1_%2").arg(threadIndex).arg(iteration);
  248. QString extraFolderName = QString("extra%1_%2").arg(threadIndex).arg(iteration);
  249. QString archivePath = QDir(m_tempDir.GetDirectory()).filePath(QString("TestArchive%1_%2.zip").arg(threadIndex).arg(iteration));
  250. QString folderPath = QDir(m_tempDir.GetDirectory()).filePath(folderName);
  251. QString extraFolderPath = QDir(m_tempDir.GetDirectory()).filePath(extraFolderName);
  252. QString dummyFileContent;
  253. for (int fileToArchive = 0; fileToArchive < numDummyFiles; ++fileToArchive)
  254. {
  255. QString filePath = QDir(folderPath).filePath(QString("file%1.txt").arg(fileToArchive));
  256. QString extraFileName = QString("extrafile%1.txt").arg(fileToArchive);
  257. QString extraFilePath = QDir(extraFolderPath).filePath(extraFileName);
  258. CreateDummyFile(filePath, QString(1024 * iteration, QChar('C')));
  259. CreateDummyFile(extraFilePath, QString(1024 * iteration, QChar('C')));
  260. dummyFileContent.append(QDir::toNativeSeparators(extraFileName));
  261. dummyFileContent.append("\n");
  262. }
  263. QString fileListPath = QDir(extraFolderPath).filePath("filelist.txt");
  264. CreateDummyFile(fileListPath, dummyFileContent);
  265. std::future<bool> createResult;
  266. AzToolsFramework::ArchiveCommandsBus::BroadcastResult(
  267. createResult,
  268. &AzToolsFramework::ArchiveCommandsBus::Events::CreateArchive,
  269. archivePath.toUtf8().constData(),
  270. folderPath.toUtf8().constData());
  271. EXPECT_TRUE(createResult.valid() ? createResult.get() : false);
  272. std::future<bool> addResult;
  273. AzToolsFramework::ArchiveCommandsBus::BroadcastResult(
  274. addResult,
  275. &AzToolsFramework::ArchiveCommandsBus::Events::AddFilesToArchive,
  276. archivePath.toUtf8().constData(),
  277. extraFolderPath.toUtf8().constData(),
  278. fileListPath.toUtf8().constData());
  279. EXPECT_TRUE(addResult.valid() ? addResult.get() : false);
  280. }
  281. };
  282. // spawn 8 threads to do the above and then wait for all of them to complete.
  283. AZStd::vector<AZStd::thread> threads;
  284. for (int i = 0; i < numThreads; ++i)
  285. {
  286. threads.emplace_back(threadFn, i, numIterationsPerThread);
  287. }
  288. for (int i = 0; i < numThreads; ++i)
  289. {
  290. if (threads[i].joinable())
  291. {
  292. threads[i].join();
  293. }
  294. }
  295. }
  296. }
  297. }