encryption_info.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * This file is part of FFmpeg.
  3. *
  4. * FFmpeg is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Lesser General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2.1 of the License, or (at your option) any later version.
  8. *
  9. * FFmpeg is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Lesser General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Lesser General Public
  15. * License along with FFmpeg; if not, write to the Free Software
  16. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17. */
  18. #include "libavutil/encryption_info.h"
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include "libavutil/avassert.h"
  22. static const AVSubsampleEncryptionInfo test_subsamples[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}};
  23. static const size_t test_subsample_count = sizeof(test_subsamples) / sizeof(test_subsamples[0]);
  24. static const uint8_t test_iv[] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18};
  25. static const uint8_t test_key_id[] = {0x21, 0x22, 0x23, 0x24};
  26. static const uint8_t test_key_id_2[] = {0x31, 0x32, 0x33, 0x34};
  27. static const uint8_t test_system_id[] = {0x41, 0x42, 0x43};
  28. static const uint8_t test_data[] = {0x51, 0x52};
  29. static int compare_encryption_info(const AVEncryptionInfo *a, const AVEncryptionInfo *b) {
  30. if (!a || !b || a->scheme != b->scheme || a->crypt_byte_block != b->crypt_byte_block ||
  31. a->skip_byte_block != b->skip_byte_block || a->key_id_size != b->key_id_size ||
  32. a->iv_size != b->iv_size || a->subsample_count != b->subsample_count)
  33. return 1;
  34. if (memcmp(a->key_id, b->key_id, a->key_id_size) != 0 ||
  35. memcmp(a->iv, b->iv, a->iv_size) != 0 ||
  36. memcmp(a->subsamples, b->subsamples, a->subsample_count * sizeof(a->subsamples[0])))
  37. return 1;
  38. return 0;
  39. }
  40. static int compare_encryption_init_info(const AVEncryptionInitInfo *a, const AVEncryptionInitInfo *b) {
  41. if (!a || !b || a->system_id_size != b->system_id_size ||
  42. a->num_key_ids != b->num_key_ids || a->key_id_size != b->key_id_size ||
  43. a->data_size != b->data_size)
  44. return 1;
  45. if (memcmp(a->system_id, b->system_id, a->system_id_size) != 0 ||
  46. memcmp(a->data, b->data, a->data_size) != 0)
  47. return 1;
  48. for (uint32_t i = 0; i < a->num_key_ids; i++) {
  49. if (memcmp(a->key_ids[i], b->key_ids[i], a->key_id_size) != 0)
  50. return 1;
  51. }
  52. if (a->next || b->next) {
  53. if (!a->next || !b->next)
  54. return 1;
  55. if (compare_encryption_init_info(a->next, b->next) != 0)
  56. return 1;
  57. }
  58. return 0;
  59. }
  60. static void run_encryption_info_test(void)
  61. {
  62. AVEncryptionInfo *info, *copy;
  63. uint8_t *side_data;
  64. size_t side_data_size;
  65. info = av_encryption_info_alloc(test_subsample_count, sizeof(test_key_id), sizeof(test_iv));
  66. av_assert0(info);
  67. av_assert0(info->key_id);
  68. av_assert0(info->key_id_size == sizeof(test_key_id));
  69. av_assert0(info->iv);
  70. av_assert0(info->iv_size == sizeof(test_iv));
  71. av_assert0(info->subsamples);
  72. av_assert0(info->subsample_count == test_subsample_count);
  73. info->scheme = 1234;
  74. info->crypt_byte_block = 333;
  75. info->skip_byte_block = 444;
  76. memcpy(info->key_id, test_key_id, sizeof(test_key_id));
  77. memcpy(info->iv, test_iv, sizeof(test_iv));
  78. memcpy(info->subsamples, test_subsamples, sizeof(test_subsamples));
  79. copy = av_encryption_info_clone(info);
  80. av_assert0(copy);
  81. av_assert0(copy != info);
  82. av_assert0(compare_encryption_info(info, copy) == 0);
  83. av_encryption_info_free(copy);
  84. side_data = av_encryption_info_add_side_data(info, &side_data_size);
  85. av_assert0(side_data);
  86. av_assert0(side_data_size > 0);
  87. copy = av_encryption_info_get_side_data(side_data, side_data_size);
  88. av_assert0(copy);
  89. av_assert0(copy != info);
  90. av_assert0(compare_encryption_info(info, copy) == 0);
  91. av_encryption_info_free(copy);
  92. av_free(side_data);
  93. av_encryption_info_free(info);
  94. }
  95. static AVEncryptionInitInfo *create_init_info(void)
  96. {
  97. AVEncryptionInitInfo *info;
  98. info = av_encryption_init_info_alloc(sizeof(test_system_id), 2, sizeof(test_key_id), sizeof(test_data));
  99. av_assert0(info);
  100. av_assert0(info->system_id);
  101. av_assert0(info->system_id_size == sizeof(test_system_id));
  102. av_assert0(info->key_ids);
  103. av_assert0(info->num_key_ids == 2);
  104. av_assert0(info->key_id_size == sizeof(test_key_id));
  105. av_assert0(info->key_ids[0]);
  106. av_assert0(info->key_ids[1]);
  107. av_assert0(info->data);
  108. av_assert0(info->data_size == sizeof(test_data));
  109. av_assert0(!info->next);
  110. memcpy(info->system_id, test_system_id, sizeof(test_system_id));
  111. memcpy(info->key_ids[0], test_key_id, sizeof(test_key_id));
  112. memcpy(info->key_ids[1], test_key_id_2, sizeof(test_key_id_2));
  113. memcpy(info->data, test_data, sizeof(test_data));
  114. return info;
  115. }
  116. static void run_encryption_init_info_test(void)
  117. {
  118. AVEncryptionInitInfo *info, *copy;
  119. uint8_t *side_data;
  120. size_t side_data_size;
  121. info = create_init_info();
  122. side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
  123. av_assert0(side_data);
  124. av_assert0(side_data_size > 0);
  125. copy = av_encryption_init_info_get_side_data(side_data, side_data_size);
  126. av_assert0(copy);
  127. av_assert0(compare_encryption_init_info(info, copy) == 0);
  128. av_encryption_init_info_free(copy);
  129. av_free(side_data);
  130. // Make the first init info different from the second to test the correct order.
  131. memset(info->system_id, 0, info->system_id_size);
  132. info->next = create_init_info();
  133. side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
  134. av_assert0(side_data);
  135. copy = av_encryption_init_info_get_side_data(side_data, side_data_size);
  136. av_assert0(copy);
  137. av_assert0(compare_encryption_init_info(info, copy) == 0);
  138. av_encryption_init_info_free(copy);
  139. av_free(side_data);
  140. av_encryption_init_info_free(info);
  141. }
  142. int main(int argc, char **argv)
  143. {
  144. run_encryption_info_test();
  145. run_encryption_init_info_test();
  146. return 0;
  147. }