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

Side by Side Diff: components/rappor/rappor_prefs.cc

Issue 845863002: Add stricter tests for RapporService::LoadSecret (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Internal namespace Created 5 years, 11 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 unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "components/rappor/rappor_prefs.h"
6
7 #include "base/base64.h"
8 #include "base/metrics/histogram.h"
9 #include "base/prefs/pref_registry_simple.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/rand_util.h"
12 #include "components/metrics/daily_event.h"
13 #include "components/rappor/byte_vector_utils.h"
14 #include "components/rappor/rappor_parameters.h"
15 #include "components/rappor/rappor_pref_names.h"
16
17 namespace rappor {
18
19 const char kLoadCohortHistogramName[] = "Rappor.LoadCohortResult";
20 const char kLoadSecretHistogramName[] = "Rappor.LoadSecretResult";
21
22 namespace {
23
24 void RecordLoadCohortResult(LoadResult reason) {
25 UMA_HISTOGRAM_ENUMERATION(kLoadCohortHistogramName,
26 reason,
27 NUM_LOAD_RESULTS);
28 }
29
30 void RecordLoadSecretResult(LoadResult reason) {
31 UMA_HISTOGRAM_ENUMERATION(kLoadSecretHistogramName,
32 reason,
33 NUM_LOAD_RESULTS);
34 }
35
36 } // namespace
37
38 void RegisterPrefs(PrefRegistrySimple* registry) {
39 registry->RegisterStringPref(prefs::kRapporSecret, std::string());
40 registry->RegisterIntegerPref(prefs::kRapporCohortDeprecated, -1);
41 registry->RegisterIntegerPref(prefs::kRapporCohortSeed, -1);
42 metrics::DailyEvent::RegisterPref(registry, prefs::kRapporLastDailySample);
43 }
44
45 int32_t LoadCohort(PrefService* pref_service) {
46 // Ignore and delete old cohort parameter.
47 pref_service->ClearPref(prefs::kRapporCohortDeprecated);
48
49 int32_t cohort = pref_service->GetInteger(prefs::kRapporCohortSeed);
50 // If the user is already assigned to a valid cohort, we're done.
51 if (cohort >= 0 && cohort < RapporParameters::kMaxCohorts) {
52 RecordLoadCohortResult(LOAD_SUCCESS);
53 DVLOG(2) << "Rappor cohort loaded.";
54 return cohort;
55 }
56
57 // This is the first time the client has started the service (or their
58 // preferences were corrupted). Randomly assign them to a cohort.
59 RecordLoadCohortResult(cohort == -1 ? LOAD_EMPTY_VALUE : LOAD_CORRUPT_VALUE);
60 cohort = base::RandGenerator(RapporParameters::kMaxCohorts);
61 DVLOG(2) << "Selected a new Rappor cohort: " << cohort;
62 pref_service->SetInteger(prefs::kRapporCohortSeed, cohort);
63 return cohort;
64 }
65
66 std::string LoadSecret(PrefService* pref_service) {
67 std::string secret;
68 std::string secret_base64 = pref_service->GetString(prefs::kRapporSecret);
69 if (!secret_base64.empty()) {
70 bool decoded = base::Base64Decode(secret_base64, &secret);
71 if (decoded &&
72 secret.size() == HmacByteVectorGenerator::kEntropyInputSize) {
73 DVLOG(2) << "Rappor secret loaded.";
74 RecordLoadSecretResult(LOAD_SUCCESS);
75 return secret;
76 }
77 // If the preference fails to decode, or is the wrong size, it must be
78 // corrupt, so continue as though it didn't exist yet and generate a new
79 // one.
80 DVLOG(2) << "Corrupt Rappor secret found.";
81 RecordLoadSecretResult(LOAD_CORRUPT_VALUE);
82 } else {
83 DVLOG(2) << "No Rappor secret found.";
84 RecordLoadSecretResult(LOAD_EMPTY_VALUE);
85 }
86
87 DVLOG(2) << "Generated a new Rappor secret.";
88 secret = HmacByteVectorGenerator::GenerateEntropyInput();
89 base::Base64Encode(secret, &secret_base64);
90 pref_service->SetString(prefs::kRapporSecret, secret_base64);
91 return secret;
92 }
93
94 } // namespace rappor
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698