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

Side by Side Diff: components/gcm_driver/crypto/message_payload_parser_unittest.cc

Issue 2888763006: Add a parser for messages with a Web Push Protocol-based payload (Closed)
Patch Set: rebase Created 3 years, 7 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 | « components/gcm_driver/crypto/message_payload_parser.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
(Empty)
1 // Copyright 2017 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/message_payload_parser.h"
6
7 #include "base/big_endian.h"
8 #include "testing/gtest/include/gtest/gtest.h"
9
10 namespace gcm {
11
12 namespace {
13
14 constexpr size_t kSaltSize = 16;
15 constexpr size_t kPublicKeySize = 65;
16 constexpr size_t kCiphertextSize = 18;
17
18 const uint8_t kValidMessage[] = {
19 // salt (16 bytes, kSaltSize)
20 0x59, 0xFD, 0x35, 0x97, 0x3B, 0xF3, 0x66, 0xA7, 0xEB, 0x8D, 0x44, 0x1E,
21 0xCB, 0x4D, 0xFC, 0xD8,
22 // rs (4 bytes, in network byte order)
23 0x00, 0x00, 0x00, 0x12,
24 // idlen (1 byte)
25 0x41,
26 // public key (65 bytes, kPublicKeySize)
27 0x04, 0x35, 0x02, 0x67, 0xB9, 0x10, 0x8F, 0x9B, 0xF1, 0x85, 0xF5, 0x1B,
28 0xD7, 0xA4, 0xEF, 0xBD, 0x28, 0xB3, 0x11, 0x40, 0xBA, 0xD0, 0xEE, 0xB2,
29 0x97, 0xDA, 0x6A, 0x93, 0x2D, 0x26, 0x45, 0xBD, 0xB2, 0x9A, 0x9F, 0xB8,
30 0x19, 0xD8, 0x21, 0x6F, 0x66, 0xE3, 0xF6, 0x0B, 0x74, 0xB2, 0x28, 0x38,
31 0xDC, 0xA7, 0x8A, 0x58, 0x0D, 0x56, 0x47, 0x3E, 0xD0, 0x5B, 0x5C, 0x93,
32 0x4E, 0xB3, 0x89, 0x87, 0x64,
33 // payload (18 bytes, kCiphertextSize)
34 0x3F, 0xD8, 0x95, 0x2C, 0xA2, 0x11, 0xBD, 0x7B, 0x57, 0xB2, 0x00, 0xBD,
35 0x57, 0x68, 0x3F, 0xF0, 0x14, 0x57};
36
37 static_assert(arraysize(kValidMessage) == 104,
38 "The smallest valid message is 104 bytes in size.");
39
40 // Creates an std::string for the |kValidMessage| constant.
41 std::string CreateMessageString() {
42 return std::string(reinterpret_cast<const char*>(kValidMessage),
43 arraysize(kValidMessage));
44 }
45
46 TEST(MessagePayloadParserTest, ValidMessage) {
47 MessagePayloadParser parser(CreateMessageString());
48 ASSERT_TRUE(parser.IsValid());
49
50 const uint8_t* salt = kValidMessage;
51
52 ASSERT_EQ(parser.salt().size(), kSaltSize);
53 EXPECT_EQ(parser.salt(), std::string(salt, salt + kSaltSize));
54
55 ASSERT_EQ(parser.record_size(), 18u);
56
57 const uint8_t* public_key =
58 kValidMessage + kSaltSize + sizeof(uint32_t) + sizeof(uint8_t);
59
60 ASSERT_EQ(parser.public_key().size(), kPublicKeySize);
61 EXPECT_EQ(parser.public_key(),
62 std::string(public_key, public_key + kPublicKeySize));
63
64 const uint8_t* ciphertext = kValidMessage + kSaltSize + sizeof(uint32_t) +
65 sizeof(uint8_t) + kPublicKeySize;
66
67 ASSERT_EQ(parser.ciphertext().size(), kCiphertextSize);
68 EXPECT_EQ(parser.ciphertext(),
69 std::string(ciphertext, ciphertext + kCiphertextSize));
70 }
71
72 TEST(MessagePayloadParserTest, MinimumMessageSize) {
73 std::string message = CreateMessageString();
74 message.resize(arraysize(kValidMessage) / 2);
75
76 MessagePayloadParser parser(message);
77 EXPECT_FALSE(parser.IsValid());
78 }
79
80 TEST(MessagePayloadParserTest, MinimumRecordSize) {
81 std::string message = CreateMessageString();
82
83 uint32_t invalid_record_size = 11;
84 base::WriteBigEndian(&message[0] + 16 /* salt */, invalid_record_size);
85
86 MessagePayloadParser parser(message);
87 EXPECT_FALSE(parser.IsValid());
88 }
89
90 TEST(MessagePayloadParserTest, InvalidPublicKey) {
91 std::string message = CreateMessageString();
92
93 uint8_t invalid_public_key_size = 42;
94 base::WriteBigEndian(&message[0] + 16 /* salt */ + 4 /* rs */,
95 invalid_public_key_size);
96
97 MessagePayloadParser parser(message);
98 EXPECT_FALSE(parser.IsValid());
99 }
100
101 } // namespace
102
103 } // namespace gcm
OLDNEW
« no previous file with comments | « components/gcm_driver/crypto/message_payload_parser.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698