test_stringutils.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # Copyright 2020 The Matrix.org Foundation C.I.C.
  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. from synapse.api.errors import SynapseError
  15. from synapse.util.stringutils import assert_valid_client_secret, base62_encode
  16. from .. import unittest
  17. class StringUtilsTestCase(unittest.TestCase):
  18. def test_client_secret_regex(self) -> None:
  19. """Ensure that client_secret does not contain illegal characters"""
  20. good = [
  21. "abcde12345",
  22. "ABCabc123",
  23. "_--something==_",
  24. "...--==-18913",
  25. "8Dj2odd-e9asd.cd==_--ddas-secret-",
  26. ]
  27. bad = [
  28. "--+-/secret",
  29. "\\dx--dsa288",
  30. "",
  31. "AAS//",
  32. "asdj**",
  33. ">X><Z<!!-)))",
  34. "a@b.com",
  35. ]
  36. for client_secret in good:
  37. assert_valid_client_secret(client_secret)
  38. for client_secret in bad:
  39. with self.assertRaises(SynapseError):
  40. assert_valid_client_secret(client_secret)
  41. def test_base62_encode(self) -> None:
  42. self.assertEqual("0", base62_encode(0))
  43. self.assertEqual("10", base62_encode(62))
  44. self.assertEqual("1c", base62_encode(100))
  45. self.assertEqual("001c", base62_encode(100, minwidth=4))