TestUtil.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #include "TestUtil.h"
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <cerrno>
  6. #include <cstring>
  7. #include <sstream>
  8. #include <fstream>
  9. #include "a2io.h"
  10. #include "File.h"
  11. #include "FatalException.h"
  12. #include "Cookie.h"
  13. #include "DefaultDiskWriter.h"
  14. #include "fmt.h"
  15. #include "util.h"
  16. #include "RequestGroupMan.h"
  17. #include "RequestGroup.h"
  18. #include "DownloadContext.h"
  19. #include "Option.h"
  20. #include "FileEntry.h"
  21. #include "DownloadResult.h"
  22. #include "message_digest_helper.h"
  23. namespace aria2 {
  24. void createFile(const std::string& path, size_t length)
  25. {
  26. File(File(path).getDirname()).mkdirs();
  27. DefaultDiskWriter dw(path);
  28. dw.initAndOpenFile();
  29. dw.truncate(length);
  30. }
  31. std::string readFile(const std::string& path)
  32. {
  33. std::stringstream ss;
  34. std::ifstream in(path.c_str(), std::ios::binary);
  35. char buf[4_k];
  36. while (1) {
  37. in.read(buf, sizeof(buf));
  38. ss.write(buf, in.gcount());
  39. if (in.gcount() != sizeof(buf)) {
  40. break;
  41. }
  42. }
  43. return ss.str();
  44. }
  45. std::unique_ptr<Cookie> createCookie(const std::string& name,
  46. const std::string& value,
  47. const std::string& domain, bool hostOnly,
  48. const std::string& path, bool secure)
  49. {
  50. return make_unique<Cookie>(name, value, 0, false, domain, hostOnly, path,
  51. secure, false, 0);
  52. }
  53. std::unique_ptr<Cookie> createCookie(const std::string& name,
  54. const std::string& value,
  55. time_t expiryTime,
  56. const std::string& domain, bool hostOnly,
  57. const std::string& path, bool secure)
  58. {
  59. return make_unique<Cookie>(name, value, expiryTime, true, domain, hostOnly,
  60. path, secure, false, 0);
  61. }
  62. std::string fromHex(const std::string& s)
  63. {
  64. return util::fromHex(s.begin(), s.end());
  65. }
  66. std::string fileHexDigest(MessageDigest* ctx, const std::string& filename)
  67. {
  68. std::shared_ptr<DiskWriter> writer(new DefaultDiskWriter(filename));
  69. writer->openExistingFile();
  70. return util::toHex(message_digest::digest(ctx, writer, 0, writer->size()));
  71. }
  72. WrDiskCacheEntry::DataCell* createDataCell(int64_t goff, const char* data,
  73. size_t offset)
  74. {
  75. WrDiskCacheEntry::DataCell* cell = new WrDiskCacheEntry::DataCell();
  76. cell->goff = goff;
  77. size_t len = strlen(data);
  78. cell->data = new unsigned char[len];
  79. memcpy(cell->data, data, len);
  80. cell->offset = offset;
  81. cell->len = cell->capacity = len - offset;
  82. return cell;
  83. }
  84. std::shared_ptr<RequestGroup> findReservedGroup(RequestGroupMan* rgman,
  85. a2_gid_t gid)
  86. {
  87. auto rg = rgman->findGroup(gid);
  88. if (rg) {
  89. if (rg->getState() == RequestGroup::STATE_WAITING) {
  90. return rg;
  91. }
  92. else {
  93. rg.reset();
  94. }
  95. }
  96. return rg;
  97. }
  98. std::shared_ptr<RequestGroup> getReservedGroup(RequestGroupMan* rgman,
  99. size_t index)
  100. {
  101. assert(rgman->getReservedGroups().size() > index);
  102. auto i = rgman->getReservedGroups().begin();
  103. std::advance(i, index);
  104. return *i;
  105. }
  106. std::shared_ptr<RequestGroup>
  107. createRequestGroup(int32_t pieceLength, int64_t totalLength,
  108. const std::string& path, const std::string& uri,
  109. const std::shared_ptr<Option>& opt)
  110. {
  111. std::shared_ptr<DownloadContext> dctx(
  112. new DownloadContext(pieceLength, totalLength, path));
  113. std::vector<std::string> uris;
  114. uris.push_back(uri);
  115. dctx->getFirstFileEntry()->addUris(uris.begin(), uris.end());
  116. std::shared_ptr<RequestGroup> group(new RequestGroup(GroupId::create(), opt));
  117. group->setDownloadContext(dctx);
  118. return group;
  119. }
  120. std::shared_ptr<DownloadResult> createDownloadResult(error_code::Value result,
  121. const std::string& uri)
  122. {
  123. std::vector<std::string> uris;
  124. uris.push_back(uri);
  125. std::shared_ptr<FileEntry> entry(new FileEntry("/tmp/path", 1, 0, uris));
  126. std::vector<std::shared_ptr<FileEntry>> entries;
  127. entries.push_back(entry);
  128. std::shared_ptr<DownloadResult> dr(new DownloadResult());
  129. dr->gid = GroupId::create();
  130. dr->fileEntries = entries;
  131. dr->result = result;
  132. dr->belongsTo = 0;
  133. dr->inMemoryDownload = false;
  134. dr->option = std::shared_ptr<Option>(new Option());
  135. return dr;
  136. }
  137. } // namespace aria2