TestUtil.cc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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 "StringFormat.h"
  12. #include "FatalException.h"
  13. #include "Cookie.h"
  14. namespace aria2 {
  15. void createFile(const std::string& path, size_t length)
  16. {
  17. File(File(path).getDirname()).mkdirs();
  18. int fd = creat(path.c_str(), OPEN_MODE);
  19. if(fd == -1) {
  20. throw FATAL_EXCEPTION(StringFormat("Could not create file=%s. cause:%s",
  21. path.c_str(), strerror(errno)).str());
  22. }
  23. if(-1 == ftruncate(fd, length)) {
  24. throw FATAL_EXCEPTION(StringFormat("ftruncate failed. cause:%s",
  25. strerror(errno)).str());
  26. }
  27. close(fd);
  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. } // namespace aria2