2021-03-02 09:44:24 +03:00
|
|
|
# Copyright (c) Microsoft Corporation.
|
|
|
|
# Licensed under the MIT license.
|
|
|
|
|
2020-08-28 14:53:36 +03:00
|
|
|
import re
|
|
|
|
from unittest.case import TestCase
|
|
|
|
|
|
|
|
from lisa.secret import PATTERN_GUID, add_secret, mask, reset
|
|
|
|
from lisa.util.logger import get_logger
|
|
|
|
|
|
|
|
|
|
|
|
class SecretTestCase(TestCase):
|
|
|
|
def setUp(self) -> None:
|
|
|
|
reset()
|
|
|
|
|
2023-05-31 15:33:54 +03:00
|
|
|
def tearDown(self) -> None:
|
|
|
|
reset()
|
|
|
|
|
2020-08-28 14:53:36 +03:00
|
|
|
def test_happen_twice(self) -> None:
|
|
|
|
add_secret("test1", sub="*")
|
|
|
|
result = mask("test1 test1 test3")
|
|
|
|
self.assertEqual(result, "* * test3")
|
|
|
|
|
|
|
|
def test_big_contains_small(self) -> None:
|
|
|
|
add_secret("t1", sub="*")
|
|
|
|
add_secret("t1t2", sub="**")
|
|
|
|
result = mask("t1t2 t1 test3")
|
|
|
|
self.assertEqual(result, "** * test3")
|
|
|
|
|
|
|
|
def test_default_mask(self) -> None:
|
|
|
|
add_secret("test1")
|
|
|
|
result = mask("test1 test3")
|
|
|
|
self.assertEqual(result, "****** test3")
|
|
|
|
|
|
|
|
def test_pattern(self) -> None:
|
|
|
|
add_secret("test", sub="*")
|
|
|
|
add_secret(
|
|
|
|
"f5132846-2aff-4726-baea-8c480ce9eb06",
|
|
|
|
mask=re.compile(
|
|
|
|
r"^([0-9a-f]{8})-(?:[0-9a-f]{4}-){3}[0-9a-f]{8}([0-9a-f]{4})$"
|
|
|
|
),
|
|
|
|
sub=r"\1-****-****-****-********\2",
|
|
|
|
)
|
|
|
|
result = mask("my test f5132846-2aff-4726-baea-8c480ce9eb06 not")
|
|
|
|
self.assertEqual(result, "my * f5132846-****-****-****-********eb06 not")
|
|
|
|
|
|
|
|
def test_built_in_pattern(self) -> None:
|
|
|
|
add_secret("test", sub="*")
|
|
|
|
add_secret("f5132846-2aff-4726-baea-8c480ce9eb06", mask=PATTERN_GUID)
|
|
|
|
result = mask("my test f5132846-2aff-4726-baea-8c480ce9eb06 not")
|
|
|
|
self.assertEqual(result, "my * f5132846-****-****-****-********eb06 not")
|
|
|
|
|
|
|
|
def test_fallback(self) -> None:
|
|
|
|
add_secret(
|
2020-09-24 02:42:14 +03:00
|
|
|
"test1",
|
|
|
|
mask=re.compile(r"^doesn't match$"),
|
|
|
|
sub=r"*****",
|
2020-08-28 14:53:36 +03:00
|
|
|
)
|
|
|
|
result = mask("my test1 test2 not")
|
|
|
|
self.assertEqual(result, "my ***** test2 not")
|
|
|
|
|
|
|
|
def test_log(self) -> None:
|
|
|
|
add_secret("t1", sub="*")
|
|
|
|
add_secret("t1t2", sub="**")
|
|
|
|
log = get_logger()
|
|
|
|
|
2021-05-25 06:10:52 +03:00
|
|
|
with self.assertLogs("lisa") as cm:
|
2020-08-28 14:53:36 +03:00
|
|
|
log.info("t1t2 2")
|
2021-07-31 01:03:42 +03:00
|
|
|
self.assertListEqual(["INFO:lisa.:** 2"], cm.output)
|
2020-08-28 14:53:36 +03:00
|
|
|
|
2020-09-01 05:17:48 +03:00
|
|
|
def test_stdout(self) -> None:
|
|
|
|
add_secret("t1", sub="*")
|
|
|
|
add_secret("t1t2", sub="**")
|
|
|
|
|
2021-05-25 06:10:52 +03:00
|
|
|
with self.assertLogs("lisa") as cm:
|
2020-09-01 05:17:48 +03:00
|
|
|
print("t1t2 2")
|
2021-05-25 06:10:52 +03:00
|
|
|
self.assertListEqual(["INFO:lisa.stdout:** 2"], cm.output)
|
2020-09-01 05:17:48 +03:00
|
|
|
|
2020-08-28 14:53:36 +03:00
|
|
|
def test_log_with_args(self) -> None:
|
|
|
|
log = get_logger()
|
|
|
|
add_secret("t1")
|
|
|
|
add_secret("t2")
|
2021-05-25 06:10:52 +03:00
|
|
|
with self.assertLogs("lisa") as cm:
|
2020-08-28 14:53:36 +03:00
|
|
|
log.info("with args t2: %s", "t1")
|
2021-07-31 01:03:42 +03:00
|
|
|
self.assertListEqual(["INFO:lisa.:with args ******: ******"], cm.output)
|