libavutil/tests/encryption_info.c
| Line | Branch | Exec | Source |
|---|---|---|---|
| 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 | |||
| 19 | #include "libavutil/encryption_info.h" | ||
| 20 | |||
| 21 | #include <stdio.h> | ||
| 22 | #include <string.h> | ||
| 23 | |||
| 24 | #include "libavutil/avassert.h" | ||
| 25 | #include "libavutil/mem.h" | ||
| 26 | |||
| 27 | static const AVSubsampleEncryptionInfo test_subsamples[] = {{1, 2}, {3, 4}, {5, 6}, {7, 8}}; | ||
| 28 | static const size_t test_subsample_count = sizeof(test_subsamples) / sizeof(test_subsamples[0]); | ||
| 29 | static const uint8_t test_iv[] = {0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18}; | ||
| 30 | static const uint8_t test_key_id[] = {0x21, 0x22, 0x23, 0x24}; | ||
| 31 | static const uint8_t test_key_id_2[] = {0x31, 0x32, 0x33, 0x34}; | ||
| 32 | static const uint8_t test_system_id[] = {0x41, 0x42, 0x43}; | ||
| 33 | static const uint8_t test_data[] = {0x51, 0x52}; | ||
| 34 | |||
| 35 | 2 | static int compare_encryption_info(const AVEncryptionInfo *a, const AVEncryptionInfo *b) { | |
| 36 | 2 | if (!a || !b || a->scheme != b->scheme || a->crypt_byte_block != b->crypt_byte_block || | |
| 37 | 2 | a->skip_byte_block != b->skip_byte_block || a->key_id_size != b->key_id_size || | |
| 38 | 2 | a->iv_size != b->iv_size || a->subsample_count != b->subsample_count) | |
| 39 | ✗ | return 1; | |
| 40 | |||
| 41 | 2 | if (memcmp(a->key_id, b->key_id, a->key_id_size) != 0 || | |
| 42 | 2 | memcmp(a->iv, b->iv, a->iv_size) != 0 || | |
| 43 | 2 | memcmp(a->subsamples, b->subsamples, a->subsample_count * sizeof(a->subsamples[0]))) | |
| 44 | ✗ | return 1; | |
| 45 | |||
| 46 | 2 | return 0; | |
| 47 | } | ||
| 48 | |||
| 49 | 2 | static int compare_encryption_init_info(const AVEncryptionInitInfo *a, const AVEncryptionInitInfo *b) { | |
| 50 | 2 | if (!a || !b) | |
| 51 | ✗ | return 1; | |
| 52 | |||
| 53 | 5 | while (a && b) { | |
| 54 | 3 | if (a->system_id_size != b->system_id_size || | |
| 55 | 3 | a->num_key_ids != b->num_key_ids || a->key_id_size != b->key_id_size || | |
| 56 | 3 | a->data_size != b->data_size) | |
| 57 | ✗ | return 1; | |
| 58 | |||
| 59 | 3 | if (memcmp(a->system_id, b->system_id, a->system_id_size) != 0 || | |
| 60 | 3 | memcmp(a->data, b->data, a->data_size) != 0) | |
| 61 | ✗ | return 1; | |
| 62 | |||
| 63 | 9 | for (uint32_t i = 0; i < a->num_key_ids; i++) { | |
| 64 | 6 | if (memcmp(a->key_ids[i], b->key_ids[i], a->key_id_size) != 0) | |
| 65 | ✗ | return 1; | |
| 66 | } | ||
| 67 | |||
| 68 | 3 | a = a->next; | |
| 69 | 3 | b = b->next; | |
| 70 | } | ||
| 71 | |||
| 72 | 2 | return a != b; | |
| 73 | } | ||
| 74 | |||
| 75 | 1 | static void run_encryption_info_test(void) | |
| 76 | { | ||
| 77 | AVEncryptionInfo *info, *copy; | ||
| 78 | uint8_t *side_data; | ||
| 79 | size_t side_data_size; | ||
| 80 | |||
| 81 | 1 | info = av_encryption_info_alloc(test_subsample_count, sizeof(test_key_id), sizeof(test_iv)); | |
| 82 | 1 | av_assert0(info); | |
| 83 | 1 | av_assert0(info->key_id); | |
| 84 | 1 | av_assert0(info->key_id_size == sizeof(test_key_id)); | |
| 85 | 1 | av_assert0(info->iv); | |
| 86 | 1 | av_assert0(info->iv_size == sizeof(test_iv)); | |
| 87 | 1 | av_assert0(info->subsamples); | |
| 88 | 1 | av_assert0(info->subsample_count == test_subsample_count); | |
| 89 | |||
| 90 | 1 | info->scheme = 1234; | |
| 91 | 1 | info->crypt_byte_block = 333; | |
| 92 | 1 | info->skip_byte_block = 444; | |
| 93 | 1 | memcpy(info->key_id, test_key_id, sizeof(test_key_id)); | |
| 94 | 1 | memcpy(info->iv, test_iv, sizeof(test_iv)); | |
| 95 | 1 | memcpy(info->subsamples, test_subsamples, sizeof(test_subsamples)); | |
| 96 | |||
| 97 | 1 | copy = av_encryption_info_clone(info); | |
| 98 | 1 | av_assert0(copy); | |
| 99 | 1 | av_assert0(copy != info); | |
| 100 | 1 | av_assert0(compare_encryption_info(info, copy) == 0); | |
| 101 | 1 | av_encryption_info_free(copy); | |
| 102 | |||
| 103 | 1 | side_data = av_encryption_info_add_side_data(info, &side_data_size); | |
| 104 | 1 | av_assert0(side_data); | |
| 105 | 1 | av_assert0(side_data_size > 0); | |
| 106 | |||
| 107 | 1 | copy = av_encryption_info_get_side_data(side_data, side_data_size); | |
| 108 | 1 | av_assert0(copy); | |
| 109 | 1 | av_assert0(copy != info); | |
| 110 | 1 | av_assert0(compare_encryption_info(info, copy) == 0); | |
| 111 | 1 | av_encryption_info_free(copy); | |
| 112 | 1 | av_free(side_data); | |
| 113 | |||
| 114 | 1 | av_encryption_info_free(info); | |
| 115 | 1 | } | |
| 116 | |||
| 117 | 2 | static AVEncryptionInitInfo *create_init_info(void) | |
| 118 | { | ||
| 119 | AVEncryptionInitInfo *info; | ||
| 120 | |||
| 121 | 2 | info = av_encryption_init_info_alloc(sizeof(test_system_id), 2, sizeof(test_key_id), sizeof(test_data)); | |
| 122 | 2 | av_assert0(info); | |
| 123 | 2 | av_assert0(info->system_id); | |
| 124 | 2 | av_assert0(info->system_id_size == sizeof(test_system_id)); | |
| 125 | 2 | av_assert0(info->key_ids); | |
| 126 | 2 | av_assert0(info->num_key_ids == 2); | |
| 127 | 2 | av_assert0(info->key_id_size == sizeof(test_key_id)); | |
| 128 | 2 | av_assert0(info->key_ids[0]); | |
| 129 | 2 | av_assert0(info->key_ids[1]); | |
| 130 | 2 | av_assert0(info->data); | |
| 131 | 2 | av_assert0(info->data_size == sizeof(test_data)); | |
| 132 | 2 | av_assert0(!info->next); | |
| 133 | |||
| 134 | 2 | memcpy(info->system_id, test_system_id, sizeof(test_system_id)); | |
| 135 | 2 | memcpy(info->key_ids[0], test_key_id, sizeof(test_key_id)); | |
| 136 | 2 | memcpy(info->key_ids[1], test_key_id_2, sizeof(test_key_id_2)); | |
| 137 | 2 | memcpy(info->data, test_data, sizeof(test_data)); | |
| 138 | |||
| 139 | 2 | return info; | |
| 140 | } | ||
| 141 | |||
| 142 | 1 | static void run_encryption_init_info_test(void) | |
| 143 | { | ||
| 144 | AVEncryptionInitInfo *info, *copy; | ||
| 145 | uint8_t *side_data, *appended; | ||
| 146 | size_t side_data_size, appended_size; | ||
| 147 | 1 | unsigned int allocated_size = 0; | |
| 148 | |||
| 149 | 1 | info = create_init_info(); | |
| 150 | |||
| 151 | 1 | side_data = av_encryption_init_info_add_side_data(info, &side_data_size); | |
| 152 | 1 | av_assert0(side_data); | |
| 153 | 1 | av_assert0(side_data_size > 0); | |
| 154 | 1 | copy = av_encryption_init_info_get_side_data(side_data, side_data_size); | |
| 155 | 1 | av_assert0(copy); | |
| 156 | 1 | av_assert0(compare_encryption_init_info(info, copy) == 0); | |
| 157 | 1 | av_encryption_init_info_free(copy); | |
| 158 | 1 | av_free(side_data); | |
| 159 | |||
| 160 | // Make the first init info different from the second to test the correct order. | ||
| 161 | 1 | memset(info->system_id, 0, info->system_id_size); | |
| 162 | 1 | appended = av_encryption_init_info_add_side_data(info, &appended_size); | |
| 163 | 1 | av_assert0(appended); | |
| 164 | 1 | info->next = create_init_info(); | |
| 165 | 1 | side_data = av_encryption_init_info_add_side_data(info, &side_data_size); | |
| 166 | 1 | av_assert0(side_data); | |
| 167 | 1 | copy = av_encryption_init_info_get_side_data(side_data, side_data_size); | |
| 168 | 1 | av_assert0(copy); | |
| 169 | 1 | av_assert0(compare_encryption_init_info(info, copy) == 0); | |
| 170 | 1 | av_encryption_init_info_free(copy); | |
| 171 | |||
| 172 | 1 | av_assert0(av_encryption_init_info_append_side_data(&appended, &appended_size, &allocated_size, info->next) == 0); | |
| 173 | 1 | av_assert0(appended_size == side_data_size); | |
| 174 | 1 | av_assert0(memcmp(appended, side_data, side_data_size) == 0); | |
| 175 | 1 | av_free(appended); | |
| 176 | 1 | av_free(side_data); | |
| 177 | |||
| 178 | 1 | av_encryption_init_info_free(info); | |
| 179 | 1 | } | |
| 180 | |||
| 181 | 1 | int main(int argc, char **argv) | |
| 182 | { | ||
| 183 | 1 | run_encryption_info_test(); | |
| 184 | 1 | run_encryption_init_info_test(); | |
| 185 | 1 | return 0; | |
| 186 | } | ||
| 187 |