summaryrefslogtreecommitdiff
path: root/cros/omaha_request_builder_xml_unittest.cc
blob: 76a724114c8cf67698d7dabfa4107e9d1544d61f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
//
// Copyright (C) 2019 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//      http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

#include "update_engine/cros/omaha_request_builder_xml.h"

#include <string>
#include <utility>
#include <vector>

#include <base/guid.h>
#include <base/strings/stringprintf.h>
#include <gtest/gtest.h>

#include "update_engine/cros/fake_system_state.h"

using std::pair;
using std::string;
using std::vector;
using testing::_;
using testing::DoAll;
using testing::Return;
using testing::SetArgPointee;

namespace chromeos_update_engine {

namespace {
// Helper to find key and extract value from the given string |xml|, instead
// of using a full parser. The attribute key will be followed by "=\"" as xml
// attribute values must be within double quotes (not single quotes).
static string FindAttributeKeyValueInXml(const string& xml,
                                         const string& key,
                                         const size_t val_size) {
  string key_with_quotes = key + "=\"";
  const size_t val_start_pos = xml.find(key);
  if (val_start_pos == string::npos)
    return "";
  return xml.substr(val_start_pos + key_with_quotes.size(), val_size);
}
// Helper to find the count of substring in a string.
static size_t CountSubstringInString(const string& str, const string& substr) {
  size_t count = 0, pos = 0;
  while ((pos = str.find(substr, pos ? pos + 1 : 0)) != string::npos)
    ++count;
  return count;
}
}  // namespace

class OmahaRequestBuilderXmlTest : public ::testing::Test {
 protected:
  void SetUp() override {
    FakeSystemState::CreateInstance();
    FakeSystemState::Get()->set_request_params(&params_);
  }
  void TearDown() override {}

  static constexpr size_t kGuidSize = 36;

  OmahaRequestParams params_;
};

TEST_F(OmahaRequestBuilderXmlTest, XmlEncodeTest) {
  string output;
  vector<pair<string, string>> xml_encode_pairs = {
      {"ab", "ab"},
      {"a<b", "a&lt;b"},
      {"<&>\"\'\\", "&lt;&amp;&gt;&quot;&apos;\\"},
      {"&lt;&amp;&gt;", "&amp;lt;&amp;amp;&amp;gt;"}};
  for (const auto& xml_encode_pair : xml_encode_pairs) {
    const auto& before_encoding = xml_encode_pair.first;
    const auto& after_encoding = xml_encode_pair.second;
    EXPECT_TRUE(XmlEncode(before_encoding, &output));
    EXPECT_EQ(after_encoding, output);
  }
  // Check that unterminated UTF-8 strings are handled properly.
  EXPECT_FALSE(XmlEncode("\xc2", &output));
  // Fail with invalid ASCII-7 chars.
  EXPECT_FALSE(XmlEncode("This is an 'n' with a tilde: \xc3\xb1", &output));
}

TEST_F(OmahaRequestBuilderXmlTest, XmlEncodeWithDefaultTest) {
  EXPECT_EQ("", XmlEncodeWithDefault(""));
  EXPECT_EQ("&lt;&amp;&gt;", XmlEncodeWithDefault("<&>", "something else"));
  EXPECT_EQ("<not escaped>", XmlEncodeWithDefault("\xc2", "<not escaped>"));
}

TEST_F(OmahaRequestBuilderXmlTest, PlatformGetAppTest) {
  params_.set_device_requisition("device requisition");
  OmahaRequestBuilderXml omaha_request{nullptr,
                                       false,
                                       false,
                                       0,
                                       0,
                                       0,
                                       ""};
  OmahaAppData dlc_app_data = {.id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX",
                               .version = "",
                               .skip_update = false,
                               .is_dlc = false};

  // Verify that the attributes that shouldn't be missing for Platform AppID are
  // in fact present in the <app ...></app>.
  const string app = omaha_request.GetApp(dlc_app_data);
  EXPECT_NE(string::npos, app.find("lang="));
  EXPECT_NE(string::npos, app.find("requisition="));
}

TEST_F(OmahaRequestBuilderXmlTest, DlcGetAppTest) {
  params_.set_device_requisition("device requisition");
  OmahaRequestBuilderXml omaha_request{nullptr,
                                       false,
                                       false,
                                       0,
                                       0,
                                       0,
                                       ""};
  OmahaAppData dlc_app_data = {
      .id = "_dlc_id", .version = "", .skip_update = false, .is_dlc = true};

  // Verify that the attributes that should be missing for DLC AppIDs are in
  // fact not present in the <app ...></app>.
  const string app = omaha_request.GetApp(dlc_app_data);
  EXPECT_EQ(string::npos, app.find("lang="));
  EXPECT_EQ(string::npos, app.find("requisition="));
}

TEST_F(OmahaRequestBuilderXmlTest, GetRequestXmlRequestIdTest) {
  OmahaRequestBuilderXml omaha_request{nullptr,
                                       false,
                                       false,
                                       0,
                                       0,
                                       0,
                                       ""};
  const string request_xml = omaha_request.GetRequest();
  const string key = "requestid";
  const string request_id =
      FindAttributeKeyValueInXml(request_xml, key, kGuidSize);
  // A valid |request_id| is either a GUID version 4 or empty string.
  if (!request_id.empty())
    EXPECT_TRUE(base::IsValidGUID(request_id));
}

TEST_F(OmahaRequestBuilderXmlTest, GetRequestXmlSessionIdTest) {
  const string gen_session_id = base::GenerateGUID();
  OmahaRequestBuilderXml omaha_request{nullptr,
                                       false,
                                       false,
                                       0,
                                       0,
                                       0,
                                       gen_session_id};
  const string request_xml = omaha_request.GetRequest();
  const string key = "sessionid";
  const string session_id =
      FindAttributeKeyValueInXml(request_xml, key, kGuidSize);
  // A valid |session_id| is either a GUID version 4 or empty string.
  if (!session_id.empty()) {
    EXPECT_TRUE(base::IsValidGUID(session_id));
  }
  EXPECT_EQ(gen_session_id, session_id);
}

TEST_F(OmahaRequestBuilderXmlTest, GetRequestXmlPlatformUpdateTest) {
  OmahaRequestBuilderXml omaha_request{nullptr,
                                       false,
                                       false,
                                       0,
                                       0,
                                       0,
                                       ""};
  const string request_xml = omaha_request.GetRequest();
  EXPECT_EQ(1, CountSubstringInString(request_xml, "<updatecheck"))
      << request_xml;
}

TEST_F(OmahaRequestBuilderXmlTest, GetRequestXmlPlatformUpdateWithDlcsTest) {
  params_.set_dlc_apps_params(
      {{params_.GetDlcAppId("dlc_no_0"), {.name = "dlc_no_0"}},
       {params_.GetDlcAppId("dlc_no_1"), {.name = "dlc_no_1"}}});
  OmahaRequestBuilderXml omaha_request{nullptr,
                                       false,
                                       false,
                                       0,
                                       0,
                                       0,
                                       ""};
  const string request_xml = omaha_request.GetRequest();
  EXPECT_EQ(3, CountSubstringInString(request_xml, "<updatecheck"))
      << request_xml;
}

TEST_F(OmahaRequestBuilderXmlTest, GetRequestXmlDlcInstallationTest) {
  const std::map<std::string, OmahaRequestParams::AppParams> dlcs = {
      {params_.GetDlcAppId("dlc_no_0"), {.name = "dlc_no_0"}},
      {params_.GetDlcAppId("dlc_no_1"), {.name = "dlc_no_1"}}};
  params_.set_dlc_apps_params(dlcs);
  params_.set_is_install(true);
  OmahaRequestBuilderXml omaha_request{nullptr,
                                       false,
                                       false,
                                       0,
                                       0,
                                       0,
                                       ""};
  const string request_xml = omaha_request.GetRequest();
  EXPECT_EQ(2, CountSubstringInString(request_xml, "<updatecheck"))
      << request_xml;

  auto FindAppId = [request_xml](size_t pos) -> size_t {
    return request_xml.find("<app appid", pos);
  };
  // Skip over the Platform AppID, which is always first.
  size_t pos = FindAppId(0);
  for (auto&& _ : dlcs) {
    (void)_;
    EXPECT_NE(string::npos, (pos = FindAppId(pos + 1))) << request_xml;
    const string dlc_app_id_version = FindAttributeKeyValueInXml(
        request_xml.substr(pos), "version", string(kNoVersion).size());
    EXPECT_EQ(kNoVersion, dlc_app_id_version);

    const string false_str = "false";
    const string dlc_app_id_delta_okay = FindAttributeKeyValueInXml(
        request_xml.substr(pos), "delta_okay", false_str.length());
    EXPECT_EQ(false_str, dlc_app_id_delta_okay);
  }
}

TEST_F(OmahaRequestBuilderXmlTest, GetRequestXmlDlcNoPing) {
  params_.set_dlc_apps_params(
      {{params_.GetDlcAppId("dlc_no_0"), {.name = "dlc_no_0"}}});
  OmahaRequestBuilderXml omaha_request{nullptr,
                                       false,
                                       false,
                                       0,
                                       0,
                                       0,
                                       ""};
  const string request_xml = omaha_request.GetRequest();
  EXPECT_EQ(0, CountSubstringInString(request_xml, "<ping")) << request_xml;
}

TEST_F(OmahaRequestBuilderXmlTest, GetRequestXmlDlcPingRollCallNoActive) {
  params_.set_dlc_apps_params(
      {{params_.GetDlcAppId("dlc_no_0"),
        {.active_counting_type = OmahaRequestParams::kDateBased,
         .name = "dlc_no_0",
         .ping_date_last_active = 25,
         .ping_date_last_rollcall = 36,
         .send_ping = true}}});
  OmahaRequestBuilderXml omaha_request{nullptr,
                                       false,
                                       false,
                                       0,
                                       0,
                                       0,
                                       ""};
  const string request_xml = omaha_request.GetRequest();
  EXPECT_EQ(1, CountSubstringInString(request_xml, "<ping rd=\"36\""))
      << request_xml;
}

TEST_F(OmahaRequestBuilderXmlTest, GetRequestXmlDlcPingRollCallAndActive) {
  params_.set_dlc_apps_params(
      {{params_.GetDlcAppId("dlc_no_0"),
        {.active_counting_type = OmahaRequestParams::kDateBased,
         .name = "dlc_no_0",
         .ping_active = 1,
         .ping_date_last_active = 25,
         .ping_date_last_rollcall = 36,
         .send_ping = true}}});
  OmahaRequestBuilderXml omaha_request{nullptr,
                                       false,
                                       false,
                                       0,
                                       0,
                                       0,
                                       ""};
  const string request_xml = omaha_request.GetRequest();
  EXPECT_EQ(1,
            CountSubstringInString(request_xml,
                                   "<ping active=\"1\" ad=\"25\" rd=\"36\""))
      << request_xml;
}

TEST_F(OmahaRequestBuilderXmlTest, GetRequestXmlUpdateCompleteEvent) {
  OmahaEvent event(OmahaEvent::kTypeUpdateComplete);
  OmahaRequestBuilderXml omaha_request{&event,
                                       false,
                                       false,
                                       0,
                                       0,
                                       0,
                                       ""};
  const string request_xml = omaha_request.GetRequest();
  LOG(INFO) << request_xml;
  EXPECT_EQ(
      1,
      CountSubstringInString(
          request_xml, "<event eventtype=\"3\" eventresult=\"1\"></event>"))
      << request_xml;
}

TEST_F(OmahaRequestBuilderXmlTest,
       GetRequestXmlUpdateCompleteEventSomeDlcsExcluded) {
  params_.set_dlc_apps_params({
      {params_.GetDlcAppId("dlc_1"), {.updated = true}},
      {params_.GetDlcAppId("dlc_2"), {.updated = false}},
  });
  OmahaEvent event(OmahaEvent::kTypeUpdateComplete);
  OmahaRequestBuilderXml omaha_request{&event,
                                       false,
                                       false,
                                       0,
                                       0,
                                       0,
                                       ""};
  const string request_xml = omaha_request.GetRequest();
  EXPECT_EQ(
      2,
      CountSubstringInString(
          request_xml, "<event eventtype=\"3\" eventresult=\"1\"></event>"))
      << request_xml;
  EXPECT_EQ(
      1,
      CountSubstringInString(
          request_xml,
          "<event eventtype=\"3\" eventresult=\"0\" errorcode=\"62\"></event>"))
      << request_xml;
}

TEST_F(OmahaRequestBuilderXmlTest,
       GetRequestXmlUpdateCompleteEventAllDlcsExcluded) {
  params_.set_dlc_apps_params({
      {params_.GetDlcAppId("dlc_1"), {.updated = false}},
      {params_.GetDlcAppId("dlc_2"), {.updated = false}},
  });
  OmahaEvent event(OmahaEvent::kTypeUpdateComplete);
  OmahaRequestBuilderXml omaha_request{&event,
                                       false,
                                       false,
                                       0,
                                       0,
                                       0,
                                       ""};
  const string request_xml = omaha_request.GetRequest();
  EXPECT_EQ(
      1,
      CountSubstringInString(
          request_xml, "<event eventtype=\"3\" eventresult=\"1\"></event>"))
      << request_xml;
  EXPECT_EQ(
      2,
      CountSubstringInString(
          request_xml,
          "<event eventtype=\"3\" eventresult=\"0\" errorcode=\"62\"></event>"))
      << request_xml;
}

TEST_F(OmahaRequestBuilderXmlTest, GetRequestXmlDlcCohortMissingCheck) {
  constexpr char kDlcId[] = "test-dlc-id";
  params_.set_dlc_apps_params(
      {{params_.GetDlcAppId(kDlcId), {.name = kDlcId}}});
  OmahaEvent event(OmahaEvent::kTypeUpdateDownloadStarted);
  OmahaRequestBuilderXml omaha_request{&event, false, false, 0, 0, 0, ""};
  const string request_xml = omaha_request.GetRequest();

  // Check that no cohorts are in the request.
  EXPECT_EQ(0, CountSubstringInString(request_xml, "cohort=")) << request_xml;
  EXPECT_EQ(0, CountSubstringInString(request_xml, "cohortname="))
      << request_xml;
  EXPECT_EQ(0, CountSubstringInString(request_xml, "cohorthint="))
      << request_xml;
}

TEST_F(OmahaRequestBuilderXmlTest, GetRequestXmlDlcCohortCheck) {
  const string kDlcId = "test-dlc-id";
  params_.set_dlc_apps_params(
      {{params_.GetDlcAppId(kDlcId), {.name = kDlcId}}});
  auto* fake_prefs = FakeSystemState::Get()->fake_prefs();
  OmahaEvent event(OmahaEvent::kTypeUpdateDownloadStarted);
  OmahaRequestBuilderXml omaha_request{&event, false, false, 0, 0, 0, ""};
  // DLC App ID Expectations.
  const string dlc_cohort_key = PrefsInterface::CreateSubKey(
      {kDlcPrefsSubDir, kDlcId, kPrefsOmahaCohort});
  const string kDlcCohortVal = "test-cohort";
  EXPECT_TRUE(fake_prefs->SetString(dlc_cohort_key, kDlcCohortVal));
  const string dlc_cohort_name_key = PrefsInterface::CreateSubKey(
      {kDlcPrefsSubDir, kDlcId, kPrefsOmahaCohortName});
  const string kDlcCohortNameVal = "test-cohortname";
  EXPECT_TRUE(fake_prefs->SetString(dlc_cohort_name_key, kDlcCohortNameVal));
  const string dlc_cohort_hint_key = PrefsInterface::CreateSubKey(
      {kDlcPrefsSubDir, kDlcId, kPrefsOmahaCohortHint});
  const string kDlcCohortHintVal = "test-cohortval";
  EXPECT_TRUE(fake_prefs->SetString(dlc_cohort_hint_key, kDlcCohortHintVal));
  const string request_xml = omaha_request.GetRequest();

  EXPECT_EQ(1,
            CountSubstringInString(
                request_xml,
                base::StringPrintf(
                    "cohort=\"%s\" cohortname=\"%s\" cohorthint=\"%s\"",
                    kDlcCohortVal.c_str(),
                    kDlcCohortNameVal.c_str(),
                    kDlcCohortHintVal.c_str())))
      << request_xml;
}

}  // namespace chromeos_update_engine