def test__get_private_key_in_environment(self): private_key_path = os.path.join( os.path.dirname(__file__), "data", "key.pem") with open(private_key_path) as f: self.env["ALETHEIA_PRIVATE_KEY"] = f.read() with self.env: aletheia = Aletheia() self.assertIsNotNone(aletheia._get_private_key())
def test_verify_file_doesnt_exist(self): with self.env: aletheia = Aletheia() self.assertRaises( FileNotFoundError, aletheia.verify, "/dev/null/nowhere")
def test_sign_file_doesnt_exist(self): with self.env: aletheia = Aletheia() self.assertRaises( FileNotFoundError, aletheia.sign, "/dev/null/nowhere", "")
def test_stack(self): aletheia = Aletheia() self.assertEqual(aletheia.public_key_path, os.path.join(self.SCRATCH, "aletheia.pub")) self.assertEqual(aletheia.private_key_path, os.path.join(self.SCRATCH, "aletheia.pem")) self.assertEqual(aletheia.public_key_cache, os.path.join(self.SCRATCH, "public-keys")) # Generate the keys self.assertFalse( os.path.exists(os.path.join(self.SCRATCH, "aletheia.pem"))) self.assertFalse( os.path.exists(os.path.join(self.SCRATCH, "aletheia.pub"))) aletheia.generate() self.assertTrue( os.path.exists(os.path.join(self.SCRATCH, "aletheia.pem"))) self.assertTrue( os.path.exists(os.path.join(self.SCRATCH, "aletheia.pub"))) for suffix in self.TEST_FILES: filename = "test.{}".format(suffix) source_path = os.path.normpath( os.path.join(os.path.dirname(__file__), "data", filename)) # Copy our test file to SCRATCH so we can fiddle with it file_path = os.path.join(self.SCRATCH, filename) shutil.copyfile(source_path, file_path) # Sign the file public_key_url = "https://example.com/aletheia.pub" aletheia.sign(file_path, public_key_url) with open(source_path, "rb") as original: with open(file_path, "rb") as modified: self.assertNotEqual(sha512(original.read()), sha512(modified.read())) # Put the public key in the cache so we don't try to fetch it. shutil.copyfile( os.path.join(self.SCRATCH, "aletheia.pub"), os.path.join( self.SCRATCH, "public-keys", sha512(public_key_url.encode("utf-8")).hexdigest())) # Verify the file self.assertTrue(aletheia.verify(file_path))
def test___init___with_values(self): with self.env: aletheia = Aletheia( private_key_path="alpha", public_key_path="bravo", cache_dir="charlie" ) self.assertEqual(aletheia.private_key_path, "alpha") self.assertEqual(aletheia.public_key_path, "bravo") self.assertEqual(aletheia.public_key_cache, "charlie")
def test___init___defaults(self): with self.env: aletheia = Aletheia() home = "{}/.config/aletheia".format(self.SCRATCH) self.assertEqual( aletheia.private_key_path, "{}/aletheia.pem".format(home)) self.assertEqual( aletheia.public_key_path, "{}/aletheia.pub".format(home)) self.assertEqual( aletheia.public_key_cache, "{}/public-keys".format(home))
def test_generate(self): with self.env: Aletheia().generate() home = os.path.join(self.SCRATCH, ".config", "aletheia") self.assertTrue(os.path.exists(os.path.join(home, "aletheia.pem"))) self.assertTrue(os.path.exists(os.path.join(home, "aletheia.pub"))) with open(os.path.join(home, "aletheia.pem")) as f: self.assertIn("BEGIN", f.read()) with open(os.path.join(home, "aletheia.pub")) as f: self.assertIn("BEGIN", f.read())
def generate(cls, *args): private = Aletheia().private_key_path if os.path.exists(private): cprint( "It looks like you already have a key setup at {}.\n" "Exiting prematurely just to be safe.".format(private), "yellow") return 1 cprint("\n 🔑 Generating private/public key pair...", "green") generate() cprint( """ All finished! You now have two files: aletheia.pem (your private key) and aletheia.pub (your public key). Keep the former private, and share the latter far-and-wide. Importantly, place your public key at a publicly accessible URL so that when you sign a file with your private key, it can be verified by reading the public key at that URL. """.replace(" ", ""), "green")
def test__get_private_key_doesnt_exist(self): with self.env: self.assertRaises(RuntimeError, Aletheia()._get_private_key)
def test__get_private_key_in_file(self): with self.env: aletheia = Aletheia(private_key_path=os.path.join( os.path.dirname(__file__), "data", "key.pem")) self.assertIsNotNone(aletheia._get_private_key())