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_ENCRYPTOR_H_
12 #define API_TEST_FAKE_FRAME_ENCRYPTOR_H_
13 
14 #include <stddef.h>
15 #include <stdint.h>
16 
17 #include "api/array_view.h"
18 #include "api/crypto/frame_encryptor_interface.h"
19 #include "api/media_types.h"
20 #include "rtc_base/ref_counted_object.h"
21 
22 namespace webrtc {
23 
24 // The FakeFrameEncryptor is a TEST ONLY fake implementation of the
25 // FrameEncryptorInterface. It is constructed with a simple single digit key and
26 // a fixed postfix byte. This is just to validate that the core code works
27 // as expected.
28 class FakeFrameEncryptor
29     : public rtc::RefCountedObject<FrameEncryptorInterface> {
30  public:
31   // Provide a key (0,255) and some postfix byte (0,255).
32   explicit FakeFrameEncryptor(uint8_t fake_key = 0xAA,
33                               uint8_t postfix_byte = 255);
34   // Simply xors each payload with the provided fake key and adds the postfix
35   // bit to the end. This will always fail if fail_encryption_ is set to true.
36   int Encrypt(cricket::MediaType media_type,
37               uint32_t ssrc,
38               rtc::ArrayView<const uint8_t> additional_data,
39               rtc::ArrayView<const uint8_t> frame,
40               rtc::ArrayView<uint8_t> encrypted_frame,
41               size_t* bytes_written) override;
42   // Always returns 1 more than the size of the frame.
43   size_t GetMaxCiphertextByteSize(cricket::MediaType media_type,
44                                   size_t frame_size) override;
45   // Sets the fake key to use during encryption.
46   void SetFakeKey(uint8_t fake_key);
47   // Returns the fake key used during encryption.
48   uint8_t GetFakeKey() const;
49   // Set the postfix byte to use.
50   void SetPostfixByte(uint8_t expected_postfix_byte);
51   // Return a postfix byte added to each outgoing payload.
52   uint8_t GetPostfixByte() const;
53   // Force all encryptions to fail.
54   void SetFailEncryption(bool fail_encryption);
55 
56   enum class FakeEncryptionStatus : int {
57     OK = 0,
58     FORCED_FAILURE = 1,
59   };
60 
61  private:
62   uint8_t fake_key_ = 0;
63   uint8_t postfix_byte_ = 0;
64   bool fail_encryption_ = false;
65 };
66 
67 }  // namespace webrtc
68 
69 #endif  // API_TEST_FAKE_FRAME_ENCRYPTOR_H_
70