Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/security/certverifier/tests/gtest/BTSerializationTest.cpp
Line
Count
Source (jump to first uncovered line)
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 { namespace ct {
12
13
using namespace pkix;
14
15
class BTSerializationTest : public ::testing::Test
16
{
17
public:
18
  void SetUp() override
19
0
  {
20
0
    mTestInclusionProof = GetTestInclusionProof();
21
0
    mTestInclusionProofUnexpectedData = GetTestInclusionProofUnexpectedData();
22
0
    mTestInclusionProofInvalidHashSize = GetTestInclusionProofInvalidHashSize();
23
0
    mTestInclusionProofInvalidHash = GetTestInclusionProofInvalidHash();
24
0
    mTestInclusionProofMissingLogId = GetTestInclusionProofMissingLogId();
25
0
    mTestInclusionProofNullPathLength = GetTestInclusionProofNullPathLength();
26
0
    mTestInclusionProofPathLengthTooSmall = GetTestInclusionProofPathLengthTooSmall();
27
0
    mTestInclusionProofPathLengthTooLarge = GetTestInclusionProofPathLengthTooLarge();
28
0
    mTestInclusionProofNullTreeSize = GetTestInclusionProofNullTreeSize();
29
0
    mTestInclusionProofLeafIndexOutOfBounds = GetTestInclusionProofLeafIndexOutOfBounds();
30
0
    mTestInclusionProofExtraData = GetTestInclusionProofExtraData();
31
0
  }
32
33
protected:
34
  Buffer mTestInclusionProof;
35
  Buffer mTestInclusionProofUnexpectedData;
36
  Buffer mTestInclusionProofInvalidHashSize;
37
  Buffer mTestInclusionProofInvalidHash;
38
  Buffer mTestInclusionProofMissingLogId;
39
  Buffer mTestInclusionProofNullPathLength;
40
  Buffer mTestInclusionProofPathLengthTooSmall;
41
  Buffer mTestInclusionProofPathLengthTooLarge;
42
  Buffer mTestInclusionProofNullTreeSize;
43
  Buffer mTestInclusionProofLeafIndexOutOfBounds;
44
  Buffer mTestInclusionProofExtraData;
45
};
46
47
TEST_F(BTSerializationTest, DecodesInclusionProof)
48
0
{
49
0
  const uint64_t expectedTreeSize = 4;
50
0
  const uint64_t expectedLeafIndex = 2;
51
0
  const uint64_t expectedInclusionPathElements = 2;
52
0
53
0
  const uint8_t EXPECTED_LOGID[] = { 0x01, 0x00 };
54
0
  Buffer expectedLogId;
55
0
  MOZ_RELEASE_ASSERT(expectedLogId.append(EXPECTED_LOGID, 2));
56
0
57
0
58
0
  Input encodedProofInput = InputForBuffer(mTestInclusionProof);
59
0
  Reader encodedProofReader(encodedProofInput);
60
0
61
0
  InclusionProofDataV2 ipr;
62
0
  ASSERT_EQ(Success, DecodeInclusionProof(encodedProofReader, ipr));
63
0
  EXPECT_EQ(expectedLogId, ipr.logId);
64
0
  EXPECT_EQ(expectedTreeSize, ipr.treeSize);
65
0
  EXPECT_EQ(expectedLeafIndex, ipr.leafIndex);
66
0
  EXPECT_EQ(expectedInclusionPathElements, ipr.inclusionPath.length());
67
0
  EXPECT_EQ(GetTestNodeHash0(), ipr.inclusionPath[0]);
68
0
  EXPECT_EQ(GetTestNodeHash1(), ipr.inclusionPath[1]);
69
0
}
70
71
TEST_F(BTSerializationTest, FailsDecodingInclusionProofUnexpectedData)
72
0
{
73
0
  Input encodedProofInput = InputForBuffer(mTestInclusionProofUnexpectedData);
74
0
  Reader encodedProofReader(encodedProofInput);
75
0
  InclusionProofDataV2 ipr;
76
0
77
0
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER, DecodeInclusionProof(encodedProofReader, ipr));
78
0
}
79
80
TEST_F(BTSerializationTest, FailsDecodingInvalidHashSize)
81
0
{
82
0
  Input encodedProofInput = InputForBuffer(mTestInclusionProofInvalidHashSize);
83
0
  Reader encodedProofReader(encodedProofInput);
84
0
  InclusionProofDataV2 ipr;
85
0
86
0
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER, DecodeInclusionProof(encodedProofReader, ipr));
87
0
}
88
89
TEST_F(BTSerializationTest, FailsDecodingInvalidHash)
90
0
{
91
0
  Input encodedProofInput = InputForBuffer(mTestInclusionProofInvalidHash);
92
0
  Reader encodedProofReader(encodedProofInput);
93
0
  InclusionProofDataV2 ipr;
94
0
95
0
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER, DecodeInclusionProof(encodedProofReader, ipr));
96
0
}
97
98
TEST_F(BTSerializationTest, FailsDecodingMissingLogId)
99
0
{
100
0
  Input encodedProofInput = InputForBuffer(mTestInclusionProofMissingLogId);
101
0
  Reader encodedProofReader(encodedProofInput);
102
0
  InclusionProofDataV2 ipr;
103
0
104
0
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER, DecodeInclusionProof(encodedProofReader, ipr));
105
0
}
106
107
TEST_F(BTSerializationTest, FailsDecodingNullPathLength)
108
0
{
109
0
  Input encodedProofInput = InputForBuffer(mTestInclusionProofNullPathLength);
110
0
  Reader encodedProofReader(encodedProofInput);
111
0
  InclusionProofDataV2 ipr;
112
0
113
0
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER, DecodeInclusionProof(encodedProofReader, ipr));
114
0
}
115
116
TEST_F(BTSerializationTest, FailsDecodingPathLengthTooSmall)
117
0
{
118
0
  Input encodedProofInput = InputForBuffer(mTestInclusionProofPathLengthTooSmall);
119
0
  Reader encodedProofReader(encodedProofInput);
120
0
  InclusionProofDataV2 ipr;
121
0
122
0
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER, DecodeInclusionProof(encodedProofReader, ipr));
123
0
}
124
125
TEST_F(BTSerializationTest, FailsDecodingPathLengthTooLarge)
126
0
{
127
0
  Input encodedProofInput = InputForBuffer(mTestInclusionProofPathLengthTooLarge);
128
0
  Reader encodedProofReader(encodedProofInput);
129
0
  InclusionProofDataV2 ipr;
130
0
131
0
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER, DecodeInclusionProof(encodedProofReader, ipr));
132
0
}
133
134
TEST_F(BTSerializationTest, FailsDecodingNullTreeSize)
135
0
{
136
0
  Input encodedProofInput = InputForBuffer(mTestInclusionProofNullTreeSize);
137
0
  Reader encodedProofReader(encodedProofInput);
138
0
  InclusionProofDataV2 ipr;
139
0
140
0
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER, DecodeInclusionProof(encodedProofReader, ipr));
141
0
}
142
143
TEST_F(BTSerializationTest, FailsDecodingLeafIndexOutOfBounds)
144
0
{
145
0
  Input encodedProofInput = InputForBuffer(mTestInclusionProofLeafIndexOutOfBounds);
146
0
  Reader encodedProofReader(encodedProofInput);
147
0
  InclusionProofDataV2 ipr;
148
0
149
0
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER, DecodeInclusionProof(encodedProofReader, ipr));
150
0
}
151
152
TEST_F(BTSerializationTest, FailsDecodingExtraData)
153
0
{
154
0
  Input encodedProofInput = InputForBuffer(mTestInclusionProofExtraData);
155
0
  Reader encodedProofReader(encodedProofInput);
156
0
  InclusionProofDataV2 ipr;
157
0
158
0
  ASSERT_EQ(pkix::Result::ERROR_BAD_DER, DecodeInclusionProof(encodedProofReader, ipr));
159
0
}
160
} } // namespace mozilla::ct