summaryrefslogtreecommitdiffstats
path: root/security/ct/tests/gtest/BTSerializationTest.cpp
blob: 75c16faf42a8da04a15cb442a3d8dcbb5798778c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "BTVerifier.h"
#include "CTTestUtils.h"
#include "gtest/gtest.h"

namespace mozilla {
namespace ct {

using namespace pkix;

class BTSerializationTest : public ::testing::Test {
 public:
  void SetUp() override {
    mTestInclusionProof = GetTestInclusionProof();
    mTestInclusionProofUnexpectedData = GetTestInclusionProofUnexpectedData();
    mTestInclusionProofInvalidHashSize = GetTestInclusionProofInvalidHashSize();
    mTestInclusionProofInvalidHash = GetTestInclusionProofInvalidHash();
    mTestInclusionProofMissingLogId = GetTestInclusionProofMissingLogId();
    mTestInclusionProofNullPathLength = GetTestInclusionProofNullPathLength();
    mTestInclusionProofPathLengthTooSmall =
        GetTestInclusionProofPathLengthTooSmall();
    mTestInclusionProofPathLengthTooLarge =
        GetTestInclusionProofPathLengthTooLarge();
    mTestInclusionProofNullTreeSize = GetTestInclusionProofNullTreeSize();
    mTestInclusionProofLeafIndexOutOfBounds =
        GetTestInclusionProofLeafIndexOutOfBounds();
    mTestInclusionProofExtraData = GetTestInclusionProofExtraData();
  }

 protected:
  Buffer mTestInclusionProof;
  Buffer mTestInclusionProofUnexpectedData;
  Buffer mTestInclusionProofInvalidHashSize;
  Buffer mTestInclusionProofInvalidHash;
  Buffer mTestInclusionProofMissingLogId;
  Buffer mTestInclusionProofNullPathLength;
  Buffer mTestInclusionProofPathLengthTooSmall;
  Buffer mTestInclusionProofPathLengthTooLarge;
  Buffer mTestInclusionProofNullTreeSize;
  Buffer mTestInclusionProofLeafIndexOutOfBounds;
  Buffer mTestInclusionProofExtraData;
};

TEST_F(BTSerializationTest, DecodesInclusionProof) {
  const uint64_t expectedTreeSize = 4;
  const uint64_t expectedLeafIndex = 2;
  const uint64_t expectedInclusionPathElements = 2;

  Buffer expectedLogId = {0x01, 0x00};

  Input encodedProofInput = InputForBuffer(mTestInclusionProof);
  InclusionProofDataV2 ipr;
  ASSERT_EQ(Success, DecodeInclusionProof(encodedProofInput, ipr));
  EXPECT_EQ(expectedLogId, ipr.logId);
  EXPECT_EQ(expectedTreeSize, ipr.treeSize);
  EXPECT_EQ(expectedLeafIndex, ipr.leafIndex);
  EXPECT_EQ(expectedInclusionPathElements, ipr.inclusionPath.size());
  EXPECT_EQ(GetTestNodeHash0(), ipr.inclusionPath[0]);
  EXPECT_EQ(GetTestNodeHash1(), ipr.inclusionPath[1]);
}

TEST_F(BTSerializationTest, FailsDecodingInclusionProofUnexpectedData) {
  Input encodedProofInput = InputForBuffer(mTestInclusionProofUnexpectedData);
  InclusionProofDataV2 ipr;
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
            DecodeInclusionProof(encodedProofInput, ipr));
}

TEST_F(BTSerializationTest, FailsDecodingInvalidHashSize) {
  Input encodedProofInput = InputForBuffer(mTestInclusionProofInvalidHashSize);
  InclusionProofDataV2 ipr;
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
            DecodeInclusionProof(encodedProofInput, ipr));
}

TEST_F(BTSerializationTest, FailsDecodingInvalidHash) {
  Input encodedProofInput = InputForBuffer(mTestInclusionProofInvalidHash);
  InclusionProofDataV2 ipr;
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
            DecodeInclusionProof(encodedProofInput, ipr));
}

TEST_F(BTSerializationTest, FailsDecodingMissingLogId) {
  Input encodedProofInput = InputForBuffer(mTestInclusionProofMissingLogId);
  InclusionProofDataV2 ipr;
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
            DecodeInclusionProof(encodedProofInput, ipr));
}

TEST_F(BTSerializationTest, FailsDecodingNullPathLength) {
  Input encodedProofInput = InputForBuffer(mTestInclusionProofNullPathLength);
  InclusionProofDataV2 ipr;
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
            DecodeInclusionProof(encodedProofInput, ipr));
}

TEST_F(BTSerializationTest, FailsDecodingPathLengthTooSmall) {
  Input encodedProofInput =
      InputForBuffer(mTestInclusionProofPathLengthTooSmall);
  InclusionProofDataV2 ipr;
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
            DecodeInclusionProof(encodedProofInput, ipr));
}

TEST_F(BTSerializationTest, FailsDecodingPathLengthTooLarge) {
  Input encodedProofInput =
      InputForBuffer(mTestInclusionProofPathLengthTooLarge);
  InclusionProofDataV2 ipr;
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
            DecodeInclusionProof(encodedProofInput, ipr));
}

TEST_F(BTSerializationTest, FailsDecodingNullTreeSize) {
  Input encodedProofInput = InputForBuffer(mTestInclusionProofNullTreeSize);
  InclusionProofDataV2 ipr;
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
            DecodeInclusionProof(encodedProofInput, ipr));
}

TEST_F(BTSerializationTest, FailsDecodingLeafIndexOutOfBounds) {
  Input encodedProofInput =
      InputForBuffer(mTestInclusionProofLeafIndexOutOfBounds);
  InclusionProofDataV2 ipr;
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
            DecodeInclusionProof(encodedProofInput, ipr));
}

TEST_F(BTSerializationTest, FailsDecodingExtraData) {
  Input encodedProofInput = InputForBuffer(mTestInclusionProofExtraData);
  InclusionProofDataV2 ipr;
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER,
            DecodeInclusionProof(encodedProofInput, ipr));
}
}  // namespace ct
}  // namespace mozilla