tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

crypto_options_unittest.cc (3961B)


      1 /*
      2 *  Copyright 2025 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/crypto/crypto_options.h"
     12 
     13 #include <cstdint>
     14 #include <set>
     15 #include <vector>
     16 
     17 #include "api/field_trials.h"
     18 #include "rtc_base/openssl_stream_adapter.h"  // IWYU pragma: keep
     19 #include "test/create_test_field_trials.h"
     20 #include "test/gtest.h"
     21 
     22 namespace webrtc {
     23 namespace {
     24 
     25 TEST(EphemeralKeyExchangeCipherGroupsTest, GetSupported) {
     26  std::set<uint16_t> expected = {
     27 #ifdef SSL_GROUP_SECP224R1
     28      SSL_GROUP_SECP224R1,
     29 #endif
     30 #ifdef SSL_GROUP_SECP256R1
     31      SSL_GROUP_SECP256R1,
     32 #endif
     33 #ifdef SSL_GROUP_SECP384R1
     34      SSL_GROUP_SECP384R1,
     35 #endif
     36 #ifdef SSL_GROUP_SECP521R1
     37      SSL_GROUP_SECP521R1,
     38 #endif
     39 #ifdef SSL_GROUP_X25519
     40      SSL_GROUP_X25519,
     41 #endif
     42 #ifdef SSL_GROUP_X25519_MLKEM768
     43      SSL_GROUP_X25519_MLKEM768,
     44 #endif
     45  };
     46  auto supported =
     47      webrtc::CryptoOptions::EphemeralKeyExchangeCipherGroups::GetSupported();
     48  for (auto group : expected) {
     49    EXPECT_TRUE(supported.contains(group));
     50  }
     51 }
     52 
     53 TEST(EphemeralKeyExchangeCipherGroupsTest, GetEnabled) {
     54  std::vector<uint16_t> expected = {
     55 #ifdef SSL_GROUP_X25519
     56      SSL_GROUP_X25519,
     57 #endif
     58 #ifdef SSL_GROUP_SECP256R1
     59      SSL_GROUP_SECP256R1,
     60 #endif
     61 #ifdef SSL_GROUP_SECP384R1
     62      SSL_GROUP_SECP384R1,
     63 #endif
     64  };
     65  webrtc::CryptoOptions::EphemeralKeyExchangeCipherGroups groups;
     66  EXPECT_EQ(groups.GetEnabled(), expected);
     67 }
     68 
     69 TEST(EphemeralKeyExchangeCipherGroupsTest, SetEnabled) {
     70  std::vector<uint16_t> expected = {
     71      webrtc::CryptoOptions::EphemeralKeyExchangeCipherGroups::kX25519,
     72  };
     73  webrtc::CryptoOptions::EphemeralKeyExchangeCipherGroups groups;
     74  groups.SetEnabled(expected);
     75  EXPECT_EQ(groups.GetEnabled(), expected);
     76 }
     77 
     78 TEST(EphemeralKeyExchangeCipherGroupsTest, AddFirst) {
     79  std::vector<uint16_t> initial = {
     80 #ifdef SSL_GROUP_X25519
     81      SSL_GROUP_X25519,
     82 #endif
     83 #ifdef SSL_GROUP_SECP256R1
     84      SSL_GROUP_SECP256R1,
     85 #endif
     86 #ifdef SSL_GROUP_SECP384R1
     87      SSL_GROUP_SECP384R1,
     88 #endif
     89  };
     90  webrtc::CryptoOptions::EphemeralKeyExchangeCipherGroups groups;
     91  EXPECT_EQ(groups.GetEnabled(), initial);
     92  groups.AddFirst(webrtc::CryptoOptions::EphemeralKeyExchangeCipherGroups::
     93                      kX25519_MLKEM768);
     94 
     95  std::vector<uint16_t> expected = {
     96      webrtc::CryptoOptions::EphemeralKeyExchangeCipherGroups::kX25519_MLKEM768,
     97 #ifdef SSL_GROUP_X25519
     98      SSL_GROUP_X25519,
     99 #endif
    100 #ifdef SSL_GROUP_SECP256R1
    101      SSL_GROUP_SECP256R1,
    102 #endif
    103 #ifdef SSL_GROUP_SECP384R1
    104      SSL_GROUP_SECP384R1,
    105 #endif
    106  };
    107  EXPECT_EQ(groups.GetEnabled(), expected);
    108 }
    109 
    110 TEST(EphemeralKeyExchangeCipherGroupsTest, Update) {
    111  std::vector<uint16_t> expected = {
    112 #ifdef SSL_GROUP_X25519_MLKEM768
    113      SSL_GROUP_X25519_MLKEM768,
    114 #endif
    115 #ifdef SSL_GROUP_SECP256R1
    116      SSL_GROUP_SECP256R1,
    117 #endif
    118 #ifdef SSL_GROUP_SECP384R1
    119      SSL_GROUP_SECP384R1,
    120 #endif
    121  };
    122 
    123  std::vector<uint16_t> disable = {
    124 #ifdef SSL_GROUP_X25519
    125      SSL_GROUP_X25519,
    126 #endif
    127  };
    128 
    129  webrtc::CryptoOptions::EphemeralKeyExchangeCipherGroups groups;
    130  FieldTrials field_trials =
    131      CreateTestFieldTrials("WebRTC-EnableDtlsPqc/Enabled/");
    132  groups.Update(&field_trials, &disable);
    133  EXPECT_EQ(groups.GetEnabled(), expected);
    134 }
    135 
    136 TEST(EphemeralKeyExchangeCipherGroupsTest, CopyCryptoOptions) {
    137  webrtc::CryptoOptions options;
    138  options.ephemeral_key_exchange_cipher_groups.SetEnabled({
    139      webrtc::CryptoOptions::EphemeralKeyExchangeCipherGroups::kX25519_MLKEM768,
    140  });
    141  webrtc::CryptoOptions copy1 = options;
    142  webrtc::CryptoOptions copy2(options);
    143  EXPECT_EQ(options, copy1);
    144  EXPECT_EQ(options, copy2);
    145 }
    146 
    147 }  // namespace
    148 }  // namespace webrtc