TestUtil.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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. 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 Cookie
  56. (name, value, 0, false, domain, hostOnly, path, secure, false, 0);
  57. }
  58. 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 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
  76. (const SharedHandle<MessageDigest>& ctx, const std::string& filename)
  77. {
  78. SharedHandle<DiskWriter> writer(new DefaultDiskWriter(filename));
  79. writer->openExistingFile();
  80. return util::toHex(message_digest::digest(ctx, writer, 0, writer->size()));
  81. }
  82. #endif // ENABLE_MESSAGE_DIGEST
  83. WrDiskCacheEntry::DataCell* createDataCell(int64_t goff,
  84. const char* data,
  85. size_t offset)
  86. {
  87. WrDiskCacheEntry::DataCell* cell = new WrDiskCacheEntry::DataCell();
  88. cell->goff = goff;
  89. size_t len = strlen(data);
  90. cell->data = new unsigned char[len];
  91. memcpy(cell->data, data, len);
  92. cell->offset = offset;
  93. cell->len = cell->capacity = len - offset;
  94. return cell;
  95. }
  96. SharedHandle<RequestGroup> findReservedGroup
  97. (const SharedHandle<RequestGroupMan>& rgman, a2_gid_t gid)
  98. {
  99. SharedHandle<RequestGroup> rg = rgman->findGroup(gid);
  100. if(rg) {
  101. if(rg->getState() == RequestGroup::STATE_WAITING) {
  102. return rg;
  103. } else {
  104. rg.reset();
  105. }
  106. }
  107. return rg;
  108. }
  109. SharedHandle<RequestGroup> getReservedGroup
  110. (const SharedHandle<RequestGroupMan>& rgman, size_t index)
  111. {
  112. assert(rgman->getReservedGroups().size() > index);
  113. RequestGroupList::SeqType::const_iterator i =
  114. rgman->getReservedGroups().begin();
  115. std::advance(i, index);
  116. return (*i).second;
  117. }
  118. SharedHandle<RequestGroup> createRequestGroup(int32_t pieceLength,
  119. int64_t totalLength,
  120. const std::string& path,
  121. const std::string& uri,
  122. const SharedHandle<Option>& opt)
  123. {
  124. SharedHandle<DownloadContext> dctx(new DownloadContext(pieceLength,
  125. totalLength,
  126. path));
  127. std::vector<std::string> uris;
  128. uris.push_back(uri);
  129. dctx->getFirstFileEntry()->addUris(uris.begin(), uris.end());
  130. SharedHandle<RequestGroup> group(new RequestGroup(GroupId::create(), opt));
  131. group->setDownloadContext(dctx);
  132. return group;
  133. }
  134. SharedHandle<DownloadResult> createDownloadResult
  135. (error_code::Value result, const std::string& uri)
  136. {
  137. std::vector<std::string> uris;
  138. uris.push_back(uri);
  139. SharedHandle<FileEntry> entry(new FileEntry("/tmp/path", 1, 0, uris));
  140. std::vector<SharedHandle<FileEntry> > entries;
  141. entries.push_back(entry);
  142. SharedHandle<DownloadResult> dr(new DownloadResult());
  143. dr->gid = GroupId::create();
  144. dr->fileEntries = entries;
  145. dr->result = result;
  146. dr->belongsTo = 0;
  147. dr->inMemoryDownload = false;
  148. dr->option = SharedHandle<Option>(new Option());
  149. return dr;
  150. }
  151. } // namespace aria2