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.
5 // A test application for the MachineDealCode class.
7 // These tests should not be executed on the build server:
8 // - They assert for the failed cases.
9 // - They modify machine state (registry).
11 // These tests require write access to HKLM and HKCU, unless
12 // rlz_lib::CreateMachineState() has been successfully called.
14 #include "base/logging.h"
15 #include "testing/gmock/include/gmock/gmock.h"
16 #include "testing/gtest/include/gtest/gtest.h"
18 #include "rlz/test/rlz_test_helpers.h"
19 #include "rlz/win/lib/machine_deal.h"
21 class MachineDealCodeHelper
: public rlz_lib::MachineDealCode
{
23 static bool Clear() { return rlz_lib::MachineDealCode::Clear(); }
26 MachineDealCodeHelper() {}
27 ~MachineDealCodeHelper() {}
30 class MachineDealCodeTest
: public RlzLibTestBase
{
33 TEST_F(MachineDealCodeTest
, CreateMachineState
) {
34 EXPECT_TRUE(rlz_lib::CreateMachineState());
37 TEST_F(MachineDealCodeTest
, Set
) {
38 MachineDealCodeHelper::Clear();
42 EXPECT_TRUE(rlz_lib::MachineDealCode::Set("dcc_value"));
44 EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50
, 50));
45 EXPECT_STREQ("dcc_value", dcc_50
);
47 EXPECT_TRUE(rlz_lib::MachineDealCode::Set("dcc_value_2"));
49 EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50
, 50));
50 EXPECT_STREQ("dcc_value_2", dcc_50
);
53 TEST_F(MachineDealCodeTest
, Get
) {
54 MachineDealCodeHelper::Clear();
55 char dcc_50
[50], dcc_2
[2];
59 EXPECT_FALSE(rlz_lib::MachineDealCode::Get(dcc_50
, 50));
61 EXPECT_TRUE(rlz_lib::MachineDealCode::Set("dcc_value"));
63 EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50
, 50));
64 EXPECT_STREQ("dcc_value", dcc_50
);
66 EXPECT_FALSE(rlz_lib::MachineDealCode::Get(dcc_2
, 2));
69 TEST_F(MachineDealCodeTest
, SetFromPingResponse
) {
70 rlz_lib::MachineDealCode::Set("MyDCCode");
75 const char kBadDccResponse
[] =
76 "dcc: NotMyDCCode \r\n"
77 "set_dcc: NewDCCode\r\n"
79 EXPECT_FALSE(rlz_lib::MachineDealCode::SetFromPingResponse(
81 EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50
, 50));
82 EXPECT_STREQ("MyDCCode", dcc_50
);
84 const char kBadCrcResponse
[] =
86 "set_dcc: NewDCCode\r\n"
88 EXPECT_FALSE(rlz_lib::MachineDealCode::SetFromPingResponse(
90 EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50
, 50));
91 EXPECT_STREQ("MyDCCode", dcc_50
);
95 const char kMissingSetResponse
[] =
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
);
103 const char kGoodResponse
[] =
105 "set_dcc: NewDCCode\r\n"
107 EXPECT_TRUE(rlz_lib::MachineDealCode::SetFromPingResponse(
109 EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50
, 50));
110 EXPECT_STREQ("NewDCCode", dcc_50
);
112 const char kGoodResponse2
[] =
113 "set_dcc: NewDCCode2 \r\n"
114 "dcc: NewDCCode \r\n"
116 EXPECT_TRUE(rlz_lib::MachineDealCode::SetFromPingResponse(
118 EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50
, 50));
119 EXPECT_STREQ("NewDCCode2", dcc_50
);
121 MachineDealCodeHelper::Clear();
122 const char kGoodResponse3
[] =
123 "set_dcc: NewDCCode \r\n"
125 EXPECT_TRUE(rlz_lib::MachineDealCode::SetFromPingResponse(
127 EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50
, 50));
128 EXPECT_STREQ("NewDCCode", dcc_50
);
130 MachineDealCodeHelper::Clear();
131 const char kGoodResponse4
[] =
133 "set_dcc: NewDCCode \r\n"
135 EXPECT_TRUE(rlz_lib::MachineDealCode::SetFromPingResponse(
137 EXPECT_TRUE(rlz_lib::MachineDealCode::Get(dcc_50
, 50));
138 EXPECT_STREQ("NewDCCode", dcc_50
);
141 TEST_F(MachineDealCodeTest
, GetAsCgi
) {
142 MachineDealCodeHelper::Clear();
143 char cgi_50
[50], cgi_2
[2];
147 EXPECT_FALSE(rlz_lib::MachineDealCode::GetAsCgi(cgi_50
, 50));
148 EXPECT_STREQ("", cgi_50
);
150 EXPECT_TRUE(rlz_lib::MachineDealCode::Set("dcc_value"));
152 EXPECT_TRUE(rlz_lib::MachineDealCode::GetAsCgi(cgi_50
, 50));
153 EXPECT_STREQ("dcc=dcc_value", cgi_50
);
155 EXPECT_FALSE(rlz_lib::MachineDealCode::GetAsCgi(cgi_2
, 2));