Skip to content

Commit

Permalink
Update test_B7_persist_temp_file
Browse files Browse the repository at this point in the history
Add a test case for persisting a file with custom permissions

Signed-off-by: Teodora Sechkova <[email protected]>
  • Loading branch information
sechkova committed Nov 27, 2020
1 parent 7fdbf5c commit d66764e
Showing 1 changed file with 15 additions and 1 deletion.
16 changes: 15 additions & 1 deletion tests/test_util.py
100755 → 100644
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import tempfile
import unittest
import timeit
import stat

import securesystemslib.settings
import securesystemslib.hash
Expand Down Expand Up @@ -255,8 +256,21 @@ def test_B9_persist_temp_file(self):
dest_path = os.path.join(dest_temp_dir, self.random_string())
tmpfile = tempfile.TemporaryFile()
tmpfile.write(self.random_string().encode('utf-8'))
securesystemslib.util.persist_temp_file(tmpfile, dest_path)

# Write a file with custom permissions
# stat.S_IRUSR: user has read permissions
# stat.S_IWUSR: user has write permissions
mode= stat.S_IRUSR | stat.S_IWUSR
securesystemslib.util.persist_temp_file(tmpfile, dest_path, mode=mode)
self.assertTrue(dest_path)

# Need to set also the stat.S_IFREG bit to match the st_mode output
# stat.S_IFREG - Regular file
expected_mode = stat.S_IFREG | mode
if os.name != 'posix':
# Windows only supports setting the read-only attribute.
expected_mode = stat.S_IFREG | 0o666
self.assertEqual(os.stat(dest_path).st_mode, expected_mode)
self.assertTrue(tmpfile.closed)

# Test persisting a file without automatically closing the tmpfile
Expand Down

0 comments on commit d66764e

Please sign in to comment.