test_oss_image.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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 "aos_transport.h"
  7. #include "aos_http_io.h"
  8. #include "oss_auth.h"
  9. #include "oss_util.h"
  10. #include "oss_xml.h"
  11. #include "oss_api.h"
  12. #include "oss_config.h"
  13. #include "oss_test_util.h"
  14. #include "apr_time.h"
  15. #include "cjson.h"
  16. typedef struct {
  17. long height;
  18. long width;
  19. long size;
  20. char format[64];
  21. } image_info_t;
  22. #if defined(WIN32)
  23. static char *image_file = "..\\oss_c_sdk_test\\example.jpg";
  24. #else
  25. static char *image_file = "oss_c_sdk_test/example.jpg";
  26. #endif
  27. static char *original_image = "oss_c_sdk_test/process/example.jpg";
  28. static char *processed_image = "oss_c_sdk_test/process/processed_example.jpg";
  29. static void put_example_image(CuTest *tc);
  30. static void get_iamge_info(CuTest *tc, image_info_t *image_info);
  31. static void parse_image_info(CuTest *tc, const char *json, image_info_t *image_info);
  32. void test_image_setup(CuTest *tc)
  33. {
  34. aos_pool_t *p = NULL;
  35. int is_cname = 0;
  36. aos_status_t *s = NULL;
  37. oss_request_options_t *options = NULL;
  38. oss_acl_e oss_acl = OSS_ACL_PRIVATE;
  39. // create test bucket
  40. aos_pool_create(&p, NULL);
  41. options = oss_request_options_create(p);
  42. init_test_request_options(options, is_cname);
  43. s = create_test_bucket(options, TEST_BUCKET_NAME, oss_acl);
  44. CuAssertIntEquals(tc, 200, s->code);
  45. aos_pool_destroy(p);
  46. }
  47. void test_image_cleanup(CuTest *tc)
  48. {
  49. aos_pool_t *p = NULL;
  50. int is_cname = 0;
  51. aos_string_t bucket;
  52. oss_request_options_t *options = NULL;
  53. aos_table_t *resp_headers = NULL;
  54. aos_pool_create(&p, NULL);
  55. options = oss_request_options_create(p);
  56. init_test_request_options(options, is_cname);
  57. /* delete test object */
  58. delete_test_object(options, TEST_BUCKET_NAME, original_image);
  59. delete_test_object(options, TEST_BUCKET_NAME, processed_image);
  60. /* delete test bucket */
  61. aos_str_set(&bucket, TEST_BUCKET_NAME);
  62. oss_delete_bucket(options, &bucket, &resp_headers);
  63. apr_sleep(apr_time_from_sec(3));
  64. aos_pool_destroy(p);
  65. }
  66. void test_resize_image(CuTest *tc) {
  67. aos_pool_t *p = NULL;
  68. aos_string_t bucket;
  69. aos_string_t object;
  70. int is_cname = 0;
  71. oss_request_options_t *options = NULL;
  72. aos_table_t *headers = NULL;
  73. aos_table_t *params = NULL;
  74. aos_table_t *resp_headers = NULL;
  75. aos_status_t *s = NULL;
  76. aos_list_t buffer;
  77. image_info_t image_info;
  78. /* put original image */
  79. put_example_image(tc);
  80. aos_pool_create(&p, NULL);
  81. options = oss_request_options_create(p);
  82. init_test_request_options(options, is_cname);
  83. aos_str_set(&bucket, TEST_BUCKET_NAME);
  84. aos_str_set(&object, original_image);
  85. aos_list_init(&buffer);
  86. params = aos_table_make(p, 1);
  87. apr_table_set(params, OSS_PROCESS, "image/resize,m_fixed,w_100,h_100");
  88. /* get processed image to buffer */
  89. s = oss_get_object_to_buffer(options, &bucket, &object, headers,
  90. params, &buffer, &resp_headers);
  91. CuAssertIntEquals(tc, 200, s->code);
  92. aos_str_set(&object, processed_image);
  93. /* put processed image */
  94. s= oss_put_object_from_buffer(options, &bucket, &object, &buffer,
  95. headers, &resp_headers);
  96. CuAssertIntEquals(tc, 200, s->code);
  97. aos_pool_destroy(p);
  98. /* check processed image */
  99. get_iamge_info(tc, &image_info);
  100. CuAssertIntEquals(tc, 100, image_info.height);
  101. CuAssertIntEquals(tc, 100, image_info.width);
  102. CuAssertIntEquals(tc, 3267, image_info.size);
  103. CuAssertStrEquals(tc, "jpg", image_info.format);
  104. printf("test_resize_image ok\n");
  105. }
  106. void test_crop_image(CuTest *tc) {
  107. aos_pool_t *p = NULL;
  108. aos_string_t bucket;
  109. aos_string_t object;
  110. int is_cname = 0;
  111. oss_request_options_t *options = NULL;
  112. aos_table_t *headers = NULL;
  113. aos_table_t *params = NULL;
  114. aos_table_t *resp_headers = NULL;
  115. aos_status_t *s = NULL;
  116. aos_list_t buffer;
  117. image_info_t image_info;
  118. /* put original image */
  119. put_example_image(tc);
  120. aos_pool_create(&p, NULL);
  121. options = oss_request_options_create(p);
  122. init_test_request_options(options, is_cname);
  123. aos_str_set(&bucket, TEST_BUCKET_NAME);
  124. aos_str_set(&object, original_image);
  125. aos_list_init(&buffer);
  126. params = aos_table_make(p, 1);
  127. apr_table_set(params, OSS_PROCESS, "image/crop,w_100,h_100,x_100,y_100,r_1");
  128. /* get processed image to buffer */
  129. s = oss_get_object_to_buffer(options, &bucket, &object, headers,
  130. params, &buffer, &resp_headers);
  131. CuAssertIntEquals(tc, 200, s->code);
  132. aos_str_set(&object, processed_image);
  133. /* put processed image */
  134. s= oss_put_object_from_buffer(options, &bucket, &object, &buffer,
  135. headers, &resp_headers);
  136. CuAssertIntEquals(tc, 200, s->code);
  137. aos_pool_destroy(p);
  138. /* check processed image */
  139. get_iamge_info(tc, &image_info);
  140. CuAssertIntEquals(tc, 100, image_info.height);
  141. CuAssertIntEquals(tc, 100, image_info.width);
  142. CuAssertIntEquals(tc, 1969, image_info.size);
  143. CuAssertStrEquals(tc, "jpg", image_info.format);
  144. printf("test_crop_image ok\n");
  145. }
  146. void test_rotate_image(CuTest *tc) {
  147. aos_pool_t *p = NULL;
  148. aos_string_t bucket;
  149. aos_string_t object;
  150. int is_cname = 0;
  151. oss_request_options_t *options = NULL;
  152. aos_table_t *headers = NULL;
  153. aos_table_t *params = NULL;
  154. aos_table_t *resp_headers = NULL;
  155. aos_status_t *s = NULL;
  156. aos_list_t buffer;
  157. image_info_t image_info;
  158. /* put original image */
  159. put_example_image(tc);
  160. aos_pool_create(&p, NULL);
  161. options = oss_request_options_create(p);
  162. init_test_request_options(options, is_cname);
  163. aos_str_set(&bucket, TEST_BUCKET_NAME);
  164. aos_str_set(&object, original_image);
  165. aos_list_init(&buffer);
  166. params = aos_table_make(p, 1);
  167. apr_table_set(params, OSS_PROCESS, "image/rotate,90");
  168. /* get processed image to buffer */
  169. s = oss_get_object_to_buffer(options, &bucket, &object, headers,
  170. params, &buffer, &resp_headers);
  171. CuAssertIntEquals(tc, 200, s->code);
  172. aos_str_set(&object, processed_image);
  173. /* put processed image */
  174. s= oss_put_object_from_buffer(options, &bucket, &object, &buffer,
  175. headers, &resp_headers);
  176. CuAssertIntEquals(tc, 200, s->code);
  177. aos_pool_destroy(p);
  178. /* check processed image */
  179. get_iamge_info(tc, &image_info);
  180. CuAssertIntEquals(tc, 400, image_info.height);
  181. CuAssertIntEquals(tc, 267, image_info.width);
  182. CuAssertIntEquals(tc, 20998, image_info.size);
  183. CuAssertStrEquals(tc, "jpg", image_info.format);
  184. printf("test_rotate_image ok\n");
  185. }
  186. void test_sharpen_image(CuTest *tc) {
  187. aos_pool_t *p = NULL;
  188. aos_string_t bucket;
  189. aos_string_t object;
  190. int is_cname = 0;
  191. oss_request_options_t *options = NULL;
  192. aos_table_t *headers = NULL;
  193. aos_table_t *params = NULL;
  194. aos_table_t *resp_headers = NULL;
  195. aos_status_t *s = NULL;
  196. aos_list_t buffer;
  197. image_info_t image_info;
  198. /* put original image */
  199. put_example_image(tc);
  200. aos_pool_create(&p, NULL);
  201. options = oss_request_options_create(p);
  202. init_test_request_options(options, is_cname);
  203. aos_str_set(&bucket, TEST_BUCKET_NAME);
  204. aos_str_set(&object, original_image);
  205. aos_list_init(&buffer);
  206. params = aos_table_make(p, 1);
  207. apr_table_set(params, OSS_PROCESS, "image/sharpen,100");
  208. /* get processed image to buffer */
  209. s = oss_get_object_to_buffer(options, &bucket, &object, headers,
  210. params, &buffer, &resp_headers);
  211. CuAssertIntEquals(tc, 200, s->code);
  212. aos_str_set(&object, processed_image);
  213. /* put processed image */
  214. s= oss_put_object_from_buffer(options, &bucket, &object, &buffer,
  215. headers, &resp_headers);
  216. CuAssertIntEquals(tc, 200, s->code);
  217. aos_pool_destroy(p);
  218. /* check processed image */
  219. get_iamge_info(tc, &image_info);
  220. CuAssertIntEquals(tc, 267, image_info.height);
  221. CuAssertIntEquals(tc, 400, image_info.width);
  222. CuAssertIntEquals(tc, 23015, image_info.size);
  223. CuAssertStrEquals(tc, "jpg", image_info.format);
  224. printf("test_sharpen_image ok\n");
  225. }
  226. void test_watermark_image(CuTest *tc) {
  227. aos_pool_t *p = NULL;
  228. aos_string_t bucket;
  229. aos_string_t object;
  230. int is_cname = 0;
  231. oss_request_options_t *options = NULL;
  232. aos_table_t *headers = NULL;
  233. aos_table_t *params = NULL;
  234. aos_table_t *resp_headers = NULL;
  235. aos_status_t *s = NULL;
  236. aos_list_t buffer;
  237. image_info_t image_info;
  238. /* put original image */
  239. put_example_image(tc);
  240. aos_pool_create(&p, NULL);
  241. options = oss_request_options_create(p);
  242. init_test_request_options(options, is_cname);
  243. aos_str_set(&bucket, TEST_BUCKET_NAME);
  244. aos_str_set(&object, original_image);
  245. aos_list_init(&buffer);
  246. params = aos_table_make(p, 1);
  247. apr_table_set(params, OSS_PROCESS, "image/watermark,text_SGVsbG8g5Zu-54mH5pyN5YqhIQ");
  248. /* get processed image to buffer */
  249. s = oss_get_object_to_buffer(options, &bucket, &object, headers,
  250. params, &buffer, &resp_headers);
  251. CuAssertIntEquals(tc, 200, s->code);
  252. aos_str_set(&object, processed_image);
  253. /* put processed image */
  254. s= oss_put_object_from_buffer(options, &bucket, &object, &buffer,
  255. headers, &resp_headers);
  256. CuAssertIntEquals(tc, 200, s->code);
  257. aos_pool_destroy(p);
  258. /* check processed image */
  259. get_iamge_info(tc, &image_info);
  260. CuAssertIntEquals(tc, 267, image_info.height);
  261. CuAssertIntEquals(tc, 400, image_info.width);
  262. CuAssertStrEquals(tc, "jpg", image_info.format);
  263. printf("test_watermark_image ok\n");
  264. }
  265. void test_format_image(CuTest *tc) {
  266. aos_pool_t *p = NULL;
  267. aos_string_t bucket;
  268. aos_string_t object;
  269. int is_cname = 0;
  270. oss_request_options_t *options = NULL;
  271. aos_table_t *headers = NULL;
  272. aos_table_t *params = NULL;
  273. aos_table_t *resp_headers = NULL;
  274. aos_status_t *s = NULL;
  275. aos_list_t buffer;
  276. image_info_t image_info;
  277. /* put original image */
  278. put_example_image(tc);
  279. aos_pool_create(&p, NULL);
  280. options = oss_request_options_create(p);
  281. init_test_request_options(options, is_cname);
  282. aos_str_set(&bucket, TEST_BUCKET_NAME);
  283. aos_str_set(&object, original_image);
  284. aos_list_init(&buffer);
  285. params = aos_table_make(p, 1);
  286. apr_table_set(params, OSS_PROCESS, "image/format,png");
  287. /* get processed image to buffer */
  288. s = oss_get_object_to_buffer(options, &bucket, &object, headers,
  289. params, &buffer, &resp_headers);
  290. CuAssertIntEquals(tc, 200, s->code);
  291. aos_str_set(&object, processed_image);
  292. /* put processed image */
  293. s= oss_put_object_from_buffer(options, &bucket, &object, &buffer,
  294. headers, &resp_headers);
  295. CuAssertIntEquals(tc, 200, s->code);
  296. aos_pool_destroy(p);
  297. /* check processed image */
  298. get_iamge_info(tc, &image_info);
  299. CuAssertIntEquals(tc, 267, image_info.height);
  300. CuAssertIntEquals(tc, 400, image_info.width);
  301. CuAssertIntEquals(tc, 160733, image_info.size);
  302. CuAssertStrEquals(tc, "png", image_info.format);
  303. printf("test_format_image ok\n");
  304. }
  305. void test_oss_put_object_with_process(CuTest *tc) {
  306. aos_pool_t *p = NULL;
  307. aos_string_t bucket;
  308. aos_string_t object;
  309. aos_string_t filename;
  310. int is_cname = 0;
  311. oss_request_options_t *options = NULL;
  312. aos_table_t *headers = NULL;
  313. aos_table_t *params = NULL;
  314. aos_table_t *resp_headers = NULL;
  315. aos_status_t *s = NULL;
  316. char *user_meta = NULL;
  317. aos_list_t buffer;
  318. aos_list_t resp_body;
  319. image_info_t image_info;
  320. aos_pool_create(&p, NULL);
  321. options = oss_request_options_create(p);
  322. init_test_request_options(options, is_cname);
  323. aos_str_set(&bucket, TEST_BUCKET_NAME);
  324. aos_str_set(&object, original_image);
  325. aos_str_set(&filename, image_file);
  326. aos_list_init(&buffer);
  327. aos_list_init(&resp_body);
  328. /* headers */
  329. headers = aos_table_make(p, 3);
  330. apr_table_set(headers, "x-oss-meta-author", "oss");
  331. apr_table_set(headers, "Expect", "");
  332. apr_table_set(headers, "Transfer-Encoding", "");
  333. /* param */
  334. params = aos_table_make(p, 1);
  335. apr_table_set(params, OSS_PROCESS, "image/resize,m_fixed,w_100,h_100");
  336. /* put original image */
  337. s = oss_do_put_object_from_file(options, &bucket, &object, &filename,
  338. headers, params, progress_callback, &resp_headers, &resp_body);
  339. CuAssertIntEquals(tc, 200, s->code);
  340. CuAssertPtrNotNull(tc, resp_headers);
  341. /* test head object */
  342. s = oss_head_object(options, &bucket, &object, NULL, &resp_headers);
  343. CuAssertIntEquals(tc, 200, s->code);
  344. CuAssertPtrNotNull(tc, resp_headers);
  345. user_meta = (char*)(apr_table_get(resp_headers, "x-oss-meta-author"));
  346. CuAssertStrEquals(tc, "oss", user_meta);
  347. /* get processed image to buffer */
  348. s = oss_get_object_to_buffer(options, &bucket, &object, NULL, NULL,
  349. &buffer, &resp_headers);
  350. CuAssertIntEquals(tc, 200, s->code);
  351. aos_str_set(&object, processed_image);
  352. /* put processed image */
  353. s= oss_do_put_object_from_buffer(options, &bucket, &object, &buffer,
  354. NULL, params, progress_callback, &resp_headers, &resp_body);
  355. CuAssertIntEquals(tc, 200, s->code);
  356. aos_pool_destroy(p);
  357. /* check processed image */
  358. get_iamge_info(tc, &image_info);
  359. CuAssertIntEquals(tc, 267, image_info.height);
  360. CuAssertIntEquals(tc, 400, image_info.width);
  361. CuAssertIntEquals(tc, 21839, image_info.size);
  362. CuAssertStrEquals(tc, "jpg", image_info.format);
  363. printf("test_oss_put_object_with_process ok\n");
  364. }
  365. void put_example_image(CuTest *tc)
  366. {
  367. aos_pool_t *p = NULL;
  368. aos_string_t bucket;
  369. aos_string_t object;
  370. aos_string_t filename;
  371. aos_status_t *s = NULL;
  372. oss_request_options_t *options = NULL;
  373. int is_cname = 0;
  374. aos_table_t *headers = NULL;
  375. aos_table_t *resp_headers = NULL;
  376. char *content_type = NULL;
  377. aos_pool_create(&p, NULL);
  378. options = oss_request_options_create(p);
  379. init_test_request_options(options, is_cname);
  380. aos_str_set(&bucket, TEST_BUCKET_NAME);
  381. aos_str_set(&object, original_image);
  382. aos_str_set(&filename, image_file);
  383. s = oss_put_object_from_file(options, &bucket, &object, &filename,
  384. headers, &resp_headers);
  385. CuAssertIntEquals(tc, 200, s->code);
  386. CuAssertPtrNotNull(tc, resp_headers);
  387. /* head object */
  388. s = oss_head_object(options, &bucket, &object, headers, &resp_headers);
  389. CuAssertIntEquals(tc, 200, s->code);
  390. CuAssertPtrNotNull(tc, resp_headers);
  391. content_type = (char*)(apr_table_get(resp_headers, OSS_CONTENT_TYPE));
  392. CuAssertStrEquals(tc, "image/jpeg", content_type);
  393. aos_pool_destroy(p);
  394. }
  395. void get_iamge_info(CuTest *tc, image_info_t *image_info)
  396. {
  397. aos_pool_t *p = NULL;
  398. aos_string_t bucket;
  399. char *object_name = processed_image;
  400. aos_string_t object;
  401. int is_cname = 0;
  402. oss_request_options_t *options = NULL;
  403. aos_table_t *headers = NULL;
  404. aos_table_t *params = NULL;
  405. aos_table_t *resp_headers = NULL;
  406. aos_status_t *s = NULL;
  407. aos_list_t buffer;
  408. aos_buf_t *content = NULL;
  409. char *buf = NULL;
  410. int64_t len = 0;
  411. int64_t size = 0;
  412. int64_t pos = 0;
  413. aos_pool_create(&p, NULL);
  414. options = oss_request_options_create(p);
  415. init_test_request_options(options, is_cname);
  416. aos_str_set(&bucket, TEST_BUCKET_NAME);
  417. aos_str_set(&object, object_name);
  418. aos_list_init(&buffer);
  419. params = aos_table_make(p, 1);
  420. apr_table_set(params, OSS_PROCESS, "image/info");
  421. /* test get object to buffer */
  422. s = oss_get_object_to_buffer(options, &bucket, &object, headers,
  423. params, &buffer, &resp_headers);
  424. CuAssertIntEquals(tc, 200, s->code);
  425. CuAssertPtrNotNull(tc, resp_headers);
  426. /* get buffer len */
  427. len = aos_buf_list_len(&buffer);
  428. buf = (char *)aos_pcalloc(p, (apr_size_t)(len + 1));
  429. buf[len] = '\0';
  430. /* copy buffer content to memory */
  431. aos_list_for_each_entry(aos_buf_t, content, &buffer, node) {
  432. size = aos_buf_size(content);
  433. memcpy(buf + pos, content->pos, (size_t)size);
  434. pos += size;
  435. }
  436. /* parse image info from json */
  437. parse_image_info(tc, buf, image_info);
  438. aos_pool_destroy(p);
  439. }
  440. void parse_image_info(CuTest *tc, const char *json, image_info_t *image_info)
  441. {
  442. cJSON * root = NULL;
  443. cJSON * item = NULL;
  444. root = cJSON_Parse(json);
  445. CuAssertPtrNotNull(tc, root);
  446. item = cJSON_GetObjectItem(root, "ImageHeight");
  447. item = cJSON_GetObjectItem(item, "value");
  448. image_info->height = atol(item->valuestring);
  449. item = cJSON_GetObjectItem(root, "ImageWidth");
  450. item = cJSON_GetObjectItem(item, "value");
  451. image_info->width = atol(item->valuestring);
  452. item = cJSON_GetObjectItem(root, "FileSize");
  453. item = cJSON_GetObjectItem(item, "value");
  454. image_info->size = atol(item->valuestring);
  455. item = cJSON_GetObjectItem(root, "Format");
  456. item = cJSON_GetObjectItem(item, "value");
  457. apr_snprintf(image_info->format, 64, "%s", item->valuestring);
  458. cJSON_Delete(root);
  459. }
  460. CuSuite *test_oss_image()
  461. {
  462. CuSuite* suite = CuSuiteNew();
  463. SUITE_ADD_TEST(suite, test_image_setup);
  464. SUITE_ADD_TEST(suite, test_resize_image);
  465. SUITE_ADD_TEST(suite, test_crop_image);
  466. SUITE_ADD_TEST(suite, test_rotate_image);
  467. SUITE_ADD_TEST(suite, test_sharpen_image);
  468. SUITE_ADD_TEST(suite, test_watermark_image);
  469. SUITE_ADD_TEST(suite, test_format_image);
  470. SUITE_ADD_TEST(suite, test_oss_put_object_with_process);
  471. SUITE_ADD_TEST(suite, test_image_cleanup);
  472. return suite;
  473. }