blob: 19da5e574edafda447afd05d1734dbf9a166ea2a [file] [log] [blame]
adlr@google.com3defe6a2009-12-04 20:57:17 +00001// Copyright (c) 2009 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 <stdio.h>
6#include <string>
7#include <gtest/gtest.h>
8#include "update_engine/install_plan.h"
9#include "update_engine/omaha_request_prep_action.h"
10#include "update_engine/test_utils.h"
11#include "update_engine/utils.h"
12
13using std::string;
14
15namespace chromeos_update_engine {
16
17class OmahaRequestPrepActionTest : public ::testing::Test {
18 public:
19 // Return true iff the OmahaResponseHandlerAction succeeded.
20 // if out is non-NULL, it's set w/ the response from the action.
21 bool DoTest(bool force_full_update, UpdateCheckParams* out);
22 static const string kTestDir;
23};
24
25const string OmahaRequestPrepActionTest::kTestDir = "request_prep_action-test";
26
27class OmahaRequestPrepActionProcessorDelegate
28 : public ActionProcessorDelegate {
29 public:
30 OmahaRequestPrepActionProcessorDelegate()
31 : success_(false),
32 success_set_(false) {}
33 void ActionCompleted(ActionProcessor* processor,
34 AbstractAction* action,
35 bool success) {
36 if (action->Type() == OmahaRequestPrepAction::StaticType()) {
37 success_ = success;
38 success_set_ = true;
39 }
40 }
41 bool success_;
42 bool success_set_;
43};
44
45bool OmahaRequestPrepActionTest::DoTest(bool force_full_update,
46 UpdateCheckParams* out) {
47 ActionProcessor processor;
48 OmahaRequestPrepActionProcessorDelegate delegate;
49 processor.set_delegate(&delegate);
50
51 OmahaRequestPrepAction request_prep_action(force_full_update);
52 request_prep_action.set_root(string("./") + kTestDir);
53 ObjectCollectorAction<UpdateCheckParams> collector_action;
54 BondActions(&request_prep_action, &collector_action);
55 processor.EnqueueAction(&request_prep_action);
56 processor.EnqueueAction(&collector_action);
57 processor.StartProcessing();
58 EXPECT_TRUE(!processor.IsRunning())
59 << "Update test to handle non-asynch actions";
60 if (out)
61 *out = collector_action.object();
62 EXPECT_TRUE(delegate.success_set_);
63 return delegate.success_;
64}
65
66namespace {
67// Returns true iff str is formatted as a mac address
68bool IsValidMac(const string& str) {
69 if (str.size() != (3 * 6 - 1))
70 return false;
71 for (unsigned int i = 0; i < str.size(); i++) {
72 char c = str[i];
73 switch (i % 3) {
74 case 0: // fall through
75 case 1:
76 if ((c >= '0') && (c <= '9'))
77 break;
78 if ((c >= 'a') && (c <= 'f'))
79 break;
80 if ((c >= 'A') && (c <= 'F'))
81 break;
82 return false;
83 case 2:
84 if (c == ':')
85 break;
86 return false;
87 }
88 }
89 return true;
90}
91string GetMachineType() {
92 FILE* fp = popen("uname -m", "r");
93 if (!fp)
94 return "";
95 string ret;
96 for (;;) {
97 char buffer[10];
98 size_t r = fread(buffer, 1, sizeof(buffer), fp);
99 if (r == 0)
100 break;
101 ret.insert(ret.begin(), buffer, buffer + r);
102 }
103 // strip trailing '\n' if it exists
104 if ((*ret.rbegin()) == '\n')
105 ret.resize(ret.size() - 1);
106 fclose(fp);
107 return ret;
108}
109} // namespace {}
110
111TEST_F(OmahaRequestPrepActionTest, SimpleTest) {
112 ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc"));
113 {
114 ASSERT_TRUE(WriteFileString(
115 kTestDir + "/etc/lsb-release",
116 "GOOGLE_FOO=bar\nGOOGLE_RELEASE=0.2.2.3\nGOOGLE_TRACK=footrack"));
117 UpdateCheckParams out;
118 EXPECT_TRUE(DoTest(false, &out));
119 EXPECT_TRUE(IsValidMac(out.machine_id));
120 // for now we're just using the machine id here
121 EXPECT_TRUE(IsValidMac(out.user_id));
122 EXPECT_EQ("Chrome OS", out.os_platform);
123 EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp);
124 EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id);
125 EXPECT_EQ("0.2.2.3", out.app_version);
126 EXPECT_EQ("en-US", out.app_lang);
127 EXPECT_EQ("footrack", out.app_track);
128 }
129 EXPECT_EQ(0, System(string("rm -rf ") + kTestDir));
130}
131
132TEST_F(OmahaRequestPrepActionTest, MissingTrackTest) {
133 ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc"));
134 {
135 ASSERT_TRUE(WriteFileString(
136 kTestDir + "/etc/lsb-release",
137 "GOOGLE_FOO=bar\nGOOGLE_RELEASE=0.2.2.3\nGOOGLE_TRXCK=footrack"));
138 UpdateCheckParams out;
139 EXPECT_TRUE(DoTest(false, &out));
140 EXPECT_TRUE(IsValidMac(out.machine_id));
141 // for now we're just using the machine id here
142 EXPECT_TRUE(IsValidMac(out.user_id));
143 EXPECT_EQ("Chrome OS", out.os_platform);
144 EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp);
145 EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id);
146 EXPECT_EQ("0.2.2.3", out.app_version);
147 EXPECT_EQ("en-US", out.app_lang);
148 EXPECT_EQ("", out.app_track);
149 }
150 EXPECT_EQ(0, System(string("rm -rf ") + kTestDir));
151}
152
153TEST_F(OmahaRequestPrepActionTest, ConfusingReleaseTest) {
154 ASSERT_EQ(0, System(string("mkdir -p ") + kTestDir + "/etc"));
155 {
156 ASSERT_TRUE(WriteFileString(
157 kTestDir + "/etc/lsb-release",
158 "GOOGLE_FOO=GOOGLE_RELEASE=1.2.3.4\n"
159 "GOOGLE_RELEASE=0.2.2.3\nGOOGLE_TRXCK=footrack"));
160 UpdateCheckParams out;
161 EXPECT_TRUE(DoTest(false, &out));
162 EXPECT_TRUE(IsValidMac(out.machine_id));
163 // for now we're just using the machine id here
164 EXPECT_TRUE(IsValidMac(out.user_id));
165 EXPECT_EQ("Chrome OS", out.os_platform);
166 EXPECT_EQ(string("0.2.2.3_") + GetMachineType(), out.os_sp);
167 EXPECT_EQ("{87efface-864d-49a5-9bb3-4b050a7c227a}", out.app_id);
168 EXPECT_EQ("0.2.2.3", out.app_version);
169 EXPECT_EQ("en-US", out.app_lang);
170 EXPECT_EQ("", out.app_track);
171 }
172 EXPECT_EQ(0, System(string("rm -rf ") + kTestDir));
173}
174
175} // namespace chromeos_update_engine