test_oss_bucket.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507
  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_bucket_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. char *object_name1 = "oss_test_object1";
  20. char *object_name2 = "oss_test_object2";
  21. char *object_name3 = "oss_tmp1/";
  22. char *object_name4 = "oss_tmp2/";
  23. char *object_name5 = "oss_tmp3/";
  24. char *object_name6 = "oss_tmp3/1";
  25. char *str = "test c oss sdk";
  26. aos_table_t *headers1 = NULL;
  27. aos_table_t *headers2 = NULL;
  28. aos_table_t *headers3 = NULL;
  29. aos_table_t *headers4 = NULL;
  30. aos_table_t *headers5 = NULL;
  31. aos_table_t *headers6 = NULL;
  32. //set log level, default AOS_LOG_WARN
  33. aos_log_set_level(AOS_LOG_WARN);
  34. //set log output, default stderr
  35. aos_log_set_output(NULL);
  36. //create test bucket
  37. aos_pool_create(&p, NULL);
  38. options = oss_request_options_create(p);
  39. init_test_request_options(options, is_cname);
  40. s = create_test_bucket(options, TEST_BUCKET_NAME, oss_acl);
  41. CuAssertIntEquals(tc, 200, s->code);
  42. CuAssertStrEquals(tc, NULL, s->error_code);
  43. //create test object
  44. headers1 = aos_table_make(p, 0);
  45. headers2 = aos_table_make(p, 0);
  46. headers3 = aos_table_make(p, 0);
  47. headers4 = aos_table_make(p, 0);
  48. headers5 = aos_table_make(p, 0);
  49. headers6 = aos_table_make(p, 0);
  50. create_test_object(options, TEST_BUCKET_NAME, object_name1, str, headers1);
  51. create_test_object(options, TEST_BUCKET_NAME, object_name2, str, headers2);
  52. create_test_object(options, TEST_BUCKET_NAME, object_name3, str, headers3);
  53. create_test_object(options, TEST_BUCKET_NAME, object_name4, str, headers4);
  54. create_test_object(options, TEST_BUCKET_NAME, object_name5, str, headers5);
  55. create_test_object(options, TEST_BUCKET_NAME, object_name6, str, headers6);
  56. aos_pool_destroy(p);
  57. }
  58. void test_bucket_cleanup(CuTest *tc)
  59. {
  60. }
  61. void test_create_bucket(CuTest *tc)
  62. {
  63. aos_pool_t *p = NULL;
  64. int is_cname = 0;
  65. aos_status_t *s = NULL;
  66. oss_request_options_t *options = NULL;
  67. oss_acl_e oss_acl;
  68. aos_pool_create(&p, NULL);
  69. options = oss_request_options_create(p);
  70. init_test_request_options(options, is_cname);
  71. oss_acl = OSS_ACL_PRIVATE;
  72. //create the same bucket twice with same bucket acl
  73. s = create_test_bucket(options, TEST_BUCKET_NAME, oss_acl);
  74. CuAssertIntEquals(tc, 200, s->code);
  75. CuAssertStrEquals(tc, NULL, s->error_code);
  76. //create the same bucket with different bucket acl
  77. oss_acl = OSS_ACL_PUBLIC_READ;
  78. s = create_test_bucket(options, TEST_BUCKET_NAME, oss_acl);
  79. CuAssertIntEquals(tc, 200, s->code);
  80. CuAssertStrEquals(tc, NULL, s->error_code);
  81. aos_pool_destroy(p);
  82. printf("test_create_bucket ok\n");
  83. }
  84. void test_delete_bucket(CuTest *tc)
  85. {
  86. aos_pool_t *p = NULL;
  87. aos_status_t *s = NULL;
  88. aos_string_t bucket;
  89. oss_acl_e oss_acl;
  90. int is_cname = 0;
  91. oss_request_options_t *options;
  92. aos_table_t *resp_headers = NULL;
  93. aos_pool_create(&p, NULL);
  94. options = oss_request_options_create(p);
  95. init_test_request_options(options, is_cname);
  96. aos_str_set(&bucket, TEST_BUCKET_NAME);
  97. oss_acl = OSS_ACL_PUBLIC_READ;
  98. s = create_test_bucket(options, TEST_BUCKET_NAME, oss_acl);
  99. //delete bucket not empty
  100. s = oss_delete_bucket(options, &bucket, &resp_headers);
  101. CuAssertIntEquals(tc, 409, s->code);
  102. CuAssertStrEquals(tc, "BucketNotEmpty", s->error_code);
  103. CuAssertTrue(tc, s->req_id != NULL);
  104. CuAssertPtrNotNull(tc, resp_headers);
  105. aos_pool_destroy(p);
  106. printf("test_delete_bucket ok\n");
  107. }
  108. void test_put_bucket_acl(CuTest *tc)
  109. {
  110. aos_pool_t *p = NULL;
  111. aos_string_t bucket;
  112. int is_cname = 0;
  113. oss_request_options_t *options = NULL;
  114. aos_table_t *resp_headers = NULL;
  115. aos_status_t *s = NULL;
  116. oss_acl_e oss_acl;
  117. aos_pool_create(&p, NULL);
  118. options = oss_request_options_create(p);
  119. init_test_request_options(options, is_cname);
  120. aos_str_set(&bucket, TEST_BUCKET_NAME);
  121. oss_acl = OSS_ACL_PUBLIC_READ_WRITE;
  122. s = oss_put_bucket_acl(options, &bucket, oss_acl, &resp_headers);
  123. CuAssertIntEquals(tc, 200, s->code);
  124. CuAssertPtrNotNull(tc, resp_headers);
  125. aos_pool_destroy(p);
  126. printf("test_put_bucket_acl ok\n");
  127. }
  128. void test_get_bucket_acl(CuTest *tc)
  129. {
  130. aos_pool_t *p = NULL;
  131. aos_string_t bucket;
  132. int is_cname = 0;
  133. oss_request_options_t *options = NULL;
  134. aos_table_t *resp_headers = NULL;
  135. aos_status_t *s = NULL;
  136. aos_string_t oss_acl;
  137. aos_pool_create(&p, NULL);
  138. options = oss_request_options_create(p);
  139. init_test_request_options(options, is_cname);
  140. aos_str_set(&bucket, TEST_BUCKET_NAME);
  141. s = oss_get_bucket_acl(options, &bucket, &oss_acl, &resp_headers);
  142. CuAssertIntEquals(tc, 200, s->code);
  143. CuAssertStrEquals(tc, "public-read-write", oss_acl.data);
  144. CuAssertPtrNotNull(tc, resp_headers);
  145. aos_pool_destroy(p);
  146. printf("test_get_bucket_acl ok\n");
  147. }
  148. void test_list_object(CuTest *tc)
  149. {
  150. aos_pool_t *p = NULL;
  151. aos_string_t bucket;
  152. oss_request_options_t *options = NULL;
  153. int is_cname = 0;
  154. aos_table_t *resp_headers = NULL;
  155. aos_status_t *s = NULL;
  156. oss_list_object_params_t *params = NULL;
  157. oss_list_object_content_t *content = NULL;
  158. int size = 0;
  159. char *key = NULL;
  160. aos_pool_create(&p, NULL);
  161. options = oss_request_options_create(p);
  162. init_test_request_options(options, is_cname);
  163. params = oss_create_list_object_params(p);
  164. params->max_ret = 1;
  165. params->truncated = 0;
  166. aos_str_set(&params->prefix, "oss_test_object");
  167. aos_str_set(&bucket, TEST_BUCKET_NAME);
  168. s = oss_list_object(options, &bucket, params, &resp_headers);
  169. CuAssertIntEquals(tc, 200, s->code);
  170. CuAssertIntEquals(tc, 1, params->truncated);
  171. CuAssertStrEquals(tc, "oss_test_object1", params->next_marker.data);
  172. CuAssertPtrNotNull(tc, resp_headers);
  173. aos_list_for_each_entry(oss_list_object_content_t, content, &params->object_list, node) {
  174. ++size;
  175. key = apr_psprintf(p, "%.*s", content->key.len, content->key.data);
  176. }
  177. CuAssertIntEquals(tc, 1 ,size);
  178. CuAssertStrEquals(tc, "oss_test_object1", key);
  179. size = 0;
  180. resp_headers = NULL;
  181. aos_list_init(&params->object_list);
  182. aos_str_set(&params->marker, params->next_marker.data);
  183. s = oss_list_object(options, &bucket, params, &resp_headers);
  184. CuAssertIntEquals(tc, 200, s->code);
  185. CuAssertIntEquals(tc, 0, params->truncated);
  186. aos_list_for_each_entry(oss_list_object_content_t, content, &params->object_list, node) {
  187. ++size;
  188. key = apr_psprintf(p, "%.*s", content->key.len, content->key.data);
  189. }
  190. CuAssertIntEquals(tc, 1 ,size);
  191. CuAssertStrEquals(tc, "oss_test_object2", key);
  192. CuAssertPtrNotNull(tc, resp_headers);
  193. aos_pool_destroy(p);
  194. printf("test_list_object ok\n");
  195. }
  196. void test_list_object_with_delimiter(CuTest *tc)
  197. {
  198. aos_pool_t *p = NULL;
  199. aos_string_t bucket;
  200. oss_request_options_t *options = NULL;
  201. int is_cname = 0;
  202. aos_table_t *resp_headers = NULL;
  203. aos_status_t *s = NULL;
  204. oss_list_object_params_t *params = NULL;
  205. oss_list_object_common_prefix_t *common_prefix = NULL;
  206. int size = 0;
  207. char *prefix = NULL;
  208. aos_pool_create(&p, NULL);
  209. options = oss_request_options_create(p);
  210. init_test_request_options(options, is_cname);
  211. params = oss_create_list_object_params(p);
  212. params->max_ret = 5;
  213. params->truncated = 0;
  214. aos_str_set(&params->prefix, "oss_tmp");
  215. aos_str_set(&params->delimiter, "/");
  216. aos_str_set(&bucket, TEST_BUCKET_NAME);
  217. s = oss_list_object(options, &bucket, params, &resp_headers);
  218. CuAssertIntEquals(tc, 200, s->code);
  219. CuAssertIntEquals(tc, 0, params->truncated);
  220. CuAssertPtrNotNull(tc, resp_headers);
  221. aos_list_for_each_entry(oss_list_object_common_prefix_t, common_prefix, &params->common_prefix_list, node) {
  222. ++size;
  223. prefix = apr_psprintf(p, "%.*s", common_prefix->prefix.len,
  224. common_prefix->prefix.data);
  225. if (size == 1) {
  226. CuAssertStrEquals(tc, "oss_tmp1/", prefix);
  227. } else if(size == 2) {
  228. CuAssertStrEquals(tc, "oss_tmp2/", prefix);
  229. }
  230. }
  231. CuAssertIntEquals(tc, 2, size);
  232. aos_pool_destroy(p);
  233. printf("test_list_object_with_delimiter ok\n");
  234. }
  235. void test_lifecycle(CuTest *tc)
  236. {
  237. aos_pool_t *p = NULL;
  238. aos_string_t bucket;
  239. int is_cname = 0;
  240. oss_request_options_t *options = NULL;
  241. aos_table_t *resp_headers = NULL;
  242. aos_status_t *s = NULL;
  243. aos_list_t lifecycle_rule_list;
  244. oss_lifecycle_rule_content_t *invalid_rule_content = NULL;
  245. oss_lifecycle_rule_content_t *rule_content = NULL;
  246. oss_lifecycle_rule_content_t *rule_content1 = NULL;
  247. oss_lifecycle_rule_content_t *rule_content2 = NULL;
  248. int size = 0;
  249. char *rule_id = NULL;
  250. char *prefix = NULL;
  251. char *status = NULL;
  252. int days = INT_MAX;
  253. char* date = NULL;
  254. aos_pool_create(&p, NULL);
  255. options = oss_request_options_create(p);
  256. init_test_request_options(options, is_cname);
  257. aos_str_set(&bucket, TEST_BUCKET_NAME);
  258. //put invalid lifecycle rule
  259. aos_list_init(&lifecycle_rule_list);
  260. invalid_rule_content = oss_create_lifecycle_rule_content(p);
  261. aos_str_set(&invalid_rule_content->id, "");
  262. aos_str_set(&invalid_rule_content->prefix, "pre");
  263. aos_list_add_tail(&invalid_rule_content->node, &lifecycle_rule_list);
  264. s = oss_put_bucket_lifecycle(options, &bucket, &lifecycle_rule_list,
  265. &resp_headers);
  266. CuAssertIntEquals(tc, 400, s->code);
  267. CuAssertPtrNotNull(tc, resp_headers);
  268. //put lifecycle
  269. resp_headers = NULL;
  270. aos_list_init(&lifecycle_rule_list);
  271. rule_content1 = oss_create_lifecycle_rule_content(p);
  272. aos_str_set(&rule_content1->id, "1");
  273. aos_str_set(&rule_content1->prefix, "pre1");
  274. aos_str_set(&rule_content1->status, "Enabled");
  275. rule_content1->days = 1;
  276. rule_content2 = oss_create_lifecycle_rule_content(p);
  277. aos_str_set(&rule_content2->id, "2");
  278. aos_str_set(&rule_content2->prefix, "pre2");
  279. aos_str_set(&rule_content2->status, "Enabled");
  280. aos_str_set(&rule_content2->date, "2022-10-11T00:00:00.000Z");
  281. aos_list_add_tail(&rule_content1->node, &lifecycle_rule_list);
  282. aos_list_add_tail(&rule_content2->node, &lifecycle_rule_list);
  283. s = oss_put_bucket_lifecycle(options, &bucket, &lifecycle_rule_list,
  284. &resp_headers);
  285. CuAssertIntEquals(tc, 200, s->code);
  286. CuAssertPtrNotNull(tc, resp_headers);
  287. //get lifecycle
  288. resp_headers = NULL;
  289. aos_list_init(&lifecycle_rule_list);
  290. s = oss_get_bucket_lifecycle(options, &bucket, &lifecycle_rule_list,
  291. &resp_headers);
  292. CuAssertIntEquals(tc, 200, s->code);
  293. CuAssertPtrNotNull(tc, resp_headers);
  294. aos_list_for_each_entry(oss_lifecycle_rule_content_t, rule_content, &lifecycle_rule_list, node) {
  295. if (size == 0) {
  296. rule_id = apr_psprintf(p, "%.*s", rule_content->id.len,
  297. rule_content->id.data);
  298. CuAssertStrEquals(tc, "1", rule_id);
  299. prefix = apr_psprintf(p, "%.*s", rule_content->prefix.len,
  300. rule_content->prefix.data);
  301. CuAssertStrEquals(tc, "pre1", prefix);
  302. date = apr_psprintf(p, "%.*s", rule_content->date.len,
  303. rule_content->date.data);
  304. CuAssertStrEquals(tc, "", date);
  305. status = apr_psprintf(p, "%.*s", rule_content->status.len,
  306. rule_content->status.data);
  307. CuAssertStrEquals(tc, "Enabled", status);
  308. days = rule_content->days;
  309. CuAssertIntEquals(tc, 1, days);
  310. }
  311. else if (size == 1){
  312. rule_id = apr_psprintf(p, "%.*s", rule_content->id.len,
  313. rule_content->id.data);
  314. CuAssertStrEquals(tc, "2", rule_id);
  315. prefix = apr_psprintf(p, "%.*s", rule_content->prefix.len,
  316. rule_content->prefix.data);
  317. CuAssertStrEquals(tc, "pre2", prefix);
  318. date = apr_psprintf(p, "%.*s", rule_content->date.len,
  319. rule_content->date.data);
  320. CuAssertStrEquals(tc, "2022-10-11T00:00:00.000Z", date);
  321. status = apr_psprintf(p, "%.*s", rule_content->status.len,
  322. rule_content->status.data);
  323. CuAssertStrEquals(tc, "Enabled", status);
  324. days = rule_content->days;
  325. CuAssertIntEquals(tc, INT_MAX, days);
  326. }
  327. ++size;
  328. }
  329. CuAssertIntEquals(tc, 2 ,size);
  330. //delete lifecycle
  331. resp_headers = NULL;
  332. s = oss_delete_bucket_lifecycle(options, &bucket, &resp_headers);
  333. CuAssertIntEquals(tc, 204, s->code);
  334. CuAssertPtrNotNull(tc, resp_headers);
  335. aos_pool_destroy(p);
  336. printf("test_lifecycle ok\n");
  337. }
  338. void test_delete_objects_quiet(CuTest *tc)
  339. {
  340. aos_pool_t *p = NULL;
  341. int is_cname = 0;
  342. aos_string_t bucket;
  343. aos_status_t *s = NULL;
  344. aos_table_t *resp_headers = NULL;
  345. oss_request_options_t *options = NULL;
  346. char *object_name1 = "oss_test_object1";
  347. char *object_name2 = "oss_test_object2";
  348. oss_object_key_t *content1 = NULL;
  349. oss_object_key_t *content2 = NULL;
  350. aos_list_t object_list;
  351. aos_list_t deleted_object_list;
  352. int is_quiet = 1;
  353. aos_pool_create(&p, NULL);
  354. options = oss_request_options_create(p);
  355. init_test_request_options(options, is_cname);
  356. aos_str_set(&bucket, TEST_BUCKET_NAME);
  357. aos_list_init(&object_list);
  358. aos_list_init(&deleted_object_list);
  359. content1 = oss_create_oss_object_key(p);
  360. aos_str_set(&content1->key, object_name1);
  361. aos_list_add_tail(&content1->node, &object_list);
  362. content2 = oss_create_oss_object_key(p);
  363. aos_str_set(&content2->key, object_name2);
  364. aos_list_add_tail(&content2->node, &object_list);
  365. s = oss_delete_objects(options, &bucket, &object_list, is_quiet,
  366. &resp_headers, &deleted_object_list);
  367. CuAssertIntEquals(tc, 200, s->code);
  368. CuAssertPtrNotNull(tc, resp_headers);
  369. aos_pool_destroy(p);
  370. printf("test_delete_objects_quiet ok\n");
  371. }
  372. void test_delete_objects_not_quiet(CuTest *tc)
  373. {
  374. aos_pool_t *p = NULL;
  375. int is_cname = 0;
  376. aos_string_t bucket;
  377. aos_status_t *s = NULL;
  378. aos_table_t *resp_headers = NULL;
  379. oss_request_options_t *options = NULL;
  380. char *object_name1 = "oss_tmp1/";
  381. char *object_name2 = "oss_tmp2/";
  382. oss_object_key_t *content = NULL;
  383. oss_object_key_t *content1 = NULL;
  384. oss_object_key_t *content2 = NULL;
  385. aos_list_t object_list;
  386. aos_list_t deleted_object_list;
  387. int is_quiet = 0;
  388. aos_pool_create(&p, NULL);
  389. options = oss_request_options_create(p);
  390. init_test_request_options(options, is_cname);
  391. aos_str_set(&bucket, TEST_BUCKET_NAME);
  392. aos_list_init(&object_list);
  393. aos_list_init(&deleted_object_list);
  394. content1 = oss_create_oss_object_key(p);
  395. aos_str_set(&content1->key, object_name1);
  396. aos_list_add_tail(&content1->node, &object_list);
  397. content2 = oss_create_oss_object_key(p);
  398. aos_str_set(&content2->key, object_name2);
  399. aos_list_add_tail(&content2->node, &object_list);
  400. s = oss_delete_objects(options, &bucket, &object_list, is_quiet,
  401. &resp_headers, &deleted_object_list);
  402. CuAssertIntEquals(tc, 200, s->code);
  403. CuAssertPtrNotNull(tc, resp_headers);
  404. aos_list_for_each_entry(oss_object_key_t, content, &deleted_object_list, node) {
  405. printf("Deleted key:%.*s\n", content->key.len, content->key.data);
  406. }
  407. aos_pool_destroy(p);
  408. printf("test_delete_objects_not_quiet ok\n");
  409. }
  410. void test_delete_objects_by_prefix(CuTest *tc)
  411. {
  412. aos_pool_t *p = NULL;
  413. oss_request_options_t *options = NULL;
  414. int is_cname = 0;
  415. aos_string_t bucket;
  416. aos_status_t *s = NULL;
  417. aos_string_t prefix;
  418. char *prefix_str = "oss_tmp3";
  419. aos_pool_create(&p, NULL);
  420. options = oss_request_options_create(p);
  421. init_test_request_options(options, is_cname);
  422. aos_str_set(&bucket, TEST_BUCKET_NAME);
  423. aos_str_set(&prefix, prefix_str);
  424. s = oss_delete_objects_by_prefix(options, &bucket, &prefix);
  425. CuAssertIntEquals(tc, 200, s->code);
  426. aos_pool_destroy(p);
  427. printf("test_delete_object_by_prefix ok\n");
  428. }
  429. CuSuite *test_oss_bucket()
  430. {
  431. CuSuite* suite = CuSuiteNew();
  432. SUITE_ADD_TEST(suite, test_bucket_setup);
  433. SUITE_ADD_TEST(suite, test_create_bucket);
  434. SUITE_ADD_TEST(suite, test_put_bucket_acl);
  435. SUITE_ADD_TEST(suite, test_get_bucket_acl);
  436. SUITE_ADD_TEST(suite, test_delete_objects_by_prefix);
  437. SUITE_ADD_TEST(suite, test_list_object);
  438. SUITE_ADD_TEST(suite, test_list_object_with_delimiter);
  439. SUITE_ADD_TEST(suite, test_delete_bucket);
  440. SUITE_ADD_TEST(suite, test_lifecycle);
  441. SUITE_ADD_TEST(suite, test_delete_objects_quiet);
  442. SUITE_ADD_TEST(suite, test_delete_objects_not_quiet);
  443. SUITE_ADD_TEST(suite, test_bucket_cleanup);
  444. return suite;
  445. }