test_oss_crc.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #include "CuTest.h"
  2. #include "aos_log.h"
  3. #include "aos_util.h"
  4. #include "aos_string.h"
  5. #include "aos_status.h"
  6. #include "oss_auth.h"
  7. #include "oss_util.h"
  8. #include "oss_xml.h"
  9. #include "oss_api.h"
  10. #include "oss_config.h"
  11. #include "oss_test_util.h"
  12. #include "aos_crc64.h"
  13. void test_crc_setup(CuTest *tc)
  14. {
  15. aos_pool_t *p = NULL;
  16. int is_cname = 0;
  17. aos_status_t *s = NULL;
  18. oss_request_options_t *options = NULL;
  19. oss_acl_e oss_acl = OSS_ACL_PRIVATE;
  20. /* create test bucket */
  21. aos_pool_create(&p, NULL);
  22. options = oss_request_options_create(p);
  23. init_test_request_options(options, is_cname);
  24. s = create_test_bucket(options, TEST_BUCKET_NAME, oss_acl);
  25. CuAssertIntEquals(tc, 200, s->code);
  26. aos_pool_destroy(p);
  27. }
  28. void test_crc_cleanup(CuTest *tc)
  29. {
  30. aos_pool_t *p = NULL;
  31. int is_cname = 0;
  32. aos_string_t bucket;
  33. oss_request_options_t *options = NULL;
  34. char *object_name1 = "oss_test_crc_put_object.txt";
  35. char *object_name2 = "oss_test_crc_append_object.txt";
  36. char *object_name3 = "oss_test_crc_multipart_object.txt";
  37. aos_table_t *resp_headers = NULL;
  38. aos_pool_create(&p, NULL);
  39. options = oss_request_options_create(p);
  40. init_test_request_options(options, is_cname);
  41. /* delete test object */
  42. delete_test_object(options, TEST_BUCKET_NAME, object_name1);
  43. delete_test_object(options, TEST_BUCKET_NAME, object_name2);
  44. delete_test_object(options, TEST_BUCKET_NAME, object_name3);
  45. /* delete test bucket */
  46. aos_str_set(&bucket, TEST_BUCKET_NAME);
  47. oss_delete_bucket(options, &bucket, &resp_headers);
  48. apr_sleep(apr_time_from_sec(3));
  49. aos_pool_destroy(p);
  50. }
  51. void test_crc_append_object_from_buffer(CuTest *tc)
  52. {
  53. aos_pool_t *p = NULL;
  54. char *object_name = "oss_test_crc_append_object.txt";
  55. aos_string_t bucket;
  56. aos_string_t object;
  57. char *str = "Time is a bird for ever on the wing.";
  58. aos_status_t *s = NULL;
  59. int is_cname = 0;
  60. int64_t position = 0;
  61. uint64_t initcrc = 0;
  62. aos_table_t *headers = NULL;
  63. aos_table_t *resp_headers = NULL;
  64. aos_list_t resp_body;
  65. oss_request_options_t *options = NULL;
  66. aos_list_t buffer;
  67. aos_buf_t *content = NULL;
  68. aos_pool_create(&p, NULL);
  69. options = oss_request_options_create(p);
  70. init_test_request_options(options, is_cname);
  71. headers = aos_table_make(p, 0);
  72. aos_str_set(&bucket, TEST_BUCKET_NAME);
  73. aos_str_set(&object, object_name);
  74. aos_list_init(&resp_body);
  75. /* append object */
  76. aos_list_init(&buffer);
  77. content = aos_buf_pack(p, str, strlen(str));
  78. aos_list_add_tail(&content->node, &buffer);
  79. oss_delete_object(options, &bucket, &object, NULL);
  80. s = oss_do_append_object_from_buffer(options, &bucket, &object, position,
  81. initcrc, &buffer, headers, NULL, NULL, &resp_headers, &resp_body);
  82. CuAssertIntEquals(tc, 200, s->code);
  83. position = aos_atoi64((char*)(apr_table_get(resp_headers, OSS_NEXT_APPEND_POSITION)));
  84. initcrc = aos_atoui64((char*)(apr_table_get(resp_headers, OSS_HASH_CRC64_ECMA)));
  85. /* append object */
  86. s = oss_do_append_object_from_buffer(options, &bucket, &object, position,
  87. initcrc, &buffer, NULL, NULL, NULL, NULL, NULL);
  88. CuAssertIntEquals(tc, 200, s->code);
  89. /* delete object */
  90. s= oss_delete_object(options, &bucket, &object, NULL);
  91. CuAssertIntEquals(tc, 204, s->code);
  92. aos_pool_destroy(p);
  93. printf("test_crc_append_object_from_buffer ok\n");
  94. }
  95. void test_crc_append_object_from_file(CuTest *tc)
  96. {
  97. aos_pool_t *p = NULL;
  98. char *object_name = "oss_test_crc_append_object.txt";
  99. aos_string_t bucket;
  100. aos_string_t object;
  101. aos_string_t filename;
  102. aos_status_t *s = NULL;
  103. int is_cname = 0;
  104. int64_t position = 0;
  105. uint64_t initcrc = 0;
  106. aos_table_t *headers = NULL;
  107. aos_table_t *resp_headers = NULL;
  108. aos_list_t resp_body;
  109. oss_request_options_t *options = NULL;
  110. aos_pool_create(&p, NULL);
  111. options = oss_request_options_create(p);
  112. init_test_request_options(options, is_cname);
  113. headers = aos_table_make(p, 0);
  114. aos_str_set(&bucket, TEST_BUCKET_NAME);
  115. aos_str_set(&object, object_name);
  116. aos_list_init(&resp_body);
  117. make_random_file(p, object_name, 10240);
  118. aos_str_set(&filename, object_name);
  119. oss_delete_object(options, &bucket, &object, NULL);
  120. /* append object */
  121. s = oss_do_append_object_from_file(options, &bucket, &object, position,
  122. initcrc, &filename, headers, NULL, NULL, &resp_headers, &resp_body);
  123. CuAssertIntEquals(tc, 200, s->code);
  124. position = aos_atoi64((char*)(apr_table_get(resp_headers, OSS_NEXT_APPEND_POSITION)));
  125. initcrc = aos_atoui64((char*)(apr_table_get(resp_headers, OSS_HASH_CRC64_ECMA)));
  126. /* append object */
  127. s = oss_do_append_object_from_file(options, &bucket, &object, position,
  128. initcrc, &filename, NULL, NULL, NULL, NULL, NULL);
  129. CuAssertIntEquals(tc, 200, s->code);
  130. /* delete object */
  131. s= oss_delete_object(options, &bucket, &object, NULL);
  132. CuAssertIntEquals(tc, 204, s->code);
  133. apr_file_remove(object_name, p);
  134. aos_pool_destroy(p);
  135. printf("test_crc_append_object_from_file ok\n");
  136. }
  137. void test_crc_disable_crc(CuTest *tc)
  138. {
  139. aos_pool_t *p = NULL;
  140. char *object_name = "oss_test_crc_put_object.txt";
  141. char *str = "Sow nothing, reap nothing.";
  142. aos_status_t *s = NULL;
  143. int is_cname = 0;
  144. aos_string_t bucket;
  145. aos_string_t object;
  146. oss_request_options_t *options = NULL;
  147. aos_list_t resp_body;
  148. aos_list_t buffer;
  149. aos_buf_t *content;
  150. /* init test*/
  151. aos_pool_create(&p, NULL);
  152. options = oss_request_options_create(p);
  153. init_test_request_options(options, is_cname);
  154. aos_str_set(&bucket, TEST_BUCKET_NAME);
  155. aos_str_set(&object, object_name);
  156. aos_list_init(&resp_body);
  157. aos_list_init(&buffer);
  158. content = aos_buf_pack(options->pool, str, strlen(str));
  159. aos_list_add_tail(&content->node, &buffer);
  160. options->ctl->options->enable_crc = AOS_FALSE;
  161. /* test put object */
  162. s = oss_put_object_from_buffer(options, &bucket, &object, &buffer, NULL, NULL);
  163. CuAssertIntEquals(tc, 200, s->code);
  164. aos_pool_destroy(p);
  165. /* test get object */
  166. aos_pool_create(&p, NULL);
  167. options = oss_request_options_create(p);
  168. init_test_request_options(options, is_cname);
  169. options->ctl->options->enable_crc = AOS_FALSE;
  170. s = oss_get_object_to_buffer(options, &bucket, &object, NULL, NULL, &buffer, NULL);
  171. CuAssertIntEquals(tc, 200, s->code);
  172. aos_pool_destroy(p);
  173. printf("test_crc_disable_crc ok\n");
  174. }
  175. /* Test crc64() on vector[0..len-1] which should have CRC-64 crc. Also test
  176. crc64_combine() on vector[] split in two. */
  177. static void crc64_combine_test(CuTest *tc, void *vector, size_t len, uint64_t crc)
  178. {
  179. uint64_t crc1, crc2;
  180. /* test crc64() */
  181. crc1 = aos_crc64(0, vector, len);
  182. CuAssertTrue(tc, crc1 == crc);
  183. /* test crc64_combine() */
  184. crc1 = aos_crc64(0, vector, (len + 1) >> 1);
  185. crc2 = aos_crc64(0, (char*)vector + ((len + 1) >> 1), len >> 1);
  186. crc1 = aos_crc64_combine(crc1, crc2, len >> 1);
  187. CuAssertTrue(tc, crc1 == crc);
  188. }
  189. void test_crc_combine(CuTest *tc)
  190. {
  191. char *str1 = "123456789";
  192. size_t len1 = 9;
  193. uint64_t crc1 = UINT64_C(0x995dc9bbdf1939fa);
  194. char *str2 = "This is a test of the emergency broadcast system.";
  195. size_t len2 = 49;
  196. uint64_t crc2 = UINT64_C(0x27db187fc15bbc72);
  197. crc64_combine_test(tc, str1, len1, crc1);
  198. crc64_combine_test(tc, str2, len2, crc2);
  199. printf("test_crc_combine ok\n");
  200. }
  201. void test_crc_negative(CuTest *tc)
  202. {
  203. aos_pool_t *p = NULL;
  204. char *object_name = "oss_test_crc_append_object_neg.txt";
  205. aos_string_t bucket;
  206. aos_string_t object;
  207. aos_string_t filename;
  208. aos_status_t *s = NULL;
  209. int is_cname = 0;
  210. int64_t position = 0;
  211. oss_request_options_t *options = NULL;
  212. aos_pool_create(&p, NULL);
  213. options = oss_request_options_create(p);
  214. init_test_request_options(options, is_cname);
  215. aos_str_set(&bucket, TEST_BUCKET_NAME);
  216. aos_str_set(&object, object_name);
  217. make_random_file(p, object_name, 1024);
  218. aos_str_set(&filename, object_name);
  219. oss_delete_object(options, &bucket, &object, NULL);
  220. /* append object */
  221. s = oss_do_append_object_from_file(options, &bucket, &object, position, 1,
  222. &filename, NULL, NULL, NULL, NULL, NULL);
  223. CuAssertIntEquals(tc, 200, s->code);
  224. /* delete object */
  225. s= oss_delete_object(options, &bucket, &object, NULL);
  226. CuAssertIntEquals(tc, 204, s->code);
  227. apr_file_remove(object_name, p);
  228. aos_pool_destroy(p);
  229. printf("test_crc_negative ok\n");
  230. }
  231. CuSuite *test_oss_crc()
  232. {
  233. CuSuite* suite = CuSuiteNew();
  234. SUITE_ADD_TEST(suite, test_crc_setup);
  235. SUITE_ADD_TEST(suite, test_crc_append_object_from_buffer);
  236. SUITE_ADD_TEST(suite, test_crc_append_object_from_file);
  237. SUITE_ADD_TEST(suite, test_crc_disable_crc);
  238. SUITE_ADD_TEST(suite, test_crc_combine);
  239. SUITE_ADD_TEST(suite, test_crc_negative);
  240. SUITE_ADD_TEST(suite, test_crc_cleanup);
  241. return suite;
  242. }