fake_frame_decryptor.cc (2160B)
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 #include "api/test/fake_frame_decryptor.h" 12 13 #include <cstddef> 14 #include <cstdint> 15 #include <vector> 16 17 #include "api/array_view.h" 18 #include "api/media_types.h" 19 #include "rtc_base/checks.h" 20 21 namespace webrtc { 22 23 FakeFrameDecryptor::FakeFrameDecryptor(uint8_t fake_key, 24 uint8_t expected_postfix_byte) 25 : fake_key_(fake_key), expected_postfix_byte_(expected_postfix_byte) {} 26 27 FakeFrameDecryptor::Result FakeFrameDecryptor::Decrypt( 28 MediaType /* media_type */, 29 const std::vector<uint32_t>& /* csrcs */, 30 ArrayView<const uint8_t> /* additional_data */, 31 ArrayView<const uint8_t> encrypted_frame, 32 ArrayView<uint8_t> frame) { 33 if (fail_decryption_) { 34 return Result(Status::kFailedToDecrypt, 0); 35 } 36 37 RTC_CHECK_EQ(frame.size() + 1, encrypted_frame.size()); 38 for (size_t i = 0; i < frame.size(); i++) { 39 frame[i] = encrypted_frame[i] ^ fake_key_; 40 } 41 42 if (encrypted_frame[frame.size()] != expected_postfix_byte_) { 43 return Result(Status::kFailedToDecrypt, 0); 44 } 45 46 return Result(Status::kOk, frame.size()); 47 } 48 49 size_t FakeFrameDecryptor::GetMaxPlaintextByteSize( 50 MediaType /* media_type */, 51 size_t encrypted_frame_size) { 52 return encrypted_frame_size - 1; 53 } 54 55 void FakeFrameDecryptor::SetFakeKey(uint8_t fake_key) { 56 fake_key_ = fake_key; 57 } 58 59 uint8_t FakeFrameDecryptor::GetFakeKey() const { 60 return fake_key_; 61 } 62 63 void FakeFrameDecryptor::SetExpectedPostfixByte(uint8_t expected_postfix_byte) { 64 expected_postfix_byte_ = expected_postfix_byte; 65 } 66 67 uint8_t FakeFrameDecryptor::GetExpectedPostfixByte() const { 68 return expected_postfix_byte_; 69 } 70 71 void FakeFrameDecryptor::SetFailDecryption(bool fail_decryption) { 72 fail_decryption_ = fail_decryption; 73 } 74 75 } // namespace webrtc