satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 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 | |
satorux@chromium.org | 473c9a4f | 2013-09-05 03:09:46 | [diff] [blame] | 5 | #include "chrome/browser/chromeos/file_manager/url_util.h" |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 6 | |
dcheng | 24002d0 | 2016-04-08 02:42:40 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 9 | #include "base/files/file_path.h" |
| 10 | #include "base/json/json_reader.h" |
| 11 | #include "base/json/json_writer.h" |
Satoshi Niwa | 481d94ec | 2019-04-30 07:46:02 | [diff] [blame] | 12 | #include "base/strings/stringprintf.h" |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 13 | #include "base/strings/utf_string_conversions.h" |
satorux@chromium.org | 0bce53f | 2013-08-14 14:40:15 | [diff] [blame] | 14 | #include "base/values.h" |
Catherine Mullings | 3b967fb | 2017-06-27 02:13:06 | [diff] [blame] | 15 | #include "extensions/common/constants.h" |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 16 | #include "net/base/escape.h" |
| 17 | #include "testing/gtest/include/gtest/gtest.h" |
| 18 | |
| 19 | namespace file_manager { |
| 20 | namespace util { |
| 21 | namespace { |
| 22 | |
Sam McNally | ae118e4 | 2018-10-16 09:33:52 | [diff] [blame] | 23 | // Parse a JSON query string into a base::Value. |
| 24 | base::Value ParseJsonQueryString(const std::string& query) { |
Matt Menke | 781e04d00 | 2019-05-05 23:56:12 | [diff] [blame] | 25 | const std::string json = net::UnescapeBinaryURLComponent(query); |
Lei Zhang | 582ecd1 | 2019-02-13 20:28:54 | [diff] [blame] | 26 | std::unique_ptr<base::Value> value = base::JSONReader::ReadDeprecated(json); |
Sam McNally | ae118e4 | 2018-10-16 09:33:52 | [diff] [blame] | 27 | return value ? std::move(*value) : base::Value(); |
| 28 | } |
| 29 | |
| 30 | // Pretty print the JSON escaped in the query string. |
| 31 | std::string PrettyPrintEscapedJson(const std::string& query) { |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 32 | std::string pretty_json; |
Sam McNally | ae118e4 | 2018-10-16 09:33:52 | [diff] [blame] | 33 | base::JSONWriter::WriteWithOptions(ParseJsonQueryString(query), |
| 34 | base::JSONWriter::OPTIONS_PRETTY_PRINT, |
| 35 | &pretty_json); |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 36 | return pretty_json; |
| 37 | } |
| 38 | |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 39 | TEST(FileManagerUrlUtilTest, GetFileManagerMainPageUrl) { |
Luciano Pacheco | 58cbde10 | 2021-05-31 06:03:09 | [diff] [blame^] | 40 | EXPECT_EQ("chrome-extension://hhaomjibdihmijegdhdafkllkbggdgoj/main.html", |
| 41 | GetFileManagerMainPageUrl().spec()); |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | TEST(FileManagerUrlUtilTest, GetFileManagerMainPageUrlWithParams_NoFileTypes) { |
| 45 | const GURL url = GetFileManagerMainPageUrlWithParams( |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 46 | ui::SelectFileDialog::SELECT_OPEN_FILE, u"some title", |
mtomasz@chromium.org | 304db10e | 2014-01-30 06:41:12 | [diff] [blame] | 47 | GURL("filesystem:chrome-extension://abc/Downloads/"), |
Stuart Langley | 0cd0da345 | 2018-05-10 09:58:58 | [diff] [blame] | 48 | GURL("filesystem:chrome-extension://abc/Downloads/foo.txt"), "foo.txt", |
| 49 | nullptr, // No file types |
| 50 | 0, // Hence no file type index. |
Satoshi Niwa | 5a533453 | 2020-09-16 11:01:30 | [diff] [blame] | 51 | "", // search_query |
| 52 | false // show_android_picker_apps |
Satoshi Niwa | 481d94ec | 2019-04-30 07:46:02 | [diff] [blame] | 53 | ); |
Catherine Mullings | 3b967fb | 2017-06-27 02:13:06 | [diff] [blame] | 54 | EXPECT_EQ(extensions::kExtensionScheme, url.scheme()); |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 55 | EXPECT_EQ("hhaomjibdihmijegdhdafkllkbggdgoj", url.host()); |
Luciano Pacheco | 58cbde10 | 2021-05-31 06:03:09 | [diff] [blame^] | 56 | EXPECT_EQ("/main.html", url.path()); |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 57 | // Confirm that "%20" is used instead of "+" in the query. |
| 58 | EXPECT_TRUE(url.query().find("+") == std::string::npos); |
| 59 | EXPECT_TRUE(url.query().find("%20") != std::string::npos); |
Sam McNally | ae118e4 | 2018-10-16 09:33:52 | [diff] [blame] | 60 | // With DriveFS, Drive is always allowed where native paths are. |
Satoshi Niwa | 481d94ec | 2019-04-30 07:46:02 | [diff] [blame] | 61 | EXPECT_EQ(base::StringPrintf( |
| 62 | "{\n" |
Austin Tankiang | 786c9a15 | 2019-11-26 04:54:29 | [diff] [blame] | 63 | " \"allowedPaths\": \"nativePath\",\n" |
Satoshi Niwa | 481d94ec | 2019-04-30 07:46:02 | [diff] [blame] | 64 | " \"currentDirectoryURL\": " |
| 65 | "\"filesystem:chrome-extension://abc/Downloads/\",\n" |
Satoshi Niwa | 5a533453 | 2020-09-16 11:01:30 | [diff] [blame] | 66 | " \"searchQuery\": \"\",\n" |
Satoshi Niwa | 481d94ec | 2019-04-30 07:46:02 | [diff] [blame] | 67 | " \"selectionURL\": " |
| 68 | "\"filesystem:chrome-extension://abc/Downloads/foo.txt\",\n" |
| 69 | " \"showAndroidPickerApps\": false,\n" |
| 70 | " \"targetName\": \"foo.txt\",\n" |
| 71 | " \"title\": \"some title\",\n" |
| 72 | " \"type\": \"open-file\"\n" |
Austin Tankiang | 53f411e5 | 2019-09-17 04:09:33 | [diff] [blame] | 73 | "}\n"), |
Satoshi Niwa | 481d94ec | 2019-04-30 07:46:02 | [diff] [blame] | 74 | PrettyPrintEscapedJson(url.query())); |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 75 | } |
| 76 | |
| 77 | TEST(FileManagerUrlUtilTest, |
| 78 | GetFileManagerMainPageUrlWithParams_WithFileTypes) { |
| 79 | // Create a FileTypeInfo which looks like: |
| 80 | // extensions: [["htm", "html"], ["txt"]] |
| 81 | // descriptions: ["HTML", "TEXT"] |
| 82 | ui::SelectFileDialog::FileTypeInfo file_types; |
Stuart Langley | 0cd0da345 | 2018-05-10 09:58:58 | [diff] [blame] | 83 | file_types.extensions.emplace_back(); |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 84 | file_types.extensions[0].push_back(FILE_PATH_LITERAL("htm")); |
| 85 | file_types.extensions[0].push_back(FILE_PATH_LITERAL("html")); |
Stuart Langley | 0cd0da345 | 2018-05-10 09:58:58 | [diff] [blame] | 86 | file_types.extensions.emplace_back(); |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 87 | file_types.extensions[1].push_back(FILE_PATH_LITERAL("txt")); |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 88 | file_types.extension_description_overrides.push_back(u"HTML"); |
| 89 | file_types.extension_description_overrides.push_back(u"TEXT"); |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 90 | // "shouldReturnLocalPath" will be false if drive is supported. |
hirono | 4d4d339 | 2016-02-04 05:03:50 | [diff] [blame] | 91 | file_types.allowed_paths = ui::SelectFileDialog::FileTypeInfo::ANY_PATH; |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 92 | |
| 93 | const GURL url = GetFileManagerMainPageUrlWithParams( |
Jan Wilken Dörrie | 2c470ea | 2021-03-22 22:26:24 | [diff] [blame] | 94 | ui::SelectFileDialog::SELECT_OPEN_FILE, u"some title", |
mtomasz@chromium.org | 304db10e | 2014-01-30 06:41:12 | [diff] [blame] | 95 | GURL("filesystem:chrome-extension://abc/Downloads/"), |
Satoshi Niwa | 481d94ec | 2019-04-30 07:46:02 | [diff] [blame] | 96 | GURL("filesystem:chrome-extension://abc/Downloads/foo.txt"), "foo.txt", |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 97 | &file_types, |
| 98 | 1, // The file type index is 1-based. |
Satoshi Niwa | 5a533453 | 2020-09-16 11:01:30 | [diff] [blame] | 99 | "search query", |
Satoshi Niwa | 481d94ec | 2019-04-30 07:46:02 | [diff] [blame] | 100 | true // show_android_picker_apps |
| 101 | ); |
Catherine Mullings | 3b967fb | 2017-06-27 02:13:06 | [diff] [blame] | 102 | EXPECT_EQ(extensions::kExtensionScheme, url.scheme()); |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 103 | EXPECT_EQ("hhaomjibdihmijegdhdafkllkbggdgoj", url.host()); |
Luciano Pacheco | 58cbde10 | 2021-05-31 06:03:09 | [diff] [blame^] | 104 | EXPECT_EQ("/main.html", url.path()); |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 105 | // Confirm that "%20" is used instead of "+" in the query. |
| 106 | EXPECT_TRUE(url.query().find("+") == std::string::npos); |
| 107 | EXPECT_TRUE(url.query().find("%20") != std::string::npos); |
| 108 | // The escaped query is hard to read. Pretty print the escaped JSON. |
hirono | e820f858 | 2016-02-18 02:39:52 | [diff] [blame] | 109 | EXPECT_EQ( |
| 110 | "{\n" |
| 111 | " \"allowedPaths\": \"anyPath\",\n" |
| 112 | " \"currentDirectoryURL\": " |
| 113 | "\"filesystem:chrome-extension://abc/Downloads/\",\n" |
hirono | e820f858 | 2016-02-18 02:39:52 | [diff] [blame] | 114 | " \"includeAllFiles\": false,\n" |
Satoshi Niwa | 5a533453 | 2020-09-16 11:01:30 | [diff] [blame] | 115 | " \"searchQuery\": \"search query\",\n" |
hirono | e820f858 | 2016-02-18 02:39:52 | [diff] [blame] | 116 | " \"selectionURL\": " |
| 117 | "\"filesystem:chrome-extension://abc/Downloads/foo.txt\",\n" |
Satoshi Niwa | 481d94ec | 2019-04-30 07:46:02 | [diff] [blame] | 118 | " \"showAndroidPickerApps\": true,\n" |
hirono | e820f858 | 2016-02-18 02:39:52 | [diff] [blame] | 119 | " \"targetName\": \"foo.txt\",\n" |
| 120 | " \"title\": \"some title\",\n" |
| 121 | " \"type\": \"open-file\",\n" |
| 122 | " \"typeList\": [ {\n" |
| 123 | " \"description\": \"HTML\",\n" |
| 124 | " \"extensions\": [ \"htm\", \"html\" ],\n" |
| 125 | " \"selected\": true\n" |
| 126 | " }, {\n" |
| 127 | " \"description\": \"TEXT\",\n" |
| 128 | " \"extensions\": [ \"txt\" ],\n" |
| 129 | " \"selected\": false\n" |
| 130 | " } ]\n" |
| 131 | "}\n", |
| 132 | PrettyPrintEscapedJson(url.query())); |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 133 | } |
| 134 | |
satorux@chromium.org | 9f632b8 | 2013-08-14 04:37:11 | [diff] [blame] | 135 | } // namespace |
| 136 | } // namespace util |
| 137 | } // namespace file_manager |