blob: 783bc805c4613c170eaca860421f935bcab8e400 [file] [log] [blame]
Benjamin Wright84583f62018-10-04 21:22:341/*
2 * Copyright 2018 The WebRTC 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
11#ifndef API_TEST_FAKE_FRAME_DECRYPTOR_H_
12#define API_TEST_FAKE_FRAME_DECRYPTOR_H_
13
Yves Gerey3e707812018-11-28 15:47:4914#include <stddef.h>
15#include <stdint.h>
Jonas Olssona4d87372019-07-05 17:08:3316
Benjamin Wright84583f62018-10-04 21:22:3417#include <vector>
18
Yves Gerey3e707812018-11-28 15:47:4919#include "api/array_view.h"
Steve Anton10542f22019-01-11 17:11:0020#include "api/crypto/frame_decryptor_interface.h"
21#include "api/media_types.h"
Benjamin Wright84583f62018-10-04 21:22:3422
23namespace webrtc {
24
25// The FakeFrameDecryptor is a TEST ONLY fake implementation of the
26// FrameDecryptorInterface. It is constructed with a simple single digit key and
27// a fixed postfix byte. This is just to validate that the core code works
28// as expected.
Tomas Gunnarssonb3517fe2022-03-18 18:32:3829class FakeFrameDecryptor : public FrameDecryptorInterface {
Benjamin Wright84583f62018-10-04 21:22:3430 public:
31 // Provide a key (0,255) and some postfix byte (0,255) this should match the
32 // byte you expect from the FakeFrameEncryptor.
Benjamin Wright150a9072018-10-26 22:43:0633 explicit FakeFrameDecryptor(uint8_t fake_key = 0xAA,
Benjamin Wright84583f62018-10-04 21:22:3434 uint8_t expected_postfix_byte = 255);
Benjamin Wright150a9072018-10-26 22:43:0635 // Fake decryption that just xors the payload with the 1 byte key and checks
36 // the postfix byte. This will always fail if fail_decryption_ is set to true.
Benjamin Wright2af5dcb2019-04-09 20:08:4137 Result Decrypt(cricket::MediaType media_type,
38 const std::vector<uint32_t>& csrcs,
39 rtc::ArrayView<const uint8_t> additional_data,
40 rtc::ArrayView<const uint8_t> encrypted_frame,
41 rtc::ArrayView<uint8_t> frame) override;
Benjamin Wright150a9072018-10-26 22:43:0642 // Always returns 1 less than the size of the encrypted frame.
Benjamin Wright84583f62018-10-04 21:22:3443 size_t GetMaxPlaintextByteSize(cricket::MediaType media_type,
44 size_t encrypted_frame_size) override;
Benjamin Wright150a9072018-10-26 22:43:0645 // Sets the fake key to use for encryption.
Benjamin Wright84583f62018-10-04 21:22:3446 void SetFakeKey(uint8_t fake_key);
Benjamin Wright150a9072018-10-26 22:43:0647 // Returns the fake key used for encryption.
Benjamin Wright84583f62018-10-04 21:22:3448 uint8_t GetFakeKey() const;
Benjamin Wright150a9072018-10-26 22:43:0649 // Set the Postfix byte that is expected in the encrypted payload.
Benjamin Wright84583f62018-10-04 21:22:3450 void SetExpectedPostfixByte(uint8_t expected_postfix_byte);
Benjamin Wright150a9072018-10-26 22:43:0651 // Returns the postfix byte that will be checked for in the encrypted payload.
Benjamin Wright84583f62018-10-04 21:22:3452 uint8_t GetExpectedPostfixByte() const;
Benjamin Wright150a9072018-10-26 22:43:0653 // If set to true will force all encryption to fail.
Benjamin Wright84583f62018-10-04 21:22:3454 void SetFailDecryption(bool fail_decryption);
Benjamin Wright150a9072018-10-26 22:43:0655 // Simple error codes for tests to validate against.
56 enum class FakeDecryptStatus : int {
57 OK = 0,
58 FORCED_FAILURE = 1,
59 INVALID_POSTFIX = 2
60 };
Benjamin Wright84583f62018-10-04 21:22:3461
62 private:
63 uint8_t fake_key_ = 0;
64 uint8_t expected_postfix_byte_ = 0;
65 bool fail_decryption_ = false;
66};
67
68} // namespace webrtc
69
70#endif // API_TEST_FAKE_FRAME_DECRYPTOR_H_