OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/gcm_driver/crypto/encryption_header_parsers.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/macros.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" |
| 11 |
| 12 namespace gcm { |
| 13 |
| 14 namespace { |
| 15 |
| 16 // URL-safe base64 encoded (and plaintext representation of -) salt, key and dh. |
| 17 // Included as preprocessor string literals for more convenient concatenation. |
| 18 #define ENCODED_SALT "c2l4dGVlbmNvb2xieXRlcw" |
| 19 #define ENCODED_KEY "c2V2ZW50ZWVuYnl0ZXN5YXk" |
| 20 #define ENCODED_DH "bXlkaWZmaWVoZWxsbWFu" |
| 21 |
| 22 const char kDecodedSalt[] = "sixteencoolbytes"; |
| 23 const char kDecodedKey[] = "seventeenbytesyay"; |
| 24 const char kDecodedDh[] = "mydiffiehellman"; |
| 25 |
| 26 EncryptionHeaderValue CreateEncryptionHeaderValue( |
| 27 const std::string& keyid, const std::string& salt, int64_t rs) { |
| 28 EncryptionHeaderValue parsed; |
| 29 parsed.keyid = keyid; |
| 30 parsed.salt = salt; |
| 31 parsed.rs = rs; |
| 32 |
| 33 return parsed; |
| 34 } |
| 35 |
| 36 EncryptionKeyHeaderValue CreateEncryptionKeyHeaderValue( |
| 37 const std::string& keyid, const std::string& key, const std::string& dh) { |
| 38 EncryptionKeyHeaderValue header; |
| 39 header.keyid = keyid; |
| 40 header.key = key; |
| 41 header.dh = dh; |
| 42 |
| 43 return header; |
| 44 } |
| 45 |
| 46 TEST(EncryptionHeaderParsersTest, ParseSingleEncryptionHeaders) { |
| 47 struct { |
| 48 const char* const header; |
| 49 EncryptionHeaderValue parsed; |
| 50 } expected_results[] = { |
| 51 { "keyid=foo; salt=" ENCODED_SALT "; rs=1024", |
| 52 CreateEncryptionHeaderValue("foo", kDecodedSalt, 1024) }, |
| 53 { "KEYID=foo; SALT=" ENCODED_SALT "; RS=1024", |
| 54 CreateEncryptionHeaderValue("foo", kDecodedSalt, 1024) }, |
| 55 { "keyid='foo'; salt='" ENCODED_SALT "'; rs='1024'", |
| 56 CreateEncryptionHeaderValue("foo", kDecodedSalt, 1024) }, |
| 57 { "keyid=\"foo\"; salt=\"" ENCODED_SALT "\"; rs=\"1024\"", |
| 58 CreateEncryptionHeaderValue("foo", kDecodedSalt, 1024) }, |
| 59 { "keyid=foo;salt=" ENCODED_SALT ";rs=1024", |
| 60 CreateEncryptionHeaderValue("foo", kDecodedSalt, 1024) }, |
| 61 { "keyid=foo;salt=" ENCODED_SALT ";rs=1024;random=yes", |
| 62 CreateEncryptionHeaderValue("foo", kDecodedSalt, 1024) }, |
| 63 { "rs=1024; salt=" ENCODED_SALT "; keyid=foo", |
| 64 CreateEncryptionHeaderValue("foo", kDecodedSalt, 1024) }, |
| 65 { "salt=" ENCODED_SALT "; keyid='foo; rs=1024'", |
| 66 CreateEncryptionHeaderValue("foo; rs=1024", kDecodedSalt, 4096) }, |
| 67 { "salt=" ENCODED_SALT "; rs=1024", |
| 68 CreateEncryptionHeaderValue("", kDecodedSalt, 1024) }, |
| 69 { "salt=" ENCODED_SALT, |
| 70 CreateEncryptionHeaderValue("", kDecodedSalt, 4096) } |
| 71 }; |
| 72 |
| 73 for (size_t i = 0; i < arraysize(expected_results); i++) { |
| 74 SCOPED_TRACE(i); |
| 75 |
| 76 std::vector<EncryptionHeaderValue> result; |
| 77 ASSERT_TRUE(ParseEncryptionHeader(expected_results[i].header, &result)); |
| 78 ASSERT_EQ(1u, result.size()); |
| 79 |
| 80 EXPECT_EQ(expected_results[i].parsed.keyid, result[0].keyid); |
| 81 EXPECT_EQ(expected_results[i].parsed.salt, result[0].salt); |
| 82 EXPECT_EQ(expected_results[i].parsed.rs, result[0].rs); |
| 83 } |
| 84 } |
| 85 |
| 86 TEST(EncryptionHeaderParsersTest, ParseInvalidSingleEncryptionHeaders) { |
| 87 const char* const expected_failures[] = { |
| 88 // The "salt" attribute is required. |
| 89 "keyid=foo", |
| 90 // The "salt" attribute must be exactly 16-bytes, base64url encoded. |
| 91 "salt=foobar", |
| 92 "salt='c2l4dGVlbmNv/2xieXRlcw'", |
| 93 // Non-decimal record sizes are not supported. |
| 94 "salt=" ENCODED_SALT ";rs=bar", |
| 95 // Record sizes smaller than 2 are not supported. |
| 96 "salt=" ENCODED_SALT ";rs=0", |
| 97 // Record sizes exceeding the range of int64 are not supported. |
| 98 "salt=" ENCODED_SALT ";rs=9223372036854775808" |
| 99 }; |
| 100 |
| 101 for (size_t i = 0; i < arraysize(expected_failures); i++) { |
| 102 SCOPED_TRACE(i); |
| 103 |
| 104 std::vector<EncryptionHeaderValue> result; |
| 105 ASSERT_FALSE(ParseEncryptionHeader(expected_failures[i], &result)); |
| 106 EXPECT_EQ(0u, result.size()); |
| 107 } |
| 108 } |
| 109 |
| 110 TEST(EncryptionHeaderParsersTest, ParseMultipleEncryptionHeaders) { |
| 111 const char* const header = |
| 112 "keyid=foo1; salt=" ENCODED_SALT "; rs=1024,," |
| 113 "keyid='foo2, '; salt=" ENCODED_SALT "; rs=2048"; |
| 114 |
| 115 std::vector<EncryptionHeaderValue> results; |
| 116 ASSERT_TRUE(ParseEncryptionHeader(header, &results)); |
| 117 ASSERT_EQ(2u, results.size()); |
| 118 |
| 119 EXPECT_EQ("foo1", results[0].keyid); |
| 120 EXPECT_EQ(kDecodedSalt, results[0].salt); |
| 121 EXPECT_EQ(1024, results[0].rs); |
| 122 |
| 123 EXPECT_EQ("foo2, ", results[1].keyid); |
| 124 EXPECT_EQ(kDecodedSalt, results[1].salt); |
| 125 EXPECT_EQ(2048, results[1].rs); |
| 126 } |
| 127 |
| 128 TEST(EncryptionHeaderParsersTest, ParseSingleEncryptionKeyHeader) { |
| 129 struct { |
| 130 const char* const header; |
| 131 EncryptionKeyHeaderValue parsed; |
| 132 } expected_results[] = { |
| 133 { "keyid=foo; key=" ENCODED_KEY "; dh=" ENCODED_DH, |
| 134 CreateEncryptionKeyHeaderValue("foo", kDecodedKey, kDecodedDh) }, |
| 135 { "KEYID=foo; KEY=" ENCODED_KEY "; DH=" ENCODED_DH, |
| 136 CreateEncryptionKeyHeaderValue("foo", kDecodedKey, kDecodedDh) }, |
| 137 { "keyid='foo'; key='" ENCODED_KEY "'; dh='" ENCODED_DH "'", |
| 138 CreateEncryptionKeyHeaderValue("foo", kDecodedKey, kDecodedDh) }, |
| 139 { "keyid=\"foo\"; key=\"" ENCODED_KEY "\"; dh=\"" ENCODED_DH "\"", |
| 140 CreateEncryptionKeyHeaderValue("foo", kDecodedKey, kDecodedDh) }, |
| 141 { "keyid=foo;key=" ENCODED_KEY ";dh=" ENCODED_DH "", |
| 142 CreateEncryptionKeyHeaderValue("foo", kDecodedKey, kDecodedDh) }, |
| 143 { "keyid=foo;key=" ENCODED_KEY ";dh=" ENCODED_DH ";random=yes", |
| 144 CreateEncryptionKeyHeaderValue("foo", kDecodedKey, kDecodedDh) }, |
| 145 { "dh=" ENCODED_DH "; key=" ENCODED_KEY "; keyid=foo", |
| 146 CreateEncryptionKeyHeaderValue("foo", kDecodedKey, kDecodedDh) }, |
| 147 { "keyid=foo", |
| 148 CreateEncryptionKeyHeaderValue("foo", "", "") }, |
| 149 { "key=" ENCODED_KEY "", |
| 150 CreateEncryptionKeyHeaderValue("", kDecodedKey, "") }, |
| 151 { "dh=" ENCODED_DH "", |
| 152 CreateEncryptionKeyHeaderValue("", "", kDecodedDh) } |
| 153 }; |
| 154 |
| 155 for (size_t i = 0; i < arraysize(expected_results); i++) { |
| 156 SCOPED_TRACE(i); |
| 157 |
| 158 std::vector<EncryptionKeyHeaderValue> result; |
| 159 ASSERT_TRUE(ParseEncryptionKeyHeader(expected_results[i].header, &result)); |
| 160 ASSERT_EQ(1u, result.size()); |
| 161 |
| 162 EXPECT_EQ(expected_results[i].parsed.keyid, result[0].keyid); |
| 163 EXPECT_EQ(expected_results[i].parsed.key, result[0].key); |
| 164 EXPECT_EQ(expected_results[i].parsed.dh, result[0].dh); |
| 165 } |
| 166 } |
| 167 |
| 168 TEST(EncryptionHeaderParsersTest, ParseInvalidSingleEncryptionKeyHeaders) { |
| 169 const char* const expected_failures[] = { |
| 170 // The "key" attribute must be at least 16-bytes, base64url encoded. |
| 171 "key=foo/bar", |
| 172 "key=c2hvcnRrZXk", |
| 173 "key='c2l4dGVlbmNvb2/ieXRlcw'", |
| 174 // The "dh" attribute must contain a base64url encoded-value. |
| 175 "dh=foo/bar", |
| 176 "dh='ZGg/'" |
| 177 }; |
| 178 |
| 179 for (size_t i = 0; i < arraysize(expected_failures); i++) { |
| 180 SCOPED_TRACE(i); |
| 181 |
| 182 std::vector<EncryptionKeyHeaderValue> result; |
| 183 ASSERT_FALSE(ParseEncryptionKeyHeader(expected_failures[i], &result)); |
| 184 EXPECT_EQ(0u, result.size()); |
| 185 } |
| 186 } |
| 187 |
| 188 TEST(EncryptionHeaderParsersTest, ParseMultipleEncryptionKeyHeaders) { |
| 189 const char* const header = |
| 190 "keyid=foo1; key=" ENCODED_KEY "; dh=" ENCODED_DH "," |
| 191 "keyid='foo2, '; key=c29tZW90aGVybG9uZ2tleQ; dh=ZGhmb29iYXI"; |
| 192 |
| 193 std::vector<EncryptionKeyHeaderValue> results; |
| 194 ASSERT_TRUE(ParseEncryptionKeyHeader(header, &results)); |
| 195 ASSERT_EQ(2u, results.size()); |
| 196 |
| 197 EXPECT_EQ("foo1", results[0].keyid); |
| 198 EXPECT_EQ(kDecodedKey, results[0].key); |
| 199 EXPECT_EQ(kDecodedDh, results[0].dh); |
| 200 |
| 201 EXPECT_EQ("foo2, ", results[1].keyid); |
| 202 EXPECT_EQ("someotherlongkey", results[1].key); |
| 203 EXPECT_EQ("dhfoobar", results[1].dh); |
| 204 } |
| 205 |
| 206 } // namespace |
| 207 |
| 208 } // namespace gcm |
OLD | NEW |