tor-browser

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

BTSerializationTest.cpp (5384B)


      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 Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "BTVerifier.h"
      8 #include "CTTestUtils.h"
      9 #include "gtest/gtest.h"
     10 
     11 namespace mozilla {
     12 namespace ct {
     13 
     14 using namespace pkix;
     15 
     16 class BTSerializationTest : public ::testing::Test {
     17 public:
     18  void SetUp() override {
     19    mTestInclusionProof = GetTestInclusionProof();
     20    mTestInclusionProofUnexpectedData = GetTestInclusionProofUnexpectedData();
     21    mTestInclusionProofInvalidHashSize = GetTestInclusionProofInvalidHashSize();
     22    mTestInclusionProofInvalidHash = GetTestInclusionProofInvalidHash();
     23    mTestInclusionProofMissingLogId = GetTestInclusionProofMissingLogId();
     24    mTestInclusionProofNullPathLength = GetTestInclusionProofNullPathLength();
     25    mTestInclusionProofPathLengthTooSmall =
     26        GetTestInclusionProofPathLengthTooSmall();
     27    mTestInclusionProofPathLengthTooLarge =
     28        GetTestInclusionProofPathLengthTooLarge();
     29    mTestInclusionProofNullTreeSize = GetTestInclusionProofNullTreeSize();
     30    mTestInclusionProofLeafIndexOutOfBounds =
     31        GetTestInclusionProofLeafIndexOutOfBounds();
     32    mTestInclusionProofExtraData = GetTestInclusionProofExtraData();
     33  }
     34 
     35 protected:
     36  Buffer mTestInclusionProof;
     37  Buffer mTestInclusionProofUnexpectedData;
     38  Buffer mTestInclusionProofInvalidHashSize;
     39  Buffer mTestInclusionProofInvalidHash;
     40  Buffer mTestInclusionProofMissingLogId;
     41  Buffer mTestInclusionProofNullPathLength;
     42  Buffer mTestInclusionProofPathLengthTooSmall;
     43  Buffer mTestInclusionProofPathLengthTooLarge;
     44  Buffer mTestInclusionProofNullTreeSize;
     45  Buffer mTestInclusionProofLeafIndexOutOfBounds;
     46  Buffer mTestInclusionProofExtraData;
     47 };
     48 
     49 TEST_F(BTSerializationTest, DecodesInclusionProof) {
     50  const uint64_t expectedTreeSize = 4;
     51  const uint64_t expectedLeafIndex = 2;
     52  const uint64_t expectedInclusionPathElements = 2;
     53 
     54  Buffer expectedLogId = {0x01, 0x00};
     55 
     56  Input encodedProofInput = InputForBuffer(mTestInclusionProof);
     57  InclusionProofDataV2 ipr;
     58  ASSERT_EQ(Success, DecodeInclusionProof(encodedProofInput, ipr));
     59  EXPECT_EQ(expectedLogId, ipr.logId);
     60  EXPECT_EQ(expectedTreeSize, ipr.treeSize);
     61  EXPECT_EQ(expectedLeafIndex, ipr.leafIndex);
     62  EXPECT_EQ(expectedInclusionPathElements, ipr.inclusionPath.size());
     63  EXPECT_EQ(GetTestNodeHash0(), ipr.inclusionPath[0]);
     64  EXPECT_EQ(GetTestNodeHash1(), ipr.inclusionPath[1]);
     65 }
     66 
     67 TEST_F(BTSerializationTest, FailsDecodingInclusionProofUnexpectedData) {
     68  Input encodedProofInput = InputForBuffer(mTestInclusionProofUnexpectedData);
     69  InclusionProofDataV2 ipr;
     70  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
     71            DecodeInclusionProof(encodedProofInput, ipr));
     72 }
     73 
     74 TEST_F(BTSerializationTest, FailsDecodingInvalidHashSize) {
     75  Input encodedProofInput = InputForBuffer(mTestInclusionProofInvalidHashSize);
     76  InclusionProofDataV2 ipr;
     77  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
     78            DecodeInclusionProof(encodedProofInput, ipr));
     79 }
     80 
     81 TEST_F(BTSerializationTest, FailsDecodingInvalidHash) {
     82  Input encodedProofInput = InputForBuffer(mTestInclusionProofInvalidHash);
     83  InclusionProofDataV2 ipr;
     84  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
     85            DecodeInclusionProof(encodedProofInput, ipr));
     86 }
     87 
     88 TEST_F(BTSerializationTest, FailsDecodingMissingLogId) {
     89  Input encodedProofInput = InputForBuffer(mTestInclusionProofMissingLogId);
     90  InclusionProofDataV2 ipr;
     91  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
     92            DecodeInclusionProof(encodedProofInput, ipr));
     93 }
     94 
     95 TEST_F(BTSerializationTest, FailsDecodingNullPathLength) {
     96  Input encodedProofInput = InputForBuffer(mTestInclusionProofNullPathLength);
     97  InclusionProofDataV2 ipr;
     98  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
     99            DecodeInclusionProof(encodedProofInput, ipr));
    100 }
    101 
    102 TEST_F(BTSerializationTest, FailsDecodingPathLengthTooSmall) {
    103  Input encodedProofInput =
    104      InputForBuffer(mTestInclusionProofPathLengthTooSmall);
    105  InclusionProofDataV2 ipr;
    106  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
    107            DecodeInclusionProof(encodedProofInput, ipr));
    108 }
    109 
    110 TEST_F(BTSerializationTest, FailsDecodingPathLengthTooLarge) {
    111  Input encodedProofInput =
    112      InputForBuffer(mTestInclusionProofPathLengthTooLarge);
    113  InclusionProofDataV2 ipr;
    114  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
    115            DecodeInclusionProof(encodedProofInput, ipr));
    116 }
    117 
    118 TEST_F(BTSerializationTest, FailsDecodingNullTreeSize) {
    119  Input encodedProofInput = InputForBuffer(mTestInclusionProofNullTreeSize);
    120  InclusionProofDataV2 ipr;
    121  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
    122            DecodeInclusionProof(encodedProofInput, ipr));
    123 }
    124 
    125 TEST_F(BTSerializationTest, FailsDecodingLeafIndexOutOfBounds) {
    126  Input encodedProofInput =
    127      InputForBuffer(mTestInclusionProofLeafIndexOutOfBounds);
    128  InclusionProofDataV2 ipr;
    129  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
    130            DecodeInclusionProof(encodedProofInput, ipr));
    131 }
    132 
    133 TEST_F(BTSerializationTest, FailsDecodingExtraData) {
    134  Input encodedProofInput = InputForBuffer(mTestInclusionProofExtraData);
    135  InclusionProofDataV2 ipr;
    136  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
    137            DecodeInclusionProof(encodedProofInput, ipr));
    138 }
    139 }  // namespace ct
    140 }  // namespace mozilla