TestUtil.cc 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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[4096];
  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
  46. (const std::string& name,
  47. const std::string& value,
  48. const std::string& domain,
  49. bool hostOnly,
  50. const std::string& path,
  51. bool secure)
  52. {
  53. return make_unique<Cookie>
  54. (name, value, 0, false, domain, hostOnly, path, secure, false, 0);
  55. }
  56. std::unique_ptr<Cookie> createCookie
  57. (const std::string& name,
  58. const std::string& value,
  59. time_t expiryTime,
  60. const std::string& domain,
  61. bool hostOnly,
  62. const std::string& path,
  63. bool secure)
  64. {
  65. return make_unique<Cookie>
  66. (name, value, expiryTime, true, domain, hostOnly, path, secure, false, 0);
  67. }
  68. std::string fromHex(const std::string& s)
  69. {
  70. return util::fromHex(s.begin(), s.end());
  71. }
  72. std::string fileHexDigest(MessageDigest* ctx, const std::string& filename)
  73. {
  74. std::shared_ptr<DiskWriter> writer(new DefaultDiskWriter(filename));
  75. writer->openExistingFile();
  76. return util::toHex(message_digest::digest(ctx, writer, 0, writer->size()));
  77. }
  78. WrDiskCacheEntry::DataCell* createDataCell(int64_t goff,
  79. const char* data,
  80. size_t offset)
  81. {
  82. WrDiskCacheEntry::DataCell* cell = new WrDiskCacheEntry::DataCell();
  83. cell->goff = goff;
  84. size_t len = strlen(data);
  85. cell->data = new unsigned char[len];
  86. memcpy(cell->data, data, len);
  87. cell->offset = offset;
  88. cell->len = cell->capacity = len - offset;
  89. return cell;
  90. }
  91. std::shared_ptr<RequestGroup> findReservedGroup
  92. (RequestGroupMan* rgman, a2_gid_t gid)
  93. {
  94. auto rg = rgman->findGroup(gid);
  95. if(rg) {
  96. if(rg->getState() == RequestGroup::STATE_WAITING) {
  97. return rg;
  98. } else {
  99. rg.reset();
  100. }
  101. }
  102. return rg;
  103. }
  104. std::shared_ptr<RequestGroup> getReservedGroup
  105. (RequestGroupMan* rgman, size_t index)
  106. {
  107. assert(rgman->getReservedGroups().size() > index);
  108. auto i = rgman->getReservedGroups().begin();
  109. std::advance(i, index);
  110. return *i;
  111. }
  112. std::shared_ptr<RequestGroup> createRequestGroup(int32_t pieceLength,
  113. int64_t totalLength,
  114. const std::string& path,
  115. const std::string& uri,
  116. const std::shared_ptr<Option>& opt)
  117. {
  118. std::shared_ptr<DownloadContext> dctx(new DownloadContext(pieceLength,
  119. totalLength,
  120. path));
  121. std::vector<std::string> uris;
  122. uris.push_back(uri);
  123. dctx->getFirstFileEntry()->addUris(uris.begin(), uris.end());
  124. std::shared_ptr<RequestGroup> group(new RequestGroup(GroupId::create(), opt));
  125. group->setDownloadContext(dctx);
  126. return group;
  127. }
  128. std::shared_ptr<DownloadResult> createDownloadResult
  129. (error_code::Value result, const std::string& uri)
  130. {
  131. std::vector<std::string> uris;
  132. uris.push_back(uri);
  133. std::shared_ptr<FileEntry> entry(new FileEntry("/tmp/path", 1, 0, uris));
  134. std::vector<std::shared_ptr<FileEntry> > entries;
  135. entries.push_back(entry);
  136. std::shared_ptr<DownloadResult> dr(new DownloadResult());
  137. dr->gid = GroupId::create();
  138. dr->fileEntries = entries;
  139. dr->result = result;
  140. dr->belongsTo = 0;
  141. dr->inMemoryDownload = false;
  142. dr->option = std::shared_ptr<Option>(new Option());
  143. return dr;
  144. }
  145. } // namespace aria2