test_oss_object.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956
  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. void test_object_setup(CuTest *tc)
  13. {
  14. aos_pool_t *p = NULL;
  15. int is_cname = 0;
  16. aos_status_t *s = NULL;
  17. oss_request_options_t *options = NULL;
  18. oss_acl_e oss_acl = OSS_ACL_PRIVATE;
  19. /* create test bucket */
  20. aos_pool_create(&p, NULL);
  21. options = oss_request_options_create(p);
  22. init_test_request_options(options, is_cname);
  23. s = create_test_bucket(options, TEST_BUCKET_NAME, oss_acl);
  24. CuAssertIntEquals(tc, 200, s->code);
  25. aos_pool_destroy(p);
  26. }
  27. void test_object_cleanup(CuTest *tc)
  28. {
  29. aos_pool_t *p = NULL;
  30. int is_cname = 0;
  31. aos_string_t bucket;
  32. oss_request_options_t *options = NULL;
  33. char *object_name1 = "oss_test_put_object.ts";
  34. char *object_name2 = "oss_test_put_object_from_file.jpg";
  35. char *object_name3 = "oss_test_object_by_url";
  36. char *object_name4 = "oss_test_append_object";
  37. char *object_name5 = "oss_test_append_object_from_file";
  38. char *object_name6 = "oss_test_copy_object";
  39. char *object_name7 = "video_1.ts";
  40. char *object_name8 = "video_2.ts";
  41. char *object_name9 = "oss_test_put_object_from_file2.txt";
  42. char *object_name10 = "put_object_from_buffer_with_default_content_type";
  43. aos_table_t *resp_headers = NULL;
  44. aos_pool_create(&p, NULL);
  45. options = oss_request_options_create(p);
  46. init_test_request_options(options, is_cname);
  47. /* delete test object */
  48. delete_test_object(options, TEST_BUCKET_NAME, object_name1);
  49. delete_test_object(options, TEST_BUCKET_NAME, object_name2);
  50. delete_test_object(options, TEST_BUCKET_NAME, object_name3);
  51. delete_test_object(options, TEST_BUCKET_NAME, object_name4);
  52. delete_test_object(options, TEST_BUCKET_NAME, object_name5);
  53. delete_test_object(options, TEST_BUCKET_NAME, object_name6);
  54. delete_test_object(options, TEST_BUCKET_NAME, object_name7);
  55. delete_test_object(options, TEST_BUCKET_NAME, object_name8);
  56. delete_test_object(options, TEST_BUCKET_NAME, object_name9);
  57. delete_test_object(options, TEST_BUCKET_NAME, object_name10);
  58. /* delete test bucket */
  59. aos_str_set(&bucket, TEST_BUCKET_NAME);
  60. oss_delete_bucket(options, &bucket, &resp_headers);
  61. apr_sleep(apr_time_from_sec(3));
  62. aos_pool_destroy(p);
  63. }
  64. void test_put_object_from_buffer(CuTest *tc)
  65. {
  66. aos_pool_t *p = NULL;
  67. char *object_name = "oss_test_put_object.ts";
  68. char *str = "test oss c sdk";
  69. aos_status_t *s = NULL;
  70. int is_cname = 0;
  71. aos_string_t bucket;
  72. aos_string_t object;
  73. aos_table_t *headers = NULL;
  74. aos_table_t *head_headers = NULL;
  75. aos_table_t *head_resp_headers = NULL;
  76. char *content_type = NULL;
  77. oss_request_options_t *options = NULL;
  78. /* test put object */
  79. aos_pool_create(&p, NULL);
  80. options = oss_request_options_create(p);
  81. init_test_request_options(options, is_cname);
  82. headers = aos_table_make(p, 1);
  83. apr_table_set(headers, "x-oss-meta-author", "oss");
  84. s = create_test_object(options, TEST_BUCKET_NAME, object_name, str, headers);
  85. CuAssertIntEquals(tc, 200, s->code);
  86. CuAssertPtrNotNull(tc, headers);
  87. aos_pool_destroy(p);
  88. /* head object */
  89. aos_pool_create(&p, NULL);
  90. options = oss_request_options_create(p);
  91. aos_str_set(&bucket, TEST_BUCKET_NAME);
  92. aos_str_set(&object, object_name);
  93. init_test_request_options(options, is_cname);
  94. s = oss_head_object(options, &bucket, &object,
  95. head_headers, &head_resp_headers);
  96. CuAssertIntEquals(tc, 200, s->code);
  97. CuAssertPtrNotNull(tc, head_resp_headers);
  98. content_type = (char*)(apr_table_get(head_resp_headers, OSS_CONTENT_TYPE));
  99. CuAssertStrEquals(tc, "video/MP2T", content_type);
  100. printf("test_put_object_from_buffer ok\n");
  101. }
  102. void test_put_object_from_buffer_with_default_content_type(CuTest *tc)
  103. {
  104. aos_pool_t *p = NULL;
  105. char *object_name = "put_object_from_buffer_with_default_content_type";
  106. char *str = "test oss c sdk";
  107. aos_status_t *s = NULL;
  108. int is_cname = 0;
  109. aos_string_t bucket;
  110. aos_string_t object;
  111. aos_table_t *headers = NULL;
  112. aos_table_t *head_headers = NULL;
  113. aos_table_t *head_resp_headers = NULL;
  114. char *content_type = NULL;
  115. oss_request_options_t *options = NULL;
  116. /* test put object */
  117. aos_pool_create(&p, NULL);
  118. options = oss_request_options_create(p);
  119. init_test_request_options(options, is_cname);
  120. headers = aos_table_make(p, 1);
  121. apr_table_set(headers, "x-oss-meta-author", "oss");
  122. s = create_test_object(options, TEST_BUCKET_NAME, object_name, str, headers);
  123. CuAssertIntEquals(tc, 200, s->code);
  124. CuAssertPtrNotNull(tc, headers);
  125. aos_pool_destroy(p);
  126. /* head object */
  127. aos_pool_create(&p, NULL);
  128. options = oss_request_options_create(p);
  129. aos_str_set(&bucket, TEST_BUCKET_NAME);
  130. aos_str_set(&object, object_name);
  131. init_test_request_options(options, is_cname);
  132. s = oss_head_object(options, &bucket, &object,
  133. head_headers, &head_resp_headers);
  134. CuAssertIntEquals(tc, 200, s->code);
  135. CuAssertPtrNotNull(tc, head_resp_headers);
  136. content_type = (char*)(apr_table_get(head_resp_headers, OSS_CONTENT_TYPE));
  137. CuAssertStrEquals(tc, "application/octet-stream", content_type);
  138. printf("test_put_object_from_buffer_with_default_content_type ok\n");
  139. }
  140. void test_put_object_from_buffer_with_specified(CuTest *tc)
  141. {
  142. aos_pool_t *p = NULL;
  143. char *object_name = "./xxx/./ddd/";
  144. char *str = "test oss c sdk";
  145. aos_status_t *s = NULL;
  146. int is_cname = 0;
  147. aos_string_t bucket;
  148. aos_string_t object;
  149. aos_table_t *headers = NULL;
  150. aos_table_t *head_headers = NULL;
  151. aos_table_t *head_resp_headers = NULL;
  152. oss_request_options_t *options = NULL;
  153. /* test put object */
  154. aos_pool_create(&p, NULL);
  155. options = oss_request_options_create(p);
  156. init_test_request_options(options, is_cname);
  157. headers = aos_table_make(p, 1);
  158. apr_table_set(headers, "x-oss-meta-author", "oss");
  159. s = create_test_object(options, TEST_BUCKET_NAME, object_name, str, headers);
  160. CuAssertIntEquals(tc, 200, s->code);
  161. CuAssertPtrNotNull(tc, headers);
  162. aos_pool_destroy(p);
  163. /* head object */
  164. aos_pool_create(&p, NULL);
  165. options = oss_request_options_create(p);
  166. aos_str_set(&bucket, TEST_BUCKET_NAME);
  167. aos_str_set(&object, object_name);
  168. init_test_request_options(options, is_cname);
  169. s = oss_head_object(options, &bucket, &object,
  170. head_headers, &head_resp_headers);
  171. CuAssertIntEquals(tc, 200, s->code);
  172. CuAssertPtrNotNull(tc, head_resp_headers);
  173. printf("test_put_object_from_buffer_with_specified ok\n");
  174. }
  175. void test_put_object_from_file(CuTest *tc)
  176. {
  177. aos_pool_t *p = NULL;
  178. char *object_name = "video_1.ts";
  179. char *filename = __FILE__;
  180. aos_string_t bucket;
  181. aos_string_t object;
  182. aos_status_t *s = NULL;
  183. oss_request_options_t *options = NULL;
  184. int is_cname = 0;
  185. aos_table_t *headers = NULL;
  186. aos_table_t *head_headers = NULL;
  187. aos_table_t *head_resp_headers = NULL;
  188. char *content_type = NULL;
  189. aos_pool_create(&p, NULL);
  190. options = oss_request_options_create(p);
  191. init_test_request_options(options, is_cname);
  192. headers = aos_table_make(p, 5);
  193. s = create_test_object_from_file(options, TEST_BUCKET_NAME,
  194. object_name, filename, headers);
  195. CuAssertIntEquals(tc, 200, s->code);
  196. CuAssertPtrNotNull(tc, headers);
  197. aos_pool_destroy(p);
  198. /* head object */
  199. aos_pool_create(&p, NULL);
  200. options = oss_request_options_create(p);
  201. aos_str_set(&bucket, TEST_BUCKET_NAME);
  202. aos_str_set(&object, object_name);
  203. init_test_request_options(options, is_cname);
  204. s = oss_head_object(options, &bucket, &object,
  205. head_headers, &head_resp_headers);
  206. CuAssertIntEquals(tc, 200, s->code);
  207. CuAssertPtrNotNull(tc, head_resp_headers);
  208. content_type = (char*)(apr_table_get(head_resp_headers, OSS_CONTENT_TYPE));
  209. CuAssertStrEquals(tc, "application/octet-stream", content_type);
  210. printf("test_put_object_from_file ok\n");
  211. }
  212. void test_put_object_with_large_length_header(CuTest *tc)
  213. {
  214. aos_pool_t *p = NULL;
  215. char *object_name = "video_2.ts";
  216. char *filename = __FILE__;
  217. aos_status_t *s = NULL;
  218. oss_request_options_t *options = NULL;
  219. int is_cname = 0;
  220. int i = 0;
  221. int header_length = 0;
  222. aos_table_t *headers = NULL;
  223. char *user_meta = NULL;
  224. aos_pool_create(&p, NULL);
  225. options = oss_request_options_create(p);
  226. init_test_request_options(options, is_cname);
  227. header_length = 1024 * 5;
  228. user_meta = (char*)calloc(header_length, 1);
  229. for (; i < header_length - 1; i++) {
  230. user_meta[i] = 'a';
  231. }
  232. user_meta[header_length - 1] = '\0';
  233. headers = aos_table_make(p, 2);
  234. apr_table_set(headers, "x-oss-meta-user-meta", user_meta);
  235. s = create_test_object_from_file(options, TEST_BUCKET_NAME,
  236. object_name, filename, headers);
  237. CuAssertIntEquals(tc, 200, s->code);
  238. CuAssertPtrNotNull(tc, headers);
  239. free(user_meta);
  240. aos_pool_destroy(p);
  241. printf("test_put_object_with_large_length_header_back_bound ok\n");
  242. }
  243. void test_put_object_from_file_with_content_type(CuTest *tc)
  244. {
  245. aos_pool_t *p = NULL;
  246. char *object_name = "oss_test_put_object_from_file2.txt";
  247. char *filename = __FILE__;
  248. aos_string_t bucket;
  249. aos_string_t object;
  250. aos_status_t *s = NULL;
  251. oss_request_options_t *options = NULL;
  252. int is_cname = 0;
  253. aos_table_t *headers = NULL;
  254. aos_table_t *head_headers = NULL;
  255. aos_table_t *head_resp_headers = NULL;
  256. char *content_type = NULL;
  257. aos_pool_create(&p, NULL);
  258. options = oss_request_options_create(p);
  259. init_test_request_options(options, is_cname);
  260. headers = aos_table_make(options->pool, 1);
  261. apr_table_set(headers, OSS_CONTENT_TYPE, "image/jpeg");
  262. s = create_test_object_from_file(options, TEST_BUCKET_NAME,
  263. object_name, filename, headers);
  264. CuAssertIntEquals(tc, 200, s->code);
  265. CuAssertPtrNotNull(tc, headers);
  266. aos_pool_destroy(p);
  267. /* head object */
  268. aos_pool_create(&p, NULL);
  269. options = oss_request_options_create(p);
  270. aos_str_set(&bucket, TEST_BUCKET_NAME);
  271. aos_str_set(&object, object_name);
  272. init_test_request_options(options, is_cname);
  273. s = oss_head_object(options, &bucket, &object,
  274. head_headers, &head_resp_headers);
  275. CuAssertIntEquals(tc, 200, s->code);
  276. CuAssertPtrNotNull(tc, head_resp_headers);
  277. content_type = (char*)(apr_table_get(head_resp_headers, OSS_CONTENT_TYPE));
  278. CuAssertStrEquals(tc, "image/jpeg", content_type);
  279. printf("test_put_object_from_file ok\n");
  280. }
  281. void test_get_object_to_buffer(CuTest *tc)
  282. {
  283. aos_pool_t *p = NULL;
  284. aos_string_t bucket;
  285. char *object_name = "oss_test_put_object.ts";
  286. aos_string_t object;
  287. int is_cname = 0;
  288. oss_request_options_t *options = NULL;
  289. aos_table_t *headers = NULL;
  290. aos_table_t *params = NULL;
  291. aos_table_t *resp_headers = NULL;
  292. aos_status_t *s = NULL;
  293. aos_list_t buffer;
  294. aos_buf_t *content = NULL;
  295. char *expect_content = "test oss c sdk";
  296. char *buf = NULL;
  297. int64_t len = 0;
  298. int64_t size = 0;
  299. int64_t pos = 0;
  300. char *content_type = NULL;
  301. aos_pool_create(&p, NULL);
  302. options = oss_request_options_create(p);
  303. init_test_request_options(options, is_cname);
  304. aos_str_set(&bucket, TEST_BUCKET_NAME);
  305. aos_str_set(&object, object_name);
  306. aos_list_init(&buffer);
  307. /* test get object to buffer */
  308. s = oss_get_object_to_buffer(options, &bucket, &object, headers,
  309. params, &buffer, &resp_headers);
  310. CuAssertIntEquals(tc, 200, s->code);
  311. CuAssertPtrNotNull(tc, resp_headers);
  312. /* get buffer len */
  313. len = aos_buf_list_len(&buffer);
  314. buf = aos_pcalloc(p, (apr_size_t)(len + 1));
  315. buf[len] = '\0';
  316. /* copy buffer content to memory */
  317. aos_list_for_each_entry(aos_buf_t, content, &buffer, node) {
  318. size = aos_buf_size(content);
  319. memcpy(buf + pos, content->pos, (size_t)size);
  320. pos += size;
  321. }
  322. CuAssertStrEquals(tc, expect_content, buf);
  323. content_type = (char*)(apr_table_get(resp_headers, OSS_CONTENT_TYPE));
  324. CuAssertStrEquals(tc, "video/MP2T", content_type);
  325. aos_pool_destroy(p);
  326. printf("test_get_object_to_buffer ok\n");
  327. }
  328. void test_get_object_to_buffer_with_range(CuTest *tc)
  329. {
  330. aos_pool_t *p = NULL;
  331. aos_string_t bucket;
  332. char *object_name = "oss_test_put_object.ts";
  333. aos_string_t object;
  334. int is_cname = 0;
  335. oss_request_options_t *options = NULL;
  336. aos_table_t *headers = NULL;
  337. aos_table_t *params = NULL;
  338. aos_table_t *resp_headers = NULL;
  339. aos_status_t *s = NULL;
  340. aos_list_t buffer;
  341. aos_buf_t *content = NULL;
  342. char *expect_content = "oss c sdk";
  343. char *buf = NULL;
  344. int64_t len = 0;
  345. int64_t size = 0;
  346. int64_t pos = 0;
  347. aos_pool_create(&p, NULL);
  348. options = oss_request_options_create(p);
  349. init_test_request_options(options, is_cname);
  350. aos_str_set(&bucket, TEST_BUCKET_NAME);
  351. aos_str_set(&object, object_name);
  352. headers = aos_table_make(p, 1);
  353. apr_table_set(headers, "Range", " bytes=5-13");
  354. aos_list_init(&buffer);
  355. /* test get object to buffer */
  356. s = oss_get_object_to_buffer(options, &bucket, &object, headers,
  357. params, &buffer, &resp_headers);
  358. CuAssertIntEquals(tc, 206, s->code);
  359. CuAssertPtrNotNull(tc, resp_headers);
  360. /* get buffer len */
  361. len = aos_buf_list_len(&buffer);
  362. buf = aos_pcalloc(p, (apr_size_t)(len + 1));
  363. buf[len] = '\0';
  364. /* copy buffer content to memory */
  365. aos_list_for_each_entry(aos_buf_t, content, &buffer, node) {
  366. size = aos_buf_size(content);
  367. memcpy(buf + pos, content->pos, (size_t)size);
  368. pos += size;
  369. }
  370. CuAssertStrEquals(tc, expect_content, buf);
  371. aos_pool_destroy(p);
  372. printf("test_get_object_to_buffer_with_range ok\n");
  373. }
  374. void test_get_object_to_file(CuTest *tc)
  375. {
  376. aos_pool_t *p = NULL;
  377. aos_string_t bucket;
  378. char *object_name = "oss_test_put_object_from_file2.txt";
  379. aos_string_t object;
  380. char *filename = "oss_test_get_object_to_file";
  381. char *source_filename = __FILE__;
  382. aos_string_t file;
  383. oss_request_options_t *options = NULL;
  384. int is_cname = 0;
  385. aos_table_t *headers = NULL;
  386. aos_table_t *params = NULL;
  387. aos_table_t *resp_headers = NULL;
  388. aos_status_t *s = NULL;
  389. char *content_type = NULL;
  390. aos_pool_create(&p, NULL);
  391. options = oss_request_options_create(p);
  392. init_test_request_options(options, is_cname);
  393. aos_str_set(&bucket, TEST_BUCKET_NAME);
  394. aos_str_set(&object, object_name);
  395. aos_str_set(&file, filename);
  396. /* test get object to file */
  397. s = oss_get_object_to_file(options, &bucket, &object, headers,
  398. params, &file, &resp_headers);
  399. CuAssertIntEquals(tc, 200, s->code);
  400. CuAssertIntEquals(tc, get_file_size(source_filename), get_file_size(filename));
  401. content_type = (char*)(apr_table_get(resp_headers, OSS_CONTENT_TYPE));
  402. CuAssertStrEquals(tc, "image/jpeg", content_type);
  403. CuAssertPtrNotNull(tc, resp_headers);
  404. remove(filename);
  405. aos_pool_destroy(p);
  406. printf("test_get_object_to_file ok\n");
  407. }
  408. void test_head_object(CuTest *tc)
  409. {
  410. aos_pool_t *p = NULL;
  411. aos_string_t bucket;
  412. aos_string_t object;
  413. char *object_name = "oss_test_put_object.ts";
  414. int is_cname = 0;
  415. oss_request_options_t *options = NULL;
  416. aos_table_t *headers = NULL;
  417. aos_table_t *resp_headers = NULL;
  418. aos_status_t *s = NULL;
  419. char *user_meta = NULL;
  420. aos_pool_create(&p, NULL);
  421. options = oss_request_options_create(p);
  422. init_test_request_options(options, is_cname);
  423. aos_str_set(&bucket, TEST_BUCKET_NAME);
  424. aos_str_set(&object, object_name);
  425. headers = aos_table_make(p, 0);
  426. /* test head object */
  427. s = oss_head_object(options, &bucket, &object, headers, &resp_headers);
  428. CuAssertIntEquals(tc, 200, s->code);
  429. CuAssertPtrNotNull(tc, resp_headers);
  430. user_meta = (char*)(apr_table_get(resp_headers, "x-oss-meta-author"));
  431. CuAssertStrEquals(tc, "oss", user_meta);
  432. aos_pool_destroy(p);
  433. printf("test_head_object ok\n");
  434. }
  435. void test_head_object_with_not_exist(CuTest *tc)
  436. {
  437. aos_pool_t *p = NULL;
  438. aos_string_t bucket;
  439. aos_string_t object;
  440. char *object_name = "not_exist.object";
  441. int is_cname = 0;
  442. oss_request_options_t *options = NULL;
  443. aos_table_t *headers = NULL;
  444. aos_table_t *resp_headers = NULL;
  445. aos_status_t *s = NULL;
  446. aos_pool_create(&p, NULL);
  447. options = oss_request_options_create(p);
  448. init_test_request_options(options, is_cname);
  449. aos_str_set(&bucket, TEST_BUCKET_NAME);
  450. aos_str_set(&object, object_name);
  451. headers = aos_table_make(p, 0);
  452. /* test head object */
  453. s = oss_head_object(options, &bucket, &object, headers, &resp_headers);
  454. CuAssertIntEquals(tc, 404, s->code);
  455. CuAssertStrEquals(tc, "UnknownError", s->error_code);
  456. CuAssertTrue(tc, NULL == s->error_msg);
  457. CuAssertTrue(tc, 0 != strlen(s->req_id));
  458. CuAssertPtrNotNull(tc, resp_headers);
  459. aos_pool_destroy(p);
  460. printf("test_head_object ok\n");
  461. }
  462. void test_delete_object(CuTest *tc)
  463. {
  464. aos_pool_t *p = NULL;
  465. aos_string_t bucket;
  466. char *object_name = "oss_test_put_object";
  467. aos_string_t object;
  468. int is_cname = 0;
  469. oss_request_options_t *options = NULL;
  470. aos_table_t *resp_headers = NULL;
  471. aos_status_t *s = NULL;
  472. aos_pool_create(&p, NULL);
  473. options = oss_request_options_create(p);
  474. init_test_request_options(options, is_cname);
  475. aos_str_set(&bucket, TEST_BUCKET_NAME);
  476. aos_str_set(&object, object_name);
  477. /* test delete object */
  478. s = oss_delete_object(options, &bucket, &object, &resp_headers);
  479. CuAssertIntEquals(tc, 204, s->code);
  480. CuAssertPtrNotNull(tc, resp_headers);
  481. aos_pool_destroy(p);
  482. printf("test_delete_object ok\n");
  483. }
  484. void test_copy_object(CuTest *tc)
  485. {
  486. aos_pool_t *p = NULL;
  487. aos_string_t source_bucket;
  488. char *source_object_name = "oss_test_put_object.ts";
  489. aos_string_t source_object;
  490. aos_string_t dest_bucket;
  491. char *dest_object_name = "oss_test_copy_object";
  492. aos_string_t dest_object;
  493. oss_request_options_t *options = NULL;
  494. int is_cname = 0;
  495. aos_table_t *headers = NULL;
  496. aos_table_t *resp_headers = NULL;
  497. aos_status_t *s = NULL;
  498. aos_table_t *head_headers = NULL;
  499. aos_table_t *head_resp_headers = NULL;
  500. aos_pool_create(&p, NULL);
  501. options = oss_request_options_create(p);
  502. init_test_request_options(options, is_cname);
  503. aos_str_set(&source_bucket, TEST_BUCKET_NAME);
  504. aos_str_set(&source_object, source_object_name);
  505. aos_str_set(&dest_bucket, TEST_BUCKET_NAME);
  506. aos_str_set(&dest_object, dest_object_name);
  507. headers = aos_table_make(p, 5);
  508. /* test copy object */
  509. s = oss_copy_object(options, &source_bucket, &source_object,
  510. &dest_bucket, &dest_object, headers, &resp_headers);
  511. CuAssertIntEquals(tc, 200, s->code);
  512. CuAssertPtrNotNull(tc, resp_headers);
  513. aos_pool_destroy(p);
  514. apr_sleep(apr_time_from_sec(1));
  515. /* head object */
  516. aos_pool_create(&p, NULL);
  517. options = oss_request_options_create(p);
  518. init_test_request_options(options, is_cname);
  519. s = oss_head_object(options, &dest_bucket, &dest_object,
  520. head_headers, &head_resp_headers);
  521. CuAssertIntEquals(tc, 200, s->code);
  522. CuAssertPtrNotNull(tc, head_resp_headers);
  523. aos_pool_destroy(p);
  524. printf("test_copy_object ok\n");
  525. }
  526. void test_copy_object_with_source_url_encode(CuTest *tc)
  527. {
  528. aos_pool_t *p = NULL;
  529. aos_string_t source_bucket;
  530. char *source_object_name = "y9n/g/%E9%98%B4%E9%98%B3%E5%B8%88-%E9%A3%9F%E6%A2%A6%E8%B2%98.ts";
  531. char *filename = __FILE__;
  532. aos_string_t source_object;
  533. aos_string_t dest_bucket;
  534. char *dest_object_name = "oss_test_copy_object";
  535. aos_string_t dest_object;
  536. oss_request_options_t *options = NULL;
  537. int is_cname = 0;
  538. aos_table_t *resp_headers = NULL;
  539. aos_status_t *s = NULL;
  540. aos_table_t *head_headers = NULL;
  541. aos_table_t *head_resp_headers = NULL;
  542. aos_pool_create(&p, NULL);
  543. options = oss_request_options_create(p);
  544. init_test_request_options(options, is_cname);
  545. aos_str_set(&source_bucket, TEST_BUCKET_NAME);
  546. aos_str_set(&source_object, source_object_name);
  547. aos_str_set(&dest_bucket, TEST_BUCKET_NAME);
  548. aos_str_set(&dest_object, dest_object_name);
  549. /* put object */
  550. s = create_test_object_from_file(options, TEST_BUCKET_NAME,
  551. source_object_name, filename, NULL);
  552. CuAssertIntEquals(tc, 200, s->code);
  553. aos_pool_destroy(p);
  554. /* test copy object */
  555. aos_pool_create(&p, NULL);
  556. options = oss_request_options_create(p);
  557. init_test_request_options(options, is_cname);
  558. s = oss_copy_object(options, &source_bucket, &source_object,
  559. &dest_bucket, &dest_object, NULL, &resp_headers);
  560. CuAssertIntEquals(tc, 200, s->code);
  561. CuAssertPtrNotNull(tc, resp_headers);
  562. aos_pool_destroy(p);
  563. apr_sleep(apr_time_from_sec(1));
  564. /* head object */
  565. aos_pool_create(&p, NULL);
  566. options = oss_request_options_create(p);
  567. init_test_request_options(options, is_cname);
  568. s = oss_head_object(options, &dest_bucket, &dest_object,
  569. head_headers, &head_resp_headers);
  570. CuAssertIntEquals(tc, 200, s->code);
  571. CuAssertPtrNotNull(tc, head_resp_headers);
  572. /* delete object */
  573. s = oss_delete_object(options, &source_bucket, &source_object, &resp_headers);
  574. CuAssertIntEquals(tc, 204, s->code);
  575. s = oss_delete_object(options, &dest_bucket, &dest_object, &resp_headers);
  576. CuAssertIntEquals(tc, 204, s->code);
  577. aos_pool_destroy(p);
  578. printf("test_copy_object_with_source_url_encode ok\n");
  579. }
  580. void test_copy_object_negative(CuTest *tc)
  581. {
  582. aos_pool_t *p = NULL;
  583. aos_string_t source_bucket;
  584. char *source_object_name = NULL;
  585. aos_string_t source_object;
  586. aos_string_t dest_bucket;
  587. char *dest_object_name = "oss_test_copy_object";
  588. aos_string_t dest_object;
  589. oss_request_options_t *options = NULL;
  590. int is_cname = 0;
  591. aos_status_t *s = NULL;
  592. char buffer[AOS_MAX_QUERY_ARG_LEN+1];
  593. memset(buffer, 'A', AOS_MAX_QUERY_ARG_LEN);
  594. buffer[AOS_MAX_QUERY_ARG_LEN] = '\0';
  595. source_object_name = buffer;
  596. aos_pool_create(&p, NULL);
  597. options = oss_request_options_create(p);
  598. init_test_request_options(options, is_cname);
  599. aos_str_set(&source_bucket, TEST_BUCKET_NAME);
  600. aos_str_set(&source_object, source_object_name);
  601. aos_str_set(&dest_bucket, TEST_BUCKET_NAME);
  602. aos_str_set(&dest_object, dest_object_name);
  603. /* test copy object */
  604. s = oss_copy_object(options, &source_bucket, &source_object,
  605. &dest_bucket, &dest_object, NULL, NULL);
  606. CuAssertIntEquals(tc, 400, s->code);
  607. CuAssertStrEquals(tc, "InvalidObjectName", s->error_code);
  608. aos_pool_destroy(p);
  609. printf("test_copy_object_negative ok\n");
  610. }
  611. void test_object_by_url(CuTest *tc)
  612. {
  613. aos_pool_t *p = NULL;
  614. oss_request_options_t *options = NULL;
  615. aos_table_t *headers = NULL;
  616. aos_table_t *params = NULL;
  617. aos_table_t *resp_headers = NULL;
  618. aos_http_request_t *req = NULL;
  619. aos_list_t buffer;
  620. aos_status_t *s = NULL;
  621. aos_string_t url;
  622. apr_time_t now;
  623. int two_minute = 120;
  624. int is_cname = 0;
  625. char *object_name = "oss_test_object_by_url";
  626. aos_string_t bucket;
  627. aos_string_t object;
  628. char *str = "test oss c sdk for object url api";
  629. char *filename = __FILE__;
  630. char *filename_download = "oss_test_object_by_url";
  631. aos_string_t file;
  632. int64_t effective_time;
  633. char *url_str = NULL;
  634. aos_buf_t *content = NULL;
  635. aos_pool_create(&p, NULL);
  636. options = oss_request_options_create(p);
  637. init_test_request_options(options, is_cname);
  638. req = aos_http_request_create(p);
  639. headers = aos_table_make(p, 0);
  640. aos_str_set(&bucket, TEST_BUCKET_NAME);
  641. aos_str_set(&object, object_name);
  642. aos_str_set(&file, filename);
  643. aos_list_init(&buffer);
  644. now = apr_time_now();
  645. effective_time = now / 1000000 + two_minute;
  646. /* test effective url for put_object_from_buffer */
  647. req->method = HTTP_PUT;
  648. url_str = gen_test_signed_url(options, TEST_BUCKET_NAME,
  649. object_name, effective_time, req);
  650. aos_str_set(&url, url_str);
  651. aos_list_init(&buffer);
  652. content = aos_buf_pack(p, str, strlen(str));
  653. aos_list_add_tail(&content->node, &buffer);
  654. s = oss_put_object_from_buffer_by_url(options, &url,
  655. &buffer, headers, &resp_headers);
  656. CuAssertIntEquals(tc, 200, s->code);
  657. CuAssertPtrNotNull(tc, resp_headers);
  658. /* test effective url for put_object_from_file */
  659. resp_headers = NULL;
  660. s = oss_put_object_from_file_by_url(options, &url, &file,
  661. headers, &resp_headers);
  662. CuAssertIntEquals(tc, 200, s->code);
  663. CuAssertPtrNotNull(tc, resp_headers);
  664. /* test effective url for get_object_to_buffer */
  665. req->method = HTTP_GET;
  666. url_str = gen_test_signed_url(options, TEST_BUCKET_NAME,
  667. object_name, effective_time, req);
  668. aos_str_set(&url, url_str);
  669. s = oss_get_object_to_buffer_by_url(options, &url, headers, params,
  670. &buffer, &resp_headers);
  671. CuAssertIntEquals(tc, 200, s->code);
  672. /* test effective url for get_object_to_file */
  673. resp_headers = NULL;
  674. aos_str_set(&file, filename_download);
  675. s = oss_get_object_to_file_by_url(options, &url, headers,
  676. headers, &file, &resp_headers);
  677. CuAssertIntEquals(tc, 200, s->code);
  678. CuAssertIntEquals(tc, get_file_size(filename), get_file_size(filename_download));
  679. CuAssertPtrNotNull(tc, resp_headers);
  680. /* test effective url for head_object */
  681. resp_headers = NULL;
  682. req->method = HTTP_HEAD;
  683. url_str = gen_test_signed_url(options, TEST_BUCKET_NAME,
  684. object_name, effective_time, req);
  685. aos_str_set(&url, url_str);
  686. s = oss_head_object_by_url(options, &url, headers, &resp_headers);
  687. CuAssertIntEquals(tc, 200, s->code);
  688. CuAssertPtrNotNull(tc, resp_headers);
  689. remove(filename_download);
  690. aos_pool_destroy(p);
  691. printf("test_object_by_url ok\n");
  692. }
  693. void test_append_object_from_buffer(CuTest *tc)
  694. {
  695. aos_pool_t *p = NULL;
  696. char *object_name = "oss_test_append_object";
  697. aos_string_t bucket;
  698. aos_string_t object;
  699. char *str = "test oss c sdk";
  700. aos_status_t *s = NULL;
  701. int is_cname = 0;
  702. int64_t position = 0;
  703. aos_table_t *headers = NULL;
  704. aos_table_t *headers1 = NULL;
  705. aos_table_t *resp_headers = NULL;
  706. oss_request_options_t *options = NULL;
  707. aos_list_t buffer;
  708. aos_buf_t *content = NULL;
  709. char *next_append_position = NULL;
  710. /* test append object */
  711. aos_pool_create(&p, NULL);
  712. options = oss_request_options_create(p);
  713. init_test_request_options(options, is_cname);
  714. headers = aos_table_make(p, 0);
  715. aos_str_set(&bucket, TEST_BUCKET_NAME);
  716. aos_str_set(&object, object_name);
  717. s = oss_head_object(options, &bucket, &object, headers, &resp_headers);
  718. if(s->code == 200) {
  719. next_append_position = (char*)(apr_table_get(resp_headers,
  720. "x-oss-next-append-position"));
  721. position = atoi(next_append_position);
  722. }
  723. CuAssertPtrNotNull(tc, resp_headers);
  724. /* append object */
  725. resp_headers = NULL;
  726. headers1 = aos_table_make(p, 0);
  727. aos_list_init(&buffer);
  728. content = aos_buf_pack(p, str, strlen(str));
  729. aos_list_add_tail(&content->node, &buffer);
  730. s = oss_append_object_from_buffer(options, &bucket, &object,
  731. position, &buffer, headers1, &resp_headers);
  732. CuAssertIntEquals(tc, 200, s->code);
  733. CuAssertPtrNotNull(tc, resp_headers);
  734. aos_pool_destroy(p);
  735. printf("test_append_object_from_buffer ok\n");
  736. }
  737. void test_append_object_from_file(CuTest *tc)
  738. {
  739. aos_pool_t *p = NULL;
  740. char *object_name = "oss_test_append_object_from_file";
  741. aos_string_t bucket;
  742. aos_string_t object;
  743. char *filename = __FILE__;
  744. aos_string_t append_file;
  745. aos_status_t *s = NULL;
  746. int is_cname = 0;
  747. int64_t position = 0;
  748. aos_table_t *headers = NULL;
  749. aos_table_t *resp_headers = NULL;
  750. oss_request_options_t *options = NULL;
  751. /* test append object */
  752. aos_pool_create(&p, NULL);
  753. options = oss_request_options_create(p);
  754. init_test_request_options(options, is_cname);
  755. headers = aos_table_make(p, 0);
  756. aos_str_set(&bucket, TEST_BUCKET_NAME);
  757. aos_str_set(&object, object_name);
  758. aos_str_set(&append_file, filename);
  759. s = oss_append_object_from_file(options, &bucket, &object, position,
  760. &append_file, headers, &resp_headers);
  761. CuAssertIntEquals(tc, 200, s->code);
  762. CuAssertPtrNotNull(tc, resp_headers);
  763. aos_pool_destroy(p);
  764. printf("test_append_object_from_file ok\n");
  765. }
  766. void test_get_not_exist_object_to_file(CuTest *tc)
  767. {
  768. aos_pool_t *p = NULL;
  769. aos_string_t bucket;
  770. char *object_name = "oss_test_put_object_from_file_not_exist_.txt";
  771. aos_string_t object;
  772. char *filename = "oss_test_get_object_to_file_not_exist";
  773. aos_string_t file;
  774. oss_request_options_t *options = NULL;
  775. int is_cname = 0;
  776. aos_table_t *headers = NULL;
  777. aos_table_t *params = NULL;
  778. aos_table_t *resp_headers = NULL;
  779. aos_status_t *s = NULL;
  780. aos_pool_create(&p, NULL);
  781. options = oss_request_options_create(p);
  782. init_test_request_options(options, is_cname);
  783. aos_str_set(&bucket, TEST_BUCKET_NAME);
  784. aos_str_set(&object, object_name);
  785. aos_str_set(&file, filename);
  786. /* test get object to file */
  787. s = oss_get_object_to_file(options, &bucket, &object, headers,
  788. params, &file, &resp_headers);
  789. CuAssertIntEquals(tc, 404, s->code);
  790. CuAssertIntEquals(tc, -1, get_file_size(filename));
  791. aos_pool_destroy(p);
  792. printf("test_get_not_exist_object_to_file ok\n");
  793. }
  794. CuSuite *test_oss_object()
  795. {
  796. CuSuite* suite = CuSuiteNew();
  797. SUITE_ADD_TEST(suite, test_object_setup);
  798. SUITE_ADD_TEST(suite, test_put_object_from_buffer);
  799. SUITE_ADD_TEST(suite, test_put_object_from_file);
  800. SUITE_ADD_TEST(suite, test_put_object_from_buffer_with_specified);
  801. SUITE_ADD_TEST(suite, test_get_object_to_buffer);
  802. SUITE_ADD_TEST(suite, test_get_object_to_buffer_with_range);
  803. SUITE_ADD_TEST(suite, test_put_object_from_file_with_content_type);
  804. SUITE_ADD_TEST(suite, test_put_object_from_buffer_with_default_content_type);
  805. SUITE_ADD_TEST(suite, test_put_object_with_large_length_header);
  806. SUITE_ADD_TEST(suite, test_get_object_to_file);
  807. SUITE_ADD_TEST(suite, test_head_object);
  808. SUITE_ADD_TEST(suite, test_head_object_with_not_exist);
  809. SUITE_ADD_TEST(suite, test_copy_object);
  810. SUITE_ADD_TEST(suite, test_copy_object_with_source_url_encode);
  811. SUITE_ADD_TEST(suite, test_copy_object_negative);
  812. SUITE_ADD_TEST(suite, test_object_by_url);
  813. SUITE_ADD_TEST(suite, test_delete_object);
  814. SUITE_ADD_TEST(suite, test_append_object_from_buffer);
  815. SUITE_ADD_TEST(suite, test_append_object_from_file);
  816. SUITE_ADD_TEST(suite, test_object_cleanup);
  817. return suite;
  818. }