Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(646)

Side by Side Diff: net/der/parse_values_unittest.cc

Issue 1160643002: Remove dangerous std::string constructor for der::Input (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Put helper in anonymous namespace Created 5 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « net/der/input_unittest.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2015 The Chromium Authors. All rights reserved. 1 // Copyright 2015 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include <stdint.h> 5 #include <stdint.h>
6 6
7 #include "base/macros.h" 7 #include "base/macros.h"
8 #include "net/der/parse_values.h" 8 #include "net/der/parse_values.h"
9 #include "testing/gtest/include/gtest/gtest.h" 9 #include "testing/gtest/include/gtest/gtest.h"
10 10
11 namespace net { 11 namespace net {
12 namespace der { 12 namespace der {
13 namespace test { 13 namespace test {
14 14
15 namespace {
16
17 template <size_t N>
18 Input FromStringLiteral(const char(&data)[N]) {
19 // Strings are null-terminated. The null terminating byte shouldn't be
20 // included in the Input, so the size is N - 1 instead of N.
21 return Input(reinterpret_cast<const uint8_t*>(data), N - 1);
22 }
23
24 } // namespace
25
15 TEST(ParseValuesTest, ParseBool) { 26 TEST(ParseValuesTest, ParseBool) {
16 uint8_t buf[] = {0xFF, 0x00}; 27 uint8_t buf[] = {0xFF, 0x00};
17 Input value(buf, 1); 28 Input value(buf, 1);
18 bool out; 29 bool out;
19 EXPECT_TRUE(ParseBool(value, &out)); 30 EXPECT_TRUE(ParseBool(value, &out));
20 EXPECT_TRUE(out); 31 EXPECT_TRUE(out);
21 32
22 buf[0] = 0; 33 buf[0] = 0;
23 EXPECT_TRUE(ParseBool(value, &out)); 34 EXPECT_TRUE(ParseBool(value, &out));
24 EXPECT_FALSE(out); 35 EXPECT_FALSE(out);
25 36
26 buf[0] = 1; 37 buf[0] = 1;
27 EXPECT_FALSE(ParseBool(value, &out)); 38 EXPECT_FALSE(ParseBool(value, &out));
28 EXPECT_TRUE(ParseBoolRelaxed(value, &out)); 39 EXPECT_TRUE(ParseBoolRelaxed(value, &out));
29 EXPECT_TRUE(out); 40 EXPECT_TRUE(out);
30 41
31 buf[0] = 0xFF; 42 buf[0] = 0xFF;
32 value = Input(buf, 2); 43 value = Input(buf, 2);
33 EXPECT_FALSE(ParseBool(value, &out)); 44 EXPECT_FALSE(ParseBool(value, &out));
34 value = Input(buf, 0); 45 value = Input(buf, 0);
35 EXPECT_FALSE(ParseBool(value, &out)); 46 EXPECT_FALSE(ParseBool(value, &out));
36 } 47 }
37 48
38 TEST(ParseValuesTest, ParseTimes) { 49 TEST(ParseValuesTest, ParseTimes) {
39 GeneralizedTime out; 50 GeneralizedTime out;
40 51
41 EXPECT_TRUE(ParseUTCTime(Input("140218161200Z"), &out)); 52 EXPECT_TRUE(ParseUTCTime(FromStringLiteral("140218161200Z"), &out));
42 53
43 // DER-encoded UTCTime must end with 'Z'. 54 // DER-encoded UTCTime must end with 'Z'.
44 EXPECT_FALSE(ParseUTCTime(Input("140218161200X"), &out)); 55 EXPECT_FALSE(ParseUTCTime(FromStringLiteral("140218161200X"), &out));
45 56
46 // Check that a negative number (-4 in this case) doesn't get parsed as 57 // Check that a negative number (-4 in this case) doesn't get parsed as
47 // a 2-digit number. 58 // a 2-digit number.
48 EXPECT_FALSE(ParseUTCTime(Input("-40218161200Z"), &out)); 59 EXPECT_FALSE(ParseUTCTime(FromStringLiteral("-40218161200Z"), &out));
49 60
50 // Check that numbers with a leading 0 don't get parsed in octal by making 61 // Check that numbers with a leading 0 don't get parsed in octal by making
51 // the second digit an invalid octal digit (e.g. 09). 62 // the second digit an invalid octal digit (e.g. 09).
52 EXPECT_TRUE(ParseUTCTime(Input("090218161200Z"), &out)); 63 EXPECT_TRUE(ParseUTCTime(FromStringLiteral("090218161200Z"), &out));
53 64
54 // Check that the length is validated. 65 // Check that the length is validated.
55 EXPECT_FALSE(ParseUTCTime(Input("140218161200"), &out)); 66 EXPECT_FALSE(ParseUTCTime(FromStringLiteral("140218161200"), &out));
56 EXPECT_FALSE(ParseUTCTime(Input("140218161200Z0"), &out)); 67 EXPECT_FALSE(ParseUTCTime(FromStringLiteral("140218161200Z0"), &out));
57 EXPECT_FALSE(ParseUTCTimeRelaxed(Input("140218161200"), &out)); 68 EXPECT_FALSE(ParseUTCTimeRelaxed(FromStringLiteral("140218161200"), &out));
58 EXPECT_FALSE(ParseUTCTimeRelaxed(Input("140218161200Z0"), &out)); 69 EXPECT_FALSE(ParseUTCTimeRelaxed(FromStringLiteral("140218161200Z0"), &out));
59 70
60 // Check strictness of UTCTime parsers. 71 // Check strictness of UTCTime parsers.
61 EXPECT_FALSE(ParseUTCTime(Input("1402181612Z"), &out)); 72 EXPECT_FALSE(ParseUTCTime(FromStringLiteral("1402181612Z"), &out));
62 EXPECT_TRUE(ParseUTCTimeRelaxed(Input("1402181612Z"), &out)); 73 EXPECT_TRUE(ParseUTCTimeRelaxed(FromStringLiteral("1402181612Z"), &out));
63 74
64 // Check that the time ends in Z. 75 // Check that the time ends in Z.
65 EXPECT_FALSE(ParseUTCTimeRelaxed(Input("1402181612Z0"), &out)); 76 EXPECT_FALSE(ParseUTCTimeRelaxed(FromStringLiteral("1402181612Z0"), &out));
66 77
67 // Check format of GeneralizedTime. 78 // Check format of GeneralizedTime.
68 79
69 // Leap seconds are allowed. 80 // Leap seconds are allowed.
70 EXPECT_TRUE(ParseGeneralizedTime(Input("20140218161260Z"), &out)); 81 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20140218161260Z"), &out));
71 82
72 // But nothing larger than a leap second. 83 // But nothing larger than a leap second.
73 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218161261Z"), &out)); 84 EXPECT_FALSE(
85 ParseGeneralizedTime(FromStringLiteral("20140218161261Z"), &out));
74 86
75 // Minutes only go up to 59. 87 // Minutes only go up to 59.
76 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218166000Z"), &out)); 88 EXPECT_FALSE(
89 ParseGeneralizedTime(FromStringLiteral("20140218166000Z"), &out));
77 90
78 // Hours only go up to 23. 91 // Hours only go up to 23.
79 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218240000Z"), &out)); 92 EXPECT_FALSE(
93 ParseGeneralizedTime(FromStringLiteral("20140218240000Z"), &out));
80 // The 0th day of a month is invalid. 94 // The 0th day of a month is invalid.
81 EXPECT_FALSE(ParseGeneralizedTime(Input("20140200161200Z"), &out)); 95 EXPECT_FALSE(
96 ParseGeneralizedTime(FromStringLiteral("20140200161200Z"), &out));
82 // The 0th month is invalid. 97 // The 0th month is invalid.
83 EXPECT_FALSE(ParseGeneralizedTime(Input("20140018161200Z"), &out)); 98 EXPECT_FALSE(
99 ParseGeneralizedTime(FromStringLiteral("20140018161200Z"), &out));
84 // Months greater than 12 are invalid. 100 // Months greater than 12 are invalid.
85 EXPECT_FALSE(ParseGeneralizedTime(Input("20141318161200Z"), &out)); 101 EXPECT_FALSE(
102 ParseGeneralizedTime(FromStringLiteral("20141318161200Z"), &out));
86 103
87 // Some months have 31 days. 104 // Some months have 31 days.
88 EXPECT_TRUE(ParseGeneralizedTime(Input("20140131000000Z"), &out)); 105 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20140131000000Z"), &out));
89 106
90 // September has only 30 days. 107 // September has only 30 days.
91 EXPECT_FALSE(ParseGeneralizedTime(Input("20140931000000Z"), &out)); 108 EXPECT_FALSE(
109 ParseGeneralizedTime(FromStringLiteral("20140931000000Z"), &out));
92 110
93 // February has only 28 days... 111 // February has only 28 days...
94 EXPECT_FALSE(ParseGeneralizedTime(Input("20140229000000Z"), &out)); 112 EXPECT_FALSE(
113 ParseGeneralizedTime(FromStringLiteral("20140229000000Z"), &out));
95 114
96 // ... unless it's a leap year. 115 // ... unless it's a leap year.
97 EXPECT_TRUE(ParseGeneralizedTime(Input("20160229000000Z"), &out)); 116 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20160229000000Z"), &out));
98 117
99 // There aren't any leap days in years divisible by 100... 118 // There aren't any leap days in years divisible by 100...
100 EXPECT_FALSE(ParseGeneralizedTime(Input("21000229000000Z"), &out)); 119 EXPECT_FALSE(
120 ParseGeneralizedTime(FromStringLiteral("21000229000000Z"), &out));
101 121
102 // ...unless it's also divisible by 400. 122 // ...unless it's also divisible by 400.
103 EXPECT_TRUE(ParseGeneralizedTime(Input("20000229000000Z"), &out)); 123 EXPECT_TRUE(ParseGeneralizedTime(FromStringLiteral("20000229000000Z"), &out));
104 124
105 // Check more perverse invalid inputs. 125 // Check more perverse invalid inputs.
106 126
107 const uint8_t trailing_null_bytes[] = {'2', 127 // Check that trailing null bytes are not ignored.
108 '0', 128 EXPECT_FALSE(
109 '0', 129 ParseGeneralizedTime(FromStringLiteral("20001231010203Z\0"), &out));
110 '0', 130
111 '1', 131 // Check what happens when a null byte is in the middle of the input.
112 '2', 132 EXPECT_FALSE(ParseGeneralizedTime(FromStringLiteral(
113 '3', 133 "200\0"
114 '1', 134 "1231010203Z"),
115 '0', 135 &out));
116 '1',
117 '0',
118 '2',
119 '0',
120 '3',
121 'Z',
122 '\0'};
123 Input trailing_null(trailing_null_bytes, sizeof(trailing_null_bytes));
124 EXPECT_FALSE(ParseGeneralizedTime(trailing_null, &out));
125 const uint8_t embedded_null_bytes[] = {'2',
126 '0',
127 '0',
128 '\0',
129 '1',
130 '2',
131 '3',
132 '1',
133 '0',
134 '1',
135 '0',
136 '2',
137 '0',
138 '3',
139 'Z'};
140 Input embedded_null(embedded_null_bytes, sizeof(embedded_null_bytes));
141 EXPECT_FALSE(ParseGeneralizedTime(embedded_null, &out));
142 136
143 // The year can't be in hex. 137 // The year can't be in hex.
144 EXPECT_FALSE(ParseGeneralizedTime(Input("0x201231000000Z"), &out)); 138 EXPECT_FALSE(
139 ParseGeneralizedTime(FromStringLiteral("0x201231000000Z"), &out));
145 140
146 // The last byte must be 'Z'. 141 // The last byte must be 'Z'.
147 EXPECT_FALSE(ParseGeneralizedTime(Input("20001231000000X"), &out)); 142 EXPECT_FALSE(
143 ParseGeneralizedTime(FromStringLiteral("20001231000000X"), &out));
148 144
149 // Check that the length is validated. 145 // Check that the length is validated.
150 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218161200"), &out)); 146 EXPECT_FALSE(ParseGeneralizedTime(FromStringLiteral("20140218161200"), &out));
151 EXPECT_FALSE(ParseGeneralizedTime(Input("20140218161200Z0"), &out)); 147 EXPECT_FALSE(
148 ParseGeneralizedTime(FromStringLiteral("20140218161200Z0"), &out));
152 } 149 }
153 150
154 TEST(ParseValuesTest, TimesCompare) { 151 TEST(ParseValuesTest, TimesCompare) {
155 GeneralizedTime time1; 152 GeneralizedTime time1;
156 GeneralizedTime time2; 153 GeneralizedTime time2;
157 GeneralizedTime time3; 154 GeneralizedTime time3;
158 155
159 ASSERT_TRUE(ParseGeneralizedTime(Input("20140218161200Z"), &time1)); 156 ASSERT_TRUE(
160 ASSERT_TRUE(ParseUTCTime(Input("150218161200Z"), &time2)); 157 ParseGeneralizedTime(FromStringLiteral("20140218161200Z"), &time1));
161 ASSERT_TRUE(ParseGeneralizedTime(Input("20160218161200Z"), &time3)); 158 ASSERT_TRUE(ParseUTCTime(FromStringLiteral("150218161200Z"), &time2));
159 ASSERT_TRUE(
160 ParseGeneralizedTime(FromStringLiteral("20160218161200Z"), &time3));
162 EXPECT_TRUE(time1 < time2); 161 EXPECT_TRUE(time1 < time2);
163 EXPECT_TRUE(time2 < time3); 162 EXPECT_TRUE(time2 < time3);
164 EXPECT_TRUE(time1 < time3); 163 EXPECT_TRUE(time1 < time3);
165 } 164 }
166 165
167 struct Uint64TestData { 166 struct Uint64TestData {
168 bool should_pass; 167 bool should_pass;
169 const uint8_t input[9]; 168 const uint8_t input[9];
170 size_t length; 169 size_t length;
171 uint64_t expected_value; 170 uint64_t expected_value;
(...skipping 20 matching lines...) Expand all
192 EXPECT_EQ(test_case.should_pass, 191 EXPECT_EQ(test_case.should_pass,
193 ParseUint64(Input(test_case.input, test_case.length), &result)); 192 ParseUint64(Input(test_case.input, test_case.length), &result));
194 if (test_case.should_pass) 193 if (test_case.should_pass)
195 EXPECT_EQ(test_case.expected_value, result); 194 EXPECT_EQ(test_case.expected_value, result);
196 } 195 }
197 } 196 }
198 197
199 } // namespace test 198 } // namespace test
200 } // namespace der 199 } // namespace der
201 } // namespace net 200 } // namespace net
OLDNEW
« no previous file with comments | « net/der/input_unittest.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698