TestUtil.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. #ifdef ENABLE_MESSAGE_DIGEST
  22. # include "message_digest_helper.h"
  23. #endif // ENABLE_MESSAGE_DIGEST
  24. namespace aria2 {
  25. void createFile(const std::string& path, size_t length)
  26. {
  27. File(File(path).getDirname()).mkdirs();
  28. DefaultDiskWriter dw(path);
  29. dw.initAndOpenFile();
  30. dw.truncate(length);
  31. }
  32. std::string readFile(const std::string& path)
  33. {
  34. std::stringstream ss;
  35. std::ifstream in(path.c_str(), std::ios::binary);
  36. char buf[4096];
  37. while(1) {
  38. in.read(buf, sizeof(buf));
  39. ss.write(buf, in.gcount());
  40. if(in.gcount() != sizeof(buf)) {
  41. break;
  42. }
  43. }
  44. return ss.str();
  45. }
  46. Cookie createCookie
  47. (const std::string& name,
  48. const std::string& value,
  49. const std::string& domain,
  50. bool hostOnly,
  51. const std::string& path,
  52. bool secure)
  53. {
  54. return Cookie
  55. (name, value, 0, false, domain, hostOnly, path, secure, false, 0);
  56. }
  57. Cookie createCookie
  58. (const std::string& name,
  59. const std::string& value,
  60. time_t expiryTime,
  61. const std::string& domain,
  62. bool hostOnly,
  63. const std::string& path,
  64. bool secure)
  65. {
  66. return Cookie
  67. (name, value, expiryTime, true, domain, hostOnly, path, secure, false, 0);
  68. }
  69. std::string fromHex(const std::string& s)
  70. {
  71. return util::fromHex(s.begin(), s.end());
  72. }
  73. #ifdef ENABLE_MESSAGE_DIGEST
  74. std::string fileHexDigest
  75. (const SharedHandle<MessageDigest>& ctx, const std::string& filename)
  76. {
  77. SharedHandle<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. SharedHandle<RequestGroup> findReservedGroup
  96. (const SharedHandle<RequestGroupMan>& rgman, a2_gid_t gid)
  97. {
  98. SharedHandle<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. SharedHandle<RequestGroup> getReservedGroup
  109. (const SharedHandle<RequestGroupMan>& rgman, size_t index)
  110. {
  111. assert(rgman->getReservedGroups().size() > index);
  112. RequestGroupList::SeqType::const_iterator i =
  113. rgman->getReservedGroups().begin();
  114. std::advance(i, index);
  115. return (*i).second;
  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. } // namespace aria2