tor-browser

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

pkixcheck_TLSFeaturesSatisfiedInternal_tests.cpp (4462B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This code is made available to you under your choice of the following sets
      4 * of licensing terms:
      5 */
      6 /* This Source Code Form is subject to the terms of the Mozilla Public
      7 * License, v. 2.0. If a copy of the MPL was not distributed with this
      8 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
      9 */
     10 /* Copyright 2015 Mozilla Contributors
     11 *
     12 * Licensed under the Apache License, Version 2.0 (the "License");
     13 * you may not use this file except in compliance with the License.
     14 * You may obtain a copy of the License at
     15 *
     16 *     http://www.apache.org/licenses/LICENSE-2.0
     17 *
     18 * Unless required by applicable law or agreed to in writing, software
     19 * distributed under the License is distributed on an "AS IS" BASIS,
     20 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     21 * See the License for the specific language governing permissions and
     22 * limitations under the License.
     23 */
     24 
     25 #include "pkixgtest.h"
     26 
     27 #include "mozpkix/pkixder.h"
     28 
     29 using namespace mozilla::pkix;
     30 using namespace mozilla::pkix::test;
     31 
     32 namespace mozilla { namespace pkix {
     33  extern Result TLSFeaturesSatisfiedInternal(const Input* requiredTLSFeatures,
     34                                             const Input* stapledOCSPResponse);
     35 } } // namespace mozilla::pkix
     36 
     37 struct TLSFeaturesTestParams
     38 {
     39  ByteString requiredTLSFeatures;
     40  Result expectedResultWithResponse;
     41  Result expectedResultWithoutResponse;
     42 };
     43 
     44 ::std::ostream& operator<<(::std::ostream& os, const TLSFeaturesTestParams&)
     45 {
     46  return os << "TODO (bug 1318770)";
     47 }
     48 
     49 #define BS(s) ByteString(s, MOZILLA_PKIX_ARRAY_LENGTH(s))
     50 static const uint8_t statusRequest[] = {
     51  0x30, 0x03, 0x02, 0x01, 0x05
     52 };
     53 
     54 static const uint8_t unknown[] = {
     55  0x30, 0x03, 0x02, 0x01, 0x06
     56 };
     57 
     58 static const uint8_t statusRequestAndUnknown[] = {
     59  0x30, 0x06, 0x02, 0x01, 0x05, 0x02, 0x01, 0x06
     60 };
     61 
     62 static const uint8_t duplicateStatusRequest[] = {
     63  0x30, 0x06, 0x02, 0x01, 0x05, 0x02, 0x01, 0x05
     64 };
     65 
     66 static const uint8_t twoByteUnknown[] = {
     67  0x30, 0x04, 0x02, 0x02, 0x05, 0x05
     68 };
     69 
     70 static const uint8_t zeroByteInteger[] = {
     71  0x30, 0x02, 0x02, 0x00
     72 };
     73 
     74 static const uint8_t trailingData[] = {
     75  0x30, 0x03, 0x02, 0x01, 0x05, // statusRequest
     76  0xe5, 0xe5, 0xe5 // trailing data
     77 };
     78 
     79 static const TLSFeaturesTestParams
     80  TLSFEATURESSATISFIED_TEST_PARAMS[] =
     81 {
     82  // some tests with checks enforced
     83  { ByteString(), Result::ERROR_BAD_DER, Result::ERROR_BAD_DER },
     84  { BS(statusRequest), Success, Result::ERROR_REQUIRED_TLS_FEATURE_MISSING },
     85  { BS(unknown), Result::ERROR_REQUIRED_TLS_FEATURE_MISSING,
     86    Result::ERROR_REQUIRED_TLS_FEATURE_MISSING },
     87  { BS(statusRequestAndUnknown), Result::ERROR_REQUIRED_TLS_FEATURE_MISSING,
     88    Result::ERROR_REQUIRED_TLS_FEATURE_MISSING },
     89  { BS(duplicateStatusRequest), Success,
     90    Result::ERROR_REQUIRED_TLS_FEATURE_MISSING },
     91  { BS(twoByteUnknown), Result::ERROR_REQUIRED_TLS_FEATURE_MISSING,
     92    Result::ERROR_REQUIRED_TLS_FEATURE_MISSING },
     93  { BS(zeroByteInteger), Result::ERROR_REQUIRED_TLS_FEATURE_MISSING,
     94    Result::ERROR_REQUIRED_TLS_FEATURE_MISSING },
     95  { BS(trailingData), Result::ERROR_BAD_DER,
     96    Result::ERROR_REQUIRED_TLS_FEATURE_MISSING },
     97 };
     98 
     99 class pkixcheck_TLSFeaturesSatisfiedInternal
    100  : public ::testing::Test
    101  , public ::testing::WithParamInterface<TLSFeaturesTestParams>
    102 {
    103 };
    104 
    105 TEST_P(pkixcheck_TLSFeaturesSatisfiedInternal, TLSFeaturesSatisfiedInternal) {
    106  const TLSFeaturesTestParams& params(GetParam());
    107 
    108  Input featuresInput;
    109  ASSERT_EQ(Success, featuresInput.Init(params.requiredTLSFeatures.data(),
    110                                        params.requiredTLSFeatures.length()));
    111  Input responseInput;
    112  // just create an input with any data in it
    113  ByteString stapledOCSPResponse = BS(statusRequest);
    114  ASSERT_EQ(Success, responseInput.Init(stapledOCSPResponse.data(),
    115                                        stapledOCSPResponse.length()));
    116  // first we omit the response
    117  ASSERT_EQ(params.expectedResultWithoutResponse,
    118            TLSFeaturesSatisfiedInternal(&featuresInput, nullptr));
    119  // then we try again with the response
    120  ASSERT_EQ(params.expectedResultWithResponse,
    121            TLSFeaturesSatisfiedInternal(&featuresInput, &responseInput));
    122 }
    123 
    124 INSTANTIATE_TEST_SUITE_P(
    125  pkixcheck_TLSFeaturesSatisfiedInternal,
    126  pkixcheck_TLSFeaturesSatisfiedInternal,
    127  testing::ValuesIn(TLSFEATURESSATISFIED_TEST_PARAMS));