summaryrefslogtreecommitdiffstats
path: root/toolkit/components/resistfingerprinting/tests/gtest/test_usercharping.cpp
blob: dd1cbe7d469238fe1b9d627495093af1da8bc72b (plain)
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
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
 * vim: sw=2 ts=2 et lcs=trail\:.,tab\:>~ :
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "gtest/gtest.h"
#include "mozilla/gtest/nsUserCharacteristics.h"

#include "mozilla/glean/GleanPings.h"
#include "mozilla/glean/GleanMetrics.h"

using namespace mozilla;

const auto* const kUUIDPref =
    "toolkit.telemetry.user_characteristics_ping.uuid";

TEST(ResistFingerprinting, UserCharacteristics_Simple)
{
  mozilla::glean::characteristics::max_touch_points.Set(7);

  bool submitted = false;
  mozilla::glean_pings::UserCharacteristics.TestBeforeNextSubmit(
      [&submitted](const nsACString& aReason) {
        submitted = true;

        ASSERT_EQ(
            7, mozilla::glean::characteristics::max_touch_points.TestGetValue()
                   .unwrap()
                   .ref());
      });
  mozilla::glean_pings::UserCharacteristics.Submit();
  ASSERT_TRUE(submitted);
}

TEST(ResistFingerprinting, UserCharacteristics_Complex)
{
  nsUserCharacteristics::PopulateData(true);

  bool submitted = false;
  mozilla::glean_pings::UserCharacteristics.TestBeforeNextSubmit(
      [&submitted](const nsACString& aReason) {
        submitted = true;

        ASSERT_STRNE("", mozilla::glean::characteristics::client_identifier
                             .TestGetValue()
                             .unwrap()
                             .value()
                             .get());

        nsCString fullUuidStr;
        Preferences::GetCString(kUUIDPref, fullUuidStr);

        // Remove the '{' and '}'
        nsAutoCString uuidString;
        uuidString = Substring(fullUuidStr, 1, NSID_LENGTH - 3);

        ASSERT_STREQ(
            uuidString.get(),
            mozilla::glean::characteristics::client_identifier.TestGetValue()
                .unwrap()
                .value()
                .get());
        ASSERT_EQ(
            testing::MaxTouchPoints(),
            mozilla::glean::characteristics::max_touch_points.TestGetValue()
                .unwrap()
                .ref());
      });
  nsUserCharacteristics::SubmitPing();
  ASSERT_TRUE(submitted);
}

TEST(ResistFingerprinting, UserCharacteristics_ClearPref)
{
  nsCString originalUUID;

  mozilla::glean_pings::UserCharacteristics.TestBeforeNextSubmit(
      [&originalUUID](const nsACString& aReason) {
        originalUUID =
            mozilla::glean::characteristics::client_identifier.TestGetValue()
                .unwrap()
                .value()
                .get();
        ASSERT_STRNE("", mozilla::glean::characteristics::client_identifier
                             .TestGetValue()
                             .unwrap()
                             .value()
                             .get());

        nsCString fullUuidStr;
        Preferences::GetCString(kUUIDPref, fullUuidStr);

        // Remove the '{' and '}'
        nsAutoCString uuidString;
        uuidString = Substring(fullUuidStr, 1, NSID_LENGTH - 3);

        ASSERT_STREQ(
            uuidString.get(),
            mozilla::glean::characteristics::client_identifier.TestGetValue()
                .unwrap()
                .value()
                .get());
      });
  nsUserCharacteristics::PopulateData(true);
  nsUserCharacteristics::SubmitPing();

  auto original_value =
      Preferences::GetBool("datareporting.healthreport.uploadEnabled");
  Preferences::SetBool("datareporting.healthreport.uploadEnabled", true);
  Preferences::SetBool("datareporting.healthreport.uploadEnabled", false);

  mozilla::glean_pings::UserCharacteristics.TestBeforeNextSubmit(
      [](const nsACString& aReason) {
        // Assert that the pref is blank
        nsAutoCString uuidValue;
        Preferences::GetCString(kUUIDPref, uuidValue);
        ASSERT_STREQ("", uuidValue.get());
      });
  nsUserCharacteristics::SubmitPing();

  Preferences::SetBool("datareporting.healthreport.uploadEnabled", true);
  mozilla::glean_pings::UserCharacteristics.TestBeforeNextSubmit(
      [&originalUUID](const nsACString& aReason) {
        // Assert that the new UUID is different from the old one
        ASSERT_STRNE(
            originalUUID.get(),
            mozilla::glean::characteristics::client_identifier.TestGetValue()
                .unwrap()
                .value()
                .get());

        // Assert that the pref is not blank
        nsAutoCString uuidValue;
        Preferences::GetCString(kUUIDPref, uuidValue);
        ASSERT_STRNE("", uuidValue.get());
      });
  nsUserCharacteristics::PopulateData(true);
  nsUserCharacteristics::SubmitPing();

  Preferences::SetBool("datareporting.healthreport.uploadEnabled",
                       original_value);
}