1
0
Fork 0
forked from lthn/blockchain

cleaned up tests, fixed bug with not croped buff on flush

This commit is contained in:
cryptozoidberg 2020-07-09 23:49:26 +02:00
parent 3fa8774aab
commit 4d282f5251
No known key found for this signature in database
GPG key ID: 22DEB97A54C6FDEC
2 changed files with 75 additions and 100 deletions

View file

@ -199,6 +199,7 @@ public:
m_buff.resize(n_lambda + offset_before_flush);
}
std::memcpy((char*)&m_buff.data()[offset_before_flush], buf_lambda, n_lambda);
m_buff.resize(offset_before_flush + n_lambda);
});
//just to make sure that it's over
@ -208,6 +209,10 @@ public:
m_was_eof = true;
return withdraw_to_read_buff(s, n);
}
else
{
return -1;
}
}
}

View file

@ -17,76 +17,9 @@
#include "common/boost_serialization_helper.h"
TEST(chacha_stream_test, chacha_stream_test)
TEST(chacha_stream_test, basic_test_with_serialization_on_top)
{
LOG_PRINT_L0("chacha_stream_test");
const size_t buff_size = 10000;
std::string buff(buff_size, ' ');
for (size_t i = 0; i != buff_size; i++)
{
buff[i] = i % 255;
}
// for (size_t i = 0; i!= 100; i++)
// {
// std::cout << "Encrypting..." << std::endl;
// std::stringstream encrypted;
// crypto::chacha8_iv iv = crypto::rand<crypto::chacha8_iv>();
//
// encrypt_chacha_sink sink(encrypted, "pass", iv);
//
// size_t offset = 0;
// while (offset < buff_size)
// {
// std::streamsize count = std::rand() % 1000;
// if (count + offset > buff_size)
// {
// count = buff_size - offset;
// }
// sink.write(&buff[offset], count);
//
// offset += count;
// }
// sink.flush();
// std::string buff_encrypted = encrypted.str();
//
// std::cout << "Decrypting...";
// std::stringstream decrypted;
// encrypt_chacha_sink sink2(decrypted, "pass", iv);
// offset = 0;
// while (offset < buff_size)
// {
// std::streamsize count = std::rand() % 1000;
// if (count + offset > buff_size)
// {
// count = buff_size - offset;
// }
// sink2.write(&buff_encrypted[offset], count);
//
// offset += count;
// }
// sink2.flush();
// std::string buff_decrypted = decrypted.str();
//
// if (buff_decrypted != buff)
// {
// std::cout << "Failed" << std::endl;
// ASSERT_TRUE(false);
// }
// std::cout << "OK" << std::endl;
// }
// struct category_out: //public boost::iostreams::seekable_device_tag,
// public boost::iostreams::multichar_output_filter_tag,
// public boost::iostreams::flushable_tag
// //public boost::iostreams::seekable_filter_tag
// { };
// struct category_in : //public boost::iostreams::seekable_device_tag,
// public boost::iostreams::multichar_output_filter_tag,
// public boost::iostreams::flushable_tag
// //public boost::iostreams::seekable_filter_tag
// { };
std::list<currency::block_extended_info> test_list;
for(size_t i = 0; i != 1000; i++) {
test_list.push_back(currency::block_extended_info());
@ -95,12 +28,10 @@ TEST(chacha_stream_test, chacha_stream_test)
}
std::list<currency::block_extended_info> verification_list;
crypto::chacha8_iv iv = crypto::rand<crypto::chacha8_iv>();
boost::filesystem::ofstream store_data_file;
store_data_file.open("./test.bin", std::ios_base::binary | std::ios_base::out | std::ios::trunc);
encrypt_chacha_out_filter encrypt_filter("pass", iv);
//boost::iostreams::stream<encrypt_chacha_sink> outputStream(sink_encrypt);
boost::iostreams::filtering_ostream out;
out.push(encrypt_filter);
out.push(store_data_file);
@ -117,49 +48,88 @@ TEST(chacha_stream_test, chacha_stream_test)
encrypt_chacha_in_filter decrypt_filter("pass", iv);
boost::iostreams::filtering_istream in;
//in.push(boost::iostreams::invert(decrypt_filter));
in.push(decrypt_filter);
in.push(data_file);
//todo: read from stream
//size_t size = buff_size;//in.tellg();
///std::string str(size+10, '\0'); // construct string to stream size
//in.seekg(0);
try {
bool res2 = tools::portable_unserialize_obj_from_stream(verification_list, in);
ASSERT_TRUE(res2);
size_t i = 0;
for(auto vle: verification_list) {
ASSERT_TRUE(vle.height == i);
ASSERT_TRUE(vle.this_block_tx_fee_median == i);
i++;
}
//if(verification_list != test_list)
{
std::cout << "erroor";
}
//in.read(&str[0], size+10);
//std::streamsize sz = in.gcount();
//if(!in) {
// std::cout << "error";
//}
/*
in.read(&str[0], size + 10);
sz = in.gcount();
if(!in) {
std::cout << "error";
}
in.read(&str[0], size + 10);
sz = in.gcount();
if(!in) {
std::cout << "error";
}
*/
std::cout << "ddd";
}
catch (std::exception& err)
{
std::cout << err.what();
std::cout << err.what();
ASSERT_TRUE(false);
}
}
TEST(chacha_stream_test, diversity_test_on_different_stream_behaviour)
{
LOG_PRINT_L0("chacha_stream_test");
//prepare buff
const size_t buff_size = 10000;
std::string buff(buff_size, ' ');
for(size_t i = 0; i != buff_size; i++) {
buff[i] = i % 255;
}
crypto::chacha8_iv iv = crypto::rand<crypto::chacha8_iv>();
boost::filesystem::ofstream store_data_file;
store_data_file.open("./test.bin", std::ios_base::binary | std::ios_base::out | std::ios::trunc);
encrypt_chacha_out_filter encrypt_filter("pass", iv);
//boost::iostreams::stream<encrypt_chacha_sink> outputStream(sink_encrypt);
boost::iostreams::filtering_ostream out;
out.push(encrypt_filter);
out.push(store_data_file);
out << buff;
out.flush();
store_data_file.close();
boost::filesystem::ifstream data_file;
data_file.open("./test.bin", std::ios_base::binary | std::ios_base::in);
encrypt_chacha_in_filter decrypt_filter("pass", iv);
boost::iostreams::filtering_istream in;
in.push(decrypt_filter);
in.push(data_file);
std::string str(buff_size + 100, ' ');
try {
size_t offset = 0;
while (offset < buff_size)
{
std::streamsize count = std::rand() % 100;
// if (count + offset > buff_size)
// {
// count = buff_size - offset;
// }
in.read((char*)&str.data()[offset], count);
//self check
size_t readed_sz = in.gcount();
if(!(count + offset > buff_size || readed_sz == count))
{
ASSERT_TRUE(count + offset > buff_size || readed_sz == count);
}
offset += readed_sz;
}
if(in) {
ASSERT_TRUE(false);
}
std::cout << "OK";
}
catch(std::exception& err) {
std::cout << err.what();
ASSERT_TRUE(false);
}
}