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_encryptor.h"
12 
13 #include "rtc_base/checks.h"
14 
15 namespace webrtc {
FakeFrameEncryptor(uint8_t fake_key,uint8_t postfix_byte)16 FakeFrameEncryptor::FakeFrameEncryptor(uint8_t fake_key, uint8_t postfix_byte)
17     : fake_key_(fake_key), postfix_byte_(postfix_byte) {}
18 
19 // FrameEncryptorInterface implementation
Encrypt(cricket::MediaType media_type,uint32_t ssrc,rtc::ArrayView<const uint8_t> additional_data,rtc::ArrayView<const uint8_t> frame,rtc::ArrayView<uint8_t> encrypted_frame,size_t * bytes_written)20 int FakeFrameEncryptor::Encrypt(cricket::MediaType media_type,
21                                 uint32_t ssrc,
22                                 rtc::ArrayView<const uint8_t> additional_data,
23                                 rtc::ArrayView<const uint8_t> frame,
24                                 rtc::ArrayView<uint8_t> encrypted_frame,
25                                 size_t* bytes_written) {
26   if (fail_encryption_) {
27     return static_cast<int>(FakeEncryptionStatus::FORCED_FAILURE);
28   }
29 
30   RTC_CHECK_EQ(frame.size() + 1, encrypted_frame.size());
31   for (size_t i = 0; i < frame.size(); i++) {
32     encrypted_frame[i] = frame[i] ^ fake_key_;
33   }
34 
35   encrypted_frame[frame.size()] = postfix_byte_;
36   *bytes_written = encrypted_frame.size();
37   return static_cast<int>(FakeEncryptionStatus::OK);
38 }
39 
GetMaxCiphertextByteSize(cricket::MediaType media_type,size_t frame_size)40 size_t FakeFrameEncryptor::GetMaxCiphertextByteSize(
41     cricket::MediaType media_type,
42     size_t frame_size) {
43   return frame_size + 1;
44 }
45 
SetFakeKey(uint8_t fake_key)46 void FakeFrameEncryptor::SetFakeKey(uint8_t fake_key) {
47   fake_key_ = fake_key;
48 }
49 
GetFakeKey() const50 uint8_t FakeFrameEncryptor::GetFakeKey() const {
51   return fake_key_;
52 }
53 
SetPostfixByte(uint8_t postfix_byte)54 void FakeFrameEncryptor::SetPostfixByte(uint8_t postfix_byte) {
55   postfix_byte_ = postfix_byte;
56 }
57 
GetPostfixByte() const58 uint8_t FakeFrameEncryptor::GetPostfixByte() const {
59   return postfix_byte_;
60 }
61 
SetFailEncryption(bool fail_encryption)62 void FakeFrameEncryptor::SetFailEncryption(bool fail_encryption) {
63   fail_encryption_ = fail_encryption;
64 }
65 
66 }  // namespace webrtc
67