1// Copyright (c) 2012 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// A test application for the MachineDealCode class.
6//
7// These tests should not be executed on the build server:
8// - They assert for the failed cases.
9// - They modify machine state (registry).
10//
11// These tests require write access to HKLM and HKCU, unless
12// rlz_lib::CreateMachineState() has been successfully called.
13
14#include "base/logging.h"
15#include "testing/gmock/include/gmock/gmock.h"
16#include "testing/gtest/include/gtest/gtest.h"
17
18#include "rlz/test/rlz_test_helpers.h"
19#include "rlz/win/lib/machine_deal.h"
20
21class MachineDealCodeHelper : public rlz_lib::MachineDealCode {
22 public:
23  static bool Clear() { return rlz_lib::MachineDealCode::Clear(); }
24
25 private:
26  MachineDealCodeHelper() {}
27  ~MachineDealCodeHelper() {}
28};
29
30class MachineDealCodeTest : public RlzLibTestBase {
31};
32
33TEST_F(MachineDealCodeTest, CreateMachineState) {
34  EXPECT_TRUE(rlz_lib::CreateMachineState());
35}
36
37TEST_F(MachineDealCodeTest, Set) {
38  MachineDealCodeHelper::Clear();
39  char dcc_50[50];
40  dcc_50[0] = 0;
41
42  EXPECT_TRUE(rlz_lib::MachineDealCode::Set("dcc_value"));
43
44  EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50, 50));
45  EXPECT_STREQ("dcc_value", dcc_50);
46
47  EXPECT_TRUE(rlz_lib::MachineDealCode::Set("dcc_value_2"));
48
49  EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50, 50));
50  EXPECT_STREQ("dcc_value_2", dcc_50);
51}
52
53TEST_F(MachineDealCodeTest, Get) {
54  MachineDealCodeHelper::Clear();
55  char dcc_50[50], dcc_2[2];
56  dcc_50[0] = 0;
57  dcc_2[0] = 0;
58
59  EXPECT_FALSE(rlz_lib::MachineDealCode::Get(dcc_50, 50));
60
61  EXPECT_TRUE(rlz_lib::MachineDealCode::Set("dcc_value"));
62
63  EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50, 50));
64  EXPECT_STREQ("dcc_value", dcc_50);
65
66  EXPECT_FALSE(rlz_lib::MachineDealCode::Get(dcc_2, 2));
67}
68
69TEST_F(MachineDealCodeTest, SetFromPingResponse) {
70  rlz_lib::MachineDealCode::Set("MyDCCode");
71  char dcc_50[50];
72
73  // Bad responses
74
75  const char kBadDccResponse[] =
76    "dcc: NotMyDCCode \r\n"
77    "set_dcc: NewDCCode\r\n"
78    "crc32: 1B4D6BB3";
79  EXPECT_FALSE(rlz_lib::MachineDealCode::SetFromPingResponse(
80      kBadDccResponse));
81  EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50, 50));
82  EXPECT_STREQ("MyDCCode", dcc_50);
83
84  const char kBadCrcResponse[] =
85    "dcc: MyDCCode \r\n"
86    "set_dcc: NewDCCode\r\n"
87    "crc32: 90707106";
88  EXPECT_FALSE(rlz_lib::MachineDealCode::SetFromPingResponse(
89      kBadCrcResponse));
90  EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50, 50));
91  EXPECT_STREQ("MyDCCode", dcc_50);
92
93  // Good responses
94
95  const char kMissingSetResponse[] =
96    "dcc: MyDCCode \r\n"
97    "crc32: 35F2E717";
98  EXPECT_TRUE(rlz_lib::MachineDealCode::SetFromPingResponse(
99      kMissingSetResponse));
100  EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50, 50));
101  EXPECT_STREQ("MyDCCode", dcc_50);
102
103  const char kGoodResponse[] =
104    "dcc: MyDCCode \r\n"
105    "set_dcc: NewDCCode\r\n"
106    "crc32: C8540E02";
107  EXPECT_TRUE(rlz_lib::MachineDealCode::SetFromPingResponse(
108      kGoodResponse));
109  EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50, 50));
110  EXPECT_STREQ("NewDCCode", dcc_50);
111
112  const char kGoodResponse2[] =
113    "set_dcc: NewDCCode2  \r\n"
114    "dcc:   NewDCCode \r\n"
115    "crc32: 60B6409A";
116  EXPECT_TRUE(rlz_lib::MachineDealCode::SetFromPingResponse(
117      kGoodResponse2));
118  EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50, 50));
119  EXPECT_STREQ("NewDCCode2", dcc_50);
120
121  MachineDealCodeHelper::Clear();
122  const char kGoodResponse3[] =
123    "set_dcc: NewDCCode  \r\n"
124    "crc32: 374C1C47";
125  EXPECT_TRUE(rlz_lib::MachineDealCode::SetFromPingResponse(
126      kGoodResponse3));
127  EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50, 50));
128  EXPECT_STREQ("NewDCCode", dcc_50);
129
130  MachineDealCodeHelper::Clear();
131  const char kGoodResponse4[] =
132    "dcc:   \r\n"
133    "set_dcc: NewDCCode  \r\n"
134    "crc32: 0AB1FB39";
135  EXPECT_TRUE(rlz_lib::MachineDealCode::SetFromPingResponse(
136      kGoodResponse4));
137  EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50, 50));
138  EXPECT_STREQ("NewDCCode", dcc_50);
139}
140
141TEST_F(MachineDealCodeTest, GetAsCgi) {
142  MachineDealCodeHelper::Clear();
143  char cgi_50[50], cgi_2[2];
144  cgi_50[0] = 0;
145  cgi_2[0] = 0;
146
147  EXPECT_FALSE(rlz_lib::MachineDealCode::GetAsCgi(cgi_50, 50));
148  EXPECT_STREQ("", cgi_50);
149
150  EXPECT_TRUE(rlz_lib::MachineDealCode::Set("dcc_value"));
151
152  EXPECT_TRUE(rlz_lib::MachineDealCode::GetAsCgi(cgi_50, 50));
153  EXPECT_STREQ("dcc=dcc_value", cgi_50);
154
155  EXPECT_FALSE(rlz_lib::MachineDealCode::GetAsCgi(cgi_2, 2));
156}
157