summaryrefslogtreecommitdiffstats
path: root/components/resource_provider/file_utils_unittest.cc
blob: 914eddda8135375ec17e19f730db5d507b405675 (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
// Copyright 2015 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 "components/resource_provider/file_utils.h"

#include <stddef.h>

#include "base/files/file_path.h"
#include "base/macros.h"
#include "base/path_service.h"
#include "base/strings/string_util.h"
#include "testing/gtest/include/gtest/gtest.h"
#include "url/gurl.h"

namespace resource_provider {

// Assertions for invalid app paths.
TEST(FileUtilsTest, InvalidAppPath) {
  struct TestCase {
    std::string url;
  };
  struct TestCase invalid_cases[]{
      {"http://foo"},     // Must start with 'mojo:'.
      {"mojo://."},       // Don't allow '.'.
      {"mojo://.."},      // Don't allow '..'.
      {"mojo://foo/."},   // Don't allow '.'.
      {"mojo://bar/.."},  // Don't allow '..'.
  };

  for (size_t i = 0; i < arraysize(invalid_cases); ++i) {
    base::FilePath resulting_path(GetPathForApplicationUrl(
        invalid_cases[i].url));
    EXPECT_TRUE(resulting_path.empty()) << "i=" << i
                                        << " input=" << invalid_cases[i].url
                                        << " result=" << resulting_path.value();
  }
}

// Assertions for invalid app paths.
TEST(FileUtilsTest, InvalidResourcePath) {
  struct TestCase {
    std::string path;
  };
  struct TestCase invalid_cases[]{
      {"."},
      {".."},
      {"foo/."},
      {"bar/.."},
      {"foo/./bar"},
      {"bar/../baz"},
      {"bar/baz/"},
      {"bar//baz/"},
  };

  const base::FilePath app_path(GetPathForApplicationUrl("mojo:test"));
  ASSERT_FALSE(app_path.empty());

  for (size_t i = 0; i < arraysize(invalid_cases); ++i) {
    base::FilePath resulting_path(
        GetPathForResourceNamed(app_path, invalid_cases[i].path));
    EXPECT_TRUE(resulting_path.empty()) << i
                                        << " input=" << invalid_cases[i].path
                                        << " result=" << resulting_path.value();
  }
}

TEST(FileUtilsTest, ValidPaths) {
  const base::FilePath app_path(GetPathForApplicationUrl("mojo:test"));
  ASSERT_FALSE(app_path.empty());

  // Trivial single path element.
  const base::FilePath trivial_path(
      GetPathForResourceNamed(app_path, "single"));
  EXPECT_EQ(app_path.AppendASCII("single").value(), trivial_path.value());

  // Two path elements.
  const base::FilePath two_paths(GetPathForResourceNamed(app_path, "a/b"));
  EXPECT_EQ(app_path.AppendASCII("a").AppendASCII("b").value(),
            two_paths.value());
}

}  // namespace resource_provider