mirror of
https://codeberg.org/scip/pcp.git
synced 2025-12-17 12:00:56 +01:00
added more (and better) c++ unittests
This commit is contained in:
@@ -1,20 +1,21 @@
|
||||
#include <pcp++.h>
|
||||
#include <string>
|
||||
#include <iomanip>
|
||||
#include <fstream>
|
||||
|
||||
using namespace pcp;
|
||||
using namespace std;
|
||||
|
||||
void pr(string name, unsigned char *data, size_t len) {
|
||||
int i;
|
||||
size_t i;
|
||||
cout << name << ": ";
|
||||
for ( i = 0;i < len;++i)
|
||||
printf("%02x", (unsigned int) data[i]);
|
||||
cout << endl;
|
||||
}
|
||||
|
||||
|
||||
int main() {
|
||||
try {
|
||||
void test0() {
|
||||
// test keygen and crypto
|
||||
Key A = Key("a", "alicia", "alicia@local");
|
||||
Key B = Key("b", "bobby", "bobby@local");
|
||||
PubKey PA = A.get_public();
|
||||
@@ -23,32 +24,80 @@ int main() {
|
||||
A.decrypt("a");
|
||||
B.decrypt("b");
|
||||
|
||||
pr("A secret", A.K->secret, 32);
|
||||
pr("A public", A.K->pub, 32);
|
||||
pr("B secret", B.K->secret, 32);
|
||||
pr("B public", B.K->pub, 32);
|
||||
Crypto A2B(A, PB);
|
||||
Crypto B2A(B, PA);
|
||||
|
||||
string cipher = A2B.encrypt("Hallo");
|
||||
ResultSet res = B2A.decrypt(cipher);
|
||||
|
||||
string cipher = A.encrypt(PB, "Hallo");
|
||||
ResultSet res = B.decrypt(PA, cipher);
|
||||
if(res.String == "Hallo")
|
||||
cout << "0 ok" << endl;
|
||||
else
|
||||
throw pcp::exception("wtf - decryption failed (uncatched as well)");
|
||||
}
|
||||
|
||||
cout << " Input: Hallo" << endl;
|
||||
cout << "Cipher: " << cipher << endl;
|
||||
cout << " Clear: " << res.String << endl;
|
||||
void test1() {
|
||||
// test the vault
|
||||
Key A = Key("a", "alicia", "alicia@local");
|
||||
Key B = Key("b", "bobby", "bobby@local");
|
||||
PubKey PA = A.get_public();
|
||||
PubKey PB = B.get_public();
|
||||
|
||||
Vault vault = Vault("vcpp");
|
||||
Vault vault = Vault("vcpp1");
|
||||
vault.key_add(A);
|
||||
vault.pubkey_add(PB);
|
||||
|
||||
KeyMap m = vault.keys();
|
||||
bool gotp, gots;
|
||||
gotp = gots = false;
|
||||
for(KeyIterator it=m.begin(); it != m.end(); ++it) {
|
||||
cout << "id: " << it->first << endl;
|
||||
if(it->first == A.get_id())
|
||||
gots = true;
|
||||
}
|
||||
|
||||
PubKeyMap p = vault.pubkeys();
|
||||
for(PubKeyIterator it=p.begin(); it != p.end(); ++it) {
|
||||
cout << "id: " << it->first << endl;
|
||||
if(it->first == PB.get_id())
|
||||
gotp = true;
|
||||
}
|
||||
|
||||
if(gotp == false || gots == false)
|
||||
throw pcp::exception("wtf - didnt find installed keys");
|
||||
else
|
||||
cout << "1 ok" << endl;
|
||||
}
|
||||
|
||||
void test2() {
|
||||
// try importing a key from disk
|
||||
ifstream pf("key-bobby-pub");
|
||||
string z;
|
||||
int max = 1024;
|
||||
char buf[max];
|
||||
while(pf) {
|
||||
pf.getline(buf, max);
|
||||
if(strlen(buf) > 0)
|
||||
z += buf + string("\n");
|
||||
}
|
||||
PubKey B(z);
|
||||
//cout << B.to_text();
|
||||
cout << "2 ok" << endl;
|
||||
}
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
try {
|
||||
switch(argv[1][0]) {
|
||||
case '0':
|
||||
test0();
|
||||
break;
|
||||
|
||||
case '1':
|
||||
test1();
|
||||
break;
|
||||
|
||||
case '2':
|
||||
test2();
|
||||
break;
|
||||
};
|
||||
}
|
||||
catch (pcp::exception &E) {
|
||||
cerr << "Catched exception: " << E.what() << endl;
|
||||
|
||||
@@ -484,3 +484,21 @@ dxmorg@florida.cops.gov
|
||||
| $pcp -V $vault -k -x $passwd
|
||||
expect = /Generated new secret key/
|
||||
</test>
|
||||
|
||||
|
||||
#
|
||||
# test the c++ api
|
||||
<test check-cpp-crypto>
|
||||
cmd = ./cpptest 0
|
||||
expect = /ok/
|
||||
</test>
|
||||
|
||||
<test check-cpp-vault>
|
||||
cmd = ./cpptest 1
|
||||
expect = /ok/
|
||||
</test>
|
||||
|
||||
<test check-cpp-import-pub>
|
||||
cmd = ./cpptest 1
|
||||
expect = /ok/
|
||||
</test>
|
||||
|
||||
Reference in New Issue
Block a user