blob: e2bcf687b50e007c65a9382ee9c3a282d1248905 [file] [log] [blame]
Benjamin Wright84583f62018-10-04 14:22:34 -07001/*
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 16:47:49 +010014#include <stddef.h>
15#include <stdint.h>
Benjamin Wright84583f62018-10-04 14:22:34 -070016#include <vector>
17
Yves Gerey3e707812018-11-28 16:47:49 +010018#include "api/array_view.h"
Benjamin Wright84583f62018-10-04 14:22:34 -070019#include "api/crypto/framedecryptorinterface.h"
Yves Gerey3e707812018-11-28 16:47:49 +010020#include "api/mediatypes.h"
Benjamin Wright84583f62018-10-04 14:22:34 -070021#include "rtc_base/refcountedobject.h"
22
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.
Benjamin Wright150a9072018-10-26 15:43:06 -070029class FakeFrameDecryptor final
Benjamin Wright84583f62018-10-04 14:22:34 -070030 : public rtc::RefCountedObject<FrameDecryptorInterface> {
31 public:
32 // Provide a key (0,255) and some postfix byte (0,255) this should match the
33 // byte you expect from the FakeFrameEncryptor.
Benjamin Wright150a9072018-10-26 15:43:06 -070034 explicit FakeFrameDecryptor(uint8_t fake_key = 0xAA,
Benjamin Wright84583f62018-10-04 14:22:34 -070035 uint8_t expected_postfix_byte = 255);
Benjamin Wright150a9072018-10-26 15:43:06 -070036 // Fake decryption that just xors the payload with the 1 byte key and checks
37 // the postfix byte. This will always fail if fail_decryption_ is set to true.
Benjamin Wright84583f62018-10-04 14:22:34 -070038 int Decrypt(cricket::MediaType media_type,
39 const std::vector<uint32_t>& csrcs,
40 rtc::ArrayView<const uint8_t> additional_data,
41 rtc::ArrayView<const uint8_t> encrypted_frame,
42 rtc::ArrayView<uint8_t> frame,
43 size_t* bytes_written) override;
Benjamin Wright150a9072018-10-26 15:43:06 -070044 // Always returns 1 less than the size of the encrypted frame.
Benjamin Wright84583f62018-10-04 14:22:34 -070045 size_t GetMaxPlaintextByteSize(cricket::MediaType media_type,
46 size_t encrypted_frame_size) override;
Benjamin Wright150a9072018-10-26 15:43:06 -070047 // Sets the fake key to use for encryption.
Benjamin Wright84583f62018-10-04 14:22:34 -070048 void SetFakeKey(uint8_t fake_key);
Benjamin Wright150a9072018-10-26 15:43:06 -070049 // Returns the fake key used for encryption.
Benjamin Wright84583f62018-10-04 14:22:34 -070050 uint8_t GetFakeKey() const;
Benjamin Wright150a9072018-10-26 15:43:06 -070051 // Set the Postfix byte that is expected in the encrypted payload.
Benjamin Wright84583f62018-10-04 14:22:34 -070052 void SetExpectedPostfixByte(uint8_t expected_postfix_byte);
Benjamin Wright150a9072018-10-26 15:43:06 -070053 // Returns the postfix byte that will be checked for in the encrypted payload.
Benjamin Wright84583f62018-10-04 14:22:34 -070054 uint8_t GetExpectedPostfixByte() const;
Benjamin Wright150a9072018-10-26 15:43:06 -070055 // If set to true will force all encryption to fail.
Benjamin Wright84583f62018-10-04 14:22:34 -070056 void SetFailDecryption(bool fail_decryption);
Benjamin Wright150a9072018-10-26 15:43:06 -070057 // Simple error codes for tests to validate against.
58 enum class FakeDecryptStatus : int {
59 OK = 0,
60 FORCED_FAILURE = 1,
61 INVALID_POSTFIX = 2
62 };
Benjamin Wright84583f62018-10-04 14:22:34 -070063
64 private:
65 uint8_t fake_key_ = 0;
66 uint8_t expected_postfix_byte_ = 0;
67 bool fail_decryption_ = false;
68};
69
70} // namespace webrtc
71
72#endif // API_TEST_FAKE_FRAME_DECRYPTOR_H_