44"""
55
66import unittest
7- from test .support import os_helper
7+ from test .support import os_helper , warnings_helper
8+
9+ uu = warnings_helper .import_deprecated ("uu" )
810
911import os
1012import stat
1113import sys
12- import uu
1314import io
1415
1516plaintext = b"The symbols on top of your keyboard are !@#$%^&*()_+|~\n "
@@ -57,8 +58,6 @@ def encodedtextwrapped(mode, filename, backtick=False):
5758
5859class UUTest (unittest .TestCase ):
5960
60- # TODO: RUSTPYTHON
61- @unittest .expectedFailure
6261 def test_encode (self ):
6362 inp = io .BytesIO (plaintext )
6463 out = io .BytesIO ()
@@ -75,6 +74,7 @@ def test_encode(self):
7574 with self .assertRaises (TypeError ):
7675 uu .encode (inp , out , "t1" , 0o644 , True )
7776
77+ @os_helper .skip_unless_working_chmod
7878 def test_decode (self ):
7979 for backtick in True , False :
8080 inp = io .BytesIO (encodedtextwrapped (0o666 , "t1" , backtick = backtick ))
@@ -138,8 +138,6 @@ def test_garbage_padding(self):
138138 decoded = codecs .decode (encodedtext , "uu_codec" )
139139 self .assertEqual (decoded , plaintext )
140140
141- # TODO: RUSTPYTHON
142- @unittest .expectedFailure
143141 def test_newlines_escaped (self ):
144142 # Test newlines are escaped with uu.encode
145143 inp = io .BytesIO (plaintext )
@@ -149,6 +147,34 @@ def test_newlines_escaped(self):
149147 uu .encode (inp , out , filename )
150148 self .assertIn (safefilename , out .getvalue ())
151149
150+ def test_no_directory_traversal (self ):
151+ relative_bad = b"""\
152+ begin 644 ../../../../../../../../tmp/test1
153+ $86)C"@``
154+ `
155+ end
156+ """
157+ with self .assertRaisesRegex (uu .Error , 'directory' ):
158+ uu .decode (io .BytesIO (relative_bad ))
159+ if os .altsep :
160+ relative_bad_bs = relative_bad .replace (b'/' , b'\\ ' )
161+ with self .assertRaisesRegex (uu .Error , 'directory' ):
162+ uu .decode (io .BytesIO (relative_bad_bs ))
163+
164+ absolute_bad = b"""\
165+ begin 644 /tmp/test2
166+ $86)C"@``
167+ `
168+ end
169+ """
170+ with self .assertRaisesRegex (uu .Error , 'directory' ):
171+ uu .decode (io .BytesIO (absolute_bad ))
172+ if os .altsep :
173+ absolute_bad_bs = absolute_bad .replace (b'/' , b'\\ ' )
174+ with self .assertRaisesRegex (uu .Error , 'directory' ):
175+ uu .decode (io .BytesIO (absolute_bad_bs ))
176+
177+
152178class UUStdIOTest (unittest .TestCase ):
153179
154180 def setUp (self ):
@@ -202,6 +228,8 @@ def test_encode(self):
202228 s = fout .read ()
203229 self .assertEqual (s , encodedtextwrapped (0o644 , self .tmpin ))
204230
231+ # decode() calls chmod()
232+ @os_helper .skip_unless_working_chmod
205233 def test_decode (self ):
206234 with open (self .tmpin , 'wb' ) as f :
207235 f .write (encodedtextwrapped (0o644 , self .tmpout ))
@@ -214,6 +242,7 @@ def test_decode(self):
214242 self .assertEqual (s , plaintext )
215243 # XXX is there an xp way to verify the mode?
216244
245+ @os_helper .skip_unless_working_chmod
217246 def test_decode_filename (self ):
218247 with open (self .tmpin , 'wb' ) as f :
219248 f .write (encodedtextwrapped (0o644 , self .tmpout ))
@@ -224,6 +253,7 @@ def test_decode_filename(self):
224253 s = f .read ()
225254 self .assertEqual (s , plaintext )
226255
256+ @os_helper .skip_unless_working_chmod
227257 def test_decodetwice (self ):
228258 # Verify that decode() will refuse to overwrite an existing file
229259 with open (self .tmpin , 'wb' ) as f :
@@ -234,8 +264,7 @@ def test_decodetwice(self):
234264 with open (self .tmpin , 'rb' ) as f :
235265 self .assertRaises (uu .Error , uu .decode , f )
236266
237- # TODO: RUSTPYTHON
238- @unittest .expectedFailure
267+ @os_helper .skip_unless_working_chmod
239268 def test_decode_mode (self ):
240269 # Verify that decode() will set the given mode for the out_file
241270 expected_mode = 0o444
0 commit comments