test_manifest_xml.py 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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 unittest
  19. import error
  20. import manifest_xml
  21. class ManifestValidateFilePaths(unittest.TestCase):
  22. """Check _ValidateFilePaths helper.
  23. This doesn't access a real filesystem.
  24. """
  25. def check_both(self, *args):
  26. manifest_xml.XmlManifest._ValidateFilePaths('copyfile', *args)
  27. manifest_xml.XmlManifest._ValidateFilePaths('linkfile', *args)
  28. def test_normal_path(self):
  29. """Make sure good paths are accepted."""
  30. self.check_both('foo', 'bar')
  31. self.check_both('foo/bar', 'bar')
  32. self.check_both('foo', 'bar/bar')
  33. self.check_both('foo/bar', 'bar/bar')
  34. def test_symlink_targets(self):
  35. """Some extra checks for symlinks."""
  36. def check(*args):
  37. manifest_xml.XmlManifest._ValidateFilePaths('linkfile', *args)
  38. # We allow symlinks to end in a slash since we allow them to point to dirs
  39. # in general. Technically the slash isn't necessary.
  40. check('foo/', 'bar')
  41. # We allow a single '.' to get a reference to the project itself.
  42. check('.', 'bar')
  43. def test_bad_paths(self):
  44. """Make sure bad paths (src & dest) are rejected."""
  45. PATHS = (
  46. '..',
  47. '../',
  48. './',
  49. 'foo/',
  50. './foo',
  51. '../foo',
  52. 'foo/./bar',
  53. 'foo/../../bar',
  54. '/foo',
  55. './../foo',
  56. '.git/foo',
  57. # Check case folding.
  58. '.GIT/foo',
  59. 'blah/.git/foo',
  60. '.repo/foo',
  61. '.repoconfig',
  62. # Block ~ due to 8.3 filenames on Windows filesystems.
  63. '~',
  64. 'foo~',
  65. 'blah/foo~',
  66. # Block Unicode characters that get normalized out by filesystems.
  67. u'foo\u200Cbar',
  68. )
  69. for path in PATHS:
  70. self.assertRaises(
  71. error.ManifestInvalidPathError, self.check_both, path, 'a')
  72. self.assertRaises(
  73. error.ManifestInvalidPathError, self.check_both, 'a', path)