|
| 1 | +# Copyright 2020 Microsoft Corporation |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | +# |
| 15 | +# Requires Python 2.7+ |
| 16 | + |
| 17 | +import unittest |
| 18 | + |
| 19 | +from core.src.Utility import Utility |
| 20 | + |
| 21 | + |
| 22 | +class TestUtility(unittest.TestCase): |
| 23 | + |
| 24 | + def setUp(self): |
| 25 | + self.utility = Utility() |
| 26 | + |
| 27 | + def tearDown(self): |
| 28 | + pass |
| 29 | + |
| 30 | + def test_sanitize_credentials_from_uri_https(self): |
| 31 | + """ Test sanitization of HTTPS URIs with credentials """ |
| 32 | + message = "Error connecting to https://testuser:TESTTOKEN123456@invalid.repo.example/rpm/repodata/repomd.xml" |
| 33 | + sanitized = self.utility.sanitize_credentials_from_uri(message) |
| 34 | + expected_message = "Error connecting to https://testuser@invalid.repo.example/rpm/repodata/repomd.xml" |
| 35 | + self.assertEqual(sanitized, expected_message) |
| 36 | + |
| 37 | + def test_sanitize_credentials_from_uri_http(self): |
| 38 | + """ Test sanitization of HTTP URIs with credentials """ |
| 39 | + message = "Connection failed to http://user123:password123@example.com/path" |
| 40 | + sanitized = self.utility.sanitize_credentials_from_uri(message) |
| 41 | + # Password should be removed |
| 42 | + self.assertNotIn("password123", sanitized) |
| 43 | + # Username should be preserved |
| 44 | + self.assertIn("user123@example.com", sanitized) |
| 45 | + |
| 46 | + def test_sanitize_credentials_multiple_urls(self): |
| 47 | + """ Test sanitization with multiple URLs containing credentials """ |
| 48 | + message = "Failed to fetch from https://user1:pass1@host1.com/api and http://user2:pass2@host2.com/data" |
| 49 | + sanitized = self.utility.sanitize_credentials_from_uri(message) |
| 50 | + # Passwords should be removed |
| 51 | + self.assertNotIn("pass1", sanitized) |
| 52 | + self.assertNotIn("pass2", sanitized) |
| 53 | + # Usernames should be preserved |
| 54 | + self.assertIn("user1@host1.com", sanitized) |
| 55 | + self.assertIn("user2@host2.com", sanitized) |
| 56 | + |
| 57 | + def test_sanitize_credentials_jfrog_repo_error(self): |
| 58 | + """ ERROR with 401 status code from jfrog.io """ |
| 59 | + message = "ERROR: Failed to download metadata for repo 'packages-microsoft-com-prod': Status code: 401 for https://cec-aa.jfrog.io/artifactory/glib-rpm-hel9-lts-microsoft-com/repodata/repomd.xml" |
| 60 | + sanitized = self.utility.sanitize_credentials_from_uri(message) |
| 61 | + expected_message = "ERROR: Failed to download metadata for repo 'packages-microsoft-com-prod': Status code: 401 for https://cec-aa.jfrog.io/artifactory/glib-rpm-hel9-lts-microsoft-com/repodata/repomd.xml" |
| 62 | + self.assertEqual(sanitized, expected_message) |
| 63 | + |
| 64 | + def test_sanitize_credentials_curl_error_buildbot_token(self): |
| 65 | + """ Curl error with buildbot:BuildBotToken credentials """ |
| 66 | + message = ("Curl error (6): Couldn't resolve host 'packages.microsoft.com' Could not " |
| 67 | + "retrieve mirrorlist https://buildbot:BuildBotToken@mirror.example.com/repodata/repomd.xml") |
| 68 | + sanitized = self.utility.sanitize_credentials_from_uri(message) |
| 69 | + expected_message = ("Curl error (6): Couldn't resolve host 'packages.microsoft.com' Could not " |
| 70 | + "retrieve mirrorlist https://buildbot@mirror.example.com/repodata/repomd.xml") |
| 71 | + self.assertEqual(sanitized, expected_message) |
| 72 | + |
| 73 | + def test_sanitize_credentials_expired_ssl_certs_error(self): |
| 74 | + """ ERROR with expired SSL certs and TESTTOKEN123456 """ |
| 75 | + message = ("ERROR: Customer environment error (expired SSL certs): " |
| 76 | + "Command=sudo yum update -y --disablerepo='*' " |
| 77 | + "--enablerepo='microsoft' !!Code=11 Out- Updating " |
| 78 | + "Subscription Management repositories. " |
| 79 | + "Unable to read consumer identity This system is not registered " |
| 80 | + "with an entitlement server. Status code: 401 " |
| 81 | + "for https://testuser:TESTTOKEN123456@packages-microsoft-com-prod/CENTRAL.rpm " |
| 82 | + "Error: Failed to download metadata for repo 'packages-microsoft-com-prod': " |
| 83 | + "Cannot download repomd.xml: All mirrors were tried") |
| 84 | + sanitized = self.utility.sanitize_credentials_from_uri(message) |
| 85 | + expected_message = ("ERROR: Customer environment error (expired SSL certs): " |
| 86 | + "Command=sudo yum update -y --disablerepo='*' " |
| 87 | + "--enablerepo='microsoft' !!Code=11 Out- Updating " |
| 88 | + "Subscription Management repositories. " |
| 89 | + "Unable to read consumer identity This system is not registered " |
| 90 | + "with an entitlement server. Status code: 401 " |
| 91 | + "for https://testuser@packages-microsoft-com-prod/CENTRAL.rpm " |
| 92 | + "Error: Failed to download metadata for repo 'packages-microsoft-com-prod': " |
| 93 | + "Cannot download repomd.xml: All mirrors were tried") |
| 94 | + self.assertEqual(sanitized, expected_message) |
| 95 | + |
| 96 | + def test_sanitize_credentials_exception_handling(self): |
| 97 | + """ Test exception handling: passing None should return the input unchanged """ |
| 98 | + result = self.utility.sanitize_credentials_from_uri(None) |
| 99 | + self.assertIsNone(result) |
| 100 | + |
| 101 | + |
| 102 | +if __name__ == '__main__': |
| 103 | + unittest.main() |
| 104 | + |
| 105 | + |
0 commit comments