[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 1 | // 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 "chrome/browser/shell_integration.h" |
| 6 | |
| 7 | #include "base/file_path.h" |
| 8 | #include "base/string_util.h" |
[email protected] | 4289680 | 2009-08-28 23:39:44 | [diff] [blame] | 9 | #include "chrome/common/chrome_constants.h" |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 10 | #include "googleurl/src/gurl.h" |
| 11 | #include "testing/gtest/include/gtest/gtest.h" |
| 12 | |
| 13 | #define FPL FILE_PATH_LITERAL |
| 14 | |
| 15 | #if defined(OS_LINUX) |
| 16 | TEST(ShellIntegrationTest, GetDesktopShortcutFilename) { |
| 17 | const struct { |
| 18 | const FilePath::CharType* path; |
| 19 | const char* url; |
| 20 | } test_cases[] = { |
| 21 | { FPL("http___foo_.desktop"), "http://foo" }, |
| 22 | { FPL("http___foo_bar_.desktop"), "http://foo/bar/" }, |
| 23 | { FPL("http___foo_bar_a=b&c=d.desktop"), "http://foo/bar?a=b&c=d" }, |
| 24 | |
| 25 | // Now we're starting to be more evil... |
| 26 | { FPL("http___foo_.desktop"), "http://foo/bar/baz/../../../../../" }, |
| 27 | { FPL("http___foo_.desktop"), "http://foo/bar/././../baz/././../" }, |
| 28 | { FPL("http___.._.desktop"), "http://../../../../" }, |
| 29 | }; |
| 30 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); i++) { |
[email protected] | 4289680 | 2009-08-28 23:39:44 | [diff] [blame] | 31 | EXPECT_EQ(WideToASCII(chrome::kBrowserProcessExecutableName) + "-" + |
| 32 | test_cases[i].path, |
| 33 | ShellIntegration::GetDesktopShortcutFilename( |
| 34 | GURL(test_cases[i].url)).value()) << |
| 35 | " while testing " << test_cases[i].url; |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 36 | } |
| 37 | } |
| 38 | |
| 39 | TEST(ShellIntegrationTest, GetDesktopFileContents) { |
| 40 | const struct { |
| 41 | const char* url; |
| 42 | const char* title; |
| 43 | const char* template_contents; |
| 44 | const char* expected_output; |
| 45 | } test_cases[] = { |
| 46 | // Dumb case. |
[email protected] | 82810fe1 | 2009-09-25 16:21:57 | [diff] [blame^] | 47 | { "ignored", "ignored", "", "#!/usr/bin/env xdg-open\n" }, |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 48 | |
| 49 | // Real-world case. |
| 50 | { "http://gmail.com", |
| 51 | "GMail", |
| 52 | |
| 53 | "[Desktop Entry]\n" |
| 54 | "Version=1.0\n" |
| 55 | "Encoding=UTF-8\n" |
| 56 | "Name=Google Chrome\n" |
| 57 | "Comment=The web browser from Google\n" |
| 58 | "Exec=/opt/google/chrome/google-chrome %U\n" |
| 59 | "Terminal=false\n" |
| 60 | "Icon=/opt/google/chrome/product_logo_48.png\n" |
| 61 | "Type=Application\n" |
| 62 | "Categories=Application;Network;WebBrowser;\n" |
| 63 | "MimeType=text/html;text/xml;application/xhtml_xml;\n", |
| 64 | |
[email protected] | 82810fe1 | 2009-09-25 16:21:57 | [diff] [blame^] | 65 | "#!/usr/bin/env xdg-open\n" |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 66 | "[Desktop Entry]\n" |
| 67 | "Version=1.0\n" |
| 68 | "Encoding=UTF-8\n" |
| 69 | "Name=GMail\n" |
| 70 | "Exec=/opt/google/chrome/google-chrome \"--app=http://gmail.com/\"\n" |
| 71 | "Terminal=false\n" |
| 72 | "Icon=/opt/google/chrome/product_logo_48.png\n" |
| 73 | "Type=Application\n" |
| 74 | "Categories=Application;Network;WebBrowser;\n" |
| 75 | "MimeType=text/html;text/xml;application/xhtml_xml;\n" |
| 76 | }, |
| 77 | |
[email protected] | 82810fe1 | 2009-09-25 16:21:57 | [diff] [blame^] | 78 | // Make sure we don't insert duplicate shebangs. |
| 79 | { "http://gmail.com", |
| 80 | "GMail", |
| 81 | |
| 82 | "#!/some/shebang\n" |
| 83 | "Name=Google Chrome\n" |
| 84 | "Exec=/opt/google/chrome/google-chrome %U\n", |
| 85 | |
| 86 | "#!/usr/bin/env xdg-open\n" |
| 87 | "Name=GMail\n" |
| 88 | "Exec=/opt/google/chrome/google-chrome \"--app=http://gmail.com/\"\n" |
| 89 | }, |
| 90 | |
| 91 | // Make sure i18n-ed comments are removed. |
| 92 | { "http://gmail.com", |
| 93 | "GMail", |
| 94 | |
| 95 | "Name=Google Chrome\n" |
| 96 | "Exec=/opt/google/chrome/google-chrome %U\n" |
| 97 | "Comment[pl]=Jakis komentarz.\n", |
| 98 | |
| 99 | "#!/usr/bin/env xdg-open\n" |
| 100 | "Name=GMail\n" |
| 101 | "Exec=/opt/google/chrome/google-chrome \"--app=http://gmail.com/\"\n" |
| 102 | }, |
| 103 | |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 104 | // Now we're starting to be more evil... |
| 105 | { "http://evil.com/evil --join-the-b0tnet", |
| 106 | "Ownz0red\nExec=rm -rf /", |
| 107 | |
| 108 | "Name=Google Chrome\n" |
| 109 | "Exec=/opt/google/chrome/google-chrome %U\n", |
| 110 | |
[email protected] | 82810fe1 | 2009-09-25 16:21:57 | [diff] [blame^] | 111 | "#!/usr/bin/env xdg-open\n" |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 112 | "Name=http://evil.com/evil%20--join-the-b0tnet\n" |
| 113 | "Exec=/opt/google/chrome/google-chrome " |
| 114 | "\"--app=http://evil.com/evil%%20--join-the-b0tnet\"\n" |
| 115 | }, |
[email protected] | 82810fe1 | 2009-09-25 16:21:57 | [diff] [blame^] | 116 | { "http://evil.com/evil; rm -rf /; \"; rm -rf $HOME >ownz0red", |
| 117 | "Innocent Title", |
| 118 | |
| 119 | "Name=Google Chrome\n" |
| 120 | "Exec=/opt/google/chrome/google-chrome %U\n", |
| 121 | |
| 122 | "#!/usr/bin/env xdg-open\n" |
| 123 | "Name=Innocent Title\n" |
| 124 | "Exec=/opt/google/chrome/google-chrome " |
| 125 | "\"--app=http://evil.com/evil%%20rm%%20-rf%%20/%%20%%22%%20rm%%20" |
| 126 | "-rf%%20HOME%%20%%3Eownz0red\"\n" |
| 127 | }, |
| 128 | { "http://evil.com/evil | cat `echo ownz0red` >/dev/null\\", |
| 129 | "Innocent Title", |
| 130 | |
| 131 | "Name=Google Chrome\n" |
| 132 | "Exec=/opt/google/chrome/google-chrome %U\n", |
| 133 | |
| 134 | "#!/usr/bin/env xdg-open\n" |
| 135 | "Name=Innocent Title\n" |
| 136 | "Exec=/opt/google/chrome/google-chrome " |
| 137 | "\"--app=http://evil.com/evil%%20%%7C%%20cat%%20%%60echo%%20ownz0red" |
| 138 | "%%60%%20%%3E/dev/null/\"\n" |
| 139 | }, |
[email protected] | b96aa93 | 2009-08-12 21:34:49 | [diff] [blame] | 140 | }; |
| 141 | for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); i++) { |
| 142 | EXPECT_EQ(test_cases[i].expected_output, |
| 143 | ShellIntegration::GetDesktopFileContents( |
| 144 | test_cases[i].template_contents, |
| 145 | GURL(test_cases[i].url), |
| 146 | ASCIIToUTF16(test_cases[i].title))); |
| 147 | } |
| 148 | } |
| 149 | #endif // defined(OS_LINUX) |