📄 validate.cpp
字号:
/* Validation routines This file is in the public domain*/#include <iostream>#include <fstream>#include <cctype>#include <string>#include <vector>#include <botan/filters.h>#include <botan/rng.h>using namespace Botan_types;#define DEBUG 0#define EXTRA_TESTS 0#ifndef DEBUG #define DEBUG 0#endifu32bit random_word(u32bit max) { u32bit r = 0; for(u32bit j = 0; j != 4; j++) r = (r << 8) | Botan::Global_RNG::random(); return ((r % max) + 1); // return between 1 and max inclusive }Botan::Filter* lookup(const std::string&, const std::string&, const std::string&);bool failed_test(const std::string&, const std::string&, const std::string&, const std::string&, const std::string&, bool, bool);std::vector<std::string> parse(const std::string&);void strip(std::string&);Botan::SecureVector<byte> decode_hex(const std::string&);u32bit do_validation_tests(const std::string& filename, bool should_pass) { std::ifstream test_data(filename.c_str()); bool first_mark = true; if(!test_data) { std::cout << "Couldn't open test file " << filename << std::endl; std::exit(1); } u32bit errors = 0, alg_count = 0; std::string algorithm; bool is_extension = false; u32bit counter = 0; while(!test_data.eof()) { if(test_data.bad() || test_data.fail()) { std::cout << "File I/O error." << std::endl; std::exit(1); } std::string line; std::getline(test_data, line); const std::string MARK = "# MARKER: "; if(line.find(MARK) != std::string::npos) { if(first_mark) first_mark = false; else std::cout << std::endl; counter = 0; std::string linex = line; linex.replace(line.find(MARK), MARK.size(), ""); if(should_pass) std::cout << "Testing " << linex << ": "; } strip(line); if(line.size() == 0) continue; // Do line continuation while(line[line.size()-1] == '\\' && !test_data.eof()) { line.replace(line.size()-1, 1, ""); std::string nextline; std::getline(test_data, nextline); strip(nextline); if(nextline.size() == 0) continue; line += nextline; } if(line[0] == '[' && line[line.size() - 1] == ']') { const std::string ext_mark = "<EXTENSION>"; algorithm = line.substr(1, line.size() - 2); is_extension = false; if(algorithm.find(ext_mark) != std::string::npos) { is_extension = true; algorithm.replace(algorithm.find(ext_mark), ext_mark.length(), ""); }#if DEBUG if(should_pass) std::cout << "Testing " << algorithm << "..." << std::endl; else std::cout << "Testing (expecing failure) " << algorithm << "..." << std::endl;#endif alg_count = 0; continue; } std::vector<std::string> substr = parse(line); alg_count++; if(should_pass && counter % 10 == 0) { std::cout << '.'; std::cout.flush(); } counter++; bool failed = failed_test(algorithm, substr[0], substr[1], substr[2], substr[3], is_extension, should_pass); if(failed && should_pass) { std::cout << "ERROR: \"" << algorithm << "\" failed test #" << alg_count << std::endl; errors++; } if(!failed && !should_pass) { std::cout << "ERROR: \"" << algorithm << "\" passed test #" << alg_count << " (unexpected pass)" << std::endl; errors++; } } if(should_pass) std::cout << std::endl; return errors; }bool failed_test(const std::string& algo, const std::string& in, const std::string& expected, const std::string& key, const std::string& iv, bool is_extension, bool exp_pass) {#if DEBUG std::cout << "Testing: " << algo; if(!exp_pass) std::cout << " (expecting failure)"; std::cout << std::endl;#endif#if !EXTRA_TESTS if(!exp_pass) return true;#endif if(in.size() % 2 == 1) { std::cout << "Can't have an odd sized hex string!" << std::endl; return true; } Botan::Pipe pipe; try { Botan::Filter* test = lookup(algo, key, iv); if(test == 0 && is_extension) return !exp_pass; if(test == 0) { std::cout << "WARNING: \"" + algo + "\" is not a known algorithm name." << std::endl; return 0; } pipe.append(test); pipe.append(new Botan::Hex_Encoder); pipe.start_msg(); Botan::SecureVector<byte> data = decode_hex(in); const byte* data_ptr = data; // this can help catch errors with buffering, etc u32bit len = data.size(); while(len) { u32bit how_much = random_word(len); pipe.write(data_ptr, how_much); data_ptr += how_much; len -= how_much; } pipe.end_msg(); } catch(Botan::Algorithm_Not_Found& e) { std::cout << "Algorithm not found: " << e.what() << std::endl; return false; } catch(Botan::Exception& e) { if(exp_pass || DEBUG) std::cout << "Exception caught: " << e.what() << std::endl; return true; } catch(std::exception& e) { if(exp_pass || DEBUG) std::cout << "Standard library exception caught: " << e.what() << std::endl; return true; } catch(...) { if(exp_pass || DEBUG) std::cout << "Unknown exception caught." << std::endl; return true; } std::string output = pipe.read_all_as_string(); if(output == expected && !exp_pass) { std::cout << "FAILED: " << expected << " == " << std::endl << " " << output << std::endl; return false; } if(output != expected && exp_pass) { std::cout << "\nFAILED: " << expected << " != " << std::endl << " " << output << std::endl; return true; } if(output != expected && !exp_pass) return true; return false; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -