summaryrefslogtreecommitdiffstats
path: root/chrome/common/cloud_print/cloud_print_helpers_unittest.cc
blob: 662ee5847f4a513adc7b5e8a131e8f94b3ddd008 (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
// Copyright 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chrome/common/cloud_print/cloud_print_helpers.h"

#include "base/md5.h"
#include "base/strings/stringprintf.h"
#include "base/sys_info.h"
#include "chrome/common/chrome_version_info.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace cloud_print {

namespace {

void CheckURLs(const GURL& server_base_url) {
  std::string expected_url_base = server_base_url.spec();
  if (expected_url_base[expected_url_base.length() - 1] != '/')
    expected_url_base += "/";

  EXPECT_EQ(base::StringPrintf("%ssearch", expected_url_base.c_str()),
            GetUrlForSearch(server_base_url).spec());

  EXPECT_EQ(base::StringPrintf("%ssubmit", expected_url_base.c_str()),
            GetUrlForSubmit(server_base_url).spec());

  EXPECT_EQ(base::StringPrintf("%slist?proxy=demoproxy",
                expected_url_base.c_str()),
            GetUrlForPrinterList(
                server_base_url, std::string("demoproxy")).spec());

  EXPECT_EQ(base::StringPrintf("%sregister", expected_url_base.c_str()),
            GetUrlForPrinterRegistration(server_base_url).spec());

  EXPECT_EQ(base::StringPrintf("%supdate?printerid=printeridfoo",
                expected_url_base.c_str()),
            GetUrlForPrinterUpdate(server_base_url, "printeridfoo").spec());

  EXPECT_EQ(base::StringPrintf("%sdelete?printerid=printeridbar&reason=deleted",
                expected_url_base.c_str()),
            GetUrlForPrinterDelete(
                server_base_url, "printeridbar", "deleted").spec());

  EXPECT_EQ(base::StringPrintf("%sfetch?printerid=myprinter&deb=nogoodreason",
                expected_url_base.c_str()),
            GetUrlForJobFetch(
                server_base_url, "myprinter", "nogoodreason").spec());

  EXPECT_EQ(base::StringPrintf("%sdeletejob?jobid=myprinter",
                expected_url_base.c_str()),
            GetUrlForJobDelete(server_base_url, "myprinter").spec());

  EXPECT_EQ(base::StringPrintf(
                "%scontrol?jobid=myprinter&status=s1&connector_code=0",
                expected_url_base.c_str()),
            GetUrlForJobStatusUpdate(
                server_base_url, "myprinter", "s1", 0).spec());

  EXPECT_EQ(base::StringPrintf("%smessage?code=testmsg",
                expected_url_base.c_str()),
            GetUrlForUserMessage(server_base_url, "testmsg").spec());

  EXPECT_EQ(base::StringPrintf(
                "%screaterobot?oauth_client_id=democlientid&proxy=demoproxy",
                expected_url_base.c_str()),
            GetUrlForGetAuthCode(
                server_base_url, "democlientid", "demoproxy").spec());
}

}  // namespace

TEST(CloudPrintHelpersTest, GetURLs) {
  CheckURLs(GURL("https://www.google.com/cloudprint"));
  CheckURLs(GURL("https://www.google.com/cloudprint/"));
  CheckURLs(GURL("http://www.myprinterserver.com"));
  CheckURLs(GURL("http://www.myprinterserver.com/"));
}

TEST(CloudPrintHelpersTest, GetHashOfPrinterTags) {
  PrinterTags printer_tags;
  printer_tags["tag1"] = std::string("value1");
  printer_tags["tag2"] = std::string("value2");

  chrome::VersionInfo version_info;
  std::string expected_list_string = base::StringPrintf(
      "chrome_version%ssystem_name%ssystem_version%stag1value1tag2value2",
      version_info.CreateVersionString().c_str(),
      base::SysInfo::OperatingSystemName().c_str(),
      base::SysInfo::OperatingSystemVersion().c_str());
  EXPECT_EQ(base::MD5String(expected_list_string),
            GetHashOfPrinterTags(printer_tags));
}

TEST(CloudPrintHelpersTest, GetPostDataForPrinterTags) {
  PrinterTags printer_tags;
  printer_tags["tag1"] = std::string("value1");
  printer_tags["tag2"] = std::string("value2");

  chrome::VersionInfo version_info;
  std::string expected = base::StringPrintf(
      "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
      "\r\n\r\n__test__chrome_version=%s\r\n"
      "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
      "\r\n\r\n__test__system_name=%s\r\n"
      "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
      "\r\n\r\n__test__system_version=%s\r\n"
      "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
      "\r\n\r\n__test__tag1=value1\r\n"
      "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
      "\r\n\r\n__test__tag2=value2\r\n"
      "--test_mime_boundary\r\nContent-Disposition: form-data; name=\"tag\""
      "\r\n\r\n__test__tagshash=%s\r\n",
      version_info.CreateVersionString().c_str(),
      base::SysInfo::OperatingSystemName().c_str(),
      base::SysInfo::OperatingSystemVersion().c_str(),
      GetHashOfPrinterTags(printer_tags).c_str());

  EXPECT_EQ(expected, GetPostDataForPrinterTags(
      printer_tags,
      std::string("test_mime_boundary"),
      std::string("__test__"),
      std::string("__test__tagshash")));
}

TEST(CloudPrintHelpersTest, GetCloudPrintAuthHeader) {
  std::string test_auth("testauth");
  EXPECT_EQ("Authorization: OAuth testauth",
            GetCloudPrintAuthHeader(test_auth));
}

}  // namespace cloud_print