1 /*
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
17 #include <stdint.h>
18
19 #include <memory>
20
21 #include <gtest/gtest.h>
22
23 #include "asn1_decoder.h"
24
TEST(Asn1DecoderTest,Empty_Failure)25 TEST(Asn1DecoderTest, Empty_Failure) {
26 uint8_t empty[] = {};
27 asn1_context ctx(empty, sizeof(empty));
28
29 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());
35
36 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));
40 }
41
TEST(Asn1DecoderTest,ConstructedGet_TruncatedLength_Failure)42 TEST(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());
46 }
47
TEST(Asn1DecoderTest,ConstructedGet_LengthTooBig_Failure)48 TEST(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());
52 }
53
TEST(Asn1DecoderTest,ConstructedGet_TooSmallForChild_Failure)54 TEST(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));
63 }
64
TEST(Asn1DecoderTest,ConstructedGet_Success)65 TEST(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);
76 }
77
TEST(Asn1DecoderTest,ConstructedSkipAll_TruncatedLength_Failure)78 TEST(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());
82 }
83
TEST(Asn1DecoderTest,ConstructedSkipAll_Success)84 TEST(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);
93 }
94
TEST(Asn1DecoderTest,SequenceGet_TruncatedLength_Failure)95 TEST(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());
99 }
100
TEST(Asn1DecoderTest,SequenceGet_TooSmallForChild_Failure)101 TEST(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));
109 }
110
TEST(Asn1DecoderTest,SequenceGet_Success)111 TEST(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);
121 }
122
TEST(Asn1DecoderTest,SetGet_TruncatedLength_Failure)123 TEST(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());
127 }
128
TEST(Asn1DecoderTest,SetGet_TooSmallForChild_Failure)129 TEST(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));
137 }
138
TEST(Asn1DecoderTest,SetGet_Success)139 TEST(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);
149 }
150
TEST(Asn1DecoderTest,OidGet_LengthZero_Failure)151 TEST(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));
157 }
158
TEST(Asn1DecoderTest,OidGet_TooSmall_Failure)159 TEST(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));
165 }
166
TEST(Asn1DecoderTest,OidGet_Success)167 TEST(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);
175 }
176
TEST(Asn1DecoderTest,OctetStringGet_LengthZero_Failure)177 TEST(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));
183 }
184
TEST(Asn1DecoderTest,OctetStringGet_TooSmall_Failure)185 TEST(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));
191 }
192
TEST(Asn1DecoderTest,OctetStringGet_Success)193 TEST(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);
201 }
202