综合平台编程器项目的远程存储
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 

75 lines
2.7 KiB

  1. #include "infrastructure/runtime_project_bundle.h"
  2. #include "support/test_support.h"
  3. #include <QFile>
  4. #include <QTemporaryDir>
  5. #include <iostream>
  6. #include <stdexcept>
  7. namespace {
  8. using TestSupport::require;
  9. void writeFile(const QString &path, const QByteArray &bytes)
  10. {
  11. QFile file(path);
  12. require(file.open(QIODevice::WriteOnly), "test file must open for writing");
  13. require(file.write(bytes) == bytes.size(), "test file must be written fully");
  14. }
  15. void testBundleRoundTripAndValidation()
  16. {
  17. QTemporaryDir directory;
  18. require(directory.isValid(), "temporary directory must be available");
  19. const QString template_path = directory.filePath(QStringLiteral("template.exe"));
  20. const QString project_path = directory.filePath(QStringLiteral("project.json"));
  21. const QString bundle_path = directory.filePath(QStringLiteral("motor.exe"));
  22. const QByteArray template_bytes("MZ-template-binary");
  23. const QByteArray project_bytes(
  24. "{\"formatVersion\":\"4.0\",\"name\":\"motor\"}");
  25. writeFile(template_path, template_bytes);
  26. writeFile(project_path, project_bytes);
  27. const RuntimeProjectBundleWriteResult write_result =
  28. RuntimeProjectBundleService::write(
  29. template_path, project_path, bundle_path);
  30. require(write_result.succeeded, "runtime bundle must be written");
  31. const RuntimeProjectBundleLoadResult loaded =
  32. RuntimeProjectBundleService::load(bundle_path);
  33. require(loaded.status == RuntimeProjectBundleStatus::Loaded,
  34. "runtime bundle must be recognized");
  35. require(loaded.project_data == project_bytes,
  36. "runtime bundle must preserve project bytes");
  37. const RuntimeProjectBundleLoadResult ordinary =
  38. RuntimeProjectBundleService::load(template_path);
  39. require(ordinary.status == RuntimeProjectBundleStatus::NotFound,
  40. "ordinary executable must not be treated as a runtime bundle");
  41. QFile corrupted(bundle_path);
  42. require(corrupted.open(QIODevice::ReadWrite),
  43. "bundle must open for corruption regression");
  44. require(corrupted.seek(corrupted.size() - 1),
  45. "bundle footer must be seekable");
  46. const char invalid_byte = static_cast<char>(0xff);
  47. require(corrupted.write(&invalid_byte, 1) == 1,
  48. "bundle corruption must be writable");
  49. corrupted.close();
  50. const RuntimeProjectBundleLoadResult invalid =
  51. RuntimeProjectBundleService::load(bundle_path);
  52. require(invalid.status == RuntimeProjectBundleStatus::Invalid,
  53. "corrupted runtime bundle must be rejected");
  54. }
  55. } // namespace
  56. int main()
  57. {
  58. return TestSupport::runTestSuite("runtime project bundle tests", {
  59. {"testBundleRoundTripAndValidation", testBundleRoundTripAndValidation},
  60. });
  61. }