diff options
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; } |