TestUtil.cc 4.4 KB

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