@@ -1977,3 +1977,47 @@ def test_uuid_map_key(self):
19771977 self .assertEqual (llsd .format_notation (llsdmap ), b"{'00000000-0000-0000-0000-000000000000':'uuid'}" )
19781978
19791979
1980+ class InvalidInputTypes (unittest .TestCase ):
1981+ '''
1982+ Tests for handling invalid input types that should raise LLSDParseError
1983+ instead of hanging or consuming infinite memory.
1984+ '''
1985+
1986+ def test_parse_magicmock_raises_error (self ):
1987+ '''
1988+ Parsing a MagicMock object should raise LLSDParseError, not hang.
1989+ This is a regression test for a bug where llsd.parse() would go into
1990+ an infinite loop when passed a MagicMock (e.g., from an improperly
1991+ mocked requests.Response.content).
1992+ '''
1993+ from unittest .mock import MagicMock
1994+ mock = MagicMock ()
1995+ with self .assertRaises (llsd .LLSDParseError ) as context :
1996+ llsd .parse (mock )
1997+ self .assertIn ('MagicMock' , str (context .exception ))
1998+
1999+ def test_parse_string_raises_error (self ):
2000+ '''
2001+ Parsing a string (not bytes) should raise LLSDParseError.
2002+ '''
2003+ with self .assertRaises (llsd .LLSDParseError ) as context :
2004+ llsd .parse ('not bytes' )
2005+ self .assertIn ('str' , str (context .exception ))
2006+
2007+ def test_parse_none_raises_error (self ):
2008+ '''
2009+ Parsing None should raise LLSDParseError.
2010+ '''
2011+ with self .assertRaises (llsd .LLSDParseError ) as context :
2012+ llsd .parse (None )
2013+ self .assertIn ('NoneType' , str (context .exception ))
2014+
2015+ def test_parse_int_raises_error (self ):
2016+ '''
2017+ Parsing an integer should raise LLSDParseError.
2018+ '''
2019+ with self .assertRaises (llsd .LLSDParseError ) as context :
2020+ llsd .parse (42 )
2021+ self .assertIn ('int' , str (context .exception ))
2022+
2023+
0 commit comments