test_manifest_xml.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. # -*- coding:utf-8 -*-
  2. #
  3. # Copyright (C) 2019 The Android Open Source Project
  4. #
  5. # Licensed under the Apache License, Version 2.0 (the "License");
  6. # you may not use this file except in compliance with the License.
  7. # You may obtain a copy of the License at
  8. #
  9. # http://www.apache.org/licenses/LICENSE-2.0
  10. #
  11. # Unless required by applicable law or agreed to in writing, software
  12. # distributed under the License is distributed on an "AS IS" BASIS,
  13. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. # See the License for the specific language governing permissions and
  15. # limitations under the License.
  16. """Unittests for the manifest_xml.py module."""
  17. from __future__ import print_function
  18. import os
  19. import unittest
  20. import error
  21. import manifest_xml
  22. class ManifestValidateFilePaths(unittest.TestCase):
  23. """Check _ValidateFilePaths helper.
  24. This doesn't access a real filesystem.
  25. """
  26. def check_both(self, *args):
  27. manifest_xml.XmlManifest._ValidateFilePaths('copyfile', *args)
  28. manifest_xml.XmlManifest._ValidateFilePaths('linkfile', *args)
  29. def test_normal_path(self):
  30. """Make sure good paths are accepted."""
  31. self.check_both('foo', 'bar')
  32. self.check_both('foo/bar', 'bar')
  33. self.check_both('foo', 'bar/bar')
  34. self.check_both('foo/bar', 'bar/bar')
  35. def test_symlink_targets(self):
  36. """Some extra checks for symlinks."""
  37. def check(*args):
  38. manifest_xml.XmlManifest._ValidateFilePaths('linkfile', *args)
  39. # We allow symlinks to end in a slash since we allow them to point to dirs
  40. # in general. Technically the slash isn't necessary.
  41. check('foo/', 'bar')
  42. # We allow a single '.' to get a reference to the project itself.
  43. check('.', 'bar')
  44. def test_bad_paths(self):
  45. """Make sure bad paths (src & dest) are rejected."""
  46. PATHS = (
  47. '..',
  48. '../',
  49. './',
  50. 'foo/',
  51. './foo',
  52. '../foo',
  53. 'foo/./bar',
  54. 'foo/../../bar',
  55. '/foo',
  56. './../foo',
  57. '.git/foo',
  58. # Check case folding.
  59. '.GIT/foo',
  60. 'blah/.git/foo',
  61. '.repo/foo',
  62. '.repoconfig',
  63. # Block ~ due to 8.3 filenames on Windows filesystems.
  64. '~',
  65. 'foo~',
  66. 'blah/foo~',
  67. # Block Unicode characters that get normalized out by filesystems.
  68. u'foo\u200Cbar',
  69. )
  70. # Make sure platforms that use path separators (e.g. Windows) are also
  71. # rejected properly.
  72. if os.path.sep != '/':
  73. PATHS += tuple(x.replace('/', os.path.sep) for x in PATHS)
  74. for path in PATHS:
  75. self.assertRaises(
  76. error.ManifestInvalidPathError, self.check_both, path, 'a')
  77. self.assertRaises(
  78. error.ManifestInvalidPathError, self.check_both, 'a', path)