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

Unified 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « components/gcm_driver/crypto/message_payload_parser.cc ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: components/gcm_driver/crypto/message_payload_parser_unittest.cc
diff --git a/components/gcm_driver/crypto/message_payload_parser_unittest.cc b/components/gcm_driver/crypto/message_payload_parser_unittest.cc
new file mode 100644
index 0000000000000000000000000000000000000000..e0fdb822124ea641e602a5d9c98ecc83d5fac4e8
--- /dev/null
+++ b/components/gcm_driver/crypto/message_payload_parser_unittest.cc
@@ -0,0 +1,103 @@
+// Copyright 2017 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "components/gcm_driver/crypto/message_payload_parser.h"
+
+#include "base/big_endian.h"
+#include "testing/gtest/include/gtest/gtest.h"
+
+namespace gcm {
+
+namespace {
+
+constexpr size_t kSaltSize = 16;
+constexpr size_t kPublicKeySize = 65;
+constexpr size_t kCiphertextSize = 18;
+
+const uint8_t kValidMessage[] = {
+ // salt (16 bytes, kSaltSize)
+ 0x59, 0xFD, 0x35, 0x97, 0x3B, 0xF3, 0x66, 0xA7, 0xEB, 0x8D, 0x44, 0x1E,
+ 0xCB, 0x4D, 0xFC, 0xD8,
+ // rs (4 bytes, in network byte order)
+ 0x00, 0x00, 0x00, 0x12,
+ // idlen (1 byte)
+ 0x41,
+ // public key (65 bytes, kPublicKeySize)
+ 0x04, 0x35, 0x02, 0x67, 0xB9, 0x10, 0x8F, 0x9B, 0xF1, 0x85, 0xF5, 0x1B,
+ 0xD7, 0xA4, 0xEF, 0xBD, 0x28, 0xB3, 0x11, 0x40, 0xBA, 0xD0, 0xEE, 0xB2,
+ 0x97, 0xDA, 0x6A, 0x93, 0x2D, 0x26, 0x45, 0xBD, 0xB2, 0x9A, 0x9F, 0xB8,
+ 0x19, 0xD8, 0x21, 0x6F, 0x66, 0xE3, 0xF6, 0x0B, 0x74, 0xB2, 0x28, 0x38,
+ 0xDC, 0xA7, 0x8A, 0x58, 0x0D, 0x56, 0x47, 0x3E, 0xD0, 0x5B, 0x5C, 0x93,
+ 0x4E, 0xB3, 0x89, 0x87, 0x64,
+ // payload (18 bytes, kCiphertextSize)
+ 0x3F, 0xD8, 0x95, 0x2C, 0xA2, 0x11, 0xBD, 0x7B, 0x57, 0xB2, 0x00, 0xBD,
+ 0x57, 0x68, 0x3F, 0xF0, 0x14, 0x57};
+
+static_assert(arraysize(kValidMessage) == 104,
+ "The smallest valid message is 104 bytes in size.");
+
+// Creates an std::string for the |kValidMessage| constant.
+std::string CreateMessageString() {
+ return std::string(reinterpret_cast<const char*>(kValidMessage),
+ arraysize(kValidMessage));
+}
+
+TEST(MessagePayloadParserTest, ValidMessage) {
+ MessagePayloadParser parser(CreateMessageString());
+ ASSERT_TRUE(parser.IsValid());
+
+ const uint8_t* salt = kValidMessage;
+
+ ASSERT_EQ(parser.salt().size(), kSaltSize);
+ EXPECT_EQ(parser.salt(), std::string(salt, salt + kSaltSize));
+
+ ASSERT_EQ(parser.record_size(), 18u);
+
+ const uint8_t* public_key =
+ kValidMessage + kSaltSize + sizeof(uint32_t) + sizeof(uint8_t);
+
+ ASSERT_EQ(parser.public_key().size(), kPublicKeySize);
+ EXPECT_EQ(parser.public_key(),
+ std::string(public_key, public_key + kPublicKeySize));
+
+ const uint8_t* ciphertext = kValidMessage + kSaltSize + sizeof(uint32_t) +
+ sizeof(uint8_t) + kPublicKeySize;
+
+ ASSERT_EQ(parser.ciphertext().size(), kCiphertextSize);
+ EXPECT_EQ(parser.ciphertext(),
+ std::string(ciphertext, ciphertext + kCiphertextSize));
+}
+
+TEST(MessagePayloadParserTest, MinimumMessageSize) {
+ std::string message = CreateMessageString();
+ message.resize(arraysize(kValidMessage) / 2);
+
+ MessagePayloadParser parser(message);
+ EXPECT_FALSE(parser.IsValid());
+}
+
+TEST(MessagePayloadParserTest, MinimumRecordSize) {
+ std::string message = CreateMessageString();
+
+ uint32_t invalid_record_size = 11;
+ base::WriteBigEndian(&message[0] + 16 /* salt */, invalid_record_size);
+
+ MessagePayloadParser parser(message);
+ EXPECT_FALSE(parser.IsValid());
+}
+
+TEST(MessagePayloadParserTest, InvalidPublicKey) {
+ std::string message = CreateMessageString();
+
+ uint8_t invalid_public_key_size = 42;
+ base::WriteBigEndian(&message[0] + 16 /* salt */ + 4 /* rs */,
+ invalid_public_key_size);
+
+ MessagePayloadParser parser(message);
+ EXPECT_FALSE(parser.IsValid());
+}
+
+} // namespace
+
+} // namespace gcm
« 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