codekingpro/portable-devtools
114k
1# -*- coding: utf-8 -*-2import sys3import unittest4from unittest.mock import patch5 6"""7test_ssl_compat.py8websocket - WebSocket client library for Python9 10Copyright 2025 engn33r11 12Licensed under the Apache License, Version 2.0 (the "License");13you may not use this file except in compliance with the License.14You may obtain a copy of the License at15 16 http://www.apache.org/licenses/LICENSE-2.017 18Unless required by applicable law or agreed to in writing, software19distributed under the License is distributed on an "AS IS" BASIS,20WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.21See the License for the specific language governing permissions and22limitations under the License.23"""24 25class SSLCompatTest(unittest.TestCase):26 def test_ssl_available(self):27 """Test that SSL is available in normal conditions"""28 import websocket._ssl_compat as ssl_compat29 30 # In normal conditions, SSL should be available31 self.assertTrue(ssl_compat.HAVE_SSL)32 self.assertIsNotNone(ssl_compat.ssl)33 34 # SSL exception classes should be available35 self.assertTrue(hasattr(ssl_compat, "SSLError"))36 self.assertTrue(hasattr(ssl_compat, "SSLEOFError"))37 self.assertTrue(hasattr(ssl_compat, "SSLWantReadError"))38 self.assertTrue(hasattr(ssl_compat, "SSLWantWriteError"))39 40 def test_ssl_not_available(self):41 """Test fallback behavior when SSL is not available"""42 # Remove ssl_compat from modules to force reimport43 if "websocket._ssl_compat" in sys.modules:44 del sys.modules["websocket._ssl_compat"]45 46 # Mock the ssl module to not be available47 import builtins48 49 original_import = builtins.__import__50 51 def mock_import(name, *args, **kwargs):52 if name == "ssl":53 raise ImportError("No module named 'ssl'")54 return original_import(name, *args, **kwargs)55 56 with patch("builtins.__import__", side_effect=mock_import):57 import websocket._ssl_compat as ssl_compat58 59 # SSL should not be available60 self.assertFalse(ssl_compat.HAVE_SSL)61 self.assertIsNone(ssl_compat.ssl)62 63 # Fallback exception classes should be available and functional64 self.assertTrue(issubclass(ssl_compat.SSLError, Exception))65 self.assertTrue(issubclass(ssl_compat.SSLEOFError, Exception))66 self.assertTrue(issubclass(ssl_compat.SSLWantReadError, Exception))67 self.assertTrue(issubclass(ssl_compat.SSLWantWriteError, Exception))68 69 # Test that exceptions can be instantiated70 ssl_error = ssl_compat.SSLError("test error")71 self.assertIsInstance(ssl_error, Exception)72 self.assertEqual(str(ssl_error), "test error")73 74 ssl_eof_error = ssl_compat.SSLEOFError("test eof")75 self.assertIsInstance(ssl_eof_error, Exception)76 77 ssl_want_read = ssl_compat.SSLWantReadError("test read")78 self.assertIsInstance(ssl_want_read, Exception)79 80 ssl_want_write = ssl_compat.SSLWantWriteError("test write")81 self.assertIsInstance(ssl_want_write, Exception)82 83 def tearDown(self):84 """Clean up after tests"""85 # Ensure ssl_compat is reimported fresh for next test86 if "websocket._ssl_compat" in sys.modules:87 del sys.modules["websocket._ssl_compat"]88 89 90if __name__ == "__main__":91 unittest.main()92 