File: PushManagerTest.cpp

package info (click to toggle)
chromium-browser 57.0.2987.98-1~deb8u1
  • links: PTS, VCS
  • area: main
  • in suites: jessie
  • size: 2,637,852 kB
  • ctags: 2,544,394
  • sloc: cpp: 12,815,961; ansic: 3,676,222; python: 1,147,112; asm: 526,608; java: 523,212; xml: 286,794; perl: 92,654; sh: 86,408; objc: 73,271; makefile: 27,698; cs: 18,487; yacc: 13,031; tcl: 12,957; pascal: 4,875; ml: 4,716; lex: 3,904; sql: 3,862; ruby: 1,982; lisp: 1,508; php: 1,368; exp: 404; awk: 325; csh: 117; jsp: 39; sed: 37
file content (67 lines) | stat: -rw-r--r-- 2,841 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
// Copyright 2016 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 "modules/push_messaging/PushManager.h"

#include "core/dom/DOMArrayBuffer.h"
#include "modules/push_messaging/PushSubscriptionOptions.h"
#include "modules/push_messaging/PushSubscriptionOptionsInit.h"
#include "public/platform/WebString.h"
#include "public/platform/modules/push_messaging/WebPushSubscriptionOptions.h"
#include "testing/gtest/include/gtest/gtest.h"

namespace blink {
namespace {

const unsigned kMaxKeyLength = 255;

// NIST P-256 public key made available to tests. Must be an uncompressed
// point in accordance with SEC1 2.3.3.
const unsigned kApplicationServerKeyLength = 65;
const uint8_t kApplicationServerKey[kApplicationServerKeyLength] = {
    0x04, 0x55, 0x52, 0x6A, 0xA5, 0x6E, 0x8E, 0xAA, 0x47, 0x97, 0x36,
    0x10, 0xC1, 0x66, 0x3C, 0x1E, 0x65, 0xBF, 0xA1, 0x7B, 0xEE, 0x48,
    0xC9, 0xC6, 0xBB, 0xBF, 0x02, 0x18, 0x53, 0x72, 0x1D, 0x0C, 0x7B,
    0xA9, 0xE3, 0x11, 0xB7, 0x03, 0x52, 0x21, 0xD3, 0x71, 0x90, 0x13,
    0xA8, 0xC1, 0xCF, 0xED, 0x20, 0xF7, 0x1F, 0xD1, 0x7F, 0xF2, 0x76,
    0xB6, 0x01, 0x20, 0xD8, 0x35, 0xA5, 0xD9, 0x3C, 0x43, 0xDF};

TEST(PushManagerTest, ValidSenderKey) {
  PushSubscriptionOptionsInit options;
  options.setApplicationServerKey(
      ArrayBufferOrArrayBufferView::fromArrayBuffer(DOMArrayBuffer::create(
          kApplicationServerKey, kApplicationServerKeyLength)));

  DummyExceptionStateForTesting exceptionState;
  WebPushSubscriptionOptions output =
      PushSubscriptionOptions::toWeb(options, exceptionState);
  EXPECT_FALSE(exceptionState.hadException());
  EXPECT_EQ(output.applicationServerKey.length(), kApplicationServerKeyLength);

  // Copy the key into a size+1 buffer so that it can be treated as a null
  // terminated string for the purposes of EXPECT_EQ.
  uint8_t senderKey[kApplicationServerKeyLength + 1];
  for (unsigned i = 0; i < kApplicationServerKeyLength; i++)
    senderKey[i] = kApplicationServerKey[i];
  senderKey[kApplicationServerKeyLength] = 0x0;
  EXPECT_EQ(reinterpret_cast<const char*>(senderKey),
            output.applicationServerKey.latin1());
  EXPECT_FALSE(output.applicationServerKey.isEmpty());
}

TEST(PushManagerTest, InvalidSenderKeyLength) {
  uint8_t senderKey[kMaxKeyLength + 1];
  memset(senderKey, 0, sizeof(senderKey));
  PushSubscriptionOptionsInit options;
  options.setApplicationServerKey(ArrayBufferOrArrayBufferView::fromArrayBuffer(
      DOMArrayBuffer::create(senderKey, kMaxKeyLength + 1)));

  DummyExceptionStateForTesting exceptionState;
  WebPushSubscriptionOptions output =
      PushSubscriptionOptions::toWeb(options, exceptionState);
  EXPECT_TRUE(exceptionState.hadException());
}

}  // namespace
}  // namespace blink