cafs::link cafs::import( const fc::path& p ) { if( !fc::exists(p) ) { FC_THROW_MSG( "Path does not exist %s", p.string() ); } if( fc::is_regular_file( p ) ) { if( fc::file_size(p) > IMBED_THRESHOLD ) { auto file_head = import_file(p); fc::vector<char> data(MAX_CHUNK_SIZE); fc::datastream<char*> ds(data.data()+1, data.size()-1); fc::raw::pack( ds, file_head ); data[0] = cafs::file_header_type; //fc::datastream<const char*> ds2(data.data()+1, data.size()-1); //file_header tmp; //fc::raw::unpack( ds2, tmp ); //slog( "test unpack %s", fc::json::to_string( tmp ).c_str() ); data.resize( ds.tellp() ); //slog( "pre randomized... '%s'", fc::to_hex( data.data(), 16 ).c_str() ); size_t seed = randomize(data, *((uint64_t*)fc::sha1::hash(data.data(),data.size()).data()) ); auto chunk_head = slice_chunk( data ); //slog( "slice chunk %s", fc::json::to_string( chunk_head ).c_str() ); store_chunk( chunk_head, data ); return link( chunk_head.calculate_id(), seed ); } else { // no header, just raw data from the file stored in the chunk fc::vector<char> data( fc::file_size(p)+1 ); data[0] = file_data_type; fc::ifstream ifile( p.string(), fc::ifstream::binary ); ifile.read( data.data()+1, data.size()-1 ); size_t seed = randomize(data, *((uint64_t*)fc::sha1::hash(data.data(),data.size()).data()) ); auto chunk_head = slice_chunk( data ); store_chunk( chunk_head, data ); return link( chunk_head.calculate_id(), seed ); } } else if( fc::is_directory(p) ) { auto dir = import_directory(p); fc::vector<char> data(MAX_CHUNK_SIZE); fc::datastream<char*> ds(data.data()+1, data.size()-1); fc::raw::pack( ds, dir ); data[0] = directory_type; data.resize( ds.tellp()+1 ); size_t seed = randomize(data, *((uint64_t*)fc::sha1::hash(data.data(),data.size()).data()) ); auto chunk_head = slice_chunk( data ); link l( chunk_head.calculate_id(), seed ); store_chunk( chunk_head, data ); return l; } FC_THROW_MSG( "Unsupported file type while importing '%s'", p.string() ); return cafs::link(); }
bool test_file::compare_files() { //first check if files are equal size, if not, files are different size_t result_file_size = boost::filesystem::file_size(_result_file.string()); size_t expected_result_file_size = boost::filesystem::file_size(_expected_result_file.string()); bool file_sizes_equal = (result_file_size == expected_result_file_size); if (!file_sizes_equal) return false; //files may be equal since they have the same size, so check further std::ifstream lhs(_result_file.string().c_str()); std::ifstream rhs(_expected_result_file.string().c_str()); typedef std::istreambuf_iterator<char> istreambuf_iterator; return std::equal( istreambuf_iterator(lhs), istreambuf_iterator(), istreambuf_iterator(rhs)); }
fc::string detail::process_impl::windows_shell_escape_command(const fc::path& exe, const vector<string>& args) { fc::stringstream command_line; command_line << windows_shell_escape(exe.string()); for (unsigned i = 0; i < args.size(); ++i) command_line << " " << windows_shell_escape(args[i]); return command_line.str(); }
void compile_json_to_source( const fc::path& source_file_name, const fc::path& prologue_file_name, const fc::path& json_file_name, const fc::path& epilogue_file_name ) { fc::mutable_variant_object template_context; template_context[ "source_file_name"] = source_file_name.string(); template_context["prologue_file_name"] = prologue_file_name.string(); template_context[ "json_file_name"] = json_file_name.string(); template_context["epilogue_file_name"] = epilogue_file_name.string(); std::ofstream source_file(source_file_name.string()); if (prologue_file_name.string() != "") { std::ifstream prologue_file(prologue_file_name.string()); std::stringstream ss_prologue_file; ss_prologue_file << prologue_file.rdbuf(); source_file << format_string(ss_prologue_file.str(), template_context); } std::ifstream json_file(json_file_name.string()); std::string line; bool first = true; while (std::getline(json_file, line)) { if (first) first = false; else source_file << ",\n"; source_file << " " << bts::utilities::escape_string_for_c_source_code(line); } if (epilogue_file_name.string() != "") { std::ifstream epilogue_file(epilogue_file_name.string()); std::stringstream ss_epilogue_file; ss_epilogue_file << epilogue_file.rdbuf(); source_file << format_string(ss_epilogue_file.str(), template_context); } return; }
void create_genesis_block(fc::path genesis_json_file) { vector<fc::ecc::private_key> delegate_private_keys; genesis_block_config config; config.precision = BTS_BLOCKCHAIN_PRECISION; config.timestamp = bts::blockchain::now(); config.base_symbol = BTS_BLOCKCHAIN_SYMBOL; config.base_name = BTS_BLOCKCHAIN_NAME; config.base_description = BTS_BLOCKCHAIN_DESCRIPTION; config.supply = BTS_BLOCKCHAIN_INITIAL_SHARES; // set our fake random number generator to generate deterministic keys set_random_seed_for_testing(fc::sha512()); std::ofstream key_stream( genesis_json_file.string() + ".keypairs" ); //create a script for importing the delegate keys std::ofstream delegate_key_import_stream(genesis_json_file.string() + ".log"); delegate_key_import_stream << CLI_PROMPT_SUFFIX " enable_output false" << std::endl; std::cout << "*** creating delegate public/private key pairs ***" << std::endl; for( uint32_t i = 0; i < BTS_BLOCKCHAIN_NUM_DELEGATES; ++i ) { name_config delegate_account; delegate_account.name = "delegate" + fc::to_string(i); fc::ecc::private_key delegate_private_key = fc::ecc::private_key::generate(); delegate_private_keys.push_back( delegate_private_key ); auto delegate_public_key =delegate_private_key.get_public_key(); delegate_account.owner = delegate_public_key; delegate_account.is_delegate = true; config.names.push_back(delegate_account); config.balances.push_back( std::make_pair( pts_address(fc::ecc::public_key_data(delegate_account.owner)), BTS_BLOCKCHAIN_INITIAL_SHARES/BTS_BLOCKCHAIN_NUM_DELEGATES) ); //output public/private key pair for each delegate to a file string wif_key = bts::utilities::key_to_wif( delegate_private_key ); key_stream << std::string(delegate_account.owner) << " " << wif_key << std::endl; //add command to import the delegate keys into a client delegate_key_import_stream << CLI_PROMPT_SUFFIX " wallet_import_private_key " << wif_key << " " << delegate_account.name << " false" << std::endl; } delegate_key_import_stream << CLI_PROMPT_SUFFIX " enable_output true" << std::endl; fc::json::save_to_file( config, genesis_json_file); }
bts_xt_client_test_config() { // parse command-line options boost::program_options::options_description option_config("Allowed options"); option_config.add_options()("bts-client-exe", boost::program_options::value<std::string>(), "full path to the executable to test") ("bts-server-exe", boost::program_options::value<std::string>(), "full path to the server executable for testing client-server mode") ("extra-help", "display this help message"); boost::program_options::variables_map option_variables; try { boost::program_options::store(boost::program_options::command_line_parser(boost::unit_test::framework::master_test_suite().argc, boost::unit_test::framework::master_test_suite().argv). options(option_config).run(), option_variables); boost::program_options::notify(option_variables); } catch (boost::program_options::error&) { std::cerr << "Error parsing command-line options\n\n"; std::cerr << option_config << "\n"; exit(1); } if (option_variables.count("extra-help")) { std::cout << option_config << "\n"; exit(0); } if (option_variables.count("bts-client-exe")) bts_client_exe = option_variables["bts-client-exe"].as<std::string>().c_str(); if (option_variables.count("bts-server-exe")) bts_server_exe = option_variables["bts-server-exe"].as<std::string>().c_str(); std::cout << "Testing " << bts_client_exe.string() << "\n"; std::cout << "Using config directory " << config_directory.string() << "\n"; fc::create_directories(config_directory); boost::unit_test::unit_test_log.set_threshold_level(boost::unit_test::log_messages); }
/** * Imports the file chunks into the database, but does not * import the file_header itself as a chunk, as it may be * imbedded within another chunk. * * @pre is_regular_file(p) */ cafs::file_header cafs::import_file( const fc::path& p ) { file_header head; head.file_size = fc::file_size(p); fc::vector<char> chunk( MAX_CHUNK_SIZE ); // divide file up into chunks and slices fc::ifstream in( p.string(), fc::ifstream::binary ); uint32_t r = 0; while( r < head.file_size ) { size_t some = fc::min( size_t(chunk.size()), size_t(head.file_size-r) ); in.read( chunk.data(), some ); size_t seed = randomize(chunk, *((uint64_t*)fc::sha1::hash(chunk.data(),chunk.size()).data()) ); chunk.resize(some); auto chunk_head = slice_chunk(chunk); auto chunk_id = store_chunk( chunk_head, chunk ); head.add_chunk( chunk_id, seed, chunk_head ); r += some; } return head; }
void run_regression_test(fc::path test_dir, bool with_network) { set_random_seed_for_testing(fc::sha512()); // open testconfig file // for each line in testconfig file // add a verify_file object that knows the name of the input command file and the generated log file // start a process with that command line // wait for all processes to shutdown // for each verify_file object, // compare generated log files in datadirs to golden reference file (i.e. input command files) // caller of this routine should have made sure we are already in bitshares_toolkit/test dir. // so we pop dirs to create regression_tests_results as sibling to bitshares_toolkit source directory // (because we don't want the test results to be inadvertantly added to git repo). fc::path original_working_directory = boost::filesystem::current_path(); fc::path regression_test_output_directory = original_working_directory.parent_path().parent_path(); regression_test_output_directory /= "regression_tests_output"; // Create an expected output file in the test subdir for the test output. fc::path test_output_dir = regression_test_output_directory / test_dir; boost::filesystem::create_directories(test_output_dir); fc::path expected_output_file = test_output_dir / "expected_output.log"; std::ofstream expected_output_stream(expected_output_file.string()); try { std::cout << "*** Executing " << test_dir.string() << std::endl; boost::filesystem::current_path(test_dir.string()); //create a small genesis block to reduce test startup time //fc::temp_directory temp_dir; //fc::path genesis_json_file = temp_dir.path() / "genesis.json"; //create_genesis_block(genesis_json_file); //DLN: now we just used the genesis file already committed to repo, update it // using "-t make_genesis_block" if desired. fc::path genesis_json_file = "../../test_genesis.json"; //open test configuration file (contains one line per client to create) fc::path test_config_file_name = "test.config"; std::ifstream test_config_file(test_config_file_name.string()); //create one client per line and run each client's input commands auto sim_network = std::make_shared<bts::net::simulated_network>(); vector<test_file> tests; string line = " --min-delegate-connection-count=0 "; fc::future<void> client_done; while (std::getline(test_config_file,line)) { //append genesis_file to load to command-line for now (later should be pre-created in test dir I think) line += " --genesis-config " + genesis_json_file.string(); //if no data-dir specified, put in ../bitshares_toolkit/regression_tests/${test dir}/${client_name} string client_name = line.substr(0, line.find(' ')); size_t data_dir_position = line.find("--data-dir"); if (data_dir_position == string::npos) { fc::path default_data_dir = regression_test_output_directory / test_dir / client_name; line += " --data-dir=" + default_data_dir.string(); fc::remove_all(default_data_dir); } //parse line into argc/argv format for boost program_options int argc = 0; char** argv = nullptr; #ifndef WIN32 // then UNIX //use wordexp to get argv/arc wordexp_t wordexp_result; wordexp(line.c_str(), &wordexp_result, 0); auto option_variables = parse_option_variables(wordexp_result.we_wordc, wordexp_result.we_wordv); argv = wordexp_result.we_wordv; argc = wordexp_result.we_wordc; #else //use ExpandEnvironmentStrings and CommandLineToArgv to get argv/arc char expanded_line[40000]; ExpandEnvironmentStrings(line.c_str(),expanded_line,sizeof(expanded_line)); argv = CommandLineToArgvA(expanded_line,&argc); auto option_variables = bts::client::parse_option_variables(argc, argv); #endif //extract input command files from cmdline options and concatenate into //one expected output file so that we can compare against output log std::vector<string> input_logs = option_variables["input-log"].as<std::vector<string>>(); for (string input_log : input_logs) { std::ifstream input_stream(input_log); expected_output_stream << input_stream.rdbuf(); } expected_output_stream.close(); //run client with cmdline options if (with_network) { FC_ASSERT(false, "Not implemented yet!") } else { bts::client::client_ptr client = std::make_shared<bts::client::client>(sim_network); client->configure_from_command_line(argc,argv); client_done = client->start(); } #ifndef WIN32 // then UNIX wordfree(&wordexp_result); #else GlobalFree(argv); #endif //add a test that compares input command file to client's log file fc::path result_file = ::get_data_dir(option_variables) / "console.log"; tests.push_back( test_file(client_done, result_file, expected_output_file) ); } //end while not end of test config file //check each client's log file against it's golden reference log file for (test_file current_test : tests) { //current_test.compare_files(); current_test.client_done.wait(); BOOST_CHECK_MESSAGE(current_test.compare_files(), "Results mismatch with golden reference log"); } }
void ifstream::open( const fc::path& file, int m ) { my->ifs.open( file.string().c_str(), std::ios::binary ); }
bool rpc_client_impl::import_bitcoin_wallet(const fc::path& wallet_filename, const std::string& password) { return _json_connection->call<bool>("import_bitcoin_wallet", wallet_filename.string(), password); }