Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 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 | |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 17 | #include <stdint.h> |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 18 | |
| 19 | #include <memory> |
| 20 | |
| 21 | #include <gtest/gtest.h> |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 22 | |
xunchang | 2478885 | 2019-03-22 16:08:52 -0700 | [diff] [blame] | 23 | #include "private/asn1_decoder.h" |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 24 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 25 | TEST(Asn1DecoderTest, Empty_Failure) { |
| 26 | uint8_t empty[] = {}; |
| 27 | asn1_context ctx(empty, sizeof(empty)); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 28 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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()); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 35 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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)); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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()); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 46 | } |
| 47 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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()); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 52 | } |
| 53 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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)); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 63 | } |
| 64 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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()); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 82 | } |
| 83 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 93 | } |
| 94 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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()); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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)); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 109 | } |
| 110 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 121 | } |
| 122 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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()); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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)); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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)); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 157 | } |
| 158 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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)); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 165 | } |
| 166 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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)); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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)); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 191 | } |
| 192 | |
Tao Bao | 861c53c | 2017-03-20 17:09:13 -0700 | [diff] [blame] | 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); |
Kenny Root | 7a4adb5 | 2013-10-09 10:14:35 -0700 | [diff] [blame] | 201 | } |