RetroShare/tests/unittests/libretroshare/gxs/security/gxssecurity_test.cc
2017-07-24 18:48:13 +02:00

104 lines
3.4 KiB
C++

/*
* libretroshare/src/serialiser: rsturtleitems_test.cc
*
* RetroShare Serialiser.
*
* Copyright 2007-2008 by Cyril Soler
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License Version 2 as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
* USA.
*
* Please report all bugs and problems to "retroshare@lunamutt.com".
*
*/
/******************************************************************
*/
#include <gtest/gtest.h>
#include <iostream>
#include <sstream>
#include "gxs/gxssecurity.h"
#include "util/rsdir.h"
TEST(libretroshare_gxs, GxsSecurity)
{
RsTlvPublicRSAKey pub_key ;
RsTlvPrivateRSAKey priv_key ;
EXPECT_TRUE(GxsSecurity::generateKeyPair(pub_key,priv_key)) ;
#ifdef WIN32
srand(getpid()) ;
#else
srand48(getpid()) ;
#endif
EXPECT_TRUE( pub_key.keyId == priv_key.keyId );
EXPECT_TRUE( pub_key.startTS == priv_key.startTS );
RsTlvPublicRSAKey pub_key2 ;
EXPECT_TRUE(GxsSecurity::extractPublicKey(priv_key,pub_key2)) ;
EXPECT_TRUE( pub_key.keyId == pub_key2.keyId );
EXPECT_TRUE( pub_key.keyFlags == pub_key2.keyFlags );
EXPECT_TRUE( pub_key.startTS == pub_key2.startTS );
EXPECT_TRUE( pub_key.endTS == pub_key2.endTS );
EXPECT_TRUE(pub_key.keyData.bin_len == pub_key2.keyData.bin_len) ;
EXPECT_TRUE(!memcmp(pub_key.keyData.bin_data,pub_key2.keyData.bin_data,pub_key.keyData.bin_len));
// create some random data and sign it / verify the signature.
uint32_t data_len = 1000 + RSRandom::random_u32()%100 ;
RsTemporaryMemory data(data_len) ;
RSRandom::random_bytes((unsigned char *)data,data_len) ;
std::cerr << " Generated random data. size=" << data_len << ", Hash=" << RsDirUtil::sha1sum((const uint8_t*)data,data_len) << std::endl;
RsTlvKeySignature signature ;
EXPECT_TRUE(GxsSecurity::getSignature((char*)(unsigned char*)data,data_len,priv_key,signature) );
EXPECT_TRUE(GxsSecurity::validateSignature((char*)(unsigned char*)data,data_len,pub_key,signature) );
std::cerr << " Signature: size=" << signature.signData.bin_len << ", Hash=" << RsDirUtil::sha1sum((const uint8_t*)signature.signData.bin_data,signature.signData.bin_len) << std::endl;
// test encryption/decryption
uint8_t *out = NULL ;
uint32_t outlen = 0 ;
uint8_t *out2 = NULL ;
uint32_t outlen2 = 0 ;
EXPECT_TRUE(GxsSecurity::encrypt(out,outlen,(const uint8_t*)data,data_len,pub_key) );
std::cerr << " Encrypted text: size=" << outlen << ", Hash=" << RsDirUtil::sha1sum((const uint8_t*)out,outlen) << std::endl;
EXPECT_TRUE(GxsSecurity::decrypt(out2,outlen2,out,outlen,priv_key) );
std::cerr << " Decrypted text: size=" << outlen2 << ", Hash=" << RsDirUtil::sha1sum((const uint8_t*)out2,outlen2) << std::endl;
// Check that decrypted data is equal to original data.
//
EXPECT_TRUE(data_len == outlen2) ;
EXPECT_TRUE(!memcmp(data,out2,outlen2)) ;
free(out2) ;
free(out) ;
}