@@ -537,6 +537,44 @@ def test_unselect(self):
537537 self .assertEqual (data [0 ], b'Returned to authenticated state. (Success)' )
538538 self .assertEqual (client .state , 'AUTH' )
539539
540+ def test_command_control_characters (self ):
541+ client , _ = self ._setup (SimpleIMAPHandler )
542+ with mock .patch .object (client , 'send' ) as send :
543+ for char in '\0 \r \n ' :
544+ for arg in (f'a{ char } b' , f'a{ char } b' .encode ('ascii' )):
545+ with self .subTest (arg = arg ):
546+ with self .assertRaisesRegex (ValueError ,
547+ 'NUL, CR and LF not allowed in commands' ):
548+ client ._command ('LOGIN' , arg , 'password' )
549+ send .assert_not_called ()
550+
551+ def test_quoted_control_characters (self ):
552+ client , _ = self ._setup (SimpleIMAPHandler )
553+ for char in support .control_characters_c0 ():
554+ if char in '\0 \r \n ' :
555+ continue
556+ with self .subTest (char = char ):
557+ arg = f'"a{ char } b"'
558+ with mock .patch .object (client , 'send' ) as send :
559+ tag = client ._command ('LOGIN' , 'user' , arg )
560+ send .assert_called_once_with (
561+ tag + b' LOGIN user ' + arg .encode ('ascii' ) + b'\r \n ' )
562+
563+ def test_append_literal_crlf (self ):
564+ class AppendServer (SimpleIMAPHandler ):
565+ def cmd_APPEND (self , tag , args ):
566+ size = int (args [- 1 ].strip ('{}' ))
567+ self ._send_textline ('+' )
568+ self .server .response = self .rfile .read (size + 2 )
569+ self ._send_tagged (tag , 'OK' , 'APPEND completed' )
570+
571+ client , server = self ._setup (AppendServer )
572+ client .login ('user' , 'pass' )
573+ message = b'Subject: test\r \n \r \n body\t end'
574+ typ , _ = client .append ('INBOX' , None , None , message )
575+ self .assertEqual (typ , 'OK' )
576+ self .assertEqual (server .response , message + b'\r \n ' )
577+
540578
541579class NewIMAPTests (NewIMAPTestsMixin , unittest .TestCase ):
542580 imap_class = imaplib .IMAP4
0 commit comments