OLD | NEW |
1 /* | 1 /* |
2 * Copyright (c) 2013 The WebM project authors. All Rights Reserved. | 2 * Copyright (c) 2013 The WebM project authors. All Rights Reserved. |
3 * | 3 * |
4 * Use of this source code is governed by a BSD-style license | 4 * Use of this source code is governed by a BSD-style license |
5 * that can be found in the LICENSE file in the root of the source | 5 * that can be found in the LICENSE file in the root of the source |
6 * tree. An additional intellectual property rights grant can be found | 6 * tree. An additional intellectual property rights grant can be found |
7 * in the file PATENTS. All contributing project authors may | 7 * in the file PATENTS. All contributing project authors may |
8 * be found in the AUTHORS file in the root of the source tree. | 8 * be found in the AUTHORS file in the root of the source tree. |
9 */ | 9 */ |
10 | 10 |
11 #include <cstdio> | 11 #include <cstdio> |
12 #include <cstdlib> | 12 #include <cstdlib> |
13 #include <string> | 13 #include <string> |
| 14 #include <vector> |
14 #include "third_party/googletest/src/include/gtest/gtest.h" | 15 #include "third_party/googletest/src/include/gtest/gtest.h" |
15 #include "test/decode_test_driver.h" | 16 #include "test/codec_factory.h" |
16 #include "test/ivf_video_source.h" | 17 #include "test/ivf_video_source.h" |
17 | 18 |
18 #if CONFIG_DECRYPT | |
19 | |
20 namespace { | 19 namespace { |
21 | 20 // In a real use the 'decrypt_state' parameter will be a pointer to a struct |
22 const uint8_t decrypt_key[32] = { | 21 // with whatever internal state the decryptor uses. For testing we'll just |
23 255, 0, 0, 0, 0, 0, 0, 0, | 22 // xor with a constant key, and decrypt_state will point to the start of |
24 0, 0, 0, 0, 0, 0, 0, 0, | 23 // the original buffer. |
25 0, 0, 0, 0, 0, 0, 0, 0, | 24 const uint8_t test_key[16] = { |
26 0, 0, 0, 0, 0, 0, 0, 0, | 25 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78, |
| 26 0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0 |
27 }; | 27 }; |
28 | 28 |
29 } // namespace | 29 void encrypt_buffer(const uint8_t *src, uint8_t *dst, int size, int offset = 0)
{ |
| 30 for (int i = 0; i < size; ++i) { |
| 31 dst[i] = src[i] ^ test_key[(offset + i) & 15]; |
| 32 } |
| 33 } |
| 34 |
| 35 void test_decrypt_cb(void *decrypt_state, const uint8_t *input, |
| 36 uint8_t *output, int count) { |
| 37 encrypt_buffer(input, output, count, input - (uint8_t *)decrypt_state); |
| 38 } |
| 39 |
| 40 } // namespace |
30 | 41 |
31 namespace libvpx_test { | 42 namespace libvpx_test { |
32 | 43 |
33 TEST(TestDecrypt, NullKey) { | |
34 vpx_codec_dec_cfg_t cfg = {0}; | |
35 vpx_codec_ctx_t decoder = {0}; | |
36 vpx_codec_err_t res = vpx_codec_dec_init(&decoder, &vpx_codec_vp8_dx_algo, | |
37 &cfg, 0); | |
38 ASSERT_EQ(VPX_CODEC_OK, res); | |
39 | |
40 res = vpx_codec_control(&decoder, VP8_SET_DECRYPT_KEY, NULL); | |
41 ASSERT_EQ(VPX_CODEC_INVALID_PARAM, res); | |
42 } | |
43 | |
44 TEST(TestDecrypt, DecryptWorks) { | 44 TEST(TestDecrypt, DecryptWorks) { |
45 libvpx_test::IVFVideoSource video("vp80-00-comprehensive-001.ivf"); | 45 libvpx_test::IVFVideoSource video("vp80-00-comprehensive-001.ivf"); |
46 video.Init(); | 46 video.Init(); |
47 | 47 |
48 vpx_codec_dec_cfg_t dec_cfg = {0}; | 48 vpx_codec_dec_cfg_t dec_cfg = {0}; |
49 Decoder decoder(dec_cfg, 0); | 49 VP8Decoder decoder(dec_cfg, 0); |
50 | 50 |
51 // Zero decrypt key (by default) | |
52 video.Begin(); | 51 video.Begin(); |
| 52 |
| 53 // no decryption |
53 vpx_codec_err_t res = decoder.DecodeFrame(video.cxdata(), video.frame_size()); | 54 vpx_codec_err_t res = decoder.DecodeFrame(video.cxdata(), video.frame_size()); |
54 ASSERT_EQ(VPX_CODEC_OK, res) << decoder.DecodeError(); | 55 ASSERT_EQ(VPX_CODEC_OK, res) << decoder.DecodeError(); |
55 | 56 |
56 // Non-zero decrypt key | 57 // decrypt frame |
57 video.Next(); | 58 video.Next(); |
58 decoder.Control(VP8_SET_DECRYPT_KEY, decrypt_key); | 59 |
| 60 #if CONFIG_DECRYPT |
| 61 std::vector<uint8_t> encrypted(video.frame_size()); |
| 62 encrypt_buffer(video.cxdata(), &encrypted[0], video.frame_size()); |
| 63 vp8_decrypt_init di = { test_decrypt_cb, &encrypted[0] }; |
| 64 decoder.Control(VP8D_SET_DECRYPTOR, &di); |
| 65 #endif // CONFIG_DECRYPT |
| 66 |
59 res = decoder.DecodeFrame(video.cxdata(), video.frame_size()); | 67 res = decoder.DecodeFrame(video.cxdata(), video.frame_size()); |
60 ASSERT_NE(VPX_CODEC_OK, res) << decoder.DecodeError(); | 68 ASSERT_EQ(VPX_CODEC_OK, res) << decoder.DecodeError(); |
61 } | 69 } |
62 | 70 |
63 } // namespace libvpx_test | 71 } // namespace libvpx_test |
64 | |
65 #endif // CONFIG_DECRYPT | |
OLD | NEW |