/ext/io.filetransfer/native/test_main.cpp

https://github.com/blackberry/BB10-WebWorks-Framework · C++ · 248 lines · 165 code · 64 blank · 19 comment · 1 complexity · 106126c53d443ba730a1b43f6d265de5 MD5 · raw file

  1. /*
  2. * Copyright 2012 Research In Motion Limited.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include <gtest/gtest.h>
  17. #include <curl/curl.h>
  18. #include <stdio.h>
  19. #include <string>
  20. #include "../filetransfer_curl.hpp"
  21. int createTestFile(const char *filePath)
  22. {
  23. FILE *new_file = fopen(filePath, "w");
  24. if (!new_file) {
  25. return 1;
  26. }
  27. fprintf(new_file, "abcd");
  28. fclose(new_file);
  29. return 0;
  30. }
  31. TEST(FileTransfer, DetectsIncorrectUploadFilePath)
  32. {
  33. CURL *curl = NULL;
  34. std::string source_file = "/accounts/1000/shared/camera/abcdefg.hij";
  35. std::string target_url = "http://bojap.com/omg/uploader.php";
  36. std::string source_escaped(curl_easy_escape(curl, curl_easy_escape(curl, source_file.c_str(), 0), 0));
  37. std::string target_escaped(curl_easy_escape(curl, curl_easy_escape(curl, target_url.c_str(), 0), 0));
  38. std::string expected = "upload error 1 " + source_escaped + " " + target_escaped + " 0";
  39. webworks::FileUploadInfo upload_info;
  40. upload_info.sourceFile = source_file;
  41. upload_info.targetURL = target_url;
  42. upload_info.mimeType = "image/jpeg";
  43. upload_info.fileKey = "image";
  44. upload_info.fileName = "new_image.jpg";
  45. upload_info.chunkedMode = 1;
  46. webworks::FileTransferCurl file_transfer;
  47. std::string result = file_transfer.Upload(&upload_info);
  48. EXPECT_EQ(expected, result);
  49. }
  50. TEST(FileTransfer, DetectsIncorrectUploadURL)
  51. {
  52. CURL *curl = NULL;
  53. std::string source_file = "/accounts/1000/shared/documents/filetransfer_test.txt";
  54. std::string target_url = "http://www.google.com/uploader.php";
  55. std::string source_escaped(curl_easy_escape(curl, curl_easy_escape(curl, source_file.c_str(), 0), 0));
  56. std::string target_escaped(curl_easy_escape(curl, curl_easy_escape(curl, target_url.c_str(), 0), 0));
  57. std::string expected = "upload error 2 " + source_escaped + " " + target_escaped + " 404";
  58. int file_result = createTestFile(source_file.c_str());
  59. EXPECT_EQ(0, file_result);
  60. webworks::FileUploadInfo upload_info;
  61. upload_info.sourceFile = source_file;
  62. upload_info.targetURL = target_url;
  63. upload_info.mimeType = "text/plain";
  64. upload_info.fileKey = "file";
  65. upload_info.fileName = "test_file.txt";
  66. upload_info.chunkedMode = 0;
  67. webworks::FileTransferCurl file_transfer;
  68. std::string result = file_transfer.Upload(&upload_info);
  69. EXPECT_EQ(expected, result);
  70. remove(source_file.c_str());
  71. }
  72. TEST(FileTransfer, DetectsUploadConnectionError)
  73. {
  74. CURL *curl = NULL;
  75. std::string source_file = "/accounts/1000/shared/documents/filetransfer_test.txt";
  76. std::string target_url = "http://127.0.0.1/uploader.php";
  77. std::string source_escaped(curl_easy_escape(curl, curl_easy_escape(curl, source_file.c_str(), 0), 0));
  78. std::string target_escaped(curl_easy_escape(curl, curl_easy_escape(curl, target_url.c_str(), 0), 0));
  79. std::string expected = "upload error 3 " + source_escaped + " " + target_escaped + " 0";
  80. int file_result = createTestFile(source_file.c_str());
  81. EXPECT_EQ(0, file_result);
  82. webworks::FileUploadInfo upload_info;
  83. upload_info.sourceFile = source_file;
  84. upload_info.targetURL = target_url;
  85. upload_info.mimeType = "text/plain";
  86. upload_info.fileKey = "file";
  87. upload_info.fileName = "test_file.txt";
  88. upload_info.chunkedMode = 0;
  89. webworks::FileTransferCurl file_transfer;
  90. std::string result = file_transfer.Upload(&upload_info);
  91. EXPECT_EQ(expected, result);
  92. remove(source_file.c_str());
  93. }
  94. TEST(FileTransfer, AllowsRedirectedUploadURL)
  95. {
  96. CURL *curl = NULL;
  97. std::string source_file = "/accounts/1000/shared/documents/filetransfer_test.txt";
  98. std::string target_url = "http://google.com/uploader.php";
  99. std::string source_escaped(curl_easy_escape(curl, curl_easy_escape(curl, source_file.c_str(), 0), 0));
  100. std::string target_escaped(curl_easy_escape(curl, curl_easy_escape(curl, target_url.c_str(), 0), 0));
  101. std::string expected = "upload error 2 " + source_escaped + " " + target_escaped + " 404";
  102. int file_result = createTestFile(source_file.c_str());
  103. EXPECT_EQ(0, file_result);
  104. webworks::FileUploadInfo upload_info;
  105. upload_info.sourceFile = source_file;
  106. upload_info.targetURL = target_url;
  107. upload_info.mimeType = "text/plain";
  108. upload_info.fileKey = "file";
  109. upload_info.fileName = "test_file.txt";
  110. upload_info.chunkedMode = 1;
  111. webworks::FileTransferCurl file_transfer;
  112. std::string result = file_transfer.Upload(&upload_info);
  113. EXPECT_EQ(expected, result);
  114. remove(source_file.c_str());
  115. }
  116. // Tests for non-existent source
  117. TEST(FileTransfer, Detects404DownloadSource)
  118. {
  119. CURL *curl = NULL;
  120. std::string source = "http://www.google.com/hello.jpg";
  121. std::string target = "/accounts/1000/shared/camera/hello.jpg";
  122. std::string source_escaped(curl_easy_escape(curl, curl_easy_escape(curl, source.c_str(), 0), 0));
  123. std::string target_escaped(curl_easy_escape(curl, curl_easy_escape(curl, target.c_str(), 0), 0));
  124. std::string expected = "download error 1 " + source_escaped + " " + target_escaped + " 404";
  125. webworks::FileDownloadInfo download_info;
  126. download_info.source = source;
  127. download_info.target = target;
  128. webworks::FileTransferCurl file_transfer;
  129. std::string result = file_transfer.Download(&download_info);
  130. EXPECT_EQ(expected, result);
  131. }
  132. // Tests for invalid source
  133. TEST(FileTransfer, DetectsInvalidDownloadSource)
  134. {
  135. CURL *curl = NULL;
  136. std::string source = "/hello.jpg";
  137. std::string target = "/accounts/1000/shared/camera/hello.jpg";
  138. std::string source_escaped(curl_easy_escape(curl, curl_easy_escape(curl, source.c_str(), 0), 0));
  139. std::string target_escaped(curl_easy_escape(curl, curl_easy_escape(curl, target.c_str(), 0), 0));
  140. std::string expected = "download error 2 " + source_escaped + " " + target_escaped + " 0";
  141. webworks::FileDownloadInfo download_info;
  142. download_info.source = source;
  143. download_info.target = target;
  144. webworks::FileTransferCurl file_transfer;
  145. std::string result = file_transfer.Download(&download_info);
  146. EXPECT_EQ(expected, result);
  147. }
  148. // Tests for connetion error
  149. TEST(FileTransfer, DetectsIncorrectDownloadSource)
  150. {
  151. CURL *curl = NULL;
  152. std::string source = "http://domain.does.not.exist/hello.jpg";
  153. std::string target = "/accounts/1000/shared/camera/hello.jpg";
  154. std::string source_escaped(curl_easy_escape(curl, curl_easy_escape(curl, source.c_str(), 0), 0));
  155. std::string target_escaped(curl_easy_escape(curl, curl_easy_escape(curl, target.c_str(), 0), 0));
  156. std::string expected = "download error 3 " + source_escaped + " " + target_escaped + " 0";
  157. webworks::FileDownloadInfo download_info;
  158. download_info.source = source;
  159. download_info.target = target;
  160. webworks::FileTransferCurl file_transfer;
  161. std::string result = file_transfer.Download(&download_info);
  162. EXPECT_EQ(expected, result);
  163. }
  164. // Tests for invalid target (permissions error)
  165. TEST(FileTransfer, DetectsInvalidDownloadTargetPermissions)
  166. {
  167. CURL *curl = NULL;
  168. std::string source = "http://www.google.ca/ig/images/jfk/google_color.png";
  169. std::string target = "/accounts/hello.jpg";
  170. std::string source_escaped(curl_easy_escape(curl, curl_easy_escape(curl, source.c_str(), 0), 0));
  171. std::string target_escaped(curl_easy_escape(curl, curl_easy_escape(curl, target.c_str(), 0), 0));
  172. std::string expected = "download error 4 " + source_escaped + " " + target_escaped + " 0";
  173. webworks::FileDownloadInfo download_info;
  174. download_info.source = source;
  175. download_info.target = target;
  176. webworks::FileTransferCurl file_transfer;
  177. std::string result = file_transfer.Download(&download_info);
  178. EXPECT_EQ(expected, result);
  179. }
  180. int main(int argc, char **argv)
  181. {
  182. ::testing::InitGoogleTest(&argc, argv);
  183. return RUN_ALL_TESTS();
  184. }