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