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 <pcp++.h>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <iomanip>
|
||||||
|
#include <fstream>
|
||||||
|
|
||||||
using namespace pcp;
|
using namespace pcp;
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
void pr(string name, unsigned char *data, size_t len) {
|
void pr(string name, unsigned char *data, size_t len) {
|
||||||
int i;
|
size_t i;
|
||||||
cout << name << ": ";
|
cout << name << ": ";
|
||||||
for ( i = 0;i < len;++i)
|
for ( i = 0;i < len;++i)
|
||||||
printf("%02x", (unsigned int) data[i]);
|
printf("%02x", (unsigned int) data[i]);
|
||||||
cout << endl;
|
cout << endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void test0() {
|
||||||
int main() {
|
// test keygen and crypto
|
||||||
try {
|
|
||||||
Key A = Key("a", "alicia", "alicia@local");
|
Key A = Key("a", "alicia", "alicia@local");
|
||||||
Key B = Key("b", "bobby", "bobby@local");
|
Key B = Key("b", "bobby", "bobby@local");
|
||||||
PubKey PA = A.get_public();
|
PubKey PA = A.get_public();
|
||||||
@@ -23,32 +24,80 @@ int main() {
|
|||||||
A.decrypt("a");
|
A.decrypt("a");
|
||||||
B.decrypt("b");
|
B.decrypt("b");
|
||||||
|
|
||||||
pr("A secret", A.K->secret, 32);
|
Crypto A2B(A, PB);
|
||||||
pr("A public", A.K->pub, 32);
|
Crypto B2A(B, PA);
|
||||||
pr("B secret", B.K->secret, 32);
|
|
||||||
pr("B public", B.K->pub, 32);
|
|
||||||
|
|
||||||
|
string cipher = A2B.encrypt("Hallo");
|
||||||
|
ResultSet res = B2A.decrypt(cipher);
|
||||||
|
|
||||||
string cipher = A.encrypt(PB, "Hallo");
|
if(res.String == "Hallo")
|
||||||
ResultSet res = B.decrypt(PA, cipher);
|
cout << "0 ok" << endl;
|
||||||
|
else
|
||||||
|
throw pcp::exception("wtf - decryption failed (uncatched as well)");
|
||||||
|
}
|
||||||
|
|
||||||
cout << " Input: Hallo" << endl;
|
void test1() {
|
||||||
cout << "Cipher: " << cipher << endl;
|
// test the vault
|
||||||
cout << " Clear: " << res.String << endl;
|
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.key_add(A);
|
||||||
vault.pubkey_add(PB);
|
vault.pubkey_add(PB);
|
||||||
|
|
||||||
KeyMap m = vault.keys();
|
KeyMap m = vault.keys();
|
||||||
|
bool gotp, gots;
|
||||||
|
gotp = gots = false;
|
||||||
for(KeyIterator it=m.begin(); it != m.end(); ++it) {
|
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();
|
PubKeyMap p = vault.pubkeys();
|
||||||
for(PubKeyIterator it=p.begin(); it != p.end(); ++it) {
|
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) {
|
catch (pcp::exception &E) {
|
||||||
cerr << "Catched exception: " << E.what() << endl;
|
cerr << "Catched exception: " << E.what() << endl;
|
||||||
|
|||||||
@@ -484,3 +484,21 @@ dxmorg@florida.cops.gov
|
|||||||
| $pcp -V $vault -k -x $passwd
|
| $pcp -V $vault -k -x $passwd
|
||||||
expect = /Generated new secret key/
|
expect = /Generated new secret key/
|
||||||
</test>
|
</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