fake_frame_encryptor.h 2.5 KB

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