TestUtil.cc 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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. #ifdef ENABLE_MESSAGE_DIGEST
  19. # include "message_digest_helper.h"
  20. #endif // ENABLE_MESSAGE_DIGEST
  21. namespace aria2 {
  22. void createFile(const std::string& path, size_t length)
  23. {
  24. File(File(path).getDirname()).mkdirs();
  25. DefaultDiskWriter dw(path);
  26. dw.initAndOpenFile();
  27. dw.truncate(length);
  28. }
  29. std::string readFile(const std::string& path)
  30. {
  31. std::stringstream ss;
  32. std::ifstream in(path.c_str(), std::ios::binary);
  33. char buf[4096];
  34. while(1) {
  35. in.read(buf, sizeof(buf));
  36. ss.write(buf, in.gcount());
  37. if(in.gcount() != sizeof(buf)) {
  38. break;
  39. }
  40. }
  41. return ss.str();
  42. }
  43. Cookie createCookie
  44. (const std::string& name,
  45. const std::string& value,
  46. const std::string& domain,
  47. bool hostOnly,
  48. const std::string& path,
  49. bool secure)
  50. {
  51. return Cookie
  52. (name, value, 0, false, domain, hostOnly, path, secure, false, 0);
  53. }
  54. Cookie createCookie
  55. (const std::string& name,
  56. const std::string& value,
  57. time_t expiryTime,
  58. const std::string& domain,
  59. bool hostOnly,
  60. const std::string& path,
  61. bool secure)
  62. {
  63. return Cookie
  64. (name, value, expiryTime, true, domain, hostOnly, path, secure, false, 0);
  65. }
  66. std::string fromHex(const std::string& s)
  67. {
  68. return util::fromHex(s.begin(), s.end());
  69. }
  70. #ifdef ENABLE_MESSAGE_DIGEST
  71. std::string fileHexDigest
  72. (const SharedHandle<MessageDigest>& ctx, const std::string& filename)
  73. {
  74. SharedHandle<DiskWriter> writer(new DefaultDiskWriter(filename));
  75. writer->openExistingFile();
  76. return util::toHex(message_digest::digest(ctx, writer, 0, writer->size()));
  77. }
  78. #endif // ENABLE_MESSAGE_DIGEST
  79. WrDiskCacheEntry::DataCell* createDataCell(int64_t goff,
  80. const char* data,
  81. size_t offset)
  82. {
  83. WrDiskCacheEntry::DataCell* cell = new WrDiskCacheEntry::DataCell();
  84. cell->goff = goff;
  85. size_t len = strlen(data);
  86. cell->data = new unsigned char[len];
  87. memcpy(cell->data, data, len);
  88. cell->offset = offset;
  89. cell->len = cell->capacity = len - offset;
  90. return cell;
  91. }
  92. SharedHandle<RequestGroup> findReservedGroup
  93. (const SharedHandle<RequestGroupMan>& rgman, a2_gid_t gid)
  94. {
  95. SharedHandle<RequestGroup> rg = rgman->findGroup(gid);
  96. if(rg) {
  97. if(rg->getState() == RequestGroup::STATE_WAITING) {
  98. return rg;
  99. } else {
  100. rg.reset();
  101. }
  102. }
  103. return rg;
  104. }
  105. SharedHandle<RequestGroup> getReservedGroup
  106. (const SharedHandle<RequestGroupMan>& rgman, size_t index)
  107. {
  108. assert(rgman->getReservedGroups().size() > index);
  109. RequestGroupList::SeqType::const_iterator i =
  110. rgman->getReservedGroups().begin();
  111. std::advance(i, index);
  112. return (*i).second;
  113. }
  114. } // namespace aria2