Bug 1552536 - add dummy page load origin; r=Ehsan,chutten

Differential Revision: https://phabricator.services.mozilla.com/D31666

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Liang-Heng Chen 2019-05-25 17:21:26 +00:00
Родитель 38a4c733c3
Коммит f784420dff
4 изменённых файлов: 78 добавлений и 63 удалений

Просмотреть файл

@ -23,6 +23,9 @@ typedef mozilla::Telemetry::OriginMetricID OriginMetricID;
namespace mozilla {
namespace dom {
// sync with TelemetryOriginData.inc
NS_NAMED_LITERAL_CSTRING(ContentBlockingLog::kDummyOriginHash, "PAGELOAD");
// randomly choose 1% users included in the content blocking measurement
// based on their client id.
static constexpr double kRatioReportUser = 0.01;
@ -113,6 +116,12 @@ void ContentBlockingLog::ReportLog() {
return;
}
LOG("ContentBlockingLog::ReportLog [this=%p]", this);
const bool testMode =
StaticPrefs::telemetry_origin_telemetry_test_mode_enabled();
OriginMetricID metricId =
testMode ? OriginMetricID::ContentBlocking_Blocked_TestOnly
: OriginMetricID::ContentBlocking_Blocked;
ReportOriginSingleHash(metricId, kDummyOriginHash);
nsTArray<HashNumber> lookupTable;
for (const auto& originEntry : mLog) {
@ -129,12 +138,9 @@ void ContentBlockingLog::ReportLog() {
const bool isBlocked = logEntry.mBlocked;
Maybe<StorageAccessGrantedReason> reason = logEntry.mReason;
const bool testMode =
StaticPrefs::telemetry_origin_telemetry_test_mode_enabled();
OriginMetricID metricId =
testMode ? OriginMetricID::ContentBlocking_Blocked_TestOnly
: OriginMetricID::ContentBlocking_Blocked;
metricId = testMode ? OriginMetricID::ContentBlocking_Blocked_TestOnly
: OriginMetricID::ContentBlocking_Blocked;
if (!isBlocked) {
MOZ_ASSERT(reason.isSome());
switch (reason.value()) {

Просмотреть файл

@ -71,6 +71,8 @@ class ContentBlockingLog final {
};
public:
static const nsLiteralCString kDummyOriginHash;
ContentBlockingLog() = default;
~ContentBlockingLog() = default;

Просмотреть файл

@ -1,3 +1,6 @@
// dummy origin, this is used to be a counter of page loads.
ORIGIN("PAGELOAD", "PAGELOAD")
ORIGIN("advertstream.com", "lzPiT1FuoHNMKQ1Hw8AaTi68TokOB24ciFBqmCk62ek=")
ORIGIN("kitaramedia.com", "r+U9PL3uMrjCKe8/T8goY9MHPA+6JckC3R+/1R9TQKA=")
ORIGIN("questionmarket.com", "3KCO/qN+KmApmfH3RaXAmdR65Z/TRfrr6pds7aDKn1c=")

Просмотреть файл

@ -6,6 +6,7 @@
#include "core/TelemetryOrigin.h"
#include "gtest/gtest.h"
#include "gmock/gmock.h"
#include "mozilla/dom/ContentBlockingLog.h"
#include "mozilla/Services.h"
#include "mozilla/Telemetry.h"
#include "mozilla/Unused.h"
@ -20,6 +21,25 @@ using ::testing::_;
using ::testing::AtLeast;
using ::testing::StrEq;
NS_NAMED_LITERAL_CSTRING(kTelemetryTest1Metric, "telemetry.test_test1");
NS_NAMED_LITERAL_CSTRING(kDoubleclickOrigin, "doubleclick.net");
NS_NAMED_LITERAL_CSTRING(kDoubleclickOriginHash,
"uXNT1PzjAVau8b402OMAIGDejKbiXfQX5iXvPASfO/s=");
NS_NAMED_LITERAL_CSTRING(kFacebookOrigin, "fb.com");
NS_NAMED_LITERAL_CSTRING(kUnknownOrigin1,
"this origin isn't known to Origin Telemetry");
NS_NAMED_LITERAL_CSTRING(kUnknownOrigin2, "neither is this one");
// Properly prepare the prio prefs
// (Sourced from PrioEncoder.cpp from when it was being prototyped)
NS_NAMED_LITERAL_CSTRING(
prioKeyA,
"35AC1C7576C7C6EDD7FED6BCFC337B34D48CB4EE45C86BEEFB40BD8875707733");
NS_NAMED_LITERAL_CSTRING(
prioKeyB,
"26E6674E65425B823F1F1D5F96E3BB3EF9E406EC7FBA7DEF8B08A35DD135AF50");
// Test that we can properly record origin stuff using the C++ API.
TEST_F(TelemetryTestFixture, RecordOrigin) {
AutoJSContextWithGlobal cx(mCleanGlobal);
@ -27,10 +47,8 @@ TEST_F(TelemetryTestFixture, RecordOrigin) {
Unused << mTelemetry->ClearOrigins();
const nsLiteralCString doubleclick("doubleclick.net");
const nsLiteralCString telemetryTest1("telemetry.test_test1");
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1, doubleclick);
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1,
mozilla::dom::ContentBlockingLog::kDummyOriginHash);
JS::RootedValue originSnapshot(aCx);
GetOriginSnapshot(aCx, &originSnapshot);
@ -40,12 +58,15 @@ TEST_F(TelemetryTestFixture, RecordOrigin) {
JS::RootedValue origins(aCx);
JS::RootedObject snapshotObj(aCx, &originSnapshot.toObject());
ASSERT_TRUE(JS_GetProperty(aCx, snapshotObj, telemetryTest1.get(), &origins))
ASSERT_TRUE(
JS_GetProperty(aCx, snapshotObj, kTelemetryTest1Metric.get(), &origins))
<< "telemetry.test_test1 must be in the snapshot.";
JS::RootedObject originsObj(aCx, &origins.toObject());
JS::RootedValue count(aCx);
ASSERT_TRUE(JS_GetProperty(aCx, originsObj, doubleclick.get(), &count));
ASSERT_TRUE(JS_GetProperty(
aCx, originsObj, mozilla::dom::ContentBlockingLog::kDummyOriginHash.get(),
&count));
ASSERT_TRUE(count.isInt32() && count.toInt32() == 1)
<< "Must have recorded the origin exactly once.";
@ -64,11 +85,10 @@ TEST_F(TelemetryTestFixture, RecordOriginTwiceAndClear) {
Unused << mTelemetry->ClearOrigins();
const nsLiteralCString doubleclick("doubleclick.net");
const nsLiteralCString telemetryTest1("telemetry.test_test1");
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1, doubleclick);
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1, doubleclick);
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1,
kDoubleclickOrigin);
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1,
kDoubleclickOrigin);
JS::RootedValue originSnapshot(aCx);
GetOriginSnapshot(aCx, &originSnapshot, true /* aClear */);
@ -78,12 +98,14 @@ TEST_F(TelemetryTestFixture, RecordOriginTwiceAndClear) {
JS::RootedValue origins(aCx);
JS::RootedObject snapshotObj(aCx, &originSnapshot.toObject());
ASSERT_TRUE(JS_GetProperty(aCx, snapshotObj, telemetryTest1.get(), &origins))
ASSERT_TRUE(
JS_GetProperty(aCx, snapshotObj, kTelemetryTest1Metric.get(), &origins))
<< "telemetry.test_test1 must be in the snapshot.";
JS::RootedObject originsObj(aCx, &origins.toObject());
JS::RootedValue count(aCx);
ASSERT_TRUE(JS_GetProperty(aCx, originsObj, doubleclick.get(), &count));
ASSERT_TRUE(
JS_GetProperty(aCx, originsObj, kDoubleclickOrigin.get(), &count));
ASSERT_TRUE(count.isInt32() && count.toInt32() == 2)
<< "Must have recorded the origin exactly twice.";
@ -102,26 +124,17 @@ TEST_F(TelemetryTestFixture, RecordOriginTwiceMixed) {
Unused << mTelemetry->ClearOrigins();
const nsLiteralCString doubleclick("doubleclick.net");
const nsLiteralCString doubleclickHash(
"uXNT1PzjAVau8b402OMAIGDejKbiXfQX5iXvPASfO/s=");
const nsLiteralCString telemetryTest1("telemetry.test_test1");
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1,
kDoubleclickOrigin);
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1,
kDoubleclickOriginHash);
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1, doubleclick);
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1, doubleclickHash);
// Properly prepare the prio prefs
// (Sourced from PrioEncoder.cpp from when it was being prototyped)
const nsLiteralCString prioKeyA(
"35AC1C7576C7C6EDD7FED6BCFC337B34D48CB4EE45C86BEEFB40BD8875707733");
const nsLiteralCString prioKeyB(
"26E6674E65425B823F1F1D5F96E3BB3EF9E406EC7FBA7DEF8B08A35DD135AF50");
Preferences::SetCString("prio.publicKeyA", prioKeyA);
Preferences::SetCString("prio.publicKeyB", prioKeyB);
nsTArray<Tuple<nsCString, nsCString>> encodedStrings;
GetEncodedOriginStrings(aCx, telemetryTest1 + NS_LITERAL_CSTRING("-%u"),
encodedStrings);
GetEncodedOriginStrings(
aCx, kTelemetryTest1Metric + NS_LITERAL_CSTRING("-%u"), encodedStrings);
ASSERT_EQ(2 * TelemetryOrigin::SizeOfPrioDatasPerMetric(),
encodedStrings.Length());
@ -133,12 +146,14 @@ TEST_F(TelemetryTestFixture, RecordOriginTwiceMixed) {
JS::RootedValue origins(aCx);
JS::RootedObject snapshotObj(aCx, &originSnapshot.toObject());
ASSERT_TRUE(JS_GetProperty(aCx, snapshotObj, telemetryTest1.get(), &origins))
ASSERT_TRUE(
JS_GetProperty(aCx, snapshotObj, kTelemetryTest1Metric.get(), &origins))
<< "telemetry.test_test1 must be in the snapshot.";
JS::RootedObject originsObj(aCx, &origins.toObject());
JS::RootedValue count(aCx);
ASSERT_TRUE(JS_GetProperty(aCx, originsObj, doubleclick.get(), &count));
ASSERT_TRUE(
JS_GetProperty(aCx, originsObj, kDoubleclickOrigin.get(), &count));
ASSERT_TRUE(count.isInt32() && count.toInt32() == 2)
<< "Must have recorded the origin exactly twice.";
}
@ -149,11 +164,7 @@ TEST_F(TelemetryTestFixture, RecordUnknownOrigin) {
Unused << mTelemetry->ClearOrigins();
const nsLiteralCString telemetryTest1("telemetry.test_test1");
const nsLiteralCString unknown("this origin isn't known to Origin Telemetry");
const nsLiteralCString unknown2("neither is this one");
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1, unknown);
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1, kUnknownOrigin1);
JS::RootedValue originSnapshot(aCx);
GetOriginSnapshot(aCx, &originSnapshot);
@ -163,7 +174,8 @@ TEST_F(TelemetryTestFixture, RecordUnknownOrigin) {
JS::RootedValue origins(aCx);
JS::RootedObject snapshotObj(aCx, &originSnapshot.toObject());
ASSERT_TRUE(JS_GetProperty(aCx, snapshotObj, telemetryTest1.get(), &origins))
ASSERT_TRUE(
JS_GetProperty(aCx, snapshotObj, kTelemetryTest1Metric.get(), &origins))
<< "telemetry.test_test1 must be in the snapshot.";
JS::RootedObject originsObj(aCx, &origins.toObject());
@ -173,7 +185,7 @@ TEST_F(TelemetryTestFixture, RecordUnknownOrigin) {
<< "Must have recorded the unknown origin exactly once.";
// Record a second, different unknown origin and ensure only one is stored.
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1, unknown2);
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1, kUnknownOrigin2);
GetOriginSnapshot(aCx, &originSnapshot);
@ -181,7 +193,8 @@ TEST_F(TelemetryTestFixture, RecordUnknownOrigin) {
<< "Origin snapshot must not be null/undefined.";
JS::RootedObject snapshotObj2(aCx, &originSnapshot.toObject());
ASSERT_TRUE(JS_GetProperty(aCx, snapshotObj2, telemetryTest1.get(), &origins))
ASSERT_TRUE(
JS_GetProperty(aCx, snapshotObj2, kTelemetryTest1Metric.get(), &origins))
<< "telemetry.test_test1 must be in the snapshot.";
JS::RootedObject originsObj2(aCx, &origins.toObject());
@ -197,30 +210,21 @@ TEST_F(TelemetryTestFixture, EncodedSnapshot) {
Unused << mTelemetry->ClearOrigins();
const nsLiteralCString doubleclick("doubleclick.net");
const nsLiteralCString unknown("this origin isn't known to Origin Telemetry");
const nsLiteralCString telemetryTest1("telemetry.test_test1");
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1,
kDoubleclickOrigin);
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1, kUnknownOrigin1);
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1, doubleclick);
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1, unknown);
// Properly prepare the prio prefs
// (Sourced from PrioEncoder.cpp from when it was being prototyped)
const nsLiteralCString prioKeyA(
"35AC1C7576C7C6EDD7FED6BCFC337B34D48CB4EE45C86BEEFB40BD8875707733");
const nsLiteralCString prioKeyB(
"26E6674E65425B823F1F1D5F96E3BB3EF9E406EC7FBA7DEF8B08A35DD135AF50");
Preferences::SetCString("prio.publicKeyA", prioKeyA);
Preferences::SetCString("prio.publicKeyB", prioKeyB);
nsTArray<Tuple<nsCString, nsCString>> firstStrings;
GetEncodedOriginStrings(aCx, telemetryTest1 + NS_LITERAL_CSTRING("-%u"),
firstStrings);
GetEncodedOriginStrings(
aCx, kTelemetryTest1Metric + NS_LITERAL_CSTRING("-%u"), firstStrings);
// Now snapshot a second time and ensure the encoded payloads change.
nsTArray<Tuple<nsCString, nsCString>> secondStrings;
GetEncodedOriginStrings(aCx, telemetryTest1 + NS_LITERAL_CSTRING("-%u"),
secondStrings);
GetEncodedOriginStrings(
aCx, kTelemetryTest1Metric + NS_LITERAL_CSTRING("-%u"), secondStrings);
const auto sizeOfPrioDatasPerMetric =
TelemetryOrigin::SizeOfPrioDatasPerMetric();
@ -265,8 +269,6 @@ TEST_F(TelemetryTestFixture, OriginTelemetryNotifiesTopic) {
Unused << mTelemetry->ClearOrigins();
const char* kTopic = "origin-telemetry-storage-limit-reached";
NS_NAMED_LITERAL_CSTRING(doubleclick, "doubleclick.net");
NS_NAMED_LITERAL_CSTRING(fb, "fb.com");
MockObserver* mo = new MockObserver();
nsCOMPtr<nsIObserver> nsMo(mo);
@ -280,9 +282,11 @@ TEST_F(TelemetryTestFixture, OriginTelemetryNotifiesTopic) {
for (size_t i = 0; i < size; ++i) {
if (i < size - 1) {
// Let's ensure we only notify the once.
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1, fb);
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1,
kFacebookOrigin);
}
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1, doubleclick);
Telemetry::RecordOrigin(OriginMetricID::TelemetryTest_Test1,
kDoubleclickOrigin);
}
os->RemoveObserver(nsMo, kTopic);