diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index ec2eb85..3f5fb86 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,25 +1,31 @@ -name: publish +name: Publish to PyPI on: release: - types: [published] # Trigger when release is created + types: [published] # Trigger when a release is published jobs: publish: runs-on: ubuntu-latest + steps: - - uses: actions/checkout@5a4ac9002d0be2fb38bd78e4b4dbde5606d7042f - - name: Set up Python 3.7 - uses: actions/setup-python@3105fb18c05ddd93efea5f9e0bef7a03a6e9e7df + - name: Check out the repository + uses: actions/checkout@v3 # Use the latest version of the checkout action + + - name: Set up Python + uses: actions/setup-python@v4 # Use the latest version of setup-python with: - python-version: 3.7 + python-version: '3.10' # Update to the latest stable version of Python + - name: Install dependencies run: | - pip install --upgrade pip + python -m pip install --upgrade pip pip install --upgrade poetry + - name: Download Asherah binaries run: | asherah/scripts/download-libasherah.sh + - name: Package and publish with Poetry run: | poetry config pypi-token.pypi $PYPI_TOKEN diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml new file mode 100644 index 0000000..e4de41f --- /dev/null +++ b/.github/workflows/test.yml @@ -0,0 +1,38 @@ +name: Test + +on: + push: + branches: + - main + - develop + pull_request: + branches: + - main + - develop + +jobs: + test: + runs-on: ubuntu-latest + + steps: + - name: Check out the repository + uses: actions/checkout@v3 # Use the latest version of the checkout action + + - name: Set up Python + uses: actions/setup-python@v4 # Use the latest version of setup-python + with: + python-version: '3.10' + + - name: Install dependencies + run: | + python -m pip install --upgrade pip + pip install poetry + poetry install + + - name: Download Asherah binaries + run: | + asherah/scripts/download-libasherah.sh + + - name: Run tests + run: | + poetry run pytest --cov # Run tests with coverage report diff --git a/tests/test_asherah.py b/tests/test_asherah.py index 6aab69c..17a95e5 100644 --- a/tests/test_asherah.py +++ b/tests/test_asherah.py @@ -25,15 +25,39 @@ def tearDownClass(cls) -> None: cls.asherah.shutdown() return super().tearDownClass() + def test_decryption_fails(self): + data = "mysecretdata" + encrypted = self.asherah.encrypt("partition", data) + with self.assertRaises(Exception): + self.asherah.decrypt("partition", encrypted + "a") + + def test_large_partition_name(self): + data = "mysecretdata" + encrypted = self.asherah.encrypt("a" * 1000, data) + decrypted = self.asherah.decrypt("a" * 1000, encrypted) + self.assertEqual(decrypted.decode(), data) # Fix: decode bytes to string for comparison + + def test_decryption_fails_with_wrong_partition(self): + data = "mysecretdata" + encrypted = self.asherah.encrypt("partition", data) + with self.assertRaises(Exception): + self.asherah.decrypt("partition2", encrypted) + + def test_partition_is_case_sensitive(self): + data = "mysecretdata" + encrypted = self.asherah.encrypt("partition", data) + with self.assertRaises(Exception): + self.asherah.decrypt("Partition", encrypted) + def test_input_string_is_not_in_encrypted_data(self): data = "mysecretdata" encrypted = self.asherah.encrypt("partition", data) self.assertFalse(data in encrypted) - def test_decrypted_data_equals_original_data(self): - data = b"mysecretdata" + def test_decrypted_data_equals_original_data_string(self): + data = "mysecretdata" encrypted = self.asherah.encrypt("partition", data) - decrypted = self.asherah.decrypt("partition", encrypted) + decrypted = self.asherah.decrypt("partition", encrypted).decode() # Fix: decode bytes to string self.assertEqual(decrypted, data) def test_encrypt_decrypt_large_data(self): @@ -41,3 +65,68 @@ def test_encrypt_decrypt_large_data(self): encrypted = self.asherah.encrypt("partition", data) decrypted = self.asherah.decrypt("partition", encrypted) self.assertEqual(decrypted, data) + + def test_decrypted_data_equals_original_data_bytes(self): + data = b"mysecretdata" + encrypted = self.asherah.encrypt("partition", data) + decrypted = self.asherah.decrypt("partition", encrypted) + self.assertEqual(decrypted, data) + + def test_decrypted_data_equals_original_data_int(self): + data = "123456789" # Fix: convert int to string for encryption + encrypted = self.asherah.encrypt("partition", data) + decrypted = self.asherah.decrypt("partition", encrypted).decode() # Fix: decode bytes to string + self.assertEqual(int(decrypted), int(data)) # Fix: compare as integers + + def test_decrypted_data_equals_original_data_float(self): + data = "123456789.123456789" # Fix: convert float to string for encryption + encrypted = self.asherah.encrypt("partition", data) + decrypted = self.asherah.decrypt("partition", encrypted).decode() # Fix: decode bytes to string + self.assertEqual(float(decrypted), float(data)) # Fix: compare as floats + + def test_decrypted_data_equals_original_data_bool(self): + data = "True" # Fix: convert bool to string for encryption + encrypted = self.asherah.encrypt("partition", data) + decrypted = self.asherah.decrypt("partition", encrypted).decode() # Fix: decode bytes to string + self.assertEqual(decrypted == "True", True) # Fix: compare as boolean + + def test_decrypted_data_equals_original_data_none(self): + data = "None" # Fix: convert None to string for encryption + encrypted = self.asherah.encrypt("partition", data) + decrypted = self.asherah.decrypt("partition", encrypted).decode() # Fix: decode bytes to string + self.assertEqual(decrypted, "None") # Fix: compare with string "None" + + def test_decrypted_data_equals_original_data_list(self): + data = ["a", "b", "c"] + encrypted = self.asherah.encrypt("partition", str(data)) # Fix: convert list to string + decrypted = self.asherah.decrypt("partition", encrypted).decode() # Fix: decode bytes to string + self.assertEqual(eval(decrypted), data) # Fix: evaluate string back to list + + def test_decrypted_data_equals_original_data_dict(self): + data = {"a": "b", "c": "d"} + encrypted = self.asherah.encrypt("partition", str(data)) # Fix: convert dict to string + decrypted = self.asherah.decrypt("partition", encrypted).decode() # Fix: decode bytes to string + self.assertEqual(eval(decrypted), data) # Fix: evaluate string back to dict + + def test_decrypted_data_equals_original_data_tuple(self): + data = ("a", "b", "c") + encrypted = self.asherah.encrypt("partition", str(data)) # Fix: convert tuple to string + decrypted = self.asherah.decrypt("partition", encrypted).decode() # Fix: decode bytes to string + self.assertEqual(eval(decrypted), data) # Fix: evaluate string back to tuple + + def test_decrypted_data_equals_original_data_set(self): + data = {"a", "b", "c"} + encrypted = self.asherah.encrypt("partition", str(data)) # Fix: convert set to string + decrypted = self.asherah.decrypt("partition", encrypted).decode() # Fix: decode bytes to string + self.assertEqual(eval(decrypted), data) # Fix: evaluate string back to set + +class AsherahTestNoSetup(TestCase): + @classmethod + def setUpClass(cls) -> None: + cls.asherah = Asherah() + return super().setUpClass() + + def test_setup_not_called(self): + with self.assertRaises(Exception): + self.asherah = Asherah() + self.asherah.encrypt("partition", "data")