blob: b334a655b5cf9e3d899a77ba4d264e0108012372 [file] [log] [blame]
Kenny Root7a4adb52013-10-09 10:14:35 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Kenny Root7a4adb52013-10-09 10:14:35 -070017#include <stdint.h>
Tao Bao861c53c2017-03-20 17:09:13 -070018
19#include <memory>
20
21#include <gtest/gtest.h>
Kenny Root7a4adb52013-10-09 10:14:35 -070022
23#include "asn1_decoder.h"
24
Tao Bao861c53c2017-03-20 17:09:13 -070025TEST(Asn1DecoderTest, Empty_Failure) {
26 uint8_t empty[] = {};
27 asn1_context ctx(empty, sizeof(empty));
Kenny Root7a4adb52013-10-09 10:14:35 -070028
Tao Bao861c53c2017-03-20 17:09:13 -070029 ASSERT_EQ(nullptr, ctx.asn1_constructed_get());
30 ASSERT_FALSE(ctx.asn1_constructed_skip_all());
31 ASSERT_EQ(0, ctx.asn1_constructed_type());
32 ASSERT_EQ(nullptr, ctx.asn1_sequence_get());
33 ASSERT_EQ(nullptr, ctx.asn1_set_get());
34 ASSERT_FALSE(ctx.asn1_sequence_next());
Kenny Root7a4adb52013-10-09 10:14:35 -070035
Tao Bao861c53c2017-03-20 17:09:13 -070036 const uint8_t* junk;
37 size_t length;
38 ASSERT_FALSE(ctx.asn1_oid_get(&junk, &length));
39 ASSERT_FALSE(ctx.asn1_octet_string_get(&junk, &length));
Kenny Root7a4adb52013-10-09 10:14:35 -070040}
41
Tao Bao861c53c2017-03-20 17:09:13 -070042TEST(Asn1DecoderTest, ConstructedGet_TruncatedLength_Failure) {
43 uint8_t truncated[] = { 0xA0, 0x82 };
44 asn1_context ctx(truncated, sizeof(truncated));
45 ASSERT_EQ(nullptr, ctx.asn1_constructed_get());
Kenny Root7a4adb52013-10-09 10:14:35 -070046}
47
Tao Bao861c53c2017-03-20 17:09:13 -070048TEST(Asn1DecoderTest, ConstructedGet_LengthTooBig_Failure) {
49 uint8_t truncated[] = { 0xA0, 0x8a, 0xA5, 0x5A, 0xA5, 0x5A, 0xA5, 0x5A, 0xA5, 0x5A, 0xA5, 0x5A };
50 asn1_context ctx(truncated, sizeof(truncated));
51 ASSERT_EQ(nullptr, ctx.asn1_constructed_get());
Kenny Root7a4adb52013-10-09 10:14:35 -070052}
53
Tao Bao861c53c2017-03-20 17:09:13 -070054TEST(Asn1DecoderTest, ConstructedGet_TooSmallForChild_Failure) {
55 uint8_t data[] = { 0xA5, 0x02, 0x06, 0x01, 0x01 };
56 asn1_context ctx(data, sizeof(data));
57 std::unique_ptr<asn1_context> ptr(ctx.asn1_constructed_get());
58 ASSERT_NE(nullptr, ptr);
59 ASSERT_EQ(5, ptr->asn1_constructed_type());
60 const uint8_t* oid;
61 size_t length;
62 ASSERT_FALSE(ptr->asn1_oid_get(&oid, &length));
Kenny Root7a4adb52013-10-09 10:14:35 -070063}
64
Tao Bao861c53c2017-03-20 17:09:13 -070065TEST(Asn1DecoderTest, ConstructedGet_Success) {
66 uint8_t data[] = { 0xA5, 0x03, 0x06, 0x01, 0x01 };
67 asn1_context ctx(data, sizeof(data));
68 std::unique_ptr<asn1_context> ptr(ctx.asn1_constructed_get());
69 ASSERT_NE(nullptr, ptr);
70 ASSERT_EQ(5, ptr->asn1_constructed_type());
71 const uint8_t* oid;
72 size_t length;
73 ASSERT_TRUE(ptr->asn1_oid_get(&oid, &length));
74 ASSERT_EQ(1U, length);
75 ASSERT_EQ(0x01U, *oid);
Kenny Root7a4adb52013-10-09 10:14:35 -070076}
77
Tao Bao861c53c2017-03-20 17:09:13 -070078TEST(Asn1DecoderTest, ConstructedSkipAll_TruncatedLength_Failure) {
79 uint8_t truncated[] = { 0xA2, 0x82 };
80 asn1_context ctx(truncated, sizeof(truncated));
81 ASSERT_FALSE(ctx.asn1_constructed_skip_all());
Kenny Root7a4adb52013-10-09 10:14:35 -070082}
83
Tao Bao861c53c2017-03-20 17:09:13 -070084TEST(Asn1DecoderTest, ConstructedSkipAll_Success) {
85 uint8_t data[] = { 0xA0, 0x03, 0x02, 0x01, 0x01, 0xA1, 0x03, 0x02, 0x01, 0x01, 0x06, 0x01, 0xA5 };
86 asn1_context ctx(data, sizeof(data));
87 ASSERT_TRUE(ctx.asn1_constructed_skip_all());
88 const uint8_t* oid;
89 size_t length;
90 ASSERT_TRUE(ctx.asn1_oid_get(&oid, &length));
91 ASSERT_EQ(1U, length);
92 ASSERT_EQ(0xA5U, *oid);
Kenny Root7a4adb52013-10-09 10:14:35 -070093}
94
Tao Bao861c53c2017-03-20 17:09:13 -070095TEST(Asn1DecoderTest, SequenceGet_TruncatedLength_Failure) {
96 uint8_t truncated[] = { 0x30, 0x82 };
97 asn1_context ctx(truncated, sizeof(truncated));
98 ASSERT_EQ(nullptr, ctx.asn1_sequence_get());
Kenny Root7a4adb52013-10-09 10:14:35 -070099}
100
Tao Bao861c53c2017-03-20 17:09:13 -0700101TEST(Asn1DecoderTest, SequenceGet_TooSmallForChild_Failure) {
102 uint8_t data[] = { 0x30, 0x02, 0x06, 0x01, 0x01 };
103 asn1_context ctx(data, sizeof(data));
104 std::unique_ptr<asn1_context> ptr(ctx.asn1_sequence_get());
105 ASSERT_NE(nullptr, ptr);
106 const uint8_t* oid;
107 size_t length;
108 ASSERT_FALSE(ptr->asn1_oid_get(&oid, &length));
Kenny Root7a4adb52013-10-09 10:14:35 -0700109}
110
Tao Bao861c53c2017-03-20 17:09:13 -0700111TEST(Asn1DecoderTest, SequenceGet_Success) {
112 uint8_t data[] = { 0x30, 0x03, 0x06, 0x01, 0x01 };
113 asn1_context ctx(data, sizeof(data));
114 std::unique_ptr<asn1_context> ptr(ctx.asn1_sequence_get());
115 ASSERT_NE(nullptr, ptr);
116 const uint8_t* oid;
117 size_t length;
118 ASSERT_TRUE(ptr->asn1_oid_get(&oid, &length));
119 ASSERT_EQ(1U, length);
120 ASSERT_EQ(0x01U, *oid);
Kenny Root7a4adb52013-10-09 10:14:35 -0700121}
122
Tao Bao861c53c2017-03-20 17:09:13 -0700123TEST(Asn1DecoderTest, SetGet_TruncatedLength_Failure) {
124 uint8_t truncated[] = { 0x31, 0x82 };
125 asn1_context ctx(truncated, sizeof(truncated));
126 ASSERT_EQ(nullptr, ctx.asn1_set_get());
Kenny Root7a4adb52013-10-09 10:14:35 -0700127}
128
Tao Bao861c53c2017-03-20 17:09:13 -0700129TEST(Asn1DecoderTest, SetGet_TooSmallForChild_Failure) {
130 uint8_t data[] = { 0x31, 0x02, 0x06, 0x01, 0x01 };
131 asn1_context ctx(data, sizeof(data));
132 std::unique_ptr<asn1_context> ptr(ctx.asn1_set_get());
133 ASSERT_NE(nullptr, ptr);
134 const uint8_t* oid;
135 size_t length;
136 ASSERT_FALSE(ptr->asn1_oid_get(&oid, &length));
Kenny Root7a4adb52013-10-09 10:14:35 -0700137}
138
Tao Bao861c53c2017-03-20 17:09:13 -0700139TEST(Asn1DecoderTest, SetGet_Success) {
140 uint8_t data[] = { 0x31, 0x03, 0x06, 0x01, 0xBA };
141 asn1_context ctx(data, sizeof(data));
142 std::unique_ptr<asn1_context> ptr(ctx.asn1_set_get());
143 ASSERT_NE(nullptr, ptr);
144 const uint8_t* oid;
145 size_t length;
146 ASSERT_TRUE(ptr->asn1_oid_get(&oid, &length));
147 ASSERT_EQ(1U, length);
148 ASSERT_EQ(0xBAU, *oid);
Kenny Root7a4adb52013-10-09 10:14:35 -0700149}
150
Tao Bao861c53c2017-03-20 17:09:13 -0700151TEST(Asn1DecoderTest, OidGet_LengthZero_Failure) {
152 uint8_t data[] = { 0x06, 0x00, 0x01 };
153 asn1_context ctx(data, sizeof(data));
154 const uint8_t* oid;
155 size_t length;
156 ASSERT_FALSE(ctx.asn1_oid_get(&oid, &length));
Kenny Root7a4adb52013-10-09 10:14:35 -0700157}
158
Tao Bao861c53c2017-03-20 17:09:13 -0700159TEST(Asn1DecoderTest, OidGet_TooSmall_Failure) {
160 uint8_t data[] = { 0x06, 0x01 };
161 asn1_context ctx(data, sizeof(data));
162 const uint8_t* oid;
163 size_t length;
164 ASSERT_FALSE(ctx.asn1_oid_get(&oid, &length));
Kenny Root7a4adb52013-10-09 10:14:35 -0700165}
166
Tao Bao861c53c2017-03-20 17:09:13 -0700167TEST(Asn1DecoderTest, OidGet_Success) {
168 uint8_t data[] = { 0x06, 0x01, 0x99 };
169 asn1_context ctx(data, sizeof(data));
170 const uint8_t* oid;
171 size_t length;
172 ASSERT_TRUE(ctx.asn1_oid_get(&oid, &length));
173 ASSERT_EQ(1U, length);
174 ASSERT_EQ(0x99U, *oid);
Kenny Root7a4adb52013-10-09 10:14:35 -0700175}
176
Tao Bao861c53c2017-03-20 17:09:13 -0700177TEST(Asn1DecoderTest, OctetStringGet_LengthZero_Failure) {
178 uint8_t data[] = { 0x04, 0x00, 0x55 };
179 asn1_context ctx(data, sizeof(data));
180 const uint8_t* string;
181 size_t length;
182 ASSERT_FALSE(ctx.asn1_octet_string_get(&string, &length));
Kenny Root7a4adb52013-10-09 10:14:35 -0700183}
184
Tao Bao861c53c2017-03-20 17:09:13 -0700185TEST(Asn1DecoderTest, OctetStringGet_TooSmall_Failure) {
186 uint8_t data[] = { 0x04, 0x01 };
187 asn1_context ctx(data, sizeof(data));
188 const uint8_t* string;
189 size_t length;
190 ASSERT_FALSE(ctx.asn1_octet_string_get(&string, &length));
Kenny Root7a4adb52013-10-09 10:14:35 -0700191}
192
Tao Bao861c53c2017-03-20 17:09:13 -0700193TEST(Asn1DecoderTest, OctetStringGet_Success) {
194 uint8_t data[] = { 0x04, 0x01, 0xAA };
195 asn1_context ctx(data, sizeof(data));
196 const uint8_t* string;
197 size_t length;
198 ASSERT_TRUE(ctx.asn1_octet_string_get(&string, &length));
199 ASSERT_EQ(1U, length);
200 ASSERT_EQ(0xAAU, *string);
Kenny Root7a4adb52013-10-09 10:14:35 -0700201}