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 #define LOG_TAG "asn1_decoder_test"
18 
19 #include <cutils/log.h>
20 #include <gtest/gtest.h>
21 #include <stdint.h>
22 #include <unistd.h>
23 
24 #include "asn1_decoder.h"
25 
26 namespace android {
27 
28 class Asn1DecoderTest : public testing::Test {
29 };
30 
TEST_F(Asn1DecoderTest,Empty_Failure)31 TEST_F(Asn1DecoderTest, Empty_Failure) {
32     uint8_t empty[] = { };
33     asn1_context_t* ctx = asn1_context_new(empty, sizeof(empty));
34 
35     EXPECT_EQ(NULL, asn1_constructed_get(ctx));
36     EXPECT_FALSE(asn1_constructed_skip_all(ctx));
37     EXPECT_EQ(0, asn1_constructed_type(ctx));
38     EXPECT_EQ(NULL, asn1_sequence_get(ctx));
39     EXPECT_EQ(NULL, asn1_set_get(ctx));
40     EXPECT_FALSE(asn1_sequence_next(ctx));
41 
42     uint8_t* junk;
43     size_t length;
44     EXPECT_FALSE(asn1_oid_get(ctx, &junk, &length));
45     EXPECT_FALSE(asn1_octet_string_get(ctx, &junk, &length));
46 
47     asn1_context_free(ctx);
48 }
49 
TEST_F(Asn1DecoderTest,ConstructedGet_TruncatedLength_Failure)50 TEST_F(Asn1DecoderTest, ConstructedGet_TruncatedLength_Failure) {
51     uint8_t truncated[] = { 0xA0, 0x82, };
52     asn1_context_t* ctx = asn1_context_new(truncated, sizeof(truncated));
53     EXPECT_EQ(NULL, asn1_constructed_get(ctx));
54     asn1_context_free(ctx);
55 }
56 
TEST_F(Asn1DecoderTest,ConstructedGet_LengthTooBig_Failure)57 TEST_F(Asn1DecoderTest, ConstructedGet_LengthTooBig_Failure) {
58     uint8_t truncated[] = { 0xA0, 0x8a, 0xA5, 0x5A, 0xA5, 0x5A,
59                             0xA5, 0x5A, 0xA5, 0x5A, 0xA5, 0x5A, };
60     asn1_context_t* ctx = asn1_context_new(truncated, sizeof(truncated));
61     EXPECT_EQ(NULL, asn1_constructed_get(ctx));
62     asn1_context_free(ctx);
63 }
64 
TEST_F(Asn1DecoderTest,ConstructedGet_TooSmallForChild_Failure)65 TEST_F(Asn1DecoderTest, ConstructedGet_TooSmallForChild_Failure) {
66     uint8_t data[] = { 0xA5, 0x02, 0x06, 0x01, 0x01, };
67     asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
68     asn1_context_t* ptr = asn1_constructed_get(ctx);
69     ASSERT_NE((asn1_context_t*)NULL, ptr);
70     EXPECT_EQ(5, asn1_constructed_type(ptr));
71     uint8_t* oid;
72     size_t length;
73     EXPECT_FALSE(asn1_oid_get(ptr, &oid, &length));
74     asn1_context_free(ptr);
75     asn1_context_free(ctx);
76 }
77 
TEST_F(Asn1DecoderTest,ConstructedGet_Success)78 TEST_F(Asn1DecoderTest, ConstructedGet_Success) {
79     uint8_t data[] = { 0xA5, 0x03, 0x06, 0x01, 0x01, };
80     asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
81     asn1_context_t* ptr = asn1_constructed_get(ctx);
82     ASSERT_NE((asn1_context_t*)NULL, ptr);
83     EXPECT_EQ(5, asn1_constructed_type(ptr));
84     uint8_t* oid;
85     size_t length;
86     ASSERT_TRUE(asn1_oid_get(ptr, &oid, &length));
87     EXPECT_EQ(1U, length);
88     EXPECT_EQ(0x01U, *oid);
89     asn1_context_free(ptr);
90     asn1_context_free(ctx);
91 }
92 
TEST_F(Asn1DecoderTest,ConstructedSkipAll_TruncatedLength_Failure)93 TEST_F(Asn1DecoderTest, ConstructedSkipAll_TruncatedLength_Failure) {
94     uint8_t truncated[] = { 0xA2, 0x82, };
95     asn1_context_t* ctx = asn1_context_new(truncated, sizeof(truncated));
96     EXPECT_FALSE(asn1_constructed_skip_all(ctx));
97     asn1_context_free(ctx);
98 }
99 
TEST_F(Asn1DecoderTest,ConstructedSkipAll_Success)100 TEST_F(Asn1DecoderTest, ConstructedSkipAll_Success) {
101     uint8_t data[] = { 0xA0, 0x03, 0x02, 0x01, 0x01,
102                             0xA1, 0x03, 0x02, 0x01, 0x01,
103                             0x06, 0x01, 0xA5, };
104     asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
105     ASSERT_TRUE(asn1_constructed_skip_all(ctx));
106     uint8_t* oid;
107     size_t length;
108     ASSERT_TRUE(asn1_oid_get(ctx, &oid, &length));
109     EXPECT_EQ(1U, length);
110     EXPECT_EQ(0xA5U, *oid);
111     asn1_context_free(ctx);
112 }
113 
TEST_F(Asn1DecoderTest,SequenceGet_TruncatedLength_Failure)114 TEST_F(Asn1DecoderTest, SequenceGet_TruncatedLength_Failure) {
115     uint8_t truncated[] = { 0x30, 0x82, };
116     asn1_context_t* ctx = asn1_context_new(truncated, sizeof(truncated));
117     EXPECT_EQ(NULL, asn1_sequence_get(ctx));
118     asn1_context_free(ctx);
119 }
120 
TEST_F(Asn1DecoderTest,SequenceGet_TooSmallForChild_Failure)121 TEST_F(Asn1DecoderTest, SequenceGet_TooSmallForChild_Failure) {
122     uint8_t data[] = { 0x30, 0x02, 0x06, 0x01, 0x01, };
123     asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
124     asn1_context_t* ptr = asn1_sequence_get(ctx);
125     ASSERT_NE((asn1_context_t*)NULL, ptr);
126     uint8_t* oid;
127     size_t length;
128     EXPECT_FALSE(asn1_oid_get(ptr, &oid, &length));
129     asn1_context_free(ptr);
130     asn1_context_free(ctx);
131 }
132 
TEST_F(Asn1DecoderTest,SequenceGet_Success)133 TEST_F(Asn1DecoderTest, SequenceGet_Success) {
134     uint8_t data[] = { 0x30, 0x03, 0x06, 0x01, 0x01, };
135     asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
136     asn1_context_t* ptr = asn1_sequence_get(ctx);
137     ASSERT_NE((asn1_context_t*)NULL, ptr);
138     uint8_t* oid;
139     size_t length;
140     ASSERT_TRUE(asn1_oid_get(ptr, &oid, &length));
141     EXPECT_EQ(1U, length);
142     EXPECT_EQ(0x01U, *oid);
143     asn1_context_free(ptr);
144     asn1_context_free(ctx);
145 }
146 
TEST_F(Asn1DecoderTest,SetGet_TruncatedLength_Failure)147 TEST_F(Asn1DecoderTest, SetGet_TruncatedLength_Failure) {
148     uint8_t truncated[] = { 0x31, 0x82, };
149     asn1_context_t* ctx = asn1_context_new(truncated, sizeof(truncated));
150     EXPECT_EQ(NULL, asn1_set_get(ctx));
151     asn1_context_free(ctx);
152 }
153 
TEST_F(Asn1DecoderTest,SetGet_TooSmallForChild_Failure)154 TEST_F(Asn1DecoderTest, SetGet_TooSmallForChild_Failure) {
155     uint8_t data[] = { 0x31, 0x02, 0x06, 0x01, 0x01, };
156     asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
157     asn1_context_t* ptr = asn1_set_get(ctx);
158     ASSERT_NE((asn1_context_t*)NULL, ptr);
159     uint8_t* oid;
160     size_t length;
161     EXPECT_FALSE(asn1_oid_get(ptr, &oid, &length));
162     asn1_context_free(ptr);
163     asn1_context_free(ctx);
164 }
165 
TEST_F(Asn1DecoderTest,SetGet_Success)166 TEST_F(Asn1DecoderTest, SetGet_Success) {
167     uint8_t data[] = { 0x31, 0x03, 0x06, 0x01, 0xBA, };
168     asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
169     asn1_context_t* ptr = asn1_set_get(ctx);
170     ASSERT_NE((asn1_context_t*)NULL, ptr);
171     uint8_t* oid;
172     size_t length;
173     ASSERT_TRUE(asn1_oid_get(ptr, &oid, &length));
174     EXPECT_EQ(1U, length);
175     EXPECT_EQ(0xBAU, *oid);
176     asn1_context_free(ptr);
177     asn1_context_free(ctx);
178 }
179 
TEST_F(Asn1DecoderTest,OidGet_LengthZero_Failure)180 TEST_F(Asn1DecoderTest, OidGet_LengthZero_Failure) {
181     uint8_t data[] = { 0x06, 0x00, 0x01, };
182     asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
183     uint8_t* oid;
184     size_t length;
185     EXPECT_FALSE(asn1_oid_get(ctx, &oid, &length));
186     asn1_context_free(ctx);
187 }
188 
TEST_F(Asn1DecoderTest,OidGet_TooSmall_Failure)189 TEST_F(Asn1DecoderTest, OidGet_TooSmall_Failure) {
190     uint8_t data[] = { 0x06, 0x01, };
191     asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
192     uint8_t* oid;
193     size_t length;
194     EXPECT_FALSE(asn1_oid_get(ctx, &oid, &length));
195     asn1_context_free(ctx);
196 }
197 
TEST_F(Asn1DecoderTest,OidGet_Success)198 TEST_F(Asn1DecoderTest, OidGet_Success) {
199     uint8_t data[] = { 0x06, 0x01, 0x99, };
200     asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
201     uint8_t* oid;
202     size_t length;
203     ASSERT_TRUE(asn1_oid_get(ctx, &oid, &length));
204     EXPECT_EQ(1U, length);
205     EXPECT_EQ(0x99U, *oid);
206     asn1_context_free(ctx);
207 }
208 
TEST_F(Asn1DecoderTest,OctetStringGet_LengthZero_Failure)209 TEST_F(Asn1DecoderTest, OctetStringGet_LengthZero_Failure) {
210     uint8_t data[] = { 0x04, 0x00, 0x55, };
211     asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
212     uint8_t* string;
213     size_t length;
214     ASSERT_FALSE(asn1_octet_string_get(ctx, &string, &length));
215     asn1_context_free(ctx);
216 }
217 
TEST_F(Asn1DecoderTest,OctetStringGet_TooSmall_Failure)218 TEST_F(Asn1DecoderTest, OctetStringGet_TooSmall_Failure) {
219     uint8_t data[] = { 0x04, 0x01, };
220     asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
221     uint8_t* string;
222     size_t length;
223     ASSERT_FALSE(asn1_octet_string_get(ctx, &string, &length));
224     asn1_context_free(ctx);
225 }
226 
TEST_F(Asn1DecoderTest,OctetStringGet_Success)227 TEST_F(Asn1DecoderTest, OctetStringGet_Success) {
228     uint8_t data[] = { 0x04, 0x01, 0xAA, };
229     asn1_context_t* ctx = asn1_context_new(data, sizeof(data));
230     uint8_t* string;
231     size_t length;
232     ASSERT_TRUE(asn1_octet_string_get(ctx, &string, &length));
233     EXPECT_EQ(1U, length);
234     EXPECT_EQ(0xAAU, *string);
235     asn1_context_free(ctx);
236 }
237 
238 } // namespace android
239