1ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang/*
2ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  Copyright (c) 2013 The WebM project authors. All Rights Reserved.
3ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *
4ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  Use of this source code is governed by a BSD-style license
5ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  that can be found in the LICENSE file in the root of the source
6ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  tree. An additional intellectual property rights grant can be found
7ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  in the file PATENTS.  All contributing project authors may
8ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang *  be found in the AUTHORS file in the root of the source tree.
9ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang */
10ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
11ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <cstdio>
12ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <cstdlib>
13ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <string>
14ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include <vector>
15ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "third_party/googletest/src/include/gtest/gtest.h"
16ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "test/codec_factory.h"
17ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang#include "test/ivf_video_source.h"
18ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
19ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangnamespace {
20ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// In a real use the 'decrypt_state' parameter will be a pointer to a struct
21ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// with whatever internal state the decryptor uses. For testing we'll just
22ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// xor with a constant key, and decrypt_state will point to the start of
23ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang// the original buffer.
247bc9febe8749e98a3812a0dc4380ceae75c29450Johannconst uint8_t test_key[16] = { 0x01, 0x12, 0x23, 0x34, 0x45, 0x56, 0x67, 0x78,
257bc9febe8749e98a3812a0dc4380ceae75c29450Johann                               0x89, 0x9a, 0xab, 0xbc, 0xcd, 0xde, 0xef, 0xf0 };
26ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
272ec72e65689c948e92b826ae1e867bf369e72f13Vignesh Venkatasubramanianvoid encrypt_buffer(const uint8_t *src, uint8_t *dst, size_t size,
282ec72e65689c948e92b826ae1e867bf369e72f13Vignesh Venkatasubramanian                    ptrdiff_t offset) {
292ec72e65689c948e92b826ae1e867bf369e72f13Vignesh Venkatasubramanian  for (size_t i = 0; i < size; ++i) {
30ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang    dst[i] = src[i] ^ test_key[(offset + i) & 15];
31ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  }
32ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
33ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
347bc9febe8749e98a3812a0dc4380ceae75c29450Johannvoid test_decrypt_cb(void *decrypt_state, const uint8_t *input, uint8_t *output,
357bc9febe8749e98a3812a0dc4380ceae75c29450Johann                     int count) {
361184aebb761cbeac9124c37189a80a1a58f04b6bhkuang  encrypt_buffer(input, output, count,
371184aebb761cbeac9124c37189a80a1a58f04b6bhkuang                 input - reinterpret_cast<uint8_t *>(decrypt_state));
38ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
39ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
401184aebb761cbeac9124c37189a80a1a58f04b6bhkuang}  // namespace
41ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
42ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuangnamespace libvpx_test {
43ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
44ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh VenkatasubramanianTEST(TestDecrypt, DecryptWorksVp8) {
45ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  libvpx_test::IVFVideoSource video("vp80-00-comprehensive-001.ivf");
46ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  video.Init();
47ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
48da49e34c1fb5e99681f4ad99c21d9cfd83eddb96Vignesh Venkatasubramanian  vpx_codec_dec_cfg_t dec_cfg = vpx_codec_dec_cfg_t();
49ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  VP8Decoder decoder(dec_cfg, 0);
50ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
51ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  video.Begin();
52ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
53ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  // no decryption
54ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  vpx_codec_err_t res = decoder.DecodeFrame(video.cxdata(), video.frame_size());
55ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  ASSERT_EQ(VPX_CODEC_OK, res) << decoder.DecodeError();
56ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
57ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  // decrypt frame
58ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  video.Next();
59ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
60ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  std::vector<uint8_t> encrypted(video.frame_size());
612ec72e65689c948e92b826ae1e867bf369e72f13Vignesh Venkatasubramanian  encrypt_buffer(video.cxdata(), &encrypted[0], video.frame_size(), 0);
62ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  vpx_decrypt_init di = { test_decrypt_cb, &encrypted[0] };
63ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  decoder.Control(VPXD_SET_DECRYPTOR, &di);
64ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
65ba6c59e9d7d7013b3906b6f4230b663422681848Vignesh Venkatasubramanian  res = decoder.DecodeFrame(&encrypted[0], encrypted.size());
66ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang  ASSERT_EQ(VPX_CODEC_OK, res) << decoder.DecodeError();
67ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}
68ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang
69ba164dffc5a6795bce97fae02b51ccf3330e15e4hkuang}  // namespace libvpx_test
70