Files
pcp/tests/cpptest.cpp

209 lines
4.3 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;
}
FILE *_openwr(string file, PcpContext *ptx) {
FILE *fd;
if((fd = fopen(file.c_str(), "wb+")) == NULL) {
throw pcp::exception(ptx, "Could not open output file " + file + "\n");
}
return fd;
}
FILE *_openrd(string file, PcpContext *ptx) {
FILE *fd;
if((fd = fopen(file.c_str(), "rb")) == NULL) {
throw pcp::exception(ptx, "Could not open input file " + file + "\n");
}
return fd;
}
2014-05-06 20:35:47 +02:00
void test0() {
2013-12-02 22:54:04 +01:00
// test keygen and crypto
PcpContext *CA = new PcpContext(); // we need different contexts for sender and recipient!
PcpContext *CB = new PcpContext();
2014-05-06 20:35:47 +02:00
FILE *CLEAR, *CIPHER, *DECRYPTED;
2014-05-06 20:35:47 +02:00
Key A = Key(CA, "a", "alicia", "alicia@local");
Key B = Key(CA, "b", "bobby", "bobby@local");
2013-12-02 22:54:04 +01:00
PubKey PA = A.get_public();
PubKey PB = B.get_public();
2013-12-02 22:54:04 +01:00
A.decrypt("a");
B.decrypt("b");
2014-05-06 20:35:47 +02:00
Crypto A2B(CA, A, PB);
Crypto B2A(CB, B, PA);
2014-05-06 20:35:47 +02:00
CLEAR = _openwr("testcppclear", CA);
fprintf(CLEAR, "HALLO\n");
fclose(CLEAR);
2013-12-02 22:54:04 +01:00
2014-05-06 20:35:47 +02:00
CIPHER = _openwr("testcpcipher", CA);
CLEAR = _openrd("testcppclear", CA);
if(A2B.encrypt(CLEAR, CIPHER, false)) {
2014-05-06 20:35:47 +02:00
CIPHER = _openrd("testcpcipher", CA);
DECRYPTED = _openwr("testcppdecrypted", CA);
2013-12-02 22:54:04 +01:00
if(B2A.decrypt(CIPHER, DECRYPTED, false)) {
2014-05-06 20:35:47 +02:00
DECRYPTED = _openrd("testcppdecrypted", CA);
char *got = (char *)ucmalloc(10);
if(fread(got, 1, 6, DECRYPTED) < 6) {
2014-05-06 20:35:47 +02:00
throw pcp::exception(CA, "read error, could not read decrypted content");
}
if(strncmp(got, "HALLO", 5) != 0) {
2014-05-06 20:35:47 +02:00
throw pcp::exception(CA);
}
2014-08-06 01:23:32 +02:00
free(got);
}
else
2014-05-06 20:35:47 +02:00
throw pcp::exception(CA, "failed to decrypt");
}
2013-12-02 22:54:04 +01:00
else
2014-05-06 20:35:47 +02:00
throw pcp::exception(CA, "failed to encrypt");
cout << "0 ok" << endl;
2014-05-06 20:35:47 +02:00
delete CA;
delete CB;
2013-12-02 22:54:04 +01:00
}
void test1(PcpContext *ptx) {
2013-12-02 22:54:04 +01:00
// test the vault
Key A = Key(ptx, "a", "alicia", "alicia@local");
Key B = Key(ptx, "b", "bobby", "bobby@local");
2013-12-02 22:54:04 +01:00
PubKey PA = A.get_public();
PubKey PB = B.get_public();
Vault vault = Vault(ptx, "vcpp1");
2013-12-02 22:54:04 +01:00
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(ptx, "wtf - didnt find installed keys");
2013-12-02 22:54:04 +01:00
else
cout << "1 ok" << endl;
}
void test2(PcpContext *ptx) {
cerr << " enter test2()" << endl;
2013-12-02 22:54:04 +01:00
// 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");
}
cerr << " PubKey B(ptx, z);" << endl;
PubKey B(ptx, z);
cout << "2 ok " << &ptx->ptx << endl;
2013-12-02 22:54:04 +01:00
}
2013-12-07 13:24:44 +01:00
void test3(PcpContext *ptx) {
2013-12-07 13:24:44 +01:00
// signature test
Key A = Key(ptx, "a", "alicia", "alicia@local");
2013-12-07 13:24:44 +01:00
A.decrypt("a");
PubKey PA = A.get_public();
string message = "hallo baby";
Signature SigA(ptx, A);
Signature SigB(ptx, PA);
2013-12-07 13:24:44 +01:00
if(SigA.sign((unsigned char*)message.c_str(), message.length()))
if(SigB.verify(SigA.sig) )
cout << "3 ok" << endl;
2013-12-07 13:24:44 +01:00
}
void test4() {
unsigned char *r = (unsigned char*)ucmalloc(32);
int i;
Buf b;
for(i=0; i<10; i++) {
arc4random_buf(r, 32);
b.add(r, 32);
}
if(b.size() == 32 * 10)
2014-05-06 11:49:09 +02:00
cout << "4 ok" << endl;
else
2014-05-06 11:49:09 +02:00
cout << "4 failed" << endl;
}
2013-12-02 22:54:04 +01:00
int main(int argc, char **argv) {
2013-12-19 12:38:10 +01:00
sodium_init();
PcpContext *ptx = new PcpContext();
2013-12-19 12:38:10 +01:00
2013-12-02 22:54:04 +01:00
try {
2013-12-07 13:24:44 +01:00
if(argc < 2)
throw pcp::exception(ptx, "usage: cpptest N");
2013-12-02 22:54:04 +01:00
switch(argv[1][0]) {
case '0':
2014-05-06 20:35:47 +02:00
test0();
2013-12-02 22:54:04 +01:00
break;
2013-12-02 22:54:04 +01:00
case '1':
test1(ptx);
2013-12-02 22:54:04 +01:00
break;
2013-12-02 22:54:04 +01:00
case '2':
test2(ptx);
2013-12-02 22:54:04 +01:00
break;
2013-12-07 13:24:44 +01:00
case '3':
test3(ptx);
2013-12-07 13:24:44 +01:00
break;
case '4':
test4();
break;
2013-12-07 13:24:44 +01:00
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;
}
2014-05-06 11:49:09 +02:00
delete ptx;
return 0;
}