py: added doc, unittests, anonymous encryption mode

This commit is contained in:
git@daemon.de
2014-12-24 11:17:03 +01:00
parent b52ac4b0df
commit 5f8d50ec47
11 changed files with 394 additions and 78 deletions

View File

@@ -1,7 +1,7 @@
#
# This file is part of Pretty Curved Privacy (pcp1).
#
# Copyright (C) 2013 T.Linden.
# Copyright (C) 2013-2015 T. von Dein.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
@@ -64,11 +64,20 @@ cpptest_SOURCES = cpptest.cpp
# %wheel ALL = NOPASSWD: /sbin/umount
# %wheel ALL = NOPASSWD: /sbin/mdmfs
test: check
if BUILDPY
MAYPY=pytest
endif
test: check $(MAYPY)
ctest:
rm -f test* v* stresstest/*
./unittests.pl unittests.cfg $(CHECK)
@echo "To run a single test only, type: 'make test CHECK=testname'"
pytest:
./unittests.pl pyunittests.cfg $(CHECK)
stresstest: check
./unittests.pl stresstests.cfg

89
tests/pytest.py Executable file
View File

@@ -0,0 +1,89 @@
#!/usr/local/bin/python
from sys import argv, stdout
from pypcp import *
from pprint import pprint
orig = "hello world"
def importkey(filename, passwd=None, secret=True, public=False):
raw = open(filename, "r").read()
if secret and not public:
key = Key(encoded=raw, passphrase=passwd)
return key
else:
key = PublicKey(encoded=raw)
return key
def asym():
# import keys
bobSec = importkey("../../tests/key-bobby-sec", "b")
bobPub = importkey("../../tests/key-bobby-pub", public=True)
aliSec = importkey("../../tests/key-alicia-sec", "a")
aliPub = importkey("../../tests/key-alicia-pub", public=True)
# one context for each
ctxA = Context()
ctxB = Context()
# prepare ctx' for crypto
ctxA.addkey(aliSec)
ctxA.recipients(bobPub)
ctxB.addkey(bobSec)
ctxB.recipients(aliPub)
# Alice encrypt => Bob
# FIXME: if passed as is, then it's empty later on
encrypted = ctxA.encrypt(source="%s" % orig, armor=True)
#print "encrypted:\n%s" % encrypted
# Bob decrypt from Alice
clear = ctxB.decrypt(source=encrypted)
#print "clear: %s" % clear
if clear == orig:
return True
else:
return False
def sym():
# always required
ctx = Context()
# symmetric encryption (self mode)
encrypted = ctx.encrypt(source="%s" % orig, passphrase="x", armor=True)
# decrypt
clear = ctx.decrypt(source=encrypted, passphrase="x")
if clear == orig:
return True
else:
return False
def defun(name):
if not globals()[name]():
print "%s: failed" % name
exit(1)
else:
print "%s: ok" % name
return True
if len(argv) == 2:
if defun(argv[1]):
exit(0)
else:
# execute all
for func in ["asym", "sym"]:
defun(func)
exit(0)

58
tests/pyunittests.cfg Normal file
View File

@@ -0,0 +1,58 @@
#
# This file is part of Pretty Curved Privacy (pcp1).
#
# Copyright (C) 2013-2015 T.Linden.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
# You can contact me by mail: <tlinden AT cpan DOT org>.
#
py = ./pytest.py
<test py-import-keys>
cmd = $py impkeys
expect = /ok/
</test>
<test py-encrypt-asym-armor>
cmd = $py asymarmor
expect = /ok/
</test>
<test py-encrypt-asym-raw>
cmd = $py asymraw
expect = /ok/
</test>
<test py-encrypt-asym-sign>
cmd = $py asymsign
expect = /ok/
</test>
<test py-encrypt-asym-anon>
cmd = $py asymanon
expect = /ok/
</test>
<test py-encrypt-sym-armor>
cmd = $py symarmor
expect = /ok/
</test>
<test py-encrypt-sym-raw>
cmd = $py symraw
expect = /ok/
</test>