../../../ffmpeg/
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
102 / 108
94.4%
Branches:
62 / 120
51.7%
Functions:
6 / 6
100.0%

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
4/8
✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 10 not taken.
✓ Branch 3 → 4 taken 2 times.
✗ Branch 3 → 10 not taken.
✓ Branch 4 → 5 taken 2 times.
✗ Branch 4 → 10 not taken.
✓ Branch 5 → 6 taken 2 times.
✗ Branch 5 → 10 not taken.
2 if (!a || !b || a->scheme != b->scheme || a->crypt_byte_block != b->crypt_byte_block ||
37
2/4
✓ Branch 6 → 7 taken 2 times.
✗ Branch 6 → 10 not taken.
✓ Branch 7 → 8 taken 2 times.
✗ Branch 7 → 10 not taken.
2 a->skip_byte_block != b->skip_byte_block || a->key_id_size != b->key_id_size ||
38
2/4
✓ Branch 8 → 9 taken 2 times.
✗ Branch 8 → 10 not taken.
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 2 times.
2 a->iv_size != b->iv_size || a->subsample_count != b->subsample_count)
39 ✗ return 1;
40
41
1/2
✓ Branch 11 → 12 taken 2 times.
✗ Branch 11 → 14 not taken.
2 if (memcmp(a->key_id, b->key_id, a->key_id_size) != 0 ||
42
1/2
✓ Branch 12 → 13 taken 2 times.
✗ Branch 12 → 14 not taken.
2 memcmp(a->iv, b->iv, a->iv_size) != 0 ||
43
1/2
✗ Branch 13 → 14 not taken.
✓ Branch 13 → 15 taken 2 times.
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/4
✓ Branch 2 → 3 taken 2 times.
✗ Branch 2 → 4 not taken.
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 5 taken 2 times.
2 if (!a || !b)
51 ✗ return 1;
52
53
3/4
✓ Branch 20 → 21 taken 3 times.
✓ Branch 20 → 22 taken 2 times.
✓ Branch 21 → 6 taken 3 times.
✗ Branch 21 → 22 not taken.
5 while (a && b) {
54
1/2
✓ Branch 6 → 7 taken 3 times.
✗ Branch 6 → 10 not taken.
3 if (a->system_id_size != b->system_id_size ||
55
2/4
✓ Branch 7 → 8 taken 3 times.
✗ Branch 7 → 10 not taken.
✓ Branch 8 → 9 taken 3 times.
✗ Branch 8 → 10 not taken.
3 a->num_key_ids != b->num_key_ids || a->key_id_size != b->key_id_size ||
56
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 11 taken 3 times.
3 a->data_size != b->data_size)
57 ✗ return 1;
58
59
1/2
✓ Branch 11 → 12 taken 3 times.
✗ Branch 11 → 13 not taken.
3 if (memcmp(a->system_id, b->system_id, a->system_id_size) != 0 ||
60
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 14 taken 3 times.
3 memcmp(a->data, b->data, a->data_size) != 0)
61 ✗ return 1;
62
63
2/2
✓ Branch 18 → 15 taken 6 times.
✓ Branch 18 → 19 taken 3 times.
9 for (uint32_t i = 0; i < a->num_key_ids; i++) {
64
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 17 taken 6 times.
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/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 1 time.
1 av_assert0(info);
83
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 1 time.
1 av_assert0(info->key_id);
84
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 1 time.
1 av_assert0(info->key_id_size == sizeof(test_key_id));
85
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 15 taken 1 time.
1 av_assert0(info->iv);
86
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 1 time.
1 av_assert0(info->iv_size == sizeof(test_iv));
87
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 21 taken 1 time.
1 av_assert0(info->subsamples);
88
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 24 taken 1 time.
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/2
✗ Branch 25 → 26 not taken.
✓ Branch 25 → 28 taken 1 time.
1 av_assert0(copy);
99
1/2
✗ Branch 28 → 29 not taken.
✓ Branch 28 → 31 taken 1 time.
1 av_assert0(copy != info);
100
1/2
✗ Branch 32 → 33 not taken.
✓ Branch 32 → 35 taken 1 time.
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/2
✗ Branch 37 → 38 not taken.
✓ Branch 37 → 40 taken 1 time.
1 av_assert0(side_data);
105
1/2
✗ Branch 40 → 41 not taken.
✓ Branch 40 → 43 taken 1 time.
1 av_assert0(side_data_size > 0);
106
107 1 copy = av_encryption_info_get_side_data(side_data, side_data_size);
108
1/2
✗ Branch 44 → 45 not taken.
✓ Branch 44 → 47 taken 1 time.
1 av_assert0(copy);
109
1/2
✗ Branch 47 → 48 not taken.
✓ Branch 47 → 50 taken 1 time.
1 av_assert0(copy != info);
110
1/2
✗ Branch 51 → 52 not taken.
✓ Branch 51 → 54 taken 1 time.
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
1/2
✗ Branch 3 → 4 not taken.
✓ Branch 3 → 6 taken 2 times.
2 av_assert0(info);
123
1/2
✗ Branch 6 → 7 not taken.
✓ Branch 6 → 9 taken 2 times.
2 av_assert0(info->system_id);
124
1/2
✗ Branch 9 → 10 not taken.
✓ Branch 9 → 12 taken 2 times.
2 av_assert0(info->system_id_size == sizeof(test_system_id));
125
1/2
✗ Branch 12 → 13 not taken.
✓ Branch 12 → 15 taken 2 times.
2 av_assert0(info->key_ids);
126
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 2 times.
2 av_assert0(info->num_key_ids == 2);
127
1/2
✗ Branch 18 → 19 not taken.
✓ Branch 18 → 21 taken 2 times.
2 av_assert0(info->key_id_size == sizeof(test_key_id));
128
1/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 24 taken 2 times.
2 av_assert0(info->key_ids[0]);
129
1/2
✗ Branch 24 → 25 not taken.
✓ Branch 24 → 27 taken 2 times.
2 av_assert0(info->key_ids[1]);
130
1/2
✗ Branch 27 → 28 not taken.
✓ Branch 27 → 30 taken 2 times.
2 av_assert0(info->data);
131
1/2
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 33 taken 2 times.
2 av_assert0(info->data_size == sizeof(test_data));
132
1/2
✗ Branch 33 → 34 not taken.
✓ Branch 33 → 36 taken 2 times.
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/2
✗ Branch 4 → 5 not taken.
✓ Branch 4 → 7 taken 1 time.
1 av_assert0(side_data);
153
1/2
✗ Branch 7 → 8 not taken.
✓ Branch 7 → 10 taken 1 time.
1 av_assert0(side_data_size > 0);
154 1 copy = av_encryption_init_info_get_side_data(side_data, side_data_size);
155
1/2
✗ Branch 11 → 12 not taken.
✓ Branch 11 → 14 taken 1 time.
1 av_assert0(copy);
156
1/2
✗ Branch 15 → 16 not taken.
✓ Branch 15 → 18 taken 1 time.
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/2
✗ Branch 21 → 22 not taken.
✓ Branch 21 → 24 taken 1 time.
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/2
✗ Branch 26 → 27 not taken.
✓ Branch 26 → 29 taken 1 time.
1 av_assert0(side_data);
167 1 copy = av_encryption_init_info_get_side_data(side_data, side_data_size);
168
1/2
✗ Branch 30 → 31 not taken.
✓ Branch 30 → 33 taken 1 time.
1 av_assert0(copy);
169
1/2
✗ Branch 34 → 35 not taken.
✓ Branch 34 → 37 taken 1 time.
1 av_assert0(compare_encryption_init_info(info, copy) == 0);
170 1 av_encryption_init_info_free(copy);
171
172
1/2
✗ Branch 39 → 40 not taken.
✓ Branch 39 → 42 taken 1 time.
1 av_assert0(av_encryption_init_info_append_side_data(&appended, &appended_size, &allocated_size, info->next) == 0);
173
1/2
✗ Branch 42 → 43 not taken.
✓ Branch 42 → 45 taken 1 time.
1 av_assert0(appended_size == side_data_size);
174
1/2
✗ Branch 45 → 46 not taken.
✓ Branch 45 → 48 taken 1 time.
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