fake_frame_decryptor.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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_DECRYPTOR_H_
  11. #define API_TEST_FAKE_FRAME_DECRYPTOR_H_
  12. #include <stddef.h>
  13. #include <stdint.h>
  14. #include <vector>
  15. #include "api/array_view.h"
  16. #include "api/crypto/frame_decryptor_interface.h"
  17. #include "api/media_types.h"
  18. #include "rtc_base/ref_counted_object.h"
  19. namespace webrtc {
  20. // The FakeFrameDecryptor is a TEST ONLY fake implementation of the
  21. // FrameDecryptorInterface. It is constructed with a simple single digit key and
  22. // a fixed postfix byte. This is just to validate that the core code works
  23. // as expected.
  24. class FakeFrameDecryptor final
  25. : public rtc::RefCountedObject<FrameDecryptorInterface> {
  26. public:
  27. // Provide a key (0,255) and some postfix byte (0,255) this should match the
  28. // byte you expect from the FakeFrameEncryptor.
  29. explicit FakeFrameDecryptor(uint8_t fake_key = 0xAA,
  30. uint8_t expected_postfix_byte = 255);
  31. // Fake decryption that just xors the payload with the 1 byte key and checks
  32. // the postfix byte. This will always fail if fail_decryption_ is set to true.
  33. Result Decrypt(cricket::MediaType media_type,
  34. const std::vector<uint32_t>& csrcs,
  35. rtc::ArrayView<const uint8_t> additional_data,
  36. rtc::ArrayView<const uint8_t> encrypted_frame,
  37. rtc::ArrayView<uint8_t> frame) override;
  38. // Always returns 1 less than the size of the encrypted frame.
  39. size_t GetMaxPlaintextByteSize(cricket::MediaType media_type,
  40. size_t encrypted_frame_size) override;
  41. // Sets the fake key to use for encryption.
  42. void SetFakeKey(uint8_t fake_key);
  43. // Returns the fake key used for encryption.
  44. uint8_t GetFakeKey() const;
  45. // Set the Postfix byte that is expected in the encrypted payload.
  46. void SetExpectedPostfixByte(uint8_t expected_postfix_byte);
  47. // Returns the postfix byte that will be checked for in the encrypted payload.
  48. uint8_t GetExpectedPostfixByte() const;
  49. // If set to true will force all encryption to fail.
  50. void SetFailDecryption(bool fail_decryption);
  51. // Simple error codes for tests to validate against.
  52. enum class FakeDecryptStatus : int {
  53. OK = 0,
  54. FORCED_FAILURE = 1,
  55. INVALID_POSTFIX = 2
  56. };
  57. private:
  58. uint8_t fake_key_ = 0;
  59. uint8_t expected_postfix_byte_ = 0;
  60. bool fail_decryption_ = false;
  61. };
  62. } // namespace webrtc
  63. #endif // API_TEST_FAKE_FRAME_DECRYPTOR_H_