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