FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/tests/encryption_info.c
Date: 2024-05-03 05:27:13
Exec Total Coverage
Lines: 94 101 93.1%
Functions: 6 6 100.0%
Branches: 61 118 51.7%

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 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 2 times.
✗ Branch 7 not taken.
2 if (!a || !b || a->scheme != b->scheme || a->crypt_byte_block != b->crypt_byte_block ||
37
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
2 a->skip_byte_block != b->skip_byte_block || a->key_id_size != b->key_id_size ||
38
2/4
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
2 a->iv_size != b->iv_size || a->subsample_count != b->subsample_count)
39 return 1;
40
41
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 if (memcmp(a->key_id, b->key_id, a->key_id_size) != 0 ||
42
1/2
✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
2 memcmp(a->iv, b->iv, a->iv_size) != 0 ||
43
1/2
✗ Branch 0 not taken.
✓ Branch 1 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 3 static int compare_encryption_init_info(const AVEncryptionInitInfo *a, const AVEncryptionInitInfo *b) {
50
3/6
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
3 if (!a || !b || a->system_id_size != b->system_id_size ||
51
2/4
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
3 a->num_key_ids != b->num_key_ids || a->key_id_size != b->key_id_size ||
52
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 a->data_size != b->data_size)
53 return 1;
54
55
1/2
✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
3 if (memcmp(a->system_id, b->system_id, a->system_id_size) != 0 ||
56
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
3 memcmp(a->data, b->data, a->data_size) != 0)
57 return 1;
58
59
2/2
✓ Branch 0 taken 6 times.
✓ Branch 1 taken 3 times.
9 for (uint32_t i = 0; i < a->num_key_ids; i++) {
60
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 6 times.
6 if (memcmp(a->key_ids[i], b->key_ids[i], a->key_id_size) != 0)
61 return 1;
62 }
63
64
3/4
✓ Branch 0 taken 2 times.
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 2 times.
3 if (a->next || b->next) {
65
2/4
✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
1 if (!a->next || !b->next)
66 return 1;
67
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (compare_encryption_init_info(a->next, b->next) != 0)
68 return 1;
69 }
70
71 3 return 0;
72 }
73
74 1 static void run_encryption_info_test(void)
75 {
76 AVEncryptionInfo *info, *copy;
77 uint8_t *side_data;
78 size_t side_data_size;
79
80 1 info = av_encryption_info_alloc(test_subsample_count, sizeof(test_key_id), sizeof(test_iv));
81
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(info);
82
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(info->key_id);
83
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(info->key_id_size == sizeof(test_key_id));
84
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(info->iv);
85
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(info->iv_size == sizeof(test_iv));
86
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(info->subsamples);
87
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(info->subsample_count == test_subsample_count);
88
89 1 info->scheme = 1234;
90 1 info->crypt_byte_block = 333;
91 1 info->skip_byte_block = 444;
92 1 memcpy(info->key_id, test_key_id, sizeof(test_key_id));
93 1 memcpy(info->iv, test_iv, sizeof(test_iv));
94 1 memcpy(info->subsamples, test_subsamples, sizeof(test_subsamples));
95
96 1 copy = av_encryption_info_clone(info);
97
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(copy);
98
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(copy != info);
99
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 av_assert0(compare_encryption_info(info, copy) == 0);
100 1 av_encryption_info_free(copy);
101
102 1 side_data = av_encryption_info_add_side_data(info, &side_data_size);
103
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(side_data);
104
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(side_data_size > 0);
105
106 1 copy = av_encryption_info_get_side_data(side_data, side_data_size);
107
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(copy);
108
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(copy != info);
109
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 av_assert0(compare_encryption_info(info, copy) == 0);
110 1 av_encryption_info_free(copy);
111 1 av_free(side_data);
112
113 1 av_encryption_info_free(info);
114 1 }
115
116 2 static AVEncryptionInitInfo *create_init_info(void)
117 {
118 AVEncryptionInitInfo *info;
119
120 2 info = av_encryption_init_info_alloc(sizeof(test_system_id), 2, sizeof(test_key_id), sizeof(test_data));
121
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(info);
122
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(info->system_id);
123
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(info->system_id_size == sizeof(test_system_id));
124
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(info->key_ids);
125
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(info->num_key_ids == 2);
126
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(info->key_id_size == sizeof(test_key_id));
127
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(info->key_ids[0]);
128
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(info->key_ids[1]);
129
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(info->data);
130
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(info->data_size == sizeof(test_data));
131
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
2 av_assert0(!info->next);
132
133 2 memcpy(info->system_id, test_system_id, sizeof(test_system_id));
134 2 memcpy(info->key_ids[0], test_key_id, sizeof(test_key_id));
135 2 memcpy(info->key_ids[1], test_key_id_2, sizeof(test_key_id_2));
136 2 memcpy(info->data, test_data, sizeof(test_data));
137
138 2 return info;
139 }
140
141 1 static void run_encryption_init_info_test(void)
142 {
143 AVEncryptionInitInfo *info, *copy;
144 uint8_t *side_data;
145 size_t side_data_size;
146
147 1 info = create_init_info();
148
149 1 side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
150
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(side_data);
151
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(side_data_size > 0);
152 1 copy = av_encryption_init_info_get_side_data(side_data, side_data_size);
153
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(copy);
154
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 av_assert0(compare_encryption_init_info(info, copy) == 0);
155 1 av_encryption_init_info_free(copy);
156 1 av_free(side_data);
157
158 // Make the first init info different from the second to test the correct order.
159 1 memset(info->system_id, 0, info->system_id_size);
160 1 info->next = create_init_info();
161 1 side_data = av_encryption_init_info_add_side_data(info, &side_data_size);
162
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(side_data);
163 1 copy = av_encryption_init_info_get_side_data(side_data, side_data_size);
164
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
1 av_assert0(copy);
165
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 av_assert0(compare_encryption_init_info(info, copy) == 0);
166 1 av_encryption_init_info_free(copy);
167 1 av_free(side_data);
168
169 1 av_encryption_init_info_free(info);
170 1 }
171
172 1 int main(int argc, char **argv)
173 {
174 1 run_encryption_info_test();
175 1 run_encryption_init_info_test();
176 1 return 0;
177 }
178