Files
pcp/tests/cpptest.cpp

135 lines
2.6 KiB
C++
Raw Normal View History

#include <pcp++.h>
#include <string>
2013-12-02 22:54:04 +01:00
#include <iomanip>
#include <fstream>
using namespace pcp;
using namespace std;
void pr(string name, unsigned char *data, size_t len) {
2013-12-02 22:54:04 +01:00
size_t i;
cout << name << ": ";
for ( i = 0;i < len;++i)
printf("%02x", (unsigned int) data[i]);
cout << endl;
}
2013-12-02 22:54:04 +01:00
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();
PubKey PB = B.get_public();
2013-12-02 22:54:04 +01:00
A.decrypt("a");
B.decrypt("b");
Crypto A2B(A, PB);
Crypto B2A(B, PA);
string cipher = A2B.encrypt("Hallo");
ResultSet res = B2A.decrypt(cipher);
if(res.String == "Hallo")
cout << "0 ok" << endl;
else
throw pcp::exception("wtf - decryption failed (uncatched as well)");
}
2013-12-02 22:54:04 +01:00
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();
2013-12-02 22:54:04 +01:00
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) {
if(it->first == A.get_id())
gots = true;
}
2013-12-02 22:54:04 +01:00
PubKeyMap p = vault.pubkeys();
for(PubKeyIterator it=p.begin(); it != p.end(); ++it) {
if(it->first == PB.get_id())
gotp = true;
}
2013-12-02 22:54:04 +01:00
if(gotp == false || gots == false)
throw pcp::exception("wtf - didnt find installed keys");
else
cout << "1 ok" << endl;
}
2013-12-02 22:54:04 +01:00
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;
}
2013-12-07 13:24:44 +01:00
void test3() {
// signature test
Key A = Key("a", "alicia", "alicia@local");
A.decrypt("a");
PubKey PA = A.get_public();
string message = "hallo baby";
Signature SigA(A);
Signature SigB(PA);
string sig = SigA.sign(message);
if(SigB.verify(sig, message) )
cout << "3 ok" << endl;
}
2013-12-02 22:54:04 +01:00
int main(int argc, char **argv) {
try {
2013-12-07 13:24:44 +01:00
if(argc < 2)
throw pcp::exception("usage: cpptest N");
2013-12-02 22:54:04 +01:00
switch(argv[1][0]) {
case '0':
test0();
break;
2013-12-02 22:54:04 +01:00
case '1':
test1();
break;
2013-12-02 22:54:04 +01:00
case '2':
test2();
break;
2013-12-07 13:24:44 +01:00
case '3':
test3();
break;
default:
cerr << "usage: cpptest N" << endl;
break;
2013-12-02 22:54:04 +01:00
};
}
catch (pcp::exception &E) {
cerr << "Catched exception: " << E.what() << endl;
}
return 0;
}