Skip to content

Commit

Permalink
Update unit tests adding tests for random access
Browse files Browse the repository at this point in the history
  • Loading branch information
evilaliv3 committed Dec 18, 2024
1 parent a2cc743 commit 27ac9d2
Showing 1 changed file with 20 additions and 10 deletions.
30 changes: 20 additions & 10 deletions tests/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@
import stat
import unittest
from tempfile import mkdtemp
from fuse import FuseOSError
from globaleaks_eph_fs import EphemeralFile, EphemeralOperations

TEST_PATH='TESTFILE.TXT'
TEST_DATA=b'TEST'
TEST_PATH = 'TESTFILE.TXT'
TEST_DATA = b"Hello, world! This is a test data for writing, seeking and reading operations."


class TestEphemeralFile(unittest.TestCase):
Expand All @@ -26,20 +27,29 @@ def test_create_and_write_file(self):

def test_encryption_and_decryption(self):
with self.ephemeral_file.open('w') as file:
for _ in range(10):
file.write(TEST_DATA)
file.write(TEST_DATA)

with self.ephemeral_file.open('r') as file:
decrypted_data = file.read()
# Define test cases: each case is a tuple (seek_position, read_size, expected_data)
seek_tests = [
(0, 1, TEST_DATA[:1]), # Seek at the start read 1 byte
(5, 5, TEST_DATA[5:10]), # Seek forward, read 5 bytes
(10, 2, TEST_DATA[10:12]), # Seek forward, read 2 bytes
(0, 3, TEST_DATA[:3]), # Seek backward, read 3 bytes
]

self.assertEqual(decrypted_data, TEST_DATA * 10)
# Test forward and backward seeking with different offsets
with self.ephemeral_file.open('r') as file:
for seek_pos, read_size, expected in seek_tests:
file.seek(seek_pos) # Seek to the given position
self.assertEqual(file.tell(), seek_pos) # Check position after seeking forward
read_data = file.read(read_size) # Read the specified number of bytes
self.assertEqual(read_data, expected) # Verify the data matches the expected value

def test_file_cleanup(self):
file_path = self.ephemeral_file.filepath
del self.ephemeral_file
self.assertFalse(os.path.exists(file_path))


class TestEphemeralOperations(unittest.TestCase):
def setUp(self):
self.storage_dir = mkdtemp()
Expand Down Expand Up @@ -89,7 +99,7 @@ def test_unlink_file(self):
self.assertNotIn(TEST_PATH, self.operations.files)

def test_file_not_found(self):
with self.assertRaises(OSError) as context:
with self.assertRaises(FuseOSError) as context:
self.operations.open('/nonexistentfile', os.O_RDONLY)
self.assertEqual(context.exception.errno, errno.ENOENT)

Expand All @@ -116,7 +126,7 @@ def test_getattr_file(self):
# Check times (just ensure they exist)
self.assertIn('st_atime', attr)
self.assertIn('st_mtime', attr)
self.assertIn('st_ctyme', attr)
self.assertIn('st_ctime', attr)

def test_getattr_nonexistent(self):
"""Test getattr on a nonexistent path."""
Expand Down

0 comments on commit 27ac9d2

Please sign in to comment.