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 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <stdint.h>
#include "base/strings/string_number_conversions.h"
#include "content/browser/notifications/notification_id_generator.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"
#include "url/origin.h"
namespace content {
namespace {
const int64_t kPersistentNotificationId = 430;
const char kExampleTag[] = "example";
class NotificationIdGeneratorTest : public ::testing::Test {
public:
NotificationIdGeneratorTest()
: origin_(url::Origin::Create(GURL("https://example.com"))) {}
protected:
url::Origin origin_;
NotificationIdGenerator generator_;
};
// -----------------------------------------------------------------------------
// Persistent notifications
// Two calls to the generator with exactly the same information should result
// in exactly the same notification ids being generated.
TEST_F(NotificationIdGeneratorTest, GenerateForPersistent_IsDetermenistic) {
EXPECT_EQ(generator_.GenerateForPersistentNotification(
origin_.GetURL(), kExampleTag, false /* is_shown_by_browser */,
kPersistentNotificationId),
generator_.GenerateForPersistentNotification(
origin_.GetURL(), kExampleTag, false /* is_shown_by_browser */,
kPersistentNotificationId));
EXPECT_EQ(generator_.GenerateForPersistentNotification(
origin_.GetURL(), "" /* tag */, false /* is_shown_by_browser */,
kPersistentNotificationId),
generator_.GenerateForPersistentNotification(
origin_.GetURL(), "" /* tag */, false /* is_shown_by_browser */,
kPersistentNotificationId));
}
// The origin of the notification will impact the generated notification id.
TEST_F(NotificationIdGeneratorTest, GenerateForPersistent_DifferentOrigins) {
url::Origin different_origin(
url::Origin::Create(GURL("https://example2.com")));
EXPECT_NE(generator_.GenerateForPersistentNotification(
origin_.GetURL(), kExampleTag, false /* is_shown_by_browser */,
kPersistentNotificationId),
generator_.GenerateForPersistentNotification(
different_origin.GetURL(), kExampleTag,
false /* is_shown_by_browser */, kPersistentNotificationId));
}
// The tag, when non-empty, will impact the generated notification id.
TEST_F(NotificationIdGeneratorTest, GenerateForPersistent_DifferentTags) {
const std::string& different_tag = std::string(kExampleTag) + "2";
EXPECT_NE(generator_.GenerateForPersistentNotification(
origin_.GetURL(), kExampleTag, false /* is_shown_by_browser */,
kPersistentNotificationId),
generator_.GenerateForPersistentNotification(
origin_.GetURL(), different_tag,
false /* is_shown_by_browser */, kPersistentNotificationId));
}
// The persistent or non-persistent notification id will impact the generated
// notification id when the tag is empty.
TEST_F(NotificationIdGeneratorTest, GenerateForPersistent_DifferentIds) {
EXPECT_NE(generator_.GenerateForPersistentNotification(
origin_.GetURL(), "" /* tag */, false /* is_shown_by_browser */,
kPersistentNotificationId),
generator_.GenerateForPersistentNotification(
origin_.GetURL(), "" /* tag */, false /* is_shown_by_browser */,
kPersistentNotificationId + 1));
}
// Using a numeric tag that could resemble a persistent notification id should
// not be equal to a notification without a tag, but with that id.
TEST_F(NotificationIdGeneratorTest, GenerateForPersistent_NumericTagAmbiguity) {
EXPECT_NE(
generator_.GenerateForPersistentNotification(
origin_.GetURL(), base::NumberToString(kPersistentNotificationId),
false /* is_shown_by_browser */, kPersistentNotificationId),
generator_.GenerateForPersistentNotification(
origin_.GetURL(), "" /* tag */, false /* is_shown_by_browser */,
kPersistentNotificationId));
}
// Using port numbers and a tag which, when concatenated, could end up being
// equal to each other if origins stop ending with slashes.
TEST_F(NotificationIdGeneratorTest, GenerateForPersistent_OriginPortAmbiguity) {
GURL origin_805("https://example.com:805");
GURL origin_8051("https://example.com:8051");
EXPECT_NE(generator_.GenerateForPersistentNotification(
origin_805, "17", false /* is_shown_by_browser */,
kPersistentNotificationId),
generator_.GenerateForPersistentNotification(
origin_8051, "7", false /* is_shown_by_browser */,
kPersistentNotificationId));
}
// -----------------------------------------------------------------------------
// Non-persistent notifications
TEST_F(NotificationIdGeneratorTest, GenerateForNonPersistent_IsDeterministic) {
EXPECT_EQ(
generator_.GenerateForNonPersistentNotification(origin_, "example-token"),
generator_.GenerateForNonPersistentNotification(origin_,
"example-token"));
}
TEST_F(NotificationIdGeneratorTest, GenerateForNonPersistent_DifferentOrigins) {
url::Origin different_origin(
url::Origin::Create(GURL("https://example2.com")));
EXPECT_NE(
generator_.GenerateForNonPersistentNotification(origin_, "example-token"),
generator_.GenerateForNonPersistentNotification(different_origin,
"example-token"));
}
TEST_F(NotificationIdGeneratorTest, GenerateForNonPersistent_DifferentTokens) {
EXPECT_NE(
generator_.GenerateForNonPersistentNotification(origin_, "example-token"),
generator_.GenerateForNonPersistentNotification(origin_, "other-token"));
}
// Use port numbers and a token which, when concatenated, could end up being
// equal to each other if origins stop ending with slashes.
TEST_F(NotificationIdGeneratorTest,
GenerateForNonPersistent_OriginPortAmbiguity) {
auto origin_805(url::Origin::Create(GURL("https://example.com:805")));
auto origin_8051(url::Origin::Create(GURL("https://example.com:8051")));
EXPECT_NE(generator_.GenerateForNonPersistentNotification(origin_805, "17"),
generator_.GenerateForNonPersistentNotification(origin_8051, "7"));
}
// -----------------------------------------------------------------------------
// Both persistent and non-persistent notifications.
// Verifies that the static Is(Non)PersistentNotification helpers can correctly
// identify persistent and non-persistent notification IDs.
TEST_F(NotificationIdGeneratorTest, DetectIdFormat) {
std::string persistent_id = generator_.GenerateForPersistentNotification(
origin_.GetURL(), "" /* tag */, false /* is_shown_by_browser */,
kPersistentNotificationId);
std::string non_persistent_id =
generator_.GenerateForNonPersistentNotification(origin_, "token");
EXPECT_TRUE(NotificationIdGenerator::IsPersistentNotification(persistent_id));
EXPECT_FALSE(
NotificationIdGenerator::IsNonPersistentNotification(persistent_id));
EXPECT_TRUE(
NotificationIdGenerator::IsNonPersistentNotification(non_persistent_id));
EXPECT_FALSE(
NotificationIdGenerator::IsPersistentNotification(non_persistent_id));
}
TEST_F(NotificationIdGeneratorTest, ShownByBrowser) {
std::string id = generator_.GenerateForPersistentNotification(
origin_.GetURL(), "" /* tag */, true /* is_shown_by_browser */,
kPersistentNotificationId);
// We should still be able to detect browser notifications as persistent.
EXPECT_TRUE(NotificationIdGenerator::IsPersistentNotification(id));
EXPECT_FALSE(NotificationIdGenerator::IsNonPersistentNotification(id));
// Browser vs non-browser ids should be different.
EXPECT_NE(generator_.GenerateForPersistentNotification(
origin_.GetURL(), kExampleTag, false /* is_shown_by_browser */,
kPersistentNotificationId),
generator_.GenerateForPersistentNotification(
origin_.GetURL(), kExampleTag, true /* is_shown_by_browser */,
kPersistentNotificationId));
}
} // namespace
} // namespace content
|