1 /* 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 14 #include <stddef.h> 15 #include <stdint.h> 16 17 #include <vector> 18 19 #include "api/array_view.h" 20 #include "api/crypto/frame_decryptor_interface.h" 21 #include "api/media_types.h" 22 #include "rtc_base/ref_counted_object.h" 23 24 namespace webrtc { 25 26 // The FakeFrameDecryptor is a TEST ONLY fake implementation of the 27 // FrameDecryptorInterface. It is constructed with a simple single digit key and 28 // a fixed postfix byte. This is just to validate that the core code works 29 // as expected. 30 class FakeFrameDecryptor final 31 : public rtc::RefCountedObject<FrameDecryptorInterface> { 32 public: 33 // Provide a key (0,255) and some postfix byte (0,255) this should match the 34 // byte you expect from the FakeFrameEncryptor. 35 explicit FakeFrameDecryptor(uint8_t fake_key = 0xAA, 36 uint8_t expected_postfix_byte = 255); 37 // Fake decryption that just xors the payload with the 1 byte key and checks 38 // the postfix byte. This will always fail if fail_decryption_ is set to true. 39 Result Decrypt(cricket::MediaType media_type, 40 const std::vector<uint32_t>& csrcs, 41 rtc::ArrayView<const uint8_t> additional_data, 42 rtc::ArrayView<const uint8_t> encrypted_frame, 43 rtc::ArrayView<uint8_t> frame) override; 44 // Always returns 1 less than the size of the encrypted frame. 45 size_t GetMaxPlaintextByteSize(cricket::MediaType media_type, 46 size_t encrypted_frame_size) override; 47 // Sets the fake key to use for encryption. 48 void SetFakeKey(uint8_t fake_key); 49 // Returns the fake key used for encryption. 50 uint8_t GetFakeKey() const; 51 // Set the Postfix byte that is expected in the encrypted payload. 52 void SetExpectedPostfixByte(uint8_t expected_postfix_byte); 53 // Returns the postfix byte that will be checked for in the encrypted payload. 54 uint8_t GetExpectedPostfixByte() const; 55 // If set to true will force all encryption to fail. 56 void SetFailDecryption(bool fail_decryption); 57 // 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 }; 63 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_ 73