fixed auto-tests in pgp directory

git-svn-id: http://svn.code.sf.net/p/retroshare/code/trunk@6047 b45a01b8-16f6-495d-af2f-9b41ad6348cc
This commit is contained in:
csoler 2012-12-27 18:02:15 +00:00
parent d1752bbdcb
commit 05df11117c
10 changed files with 168 additions and 50 deletions

View File

@ -1,6 +1,7 @@
RS_TOP_DIR = ../..
DHT_TOP_DIR = ../../../../libbitdht/src
OPS_TOP_DIR = ../../../../openpgpsdk/src
OPENPGP_INCLUDE_DIR = ../../../../openpgpsdk/src
##### Define any flags that are needed for this section #######

View File

@ -1,12 +1,15 @@
#include <fstream>
#include <string.h>
#include <util/utest.h>
#include <common/argstream.h>
//#include <pqi/cleanupxpgp.h>
#include <retroshare/rspeers.h>
#include <pgp/rscertificate.h>
#include <pgp/pgphandler.h>
INITTEST() ;
int main(int argc,char *argv[])
{
try
@ -23,8 +26,7 @@ int main(int argc,char *argv[])
FILE *f = fopen(keyfile.c_str(),"rb") ;
if(f == NULL)
throw std::runtime_error("Cannot open file. Sorry.") ;
CHECK(f != NULL) ;
std::string res ;
char c ;
@ -72,17 +74,30 @@ int main(int argc,char *argv[])
std::list<std::string> signers ;
PGPHandler handler("toto1","toto2","toto3","toto4") ;
handler.getGPGDetailsFromBinaryBlock(cert.pgp_key(),cert.pgp_key_size(),key_id,name,signers) ;
handler.getGPGDetailsFromBinaryBlock(cert.pgp_key(),cert.pgp_key_size(),key_id,name,signers) ;
CHECK(key_id == "660FB771727CE286") ;
CHECK(name == "Cyril-test (generated by Retroshare) <cyril.soler@imag.fr>") ;
std::cerr << "Details loaded from certificate:" << std::endl;
std::cerr << "PGP id\t: " << key_id << std::endl;
std::cerr << "Key name\t: " << name << std::endl;
std::cerr << "Signers\t:" << std::endl;
bool found = false ;
for(std::list<std::string>::const_iterator it(signers.begin());it!=signers.end();++it)
{
std::cerr << " " << *it << std::endl;
return 0;
if(*it == key_id)
found = true ;
}
CHECK(found) ;
FINALREPORT("Test certificate parsing") ;
return TESTRESULT();
}
catch(std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rOiKIyj1X5nJzQ660+OhmQf4lbJvkrcnvdrd
cmlfZQfupWMXHo7A3+27lOpfTvA1xpO8Gze0bRbLsbpx2dARjLUzXJAIFNCYzH5x
lIixxOwgX3tinKfoP3WvZv+nYBac/HeAPUW8BVqx6Y6V/gM3c71XNf9uhPgArN0j
vLOeqAJY+lACJZYlAIw6Jt2FzarC+orWndkvrsgrWmdxkorfZKBfdNwdf1YYrpUm
Gaf51x4DkrMWpfHxS8b6cgOmSN3LeAC8LCm5oEXPfNgaRehN9LKrSmcpcPS2jvDi
pgGBx+nL7gytCREMJkJoxppXQpVt1whaFDJVABEBAAG0OkN5cmlsLXRlc3QgKGdl
bmVyYXRlZCBieSBSZXRyb3NoYXJlKSA8Y3lyaWwuc29sZXJAaW1hZy5mcj6JATgE
EwECACIFAkyoe/MCGy8GCwkIBwMCBhUIAgkKCwQWAgMBAh4BAheAAAoJEGYPt3Fy
fOKG+h0H/A+vvryYmGcGFl82D4M0mAPAlbjZFkGCB8qJJSFuor55GJ3V8pBb4UK7
jnLhQhqsdXxAawT9R5RzTDITWHkS4KVaFalOYLxQ8x4s31FWXUiprAvJAIVw9gnB
C7kDLro2irCljZB5ro8fsg0SJ0ZFc4cZMPZxnMKm5ovC6WggbblyaHrRrV8jhHFI
R+RG9/nnw0PgvGOmV9f4Q0a/7E18AxdBXYMZSp7p/mwbQdkqgm7p73bg7E2Zz/Fh
g1/PguwjlDal+HyUEPDvcPG41NAKwpMeL78qVJTQcBzzo1Irqj5f5UFJp4/H7QDl
Esz/ZGxXXq3PdAP5BICwiOD44G9dIUE=
=BWkb
-----END PGP PUBLIC KEY BLOCK-----
--SSLID--5d52c0a6e3c2facd57fd13e716e98381;--LOCATION--laptop;
--LOCAL--192.168.0.103:7345;--EXT--82.228.181.149:7345;

View File

@ -10,8 +10,11 @@ extern "C"
#include <openpgpsdk/util.h>
}
#include <util/utest.h>
#include <common/argstream.h>
INITTEST() ;
int main(int argc,char *argv[])
{
try
@ -36,8 +39,7 @@ int main(int argc,char *argv[])
kr->nkeys_allocated = 0 ;
kr->keys = 0 ;
if(ops_false == ops_keyring_read_from_file(kr,armoured, keyfile.c_str()))
throw std::runtime_error("PGPHandler::readKeyRing(): cannot read key file. File corrupted, or missing/superfluous armour parameter.") ;
CHECK(ops_false != ops_keyring_read_from_file(kr,armoured, keyfile.c_str())) ;
for(int i=0;i<kr->nkeys;++i)
{
@ -48,7 +50,9 @@ int main(int argc,char *argv[])
ops_list_packets(const_cast<char *>(keyfile.c_str()),armoured,kr,NULL) ;
return 0 ;
FINALREPORT("Test key parsing") ;
return TESTRESULT() ;
}
catch(std::exception& e)
{

View File

@ -0,0 +1,2 @@
#!/bin/sh
./test_key_parsing -i test_pubring.gpg

View File

@ -4,10 +4,17 @@
#include <stdlib.h>
#include <iostream>
#include <pgp/pgphandler.h>
#include <util/utest.h>
#include <common/argstream.h>
INITTEST() ;
static std::string static_passphrase = "" ;
static std::string passphrase_callback(void *data,const char *uid_info,const char *what,int prev_was_bad)
{
if(!static_passphrase.empty())
return static_passphrase ;
if(prev_was_bad)
std::cerr << "Bad passphrase." << std::endl;
@ -95,6 +102,7 @@ int main(int argc,char *argv[])
bool test_output = false ;
bool test_signature = false ;
bool test_passphrase_callback = false ;
bool full_test = false ;
std::string key_id_string = "" ;
std::string secring_file = "" ;
@ -107,6 +115,7 @@ int main(int argc,char *argv[])
>> option('4',"keygen",test_gen_key,"Test key generation.")
>> option('5',"signature",test_signature,"Test signature.")
>> option('6',"output",test_output,"Test output.")
>> option('F',"fulltest",full_test,"Test everything.")
>> parameter('f',"file",file_to_encrypt,"File to encrypt. Used with -3",false)
>> parameter('p',"pubring",pubring_file,"Public keyring file.",false)
>> parameter('s',"secring",secring_file,"Secret keyring file.",false)
@ -115,20 +124,23 @@ int main(int argc,char *argv[])
as.defaultErrorHandling() ;
if(test_pgpid_type + test_keyring_read + test_file_encryption + test_gen_key + test_signature + test_output != 1)
{
std::cerr << "Options 1 to 6 are mutually exclusive." << std::endl;
return 1;
}
if(test_pgpid_type)
if(!full_test)
if(test_pgpid_type + test_keyring_read + test_file_encryption + test_gen_key + test_signature + test_output != 1)
{
std::cerr << "Options 1 to 6 are mutually exclusive." << std::endl;
return 1;
}
if(full_test || test_pgpid_type)
{
// test pgp ids.
//
PGPIdType id = PGPIdType(std::string("3e5b22140ef56abb")) ;
std::string st("3e5b22140ef56abb") ;
PGPIdType id = PGPIdType(std::string(st)) ;
//std::cerr << "Id is : " << std::hex << id.toUInt64() << std::endl;
std::cerr << "Id st : " << id.toStdString() << std::endl;
return 0 ;
CHECK(id.toStdString(false) == st) ;
}
// test PGPHandler
@ -144,7 +156,38 @@ int main(int argc,char *argv[])
PGPHandler::setPassphraseCallback(&passphrase_callback) ;
PGPHandler pgph(pubring,secring,trustdb,lockfile) ;
if(test_keyring_read)
std::string email_str("test@gmail.com") ;
std::string name_str("test") ;
std::string passw_str("test00") ;
PGPIdType cert_id ; ;
if(full_test || test_gen_key)
{
std::cerr << "Now generating a new PGP certificate: " << std::endl;
std::cerr << " email: " << email_str << std::endl;
std::cerr << " passw: " << passw_str << std::endl;
std::cerr << " name : " << name_str << std::endl;
PGPIdType newid ;
std::string errString ;
static_passphrase = passw_str ;
bool res = pgph.GeneratePGPCertificate(name_str, email_str, passw_str, newid, errString) ;
cert_id = newid ;
CHECK(res) ;
if(!res)
std::cerr << "Generation of certificate returned error: " << errString << std::endl;
else
std::cerr << "Certificate generation success. New id = " << newid.toStdString() << std::endl;
}
if(full_test)
key_id_string = cert_id.toStdString(true) ;
if(full_test || test_keyring_read)
{
pgph.printKeys() ;
@ -156,33 +199,18 @@ int main(int argc,char *argv[])
std::list<PGPIdType> lst ;
pgph.availableGPGCertificatesWithPrivateKeys(lst) ;
bool found = false ;
for(std::list<PGPIdType>::const_iterator it(lst.begin());it!=lst.end();++it)
{
std::cerr << "Found id : " << (*it).toStdString() << std::endl;
return 0 ;
if(cert_id == *it)
found = true ;
}
CHECK(found) ;
}
if(test_gen_key)
{
std::string email_str("test@gmail.com") ;
std::string name_str("test") ;
std::string passw_str("test00") ;
std::cerr << "Now generating a new PGP certificate: " << std::endl;
std::cerr << " email: " << email_str << std::endl;
std::cerr << " passw: " << passw_str << std::endl;
std::cerr << " name : " << name_str << std::endl;
PGPIdType newid ;
std::string errString ;
if(!pgph.GeneratePGPCertificate(name_str, email_str, passw_str, newid, errString))
std::cerr << "Generation of certificate returned error: " << errString << std::endl;
else
std::cerr << "Certificate generation success. New id = " << newid.toStdString() << std::endl;
return 0 ;
}
if(test_output)
if(full_test || test_output)
{
PGPIdType id2( (key_id_string.empty())?askForKeyId(pgph):key_id_string) ;
@ -197,8 +225,9 @@ int main(int argc,char *argv[])
std::cerr << "Loaded cert id: " << id3.toStdString() << ", Error string=\"" << error_string << "\"" << std::endl;
CHECK(id3 == id2) ;
std::cerr << cert << std::endl;
return 0 ;
}
if(test_passphrase_callback)
@ -207,10 +236,9 @@ int main(int argc,char *argv[])
std::string newid = "XXXXXXXXXXXXXXXX" ;
std::string pass = passphrase_callback(NULL,newid.c_str(),"Please enter password: ",false) ;
std::cerr << "Password = \"" << pass << "\"" << std::endl;
return 0 ;
}
if(test_signature)
if(full_test || test_signature)
{
if(key_id_string.empty())
key_id_string = askForKeyId(pgph) ;
@ -228,7 +256,11 @@ int main(int argc,char *argv[])
unsigned char sign[1000] ;
uint32_t signlen = 1000 ;
if(!pgph.SignDataBin(key_id,test_bin,BUFF_LEN,sign,&signlen))
bool res = pgph.SignDataBin(key_id,test_bin,BUFF_LEN,sign,&signlen) ;
CHECK(res) ;
if(!res)
std::cerr << "Signature error." << std::endl;
else
std::cerr << "Signature success." << std::endl;
@ -238,10 +270,17 @@ int main(int argc,char *argv[])
std::cerr << "Now verifying signature..." << std::endl;
PGPFingerprintType fingerprint ;
if(!pgph.getKeyFingerprint(key_id,fingerprint) )
res = pgph.getKeyFingerprint(key_id,fingerprint);
CHECK(res) ;
if(!res)
std::cerr << "Cannot find fingerprint of key id " << key_id.toStdString() << std::endl;
if(!pgph.VerifySignBin(test_bin,BUFF_LEN,sign,signlen,fingerprint))
res = pgph.VerifySignBin(test_bin,BUFF_LEN,sign,signlen,fingerprint) ;
CHECK(res) ;
if(!res)
std::cerr << "Signature verification failed." << std::endl;
else
std::cerr << "Signature verification worked!" << std::endl;
@ -249,7 +288,7 @@ int main(int argc,char *argv[])
delete[] test_bin ;
}
if(test_file_encryption)
if(full_test || test_file_encryption)
{
if(key_id_string.empty())
key_id_string = askForKeyId(pgph) ;
@ -260,7 +299,11 @@ int main(int argc,char *argv[])
std::cerr << "Checking encrypted file creation: streaming chain \"" << text_to_encrypt << "\" to file " << outfile << " with key " << key_id.toStdString() << std::endl;
if(!pgph.encryptTextToFile(key_id,text_to_encrypt,outfile))
bool res = pgph.encryptTextToFile(key_id,text_to_encrypt,outfile) ;
CHECK(res) ;
if(!res)
std::cerr << "Encryption failed" << std::endl;
else
std::cerr << "Encryption success" << std::endl;
@ -268,15 +311,21 @@ int main(int argc,char *argv[])
std::string decrypted_text = "" ;
outfile = "crypted_toto.pgp" ;
if(!pgph.decryptTextFromFile(key_id,decrypted_text,outfile))
res = pgph.decryptTextFromFile(key_id,decrypted_text,outfile) ;
CHECK(res) ;
if(!res)
std::cerr << "Decryption failed" << std::endl;
else
std::cerr << "Decryption success" << std::endl;
std::cerr << "Decrypted text: \"" << decrypted_text << "\"" << std::endl;
return 0 ;
CHECK(decrypted_text == text_to_encrypt) ;
}
return 0 ;
FINALREPORT("PGP Handler test") ;
return TESTRESULT() ;
}

View File

@ -0,0 +1,4 @@
#!/bin/sh
./test_pgp_handler -F

View File

@ -3,8 +3,11 @@
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <util/utest.h>
#include <pgp/pgphandler.h>
INITTEST() ;
static std::string passphrase_callback(void *data,const char *uid_info,const char *what,int prev_was_bad)
{
return std::string(getpass(what)) ;
@ -46,5 +49,9 @@ int main(int argc,char *argv[])
PGPHandler pgph(pubring,secring,trustdb,lockfil) ;
pgph.printKeys() ;
FINALREPORT("Signature parsing") ;
return TESTRESULT() ;
}

Binary file not shown.