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/service/cloud_print/cloud_print_service_helpers.h"
6
7#include "base/md5.h"
8#include "base/strings/stringprintf.h"
9#include "base/sys_info.h"
10#include "chrome/common/chrome_version_info.h"
11#include "testing/gtest/include/gtest/gtest.h"
12
13namespace cloud_print {
14
15namespace {
16
17void CheckJobStatusURLs(const GURL& server_base_url) {
18  std::string expected_url_base = server_base_url.spec();
19  if (expected_url_base[expected_url_base.length() - 1] != '/')
20    expected_url_base += "/";
21
22  EXPECT_EQ(base::StringPrintf(
23                "%scontrol?jobid=87654321&status=ERROR&connector_code=1",
24                expected_url_base.c_str()),
25            GetUrlForJobStatusUpdate(server_base_url, "87654321",
26                PRINT_JOB_STATUS_ERROR, 1).spec());
27
28  PrintJobDetails details;
29  details.status = PRINT_JOB_STATUS_IN_PROGRESS;
30  details.platform_status_flags = 2;
31  details.status_message = "Out of Paper";
32  details.total_pages = 345;
33  details.pages_printed = 47;
34  EXPECT_EQ(base::StringPrintf(
35                "%scontrol?jobid=87654321&status=IN_PROGRESS&code=2"
36                "&message=Out%%20of%%20Paper&numpages=345&pagesprinted=47",
37                expected_url_base.c_str()),
38            GetUrlForJobStatusUpdate(
39                server_base_url, "87654321", details).spec());
40}
41
42}  // namespace
43
44TEST(CloudPrintServiceHelpersTest, GetURLs) {
45  CheckJobStatusURLs(GURL("https://www.google.com/cloudprint"));
46  CheckJobStatusURLs(GURL("https://www.google.com/cloudprint/"));
47  CheckJobStatusURLs(GURL("http://www.myprinterserver.com"));
48  CheckJobStatusURLs(GURL("http://www.myprinterserver.com/"));
49}
50
51TEST(CloudPrintServiceHelpersTest, GetHashOfPrinterInfo) {
52  printing::PrinterBasicInfo printer_info;
53  printer_info.options["tag1"] = std::string("value1");
54  printer_info.options["tag2"] = std::string("value2");
55
56  chrome::VersionInfo version_info;
57  std::string expected_list_string = base::StringPrintf(
58      "chrome_version%ssystem_name%ssystem_version%stag1value1tag2value2",
59      version_info.CreateVersionString().c_str(),
60      base::SysInfo::OperatingSystemName().c_str(),
61      base::SysInfo::OperatingSystemVersion().c_str());
62  EXPECT_EQ(base::MD5String(expected_list_string),
63            GetHashOfPrinterInfo(printer_info));
64}
65
66TEST(CloudPrintServiceHelpersTest, GetPostDataForPrinterInfo) {
67  printing::PrinterBasicInfo printer_info;
68  printer_info.options["tag1"] = std::string("value1");
69  printer_info.options["tag2"] = std::string("value2");
70
71  chrome::VersionInfo version_info;
72  std::string expected = base::StringPrintf(
73      "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
74      "\r\n\r\n__cp__chrome_version=%s\r\n"
75      "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
76      "\r\n\r\n__cp__system_name=%s\r\n"
77      "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
78      "\r\n\r\n__cp__system_version=%s\r\n"
79      "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
80      "\r\n\r\n__cp__tag1=value1\r\n"
81      "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
82      "\r\n\r\n__cp__tag2=value2\r\n"
83      "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
84      "\r\n\r\n__cp__tagshash=%s\r\n",
85      version_info.CreateVersionString().c_str(),
86      base::SysInfo::OperatingSystemName().c_str(),
87      base::SysInfo::OperatingSystemVersion().c_str(),
88      GetHashOfPrinterInfo(printer_info).c_str());
89
90  EXPECT_EQ(expected, GetPostDataForPrinterInfo(
91      printer_info, std::string("test_mime_boundary")));
92}
93
94TEST(CloudPrintServiceHelpersTest, IsDryRunJob) {
95  std::vector<std::string> tags_not_dry_run;
96  tags_not_dry_run.push_back("tag_1");
97  EXPECT_FALSE(IsDryRunJob(tags_not_dry_run));
98
99  std::vector<std::string> tags_dry_run;
100  tags_dry_run.push_back("__cp__dry_run");
101  EXPECT_TRUE(IsDryRunJob(tags_dry_run));
102}
103
104}  // namespace cloud_print
105
106