@@ -301,9 +301,83 @@ def test_process_source(self, mock_get, mock_client,
301301 else :
302302 self .assertIsNotNone (get_result )
303303
304+ @mock .patch .dict (os .environ , clear = True )
305+ @mock .patch ('docker.APIClient' )
306+ def test_local_directory (self , mock_client ):
307+ self .conf .set_override ('install_type' , 'source' )
308+ tmpdir = tempfile .mkdtemp ()
309+ file_name = 'test.txt'
310+ file_path = os .path .join (tmpdir , file_name )
311+ saved_umask = os .umask (0o077 )
312+
313+ try :
314+ with open (file_path , 'w' ) as f :
315+ f .write ('Hello' )
316+
317+ self .dc = mock_client
318+ self .image .plugins = [{
319+ 'name' : 'fake-image-base-plugin-test' ,
320+ 'type' : 'local' ,
321+ 'enabled' : True ,
322+ 'source' : tmpdir }
323+ ]
324+ push_queue = mock .Mock ()
325+ builder = build .BuildTask (self .conf , self .image , push_queue )
326+ builder .run ()
327+ self .assertTrue (builder .success )
328+
329+ except IOError :
330+ print ('IOError' )
331+ else :
332+ os .remove (file_path )
333+ finally :
334+ os .umask (saved_umask )
335+ os .rmdir (tmpdir )
336+
304337 @mock .patch .dict (os .environ , clear = True )
305338 @mock .patch ('docker.APIClient' )
306339 def test_malicious_tar (self , mock_client ):
340+ self .conf .set_override ('install_type' , 'source' )
341+ tmpdir = tempfile .mkdtemp ()
342+ file_name = 'test.txt'
343+ archive_name = 'my_archive.tar'
344+ file_path = os .path .join (tmpdir , file_name )
345+ archive_path = os .path .join (tmpdir , archive_name )
346+ # Ensure the file is read/write by the creator only
347+ saved_umask = os .umask (0o077 )
348+
349+ try :
350+ with open (file_path , 'w' ) as f :
351+ f .write ('Hello' )
352+
353+ with tarfile .open (archive_path , 'w' ) as tar :
354+ tar .add (file_path , arcname = '../test.txt' )
355+
356+ self .dc = mock_client
357+ self .image .plugins = [{
358+ 'name' : 'fake-image-base-plugin-test' ,
359+ 'type' : 'local' ,
360+ 'enabled' : True ,
361+ 'source' : archive_path }
362+ ]
363+
364+ push_queue = mock .Mock ()
365+ builder = build .BuildTask (self .conf , self .image , push_queue )
366+ builder .run ()
367+ self .assertFalse (builder .success )
368+
369+ except IOError :
370+ print ('IOError' )
371+ else :
372+ os .remove (file_path )
373+ os .remove (archive_path )
374+ finally :
375+ os .umask (saved_umask )
376+ os .rmdir (tmpdir )
377+
378+ @mock .patch .dict (os .environ , clear = True )
379+ @mock .patch ('docker.APIClient' )
380+ def test_malicious_tar_gz (self , mock_client ):
307381 self .conf .set_override ('install_type' , 'source' )
308382 tmpdir = tempfile .mkdtemp ()
309383 file_name = 'test.txt'
0 commit comments