Fix CanonicalizeURL for file schema (#48466)
The tonic method currently returns `///Users/test/foo` instead of `file:///Users/test/foo`.
Found while looking into making isolate spawning for flutter_tester work.
diff --git a/ci/licenses_golden/excluded_files b/ci/licenses_golden/excluded_files
index 89c7c9d..e4dfe04 100644
--- a/ci/licenses_golden/excluded_files
+++ b/ci/licenses_golden/excluded_files
@@ -1046,6 +1046,7 @@
../../../flutter/third_party/tonic/AUTHORS
../../../flutter/third_party/tonic/PATENTS
../../../flutter/third_party/tonic/README.md
+../../../flutter/third_party/tonic/file_loader/file_loader_unittests.cc
../../../flutter/third_party/tonic/filesystem/README.md
../../../flutter/third_party/tonic/filesystem/tests
../../../flutter/third_party/tonic/tests
diff --git a/third_party/tonic/file_loader/file_loader.cc b/third_party/tonic/file_loader/file_loader.cc
index 4a9e179..be7f93e 100644
--- a/third_party/tonic/file_loader/file_loader.cc
+++ b/third_party/tonic/file_loader/file_loader.cc
@@ -136,8 +136,10 @@
return url;
if (string.find(kPackageScheme) == 0u)
return StdStringToDart(SanitizePath(string));
- if (string.find(kFileScheme) == 0u)
- return StdStringToDart(SanitizePath(CanonicalizeFileURL(string)));
+
+ if (string.find(kFileScheme) == 0u) {
+ return StdStringToDart(SanitizeURIEscapedCharacters(string));
+ }
std::string library_url = StdStringFromDart(Dart_LibraryUrl(library));
std::string prefix = ExtractSchemePrefix(library_url);
diff --git a/third_party/tonic/file_loader/file_loader.h b/third_party/tonic/file_loader/file_loader.h
index 8b88741..355ab1b 100644
--- a/third_party/tonic/file_loader/file_loader.h
+++ b/third_party/tonic/file_loader/file_loader.h
@@ -46,7 +46,6 @@
private:
static std::string SanitizeURIEscapedCharacters(const std::string& str);
static std::string SanitizePath(const std::string& path);
- static std::string CanonicalizeFileURL(const std::string& url);
std::string GetFilePathForURL(std::string url);
std::string GetFilePathForPackageURL(std::string url);
diff --git a/third_party/tonic/file_loader/file_loader_fuchsia.cc b/third_party/tonic/file_loader/file_loader_fuchsia.cc
index 359f88d..be2ff8b 100644
--- a/third_party/tonic/file_loader/file_loader_fuchsia.cc
+++ b/third_party/tonic/file_loader/file_loader_fuchsia.cc
@@ -26,20 +26,10 @@
const size_t FileLoader::kFileURLPrefixLength =
sizeof(FileLoader::kFileURLPrefix) - 1;
-namespace {
-
-const size_t kFileSchemeLength = FileLoader::kFileURLPrefixLength - 2;
-
-} // namespace
-
std::string FileLoader::SanitizePath(const std::string& url) {
return SanitizeURIEscapedCharacters(url);
}
-std::string FileLoader::CanonicalizeFileURL(const std::string& url) {
- return url.substr(kFileSchemeLength);
-}
-
bool FileLoader::ReadFileToString(const std::string& path,
std::string* result) {
if (dirfd_ == -1)
diff --git a/third_party/tonic/file_loader/file_loader_posix.cc b/third_party/tonic/file_loader/file_loader_posix.cc
index f5167f6..b567996 100644
--- a/third_party/tonic/file_loader/file_loader_posix.cc
+++ b/third_party/tonic/file_loader/file_loader_posix.cc
@@ -21,20 +21,10 @@
const size_t FileLoader::kFileURLPrefixLength =
sizeof(FileLoader::kFileURLPrefix) - 1;
-namespace {
-
-const size_t kFileSchemeLength = FileLoader::kFileURLPrefixLength - 2;
-
-} // namespace
-
std::string FileLoader::SanitizePath(const std::string& url) {
return SanitizeURIEscapedCharacters(url);
}
-std::string FileLoader::CanonicalizeFileURL(const std::string& url) {
- return url.substr(kFileSchemeLength);
-}
-
bool FileLoader::ReadFileToString(const std::string& path,
std::string* result) {
TONIC_DCHECK(dirfd_ == -1);
diff --git a/third_party/tonic/file_loader/file_loader_unittests.cc b/third_party/tonic/file_loader/file_loader_unittests.cc
new file mode 100644
index 0000000..2ca8489
--- /dev/null
+++ b/third_party/tonic/file_loader/file_loader_unittests.cc
@@ -0,0 +1,46 @@
+// Copyright 2013 The Flutter 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 "flutter/testing/dart_isolate_runner.h"
+#include "flutter/testing/fixture_test.h"
+
+#include "tonic/converter/dart_converter.h"
+#include "tonic/file_loader/file_loader.h"
+
+namespace flutter {
+namespace testing {
+
+using FileLoaderTest = FixtureTest;
+
+TEST_F(FileLoaderTest, CanonicalizesFileUrlCorrectly) {
+ ASSERT_FALSE(DartVMRef::IsInstanceRunning());
+ auto settings = CreateSettingsForFixture();
+ auto vm_snapshot = DartSnapshot::VMSnapshotFromSettings(settings);
+ auto isolate_snapshot = DartSnapshot::IsolateSnapshotFromSettings(settings);
+ auto vm_ref = DartVMRef::Create(settings, vm_snapshot, isolate_snapshot);
+ ASSERT_TRUE(vm_ref);
+
+ TaskRunners task_runners(GetCurrentTestName(), //
+ GetCurrentTaskRunner(), //
+ GetCurrentTaskRunner(), //
+ GetCurrentTaskRunner(), //
+ GetCurrentTaskRunner() //
+ );
+ auto isolate = RunDartCodeInIsolate(vm_ref, settings, task_runners, "main",
+ {}, GetDefaultKernelFilePath());
+ ASSERT_TRUE(isolate);
+
+ ASSERT_TRUE(isolate->RunInIsolateScope([]() {
+ tonic::FileLoader file_loader;
+ std::string original_url = "file:///Users/test/foo";
+ Dart_Handle dart_url = tonic::StdStringToDart(original_url);
+ auto canonicalized_url = file_loader.CanonicalizeURL(Dart_Null(), dart_url);
+ EXPECT_TRUE(canonicalized_url);
+ EXPECT_EQ(tonic::StdStringFromDart(canonicalized_url), original_url);
+ return true;
+ }));
+}
+
+} // namespace testing
+} // namespace flutter
diff --git a/third_party/tonic/file_loader/file_loader_win.cc b/third_party/tonic/file_loader/file_loader_win.cc
index f312a0a..a6d7322 100644
--- a/third_party/tonic/file_loader/file_loader_win.cc
+++ b/third_party/tonic/file_loader/file_loader_win.cc
@@ -39,10 +39,6 @@
return SanitizeURIEscapedCharacters(sanitized);
}
-std::string FileLoader::CanonicalizeFileURL(const std::string& url) {
- return SanitizePath(url.substr(FileLoader::kFileURLPrefixLength));
-}
-
bool FileLoader::ReadFileToString(const std::string& path,
std::string* result) {
TONIC_DCHECK(dirfd_ == -1);
diff --git a/third_party/tonic/tests/BUILD.gn b/third_party/tonic/tests/BUILD.gn
index eff79c4..84f1c3c 100644
--- a/third_party/tonic/tests/BUILD.gn
+++ b/third_party/tonic/tests/BUILD.gn
@@ -15,6 +15,7 @@
public_configs = [ "//flutter:export_dynamic_symbols" ]
sources = [
+ "../file_loader/file_loader_unittests.cc",
"dart_persistent_handle_unittest.cc",
"dart_state_unittest.cc",
"dart_weak_persistent_handle_unittest.cc",