test_manifest_xml.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. def test_bad_paths(self):
  42. """Make sure bad paths (src & dest) are rejected."""
  43. PATHS = (
  44. '..',
  45. '../',
  46. './',
  47. 'foo/',
  48. './foo',
  49. '../foo',
  50. 'foo/./bar',
  51. 'foo/../../bar',
  52. '/foo',
  53. './../foo',
  54. '.git/foo',
  55. # Check case folding.
  56. '.GIT/foo',
  57. 'blah/.git/foo',
  58. '.repo/foo',
  59. '.repoconfig',
  60. # Block ~ due to 8.3 filenames on Windows filesystems.
  61. '~',
  62. 'foo~',
  63. 'blah/foo~',
  64. # Block Unicode characters that get normalized out by filesystems.
  65. u'foo\u200Cbar',
  66. )
  67. for path in PATHS:
  68. self.assertRaises(
  69. error.ManifestInvalidPathError, self.check_both, path, 'a')
  70. self.assertRaises(
  71. error.ManifestInvalidPathError, self.check_both, 'a', path)