Skip to content

Commit

Permalink
Add new tests
Browse files Browse the repository at this point in the history
  • Loading branch information
antonvolokha committed Feb 28, 2024
1 parent a0f448a commit 1cbe196
Show file tree
Hide file tree
Showing 7 changed files with 132 additions and 2 deletions.
1 change: 1 addition & 0 deletions .github/workflows/python-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ jobs:
python-version: 3.12
- name: Install dependencies
run: |
apt install ffmpeg
make dependency
- name: Test with pytest
run: |
Expand Down
7 changes: 5 additions & 2 deletions core/encryption_core.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,13 @@ class EncryptionCore:
def __init__(
self,
infile: str,
outfile: str | None,
files: list,
outfile: str | None = None,
files: list | None = None,
passphrase: str | None = None,
):
if files is None:
files = []

self.zip_file = create_tmp_zip_file()
self.input_file = infile
self.out_file = outfile or infile
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,6 @@ pyinstaller==6.4.0
argparse
pyzipper==0.3.6
pytest==8.0.2
pydub==0.25.1
numpy==1.26.4

Binary file added test/data/out_with_password.mp3
Binary file not shown.
Binary file added test/data/out_without_pwd.mp3
Binary file not shown.
94 changes: 94 additions & 0 deletions test/test_encryption_core.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
import os
import shutil

from core.encryption_core import EncryptionCore
from test.test_helper import audio_content_compare, binary_compare


def test_encrypt_without_pass():
input_file = 'test/data/song_orig.mp3'
out_file = 'out.mp3'
files = ['test/data/very_secret_doc.pdf']

core = EncryptionCore(
infile=input_file,
outfile=out_file,
files=files,
)

core.encrypt()

assert os.path.isfile(out_file)
assert audio_content_compare(input_file, out_file)

os.remove(out_file)


def test_encrypt_with_pass():
input_file = 'test/data/song_orig.mp3'
out_file = 'out.mp3'
files = ['test/data/very_secret_doc.pdf']
password = 'myawesomepass'

core = EncryptionCore(
infile=input_file,
outfile=out_file,
files=files,
passphrase=password
)

core.encrypt()

assert os.path.isfile(out_file)
assert audio_content_compare(input_file, out_file)

os.remove(out_file)


def test_decrypt_without_pass():
input_file = 'test/data/out_without_pwd.mp3'
out_folder = 'db0c7f89-f931-4e51-919d-1c91375f3742'
out_file = f'{out_folder}/very_secret_doc.pdf'
orig_file = 'test/data/very_secret_doc.pdf'

core = EncryptionCore(infile=input_file)

core.decrypt()

assert os.path.isdir(out_folder)
assert os.path.isfile(out_file)

assert binary_compare(out_file, orig_file)
shutil.rmtree(out_folder)


def test_decrypt_with_pass():
input_file = 'test/data/out_with_password.mp3'
out_folder = '55b32fdd-5daf-4a05-bb54-8c5bfd17cc18'
out_file = f'{out_folder}/very_secret_doc.pdf'
orig_file = 'test/data/very_secret_doc.pdf'
password = 'myawesomepass'

core = EncryptionCore(infile=input_file, passphrase=password)

core.decrypt()

assert os.path.isdir(out_folder)
assert os.path.isfile(out_file)

assert binary_compare(out_file, orig_file)
shutil.rmtree(out_folder)


def test_decrypt_with_invalid_pass():
input_file = 'test/data/out_with_password.mp3'
out_folder = '55b32fdd-5daf-4a05-bb54-8c5bfd17cc18'
password = 'invalidpasswprd'

core = EncryptionCore(infile=input_file, passphrase=password)

try:
core.decrypt()
except Exception as e:
assert 'Bad password for file' in str(e)

30 changes: 30 additions & 0 deletions test/test_helper.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import uuid
from pydub import AudioSegment
import numpy as np


def is_uuid_v4(s):
Expand All @@ -8,3 +10,31 @@ def is_uuid_v4(s):
return val.version == 4
except ValueError:
return False


def audio_content_compare(file1: str, file2: str) -> bool:
# Load the two audio files
audio1 = AudioSegment.from_mp3(file1)
audio2 = AudioSegment.from_mp3(file2)

# Convert to same frame rate and channels for a fair comparison
audio1 = audio1.set_frame_rate(44100).set_channels(1)
audio2 = audio2.set_frame_rate(44100).set_channels(1)

# Getting raw audio data
samples1 = np.array(audio1.get_array_of_samples())
samples2 = np.array(audio2.get_array_of_samples())

# Compare lengths and content
if len(samples1) != len(samples2):
return False

return np.array_equal(samples1, samples2)


def binary_compare(file1, file2):
with open(file1, 'rb') as f1, open(file2, 'rb') as f2:
content1 = f1.read()
content2 = f2.read()

return content1 == content2

0 comments on commit 1cbe196

Please sign in to comment.