1 // Copyright 2013 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 "chrome/installer/gcapi/gcapi_omaha_experiment.h"
6
7 #include "base/strings/utf_string_conversions.h"
8 #include "chrome/installer/gcapi/gcapi.h"
9 #include "chrome/installer/gcapi/gcapi_test_registry_overrider.h"
10 #include "chrome/installer/util/google_update_constants.h"
11 #include "chrome/installer/util/google_update_experiment_util.h"
12 #include "chrome/installer/util/google_update_settings.h"
13 #include "testing/gtest/include/gtest/gtest.h"
14
15 namespace {
16
17 const wchar_t kBrand[] = L"ABCD";
18 const uint16 kUserLevel = GCAPI_INVOKED_STANDARD_SHELL;
19
20 const wchar_t kSomeExperiments[] = L"myexp=1|Aug 2;yourexp=2|Sep 5";
21 const wchar_t kSomeOtherExperiments[] = L"anotherexp=joe|Jun 7 2008";
22 const wchar_t kSomeMoreExperiments[] = L"moreexp=foo|Jul 31 1999";
23
24 class GCAPIOmahaExperimentTest : public ::testing::Test {
25 protected:
GCAPIOmahaExperimentTest()26 GCAPIOmahaExperimentTest()
27 : brand_(kBrand),
28 reactivation_label_(gcapi_internals::GetGCAPIExperimentLabel(
29 kBrand, gcapi_internals::kReactivationLabel)),
30 relaunch_label_(gcapi_internals::GetGCAPIExperimentLabel(
31 kBrand, gcapi_internals::kRelaunchLabel)) {
32 }
33
VerifyExperimentLabels(const string16 & expected_labels)34 void VerifyExperimentLabels(const string16& expected_labels) {
35 string16 actual_labels;
36 EXPECT_TRUE(GoogleUpdateSettings::ReadExperimentLabels(false,
37 &actual_labels));
38 EXPECT_EQ(expected_labels, actual_labels);
39 }
40
41 string16 brand_;
42 string16 reactivation_label_;
43 string16 relaunch_label_;
44
45 const GCAPITestRegistryOverrider gcapi_test_registry_overrider_;
46 };
47
TEST_F(GCAPIOmahaExperimentTest,SetReactivationLabelFromEmptyExperiments)48 TEST_F(GCAPIOmahaExperimentTest, SetReactivationLabelFromEmptyExperiments) {
49 ASSERT_TRUE(SetReactivationExperimentLabels(kBrand, kUserLevel));
50 VerifyExperimentLabels(reactivation_label_);
51 }
52
53 // Test the relaunch label once; all other tests go more in depth, but since
54 // both labels use the same logic underneath there is no need to test both in
55 // depth.
TEST_F(GCAPIOmahaExperimentTest,SetRelaunchLabelFromEmptyExperiments)56 TEST_F(GCAPIOmahaExperimentTest, SetRelaunchLabelFromEmptyExperiments) {
57 ASSERT_TRUE(SetRelaunchExperimentLabels(kBrand, kUserLevel));
58 VerifyExperimentLabels(relaunch_label_);
59 }
60
TEST_F(GCAPIOmahaExperimentTest,SetReactivationLabelWithExistingExperiments)61 TEST_F(GCAPIOmahaExperimentTest, SetReactivationLabelWithExistingExperiments) {
62 GoogleUpdateSettings::SetExperimentLabels(false, kSomeExperiments);
63
64 ASSERT_TRUE(SetReactivationExperimentLabels(kBrand, kUserLevel));
65
66 string16 expected_labels(kSomeExperiments);
67 expected_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
68 expected_labels.append(reactivation_label_);
69 VerifyExperimentLabels(expected_labels);
70 }
71
TEST_F(GCAPIOmahaExperimentTest,SetReactivationLabelWithExistingIdenticalExperiment)72 TEST_F(GCAPIOmahaExperimentTest,
73 SetReactivationLabelWithExistingIdenticalExperiment) {
74 string16 previous_labels(kSomeExperiments);
75 previous_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
76 previous_labels.append(reactivation_label_);
77 previous_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
78 previous_labels.append(kSomeOtherExperiments);
79 GoogleUpdateSettings::SetExperimentLabels(false, previous_labels);
80
81 ASSERT_TRUE(SetReactivationExperimentLabels(kBrand, kUserLevel));
82
83 string16 expected_labels(kSomeExperiments);
84 expected_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
85 expected_labels.append(kSomeOtherExperiments);
86 expected_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
87 expected_labels.append(reactivation_label_);
88 VerifyExperimentLabels(expected_labels);
89 }
90
TEST_F(GCAPIOmahaExperimentTest,SetReactivationLabelWithExistingIdenticalAtBeginning)91 TEST_F(GCAPIOmahaExperimentTest,
92 SetReactivationLabelWithExistingIdenticalAtBeginning) {
93 string16 previous_labels(reactivation_label_);
94 previous_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
95 previous_labels.append(kSomeExperiments);
96 GoogleUpdateSettings::SetExperimentLabels(false, previous_labels);
97
98 ASSERT_TRUE(SetReactivationExperimentLabels(kBrand, kUserLevel));
99
100 string16 expected_labels(kSomeExperiments);
101 expected_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
102 expected_labels.append(reactivation_label_);
103 VerifyExperimentLabels(expected_labels);
104 }
105
TEST_F(GCAPIOmahaExperimentTest,SetReactivationLabelWithFakeMatchInAnExperiment)106 TEST_F(GCAPIOmahaExperimentTest,
107 SetReactivationLabelWithFakeMatchInAnExperiment) {
108 string16 previous_labels(kSomeExperiments);
109 previous_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
110 previous_labels.append(L"blah_");
111 // Shouldn't match deletion criteria.
112 previous_labels.append(reactivation_label_);
113 previous_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
114 previous_labels.append(kSomeOtherExperiments);
115 previous_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
116 // Should match the deletion criteria.
117 previous_labels.append(reactivation_label_);
118 previous_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
119 previous_labels.append(kSomeMoreExperiments);
120 GoogleUpdateSettings::SetExperimentLabels(false, previous_labels);
121
122 ASSERT_TRUE(SetReactivationExperimentLabels(kBrand, kUserLevel));
123
124 string16 expected_labels(kSomeExperiments);
125 expected_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
126 expected_labels.append(L"blah_");
127 expected_labels.append(reactivation_label_);
128 expected_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
129 expected_labels.append(kSomeOtherExperiments);
130 expected_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
131 expected_labels.append(kSomeMoreExperiments);
132 expected_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
133 expected_labels.append(reactivation_label_);
134 VerifyExperimentLabels(expected_labels);
135 }
136
TEST_F(GCAPIOmahaExperimentTest,SetReactivationLabelWithFakeMatchInAnExperimentAndNoRealMatch)137 TEST_F(GCAPIOmahaExperimentTest,
138 SetReactivationLabelWithFakeMatchInAnExperimentAndNoRealMatch) {
139 string16 previous_labels(kSomeExperiments);
140 previous_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
141 previous_labels.append(L"blah_");
142 // Shouldn't match deletion criteria.
143 previous_labels.append(reactivation_label_);
144 previous_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
145 previous_labels.append(kSomeOtherExperiments);
146 GoogleUpdateSettings::SetExperimentLabels(false, previous_labels);
147
148 ASSERT_TRUE(SetReactivationExperimentLabels(kBrand, kUserLevel));
149
150 string16 expected_labels(kSomeExperiments);
151 expected_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
152 expected_labels.append(L"blah_");
153 expected_labels.append(reactivation_label_);
154 expected_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
155 expected_labels.append(kSomeOtherExperiments);
156 expected_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
157 expected_labels.append(reactivation_label_);
158 VerifyExperimentLabels(expected_labels);
159 }
160
TEST_F(GCAPIOmahaExperimentTest,SetReactivationLabelWithExistingEntryWithLabelAsPrefix)161 TEST_F(GCAPIOmahaExperimentTest,
162 SetReactivationLabelWithExistingEntryWithLabelAsPrefix) {
163 string16 previous_labels(kSomeExperiments);
164 previous_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
165 // Append prefix matching the label, but not followed by '='.
166 previous_labels.append(gcapi_internals::kReactivationLabel);
167 // Shouldn't match deletion criteria.
168 previous_labels.append(kSomeOtherExperiments);
169 GoogleUpdateSettings::SetExperimentLabels(false, previous_labels);
170
171 ASSERT_TRUE(SetReactivationExperimentLabels(kBrand, kUserLevel));
172
173 string16 expected_labels(kSomeExperiments);
174 expected_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
175 expected_labels.append(gcapi_internals::kReactivationLabel);
176 expected_labels.append(kSomeOtherExperiments);
177 expected_labels.append(ASCIIToUTF16(google_update::kExperimentLabelSep));
178 expected_labels.append(reactivation_label_);
179 VerifyExperimentLabels(expected_labels);
180 }
181
182 } // namespace
183