OSDN Git Service

Fixed compilation on FreeBSD (TrueOS) as well as OpenBSD and OpenSolaris (OpenIndiana).
[mhash384/mhash384.git] / frontend / src / self_test.cpp
index f872cae..4c384e8 100644 (file)
@@ -1,6 +1,6 @@
 /* ---------------------------------------------------------------------------------------------- */
 /* MHash-384 - Simple fast portable secure hashing library                                        */
-/* Copyright(c) 2016-2019 LoRd_MuldeR <mulder2@gmx.de>                                            */
+/* Copyright(c) 2016-2020 LoRd_MuldeR <mulder2@gmx.de>                                            */
 /*                                                                                                */
 /* Permission is hereby granted, free of charge, to any person obtaining a copy of this software  */
 /* and associated documentation files (the "Software"), to deal in the Software without           */
@@ -25,9 +25,8 @@
 #include <cstring>
 #include <unordered_set>
 #include <array>
-#include <iostream>
-#include <fstream>
 #include <algorithm>
+#include <errno.h>
 
 /*
  * Test-case specification
@@ -64,18 +63,18 @@ static const test_case_t SELFTEST_INPUT[] =
  */
 static const uint8_t SELFTEST_EXPECTED[][MHASH384_SIZE] =
 {
-       { 0xD6, 0x29, 0xCB, 0x37, 0xF7, 0x61, 0x56, 0x62, 0x1E, 0x09, 0xC9, 0x3B, 0x8D, 0x10, 0x29, 0xC5, 0xF0, 0xF7, 0xB7, 0x78, 0xDE, 0x5B, 0xD9, 0x57, 0x82, 0x30, 0x67, 0x3F, 0xE7, 0xFA, 0x4C, 0x45, 0xA2, 0x30, 0x8A, 0xB4, 0xE9, 0xD9, 0x9A, 0xA0, 0xEA, 0x19, 0x4C, 0xBF, 0x62, 0x30, 0xC2, 0x02 },
-       { 0x1D, 0x7B, 0x15, 0x93, 0xD7, 0xAA, 0x4D, 0xD7, 0xDC, 0x8D, 0xA6, 0xD6, 0xBD, 0x59, 0x1D, 0x8B, 0x50, 0x7C, 0xA5, 0x14, 0xE1, 0xC7, 0xEC, 0x37, 0xF4, 0x3E, 0x78, 0xDD, 0x6B, 0x83, 0x25, 0x3C, 0x35, 0x54, 0x03, 0xF6, 0x10, 0x09, 0x58, 0x28, 0x90, 0x48, 0x0C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xDD, 0xA5, 0x4F, 0xC2, 0x75, 0xD1, 0x86, 0xA4, 0x5A, 0x88, 0x3C, 0x03, 0xAA, 0x87, 0x3D, 0xDA, 0xF4, 0xFA, 0xD4, 0x74, 0x42, 0x12, 0xB9, 0x2D, 0x26, 0x05, 0x4B, 0x92, 0x85, 0x2D, 0xEA, 0x5F, 0x1D },
-       { 0x62, 0x83, 0xCE, 0x91, 0xDA, 0x8A, 0xF5, 0x09, 0x1C, 0x7B, 0x06, 0xE1, 0x35, 0x2D, 0xC9, 0x47, 0xB9, 0xE3, 0xB8, 0x96, 0x14, 0x80, 0x7C, 0x04, 0xC4, 0x3D, 0xA3, 0x22, 0x32, 0x1F, 0xBA, 0xDA, 0xD3, 0xCC, 0xCA, 0xBE, 0xCB, 0x5C, 0x1B, 0x1B, 0x26, 0x54, 0x32, 0xA6, 0x03, 0x91, 0x21, 0xE1 },
-       { 0x5D, 0xCB, 0x7C, 0x4B, 0x27, 0xBA, 0x84, 0x16, 0x88, 0xFB, 0x6A, 0x86, 0x0E, 0x97, 0xA4, 0x52, 0x56, 0x91, 0x75, 0x33, 0xF6, 0x34, 0x48, 0xCF, 0x66, 0x67, 0x81, 0xEA, 0x4B, 0x5F, 0xAB, 0x20, 0xE1, 0xC7, 0x46, 0xC8, 0x82, 0x10, 0x99, 0xA9, 0x15, 0x22, 0x2E, 0x0D, 0x77, 0x62, 0x40, 0x98 }
+       { 0x4C, 0x4B, 0x82, 0xD0, 0x7B, 0x36, 0x8E, 0x1C, 0x22, 0xD0, 0xDE, 0x37, 0x59, 0xC3, 0x2D, 0x44, 0xDA, 0x71, 0xBE, 0x62, 0x83, 0xE8, 0x55, 0x0A, 0x24, 0x68, 0xDC, 0x1F, 0xEC, 0x38, 0x91, 0x9F, 0x7E, 0xDB, 0x6C, 0x1B, 0xA0, 0x83, 0x78, 0xEC, 0x58, 0x3A, 0xE6, 0x12, 0xAB, 0x0E, 0x02, 0xBA },
+       { 0x91, 0x71, 0xD8, 0x3E, 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8D, 0xBD, 0x61, 0xD4, 0xAB, 0x7C, 0x42, 0xE5, 0x81, 0xDB, 0xFD, 0xBF, 0xD2, 0xF5, 0xD8, 0xED, 0xF8, 0x2C, 0x3B, 0x35, 0xD6 },
+       { 0xE9, 0x7C, 0x79, 0x0B, 0x19, 0x45, 0x32, 0xA5, 0x9B, 0xC8, 0x40, 0x90, 0xB5, 0xC6, 0x8C, 0x5B, 0x0D, 0x05, 0x0C, 0x6F, 0xE9, 0x37, 0xAB, 0xDF, 0x48, 0x0C, 0xC1, 0x9C, 0x34, 0x5B, 0x72, 0xFE, 0xF9, 0x25, 0xD8, 0x3B, 0xF9, 0xB4, 0x2D, 0x1A, 0x8F, 0x57, 0x2A, 0xDE, 0x7A, 0x50, 0x9F, 0xF9 },
+       { 0xA7, 0x72, 0xD7, 0xB9, 0x84, 0xAB, 0xC7, 0x90, 0xA9, 0xFF, 0xF5, 0x1F, 0x3B, 0xD7, 0xC6, 0xA5, 0x38, 0x44, 0xA2, 0x33, 0xA5, 0x64, 0xA9, 0x70, 0x87, 0x2C, 0x41, 0x34, 0x5A, 0xFE, 0x19, 0x98, 0x3B, 0x8D, 0x3C, 0xE3, 0x0B, 0x90, 0x0F, 0xD7, 0xFD, 0xD6, 0x6C, 0xED, 0x03, 0xD0, 0xCD, 0x6E },
+       { 0x61, 0x4A, 0x6B, 0x25, 0xBD, 0x67, 0x32, 0x16, 0xED, 0xEA, 0xB6, 0xA0, 0x51, 0xA8, 0xB4, 0x86, 0x9F, 0x9A, 0xD8, 0x0C, 0xC5, 0xDD, 0x4A, 0xE6, 0x29, 0xDD, 0xFB, 0x70, 0xCA, 0xA7, 0x0E, 0x49, 0xD5, 0x1E, 0x70, 0x27, 0xFF, 0x35, 0xA1, 0x83, 0xA2, 0x78, 0xFE, 0x97, 0xF8, 0x75, 0x9C, 0xF9 }
 };
 
 /*
@@ -126,9 +125,38 @@ typedef std::unordered_set<std::array<std::uint8_t,MHASH384_SIZE>, KeyHasher, Ke
 typedef UnorderedHashSet::iterator HashSetIter;
 
 /*
+ * Read the next input line
+ */
+static bool read_line(FILE *const input, char *const line, const int max_count, bool &flag)
+{
+       for(;;)
+       {
+               const bool discard = flag;
+               flag = true;
+               if(fgets(line, max_count, input) != NULL)
+               {
+                       size_t len = strlen(line);
+                       while((len > 0) && (line[len - 1U] == '\n'))
+                       {
+                               flag = false; /*line not truncated*/
+                               line[--len] = '\0';
+                       }
+                       if(!discard)
+                       {
+                               return true;
+                       }
+               }
+               else
+               {
+                       return false;
+               }
+       }
+}
+
+/*
  * Compute hash and compare against reference
  */
-static bool test_string(const uint32_t count, const char *const text, const uint8_t *const expected, const bool lower_case)
+static bool test_string(const uint32_t count, const char *const text, const uint8_t *const expected, const options_t &options)
 {
        MHash384 mhash384;
        for(uint32_t i = 0U; i < count; ++i)
@@ -139,9 +167,9 @@ static bool test_string(const uint32_t count, const char *const text, const uint
        const uint8_t *const digest = mhash384.finish();
        const bool success = (!memcmp(digest, expected, MHASH384_SIZE));
 
-       const std::string digest_hexstr = bytes_to_hex(digest, MHASH384_SIZE, lower_case);
+       const std::string string = options.base64 ? bytes_to_base64(digest, MHASH384_SIZE) : bytes_to_hex(digest, MHASH384_SIZE, options.lower_case);
        const CHAR_T *const result = success ? STR("OK") : STR("Error!");
-       FPRINTF(stderr, STR("%") PRI_char STR(" - %") PRI_CHAR STR("\n"), digest_hexstr.c_str(), result);
+       FPRINTF(stderr, STR("%") PRI_char STR(" - %") PRI_CHAR STR("\n"), string.c_str(), result);
        
        fflush(stderr);
        return success;
@@ -150,13 +178,13 @@ static bool test_string(const uint32_t count, const char *const text, const uint
 /*
  * Compute hash and append to hashset
  */
-static bool append_string(UnorderedHashSet &hash_set, std::vector<std::array<uint64_t,256U>> &stats, const char *const text, const bool lower_case)
+static bool append_string(UnorderedHashSet &hash_set, std::vector<std::array<uint64_t,256U>> &stats, const char *const text, const bool base64, const bool lower_case)
 {
        std::array<uint8_t, MHASH384_SIZE> digest;
-       mhash384_get(digest.data(), reinterpret_cast<const uint8_t*>(text), strlen(text));
+       mhash384_compute(digest.data(), reinterpret_cast<const uint8_t*>(text), strlen(text));
 
-       const std::string digest_hexstr = bytes_to_hex(digest.data(), MHASH384_SIZE, lower_case);
-       FPRINTF(stderr, STR("%") PRI_char STR("\n"), digest_hexstr.c_str());
+       const std::string string = base64 ? bytes_to_base64(digest.data(), MHASH384_SIZE) : bytes_to_hex(digest.data(), MHASH384_SIZE, lower_case);
+       FPRINTF(stderr, STR("%") PRI_char STR("\n"), string.c_str());
        
        for(size_t i = 0U; i < MHASH384_SIZE; ++i)
        {
@@ -176,7 +204,7 @@ static bool append_string(UnorderedHashSet &hash_set, std::vector<std::array<uin
 /*
  * Self-testing routine
  */
-bool self_test(const bool keep_going, const bool lower_case)
+bool self_test(const options_t &options)
 {
        bool success = mhash384_selftest();
 
@@ -184,10 +212,10 @@ bool self_test(const bool keep_going, const bool lower_case)
        {
                for(size_t i = 0U; SELFTEST_INPUT[i].count > 0U; ++i)
                {
-                       if(!test_string(SELFTEST_INPUT[i].count, SELFTEST_INPUT[i].string, SELFTEST_EXPECTED[i], lower_case))
+                       if(!test_string(SELFTEST_INPUT[i].count, SELFTEST_INPUT[i].string, SELFTEST_EXPECTED[i], options))
                        {
                                success = false;
-                               if(!keep_going)
+                               if(!options.keep_going)
                                {
                                        break; /*failure*/
                                }
@@ -210,18 +238,13 @@ bool self_test(const bool keep_going, const bool lower_case)
 /*
  * Stress-testing routine
  */
-bool stress_test(const CHAR_T *const file_name, const bool keep_going, const bool lower_case)
+bool stress_test(const CHAR_T *const file_name, const options_t &options)
 {
-       std::ifstream infile;
-       if(file_name)
+       errno = 0;
+       FILE *const input = file_name ? FOPEN(file_name, STR("r")) : stdin;
+       if(!input)
        {
-               infile.open(file_name);
-       }
-
-       std::istream &instream = file_name ? infile : std::cin;
-       if(!instream.good())
-       {
-               FPUTS(STR("Error: Failed to open input file for reading!\n"), stderr);
+               FPUTS(STR("Error: Failed to open specified input file for reading!\n"), stderr);
                return false;
        }
 
@@ -231,20 +254,19 @@ bool stress_test(const CHAR_T *const file_name, const bool keep_going, const boo
                stats[i].fill(0U);
        }
 
-       bool success = true;
        UnorderedHashSet hash_set;
-       std::string line;
+       char line[1024U];
+       bool success = true, flag = false;
 
-       while(instream.good())
+       while(read_line(input, line, 1024U, flag))
        {
-               std::getline(instream, line);
-               if(!instream.fail()) 
+               /*FPRINTF(stderr, STR("\"%") PRI_char STR("\"\n"), line);*/
+               if(line[0U])
                {
-                       //FPRINTF(stderr, STR("\"%") PRI_char STR("\"\n"), line.c_str());
-                       if(!append_string(hash_set, stats, line.c_str(), lower_case))
+                       if(!append_string(hash_set, stats, line, options.base64, options.lower_case))
                        {
                                success = false;
-                               if(!keep_going)
+                               if(!options.keep_going)
                                {
                                        break; /*collison*/
                                }
@@ -269,7 +291,7 @@ bool stress_test(const CHAR_T *const file_name, const bool keep_going, const boo
 
        if(success)
        {
-               if(instream.eof() && (!instream.bad()))
+               if(!ferror(input))
                {
                        FPRINTF(stderr, STR("\nStress-test completed successfully. [%") STR(PRIu64) STR("]\n"), static_cast<uint64_t>(hash_set.size()));
                }
@@ -284,9 +306,9 @@ bool stress_test(const CHAR_T *const file_name, const bool keep_going, const boo
                FPRINTF(stderr, STR("\nStress-test ended *with* collision! [%") STR(PRIu64) STR("]\n"), static_cast<uint64_t>(hash_set.size()));
        }
        
-       if(file_name && infile.is_open())
+       if(file_name)
        {
-               infile.close();
+               fclose(input);
        }
 
        return success;