diff options
author | Jack Lloyd <[email protected]> | 2017-01-27 22:12:50 -0500 |
---|---|---|
committer | Jack Lloyd <[email protected]> | 2017-01-27 22:12:50 -0500 |
commit | 641798154f2083258b6ffb0c398d73221ff8959e (patch) | |
tree | 7fdfb0a0ea21fd7d691ad8b2a98205613cf2e2ea /src/tests | |
parent | 5cbba3ad662fa76090a3bea546a1178a011737a7 (diff) |
Change meaning of get_processor_timestamp
Now let it return 0 if we have no hardware timestamp, and add
OS::get_high_resolution_clock for best available clock.
This is mainly because it's confusing for get_processor_timestamp to return
something that is not a processor timestamp and because it simplifies adding
cycles/byte output if we know that something is or is not a cycle counter.
Also adds Windows SEH version of run_cpu_instruction_probe. Untested, uncompiled.
Diffstat (limited to 'src/tests')
-rw-r--r-- | src/tests/test_os_utils.cpp | 64 |
1 files changed, 52 insertions, 12 deletions
diff --git a/src/tests/test_os_utils.cpp b/src/tests/test_os_utils.cpp index bdcbf2a90..7a7205e2d 100644 --- a/src/tests/test_os_utils.cpp +++ b/src/tests/test_os_utils.cpp @@ -8,6 +8,11 @@ #include "tests.h" #include <botan/internal/os_utils.h> +// For __ud2 intrinsic +#if defined(BOTAN_TARGET_COMPILER_IS_MSVC) + #include <intrin.h> +#endif + namespace Botan_Tests { namespace { @@ -31,6 +36,7 @@ class OS_Utils_Tests : public Test results.push_back(test_get_process_id()); results.push_back(test_get_processor_timestamp()); + results.push_back(test_get_high_resolution_clock()); results.push_back(test_get_system_timestamp()); results.push_back(test_memory_locking()); results.push_back(test_cpu_instruction_probe()); @@ -60,22 +66,45 @@ class OS_Utils_Tests : public Test Test::Result test_get_processor_timestamp() { + // TODO better tests Test::Result result("OS::get_processor_timestamp"); - uint64_t proc_ts1 = Botan::OS::get_processor_timestamp(); - result.test_ne("Processor timestamp value is never zero", proc_ts1, 0); + const uint64_t proc_ts1 = Botan::OS::get_processor_timestamp(); // do something that consumes a little time Botan::OS::get_process_id(); uint64_t proc_ts2 = Botan::OS::get_processor_timestamp(); - result.test_ne("Processor timestamp does not duplicate", proc_ts1, proc_ts2); + if(proc_ts1 == 0) + result.test_eq("Disabled processor timestamp stays at zero", proc_ts1, proc_ts2); + else + result.test_ne("Processor timestamp does not duplicate", proc_ts1, proc_ts2); + + return result; + } + + Test::Result test_get_high_resolution_clock() + { + // TODO better tests + + Test::Result result("OS::get_high_resolution_clock"); + + uint64_t hr_ts1 = Botan::OS::get_high_resolution_clock(); + result.test_ne("high resolution timestamp value is never zero", hr_ts1, 0); + + // do something that consumes a little time + Botan::OS::get_process_id(); + + uint64_t hr_ts2 = Botan::OS::get_high_resolution_clock(); + + result.test_ne("high resolution timestamp does not duplicate", hr_ts1, hr_ts2); return result; } Test::Result test_get_system_timestamp() { + // TODO better tests Test::Result result("OS::get_system_timestamp_ns"); uint64_t sys_ts1 = Botan::OS::get_system_timestamp_ns(); @@ -89,13 +118,14 @@ class OS_Utils_Tests : public Test result.confirm("System time moves forward", sys_ts1 <= sys_ts2); return result; - - return result; } Test::Result test_memory_locking() { Test::Result result("OS memory locked pages"); + + // TODO any tests... + return result; } @@ -103,27 +133,38 @@ class OS_Utils_Tests : public Test { Test::Result result("OS::run_cpu_instruction_probe"); -#if defined(BOTAN_TARGET_OS_TYPE_IS_UNIX) - // OS::run_cpu_instruction_probe only implemented for Unix signals right now + // OS::run_cpu_instruction_probe only implemented for Unix signals or Windows SEH std::function<int ()> ok_fn = []() -> int { return 5; }; const int run_rc = Botan::OS::run_cpu_instruction_probe(ok_fn); + + if(run_rc == -3) + { + result.test_note("run_cpu_instruction_probe not implemented on this platform"); + return {result}; + } + result.confirm("Correct result returned by working probe fn", run_rc == 5); std::function<int ()> throw_fn = []() -> int { throw 3.14159; return 5; }; const int throw_rc = Botan::OS::run_cpu_instruction_probe(throw_fn); result.confirm("Error return if probe function threw exception", throw_rc < 0); -#if defined(BOTAN_USE_GCC_INLINE_ASM) - std::function<int ()> crash_probe; +#if defined(BOTAN_TARGET_COMPILER_IS_MSVC) + crash_probe = []() -> int { __ud2(); return 3; }; + +#elif defined(BOTAN_USE_GCC_INLINE_ASM) + #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY) crash_probe = []() -> int { asm volatile("ud2"); return 3; }; + #elif defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY) //ARM: asm volatile (".word 0xf7f0a000\n"); // illegal instruction in both ARM and Thumb modes crash_probe = []() -> int { asm volatile(".word 0xe7f0def0\n"); return 3; }; + #else /* PPC: "The instruction with primary opcode 0, when the instruction does not consist @@ -132,14 +173,13 @@ class OS_Utils_Tests : public Test */ #endif +#endif + if(crash_probe) { const int crash_rc = Botan::OS::run_cpu_instruction_probe(crash_probe); result.confirm("Result for function executing undefined opcode", crash_rc < 0); } -#endif - -#endif return result; } |