TestUtil.cc 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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::const_iterator i = rgman->getReservedGroups().begin();
  114. std::advance(i, index);
  115. return *i;
  116. }
  117. SharedHandle<RequestGroup> createRequestGroup(int32_t pieceLength,
  118. int64_t totalLength,
  119. const std::string& path,
  120. const std::string& uri,
  121. const SharedHandle<Option>& opt)
  122. {
  123. SharedHandle<DownloadContext> dctx(new DownloadContext(pieceLength,
  124. totalLength,
  125. path));
  126. std::vector<std::string> uris;
  127. uris.push_back(uri);
  128. dctx->getFirstFileEntry()->addUris(uris.begin(), uris.end());
  129. SharedHandle<RequestGroup> group(new RequestGroup(GroupId::create(), opt));
  130. group->setDownloadContext(dctx);
  131. return group;
  132. }
  133. SharedHandle<DownloadResult> createDownloadResult
  134. (error_code::Value result, const std::string& uri)
  135. {
  136. std::vector<std::string> uris;
  137. uris.push_back(uri);
  138. SharedHandle<FileEntry> entry(new FileEntry("/tmp/path", 1, 0, uris));
  139. std::vector<SharedHandle<FileEntry> > entries;
  140. entries.push_back(entry);
  141. SharedHandle<DownloadResult> dr(new DownloadResult());
  142. dr->gid = GroupId::create();
  143. dr->fileEntries = entries;
  144. dr->result = result;
  145. dr->belongsTo = 0;
  146. dr->inMemoryDownload = false;
  147. dr->option = SharedHandle<Option>(new Option());
  148. return dr;
  149. }
  150. } // namespace aria2