vp8_decrypt_test.cc 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. /*
  2. * Copyright (c) 2013 The WebM project authors. All Rights Reserved.
  3. *
  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
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #include <cstdio>
  11. #include <cstdlib>
  12. #include <string>
  13. #include <vector>
  14. #include "third_party/googletest/src/include/gtest/gtest.h"
  15. #include "test/codec_factory.h"
  16. #include "test/ivf_video_source.h"
  17. namespace {
  18. // In a real use the 'decrypt_state' parameter will be a pointer to a struct
  19. // with whatever internal state the decryptor uses. For testing we'll just
  20. // xor with a constant key, and decrypt_state will point to the start of
  21. // the original buffer.
  22. const uint8_t test_key[16] = { 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
  23. 0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0 };
  24. void encrypt_buffer(const uint8_t *src, uint8_t *dst, size_t size,
  25. ptrdiff_t offset) {
  26. for (size_t i = 0; i < size; ++i) {
  27. dst[i] = src[i] ^ test_key[(offset + i) & 15];
  28. }
  29. }
  30. void test_decrypt_cb(void *decrypt_state, const uint8_t *input, uint8_t *output,
  31. int count) {
  32. encrypt_buffer(input, output, count,
  33. input - reinterpret_cast<uint8_t *>(decrypt_state));
  34. }
  35. } // namespace
  36. namespace libvpx_test {
  37. TEST(TestDecrypt, DecryptWorksVp8) {
  38. libvpx_test::IVFVideoSource video("vp80-00-comprehensive-001.ivf");
  39. video.Init();
  40. vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
  41. VP8Decoder decoder(dec_cfg, 0);
  42. video.Begin();
  43. // no decryption
  44. vpx_codec_err_t res = decoder.DecodeFrame(video.cxdata(), video.frame_size());
  45. ASSERT_EQ(VPX_CODEC_OK, res) << decoder.DecodeError();
  46. // decrypt frame
  47. video.Next();
  48. std::vector<uint8_t> encrypted(video.frame_size());
  49. encrypt_buffer(video.cxdata(), &encrypted[0], video.frame_size(), 0);
  50. vpx_decrypt_init di = { test_decrypt_cb, &encrypted[0] };
  51. decoder.Control(VPXD_SET_DECRYPTOR, &di);
  52. res = decoder.DecodeFrame(&encrypted[0], encrypted.size());
  53. ASSERT_EQ(VPX_CODEC_OK, res) << decoder.DecodeError();
  54. }
  55. } // namespace libvpx_test