• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 11844561993

14 Nov 2024 07:58PM UTC coverage: 91.178% (+0.1%) from 91.072%
11844561993

Pull #4435

github

web-flow
Merge 81dcb29da into e430f157a
Pull Request #4435: Test duration values ​​are now presented in seconds with six digits of precision. Tests without time measurements have been edited.

91856 of 100744 relevant lines covered (91.18%)

9311006.71 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

83.33
/src/tests/test_os_utils.cpp
1
/*
2
* Testing operating system specific wrapper code
3
* (C) 2017 Jack Lloyd
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "tests.h"
9
#include <botan/internal/os_utils.h>
10

11
// For __ud2 intrinsic
12
#if defined(BOTAN_TARGET_COMPILER_IS_MSVC)
13
   #include <intrin.h>
14
#endif
15

16
namespace Botan_Tests {
17

18
namespace {
19

20
/*
21
uint32_t get_process_id();
22
uint64_t get_cpu_cycle_counter();
23
uint64_t get_system_timestamp_ns();
24
size_t get_memory_locking_limit();
25
void* allocate_locked_pages(size_t length);
26
void free_locked_pages(void* ptr, size_t length);
27
int run_cpu_instruction_probe(std::function<int ()> probe_fn);
28
*/
29

30
class OS_Utils_Tests final : public Test {
×
31
   public:
32
      std::vector<Test::Result> run() override {
1✔
33
         std::vector<Test::Result> results;
1✔
34

35
         results.push_back(test_get_process_id());
2✔
36
         results.push_back(test_get_cpu_cycle_counter());
2✔
37
         results.push_back(test_get_high_resolution_clock());
2✔
38
         results.push_back(test_get_cpu_numbers());
2✔
39
         results.push_back(test_get_system_timestamp());
2✔
40
         results.push_back(test_memory_locking());
2✔
41
         results.push_back(test_cpu_instruction_probe());
2✔
42

43
         return results;
1✔
44
      }
×
45

46
   private:
47
      static Test::Result test_get_process_id() {
1✔
48
         Test::Result result("OS::get_process_id");
1✔
49
         result.start_timer();
1✔
50

51
         uint32_t pid1 = Botan::OS::get_process_id();
1✔
52
         uint32_t pid2 = Botan::OS::get_process_id();
1✔
53

54
         result.test_eq("PID same across calls", static_cast<size_t>(pid1), static_cast<size_t>(pid2));
1✔
55

56
#if defined(BOTAN_TARGET_OS_IS_LLVM) || defined(BOTAN_TARGET_OS_IS_NONE)
57
         result.test_eq("PID is expected to be zero on this platform", pid1, size_t(0));
58
#else
59
         result.test_ne("PID is non-zero on systems with processes", pid1, 0);
1✔
60
#endif
61
         result.end_timer();
1✔
62
         return result;
1✔
63
      }
×
64

65
      static Test::Result test_get_cpu_cycle_counter() {
1✔
66
         const size_t max_trials = 1024;
1✔
67
         const size_t max_repeats = 32;
1✔
68

69
         Test::Result result("OS::get_cpu_cycle_counter");
1✔
70
         result.start_timer();
1✔
71

72
         const uint64_t proc_ts1 = Botan::OS::get_cpu_cycle_counter();
1✔
73

74
         if(proc_ts1 == 0) {
1✔
75
            const uint64_t proc_ts2 = Botan::OS::get_cpu_cycle_counter();
×
76
            result.test_is_eq("Disabled processor timestamp stays at zero", proc_ts1, proc_ts2);
×
77
            return result;
×
78
         }
79

80
         size_t counts = 0;
81
         while(counts < max_trials && (Botan::OS::get_cpu_cycle_counter() == proc_ts1)) {
1✔
82
            ++counts;
×
83
         }
84

85
         result.test_lt("CPU cycle counter eventually changes value", counts, max_repeats);
1✔
86

87
         result.end_timer();
1✔
88
         return result;
89
      }
×
90

91
      static Test::Result test_get_high_resolution_clock() {
1✔
92
         const size_t max_trials = 1024;
1✔
93
         const size_t max_repeats = 128;
1✔
94

95
         Test::Result result("OS::get_high_resolution_clock");
1✔
96
         result.start_timer();
1✔
97

98
         // TODO better tests
99
         const uint64_t hr_ts1 = Botan::OS::get_high_resolution_clock();
1✔
100
         result.confirm("high resolution timestamp value is never zero", hr_ts1 != 0);
2✔
101

102
         size_t counts = 0;
1✔
103
         while(counts < max_trials && (Botan::OS::get_high_resolution_clock() == hr_ts1)) {
1✔
104
            ++counts;
×
105
         }
106

107
         result.test_lt("high resolution clock eventually changes value", counts, max_repeats);
1✔
108

109
         result.end_timer();
1✔
110
         return result;
1✔
111
      }
×
112

113
      static Test::Result test_get_cpu_numbers() {
1✔
114
         Test::Result result("OS::get_cpu_available");
1✔
115
         result.start_timer();
1✔
116

117
         const size_t ta = Botan::OS::get_cpu_available();
1✔
118

119
         result.test_gte("get_cpu_available is at least 1", ta, 1);
1✔
120

121
         result.end_timer();
1✔
122
         return result;
1✔
123
      }
×
124

125
      static Test::Result test_get_system_timestamp() {
1✔
126
         // TODO better tests
127
         Test::Result result("OS::get_system_timestamp_ns");
1✔
128
         result.start_timer();
1✔
129

130
         uint64_t sys_ts1 = Botan::OS::get_system_timestamp_ns();
1✔
131
         result.confirm("System timestamp value is never zero", sys_ts1 != 0);
2✔
132

133
         // do something that consumes a little time
134
         Botan::OS::get_process_id();
1✔
135

136
         uint64_t sys_ts2 = Botan::OS::get_system_timestamp_ns();
1✔
137

138
         result.confirm("System time moves forward", sys_ts1 <= sys_ts2);
2✔
139

140
         result.end_timer();
1✔
141
         return result;
1✔
142
      }
×
143

144
      static Test::Result test_memory_locking() {
1✔
145
         Test::Result result("OS memory locked pages");
1✔
146
         result.start_timer();
1✔
147

148
         // TODO any tests...
149

150
         result.end_timer();
1✔
151
         return result;
1✔
152
      }
×
153

154
      static Test::Result test_cpu_instruction_probe() {
1✔
155
         Test::Result result("OS::run_cpu_instruction_probe");
1✔
156
         result.start_timer();
1✔
157

158
         // OS::run_cpu_instruction_probe only implemented for Unix signals or Windows SEH
159

160
         std::function<int()> ok_fn = []() noexcept -> int { return 5; };
1✔
161
         const int run_rc = Botan::OS::run_cpu_instruction_probe(ok_fn);
1✔
162

163
         if(run_rc == -3) {
1✔
164
            result.test_note("run_cpu_instruction_probe not implemented on this platform");
×
165
            return {result};
×
166
         }
167

168
         result.confirm("Correct result returned by working probe fn", run_rc == 5);
2✔
169

170
         std::function<int()> crash_probe;
1✔
171

172
#if defined(BOTAN_TARGET_COMPILER_IS_MSVC)
173
         crash_probe = []() noexcept -> int {
174
            __ud2();
175
            return 3;
176
         };
177

178
#elif defined(BOTAN_USE_GCC_INLINE_ASM)
179

180
   #if defined(BOTAN_TARGET_CPU_IS_X86_FAMILY)
181
         crash_probe = []() noexcept -> int {
3✔
182
            asm volatile("ud2");
1✔
183
            return 3;
×
184
         };
1✔
185

186
   #elif defined(BOTAN_TARGET_CPU_IS_ARM_FAMILY)
187
         //ARM: asm volatile (".word 0xf7f0a000\n");
188
         // illegal instruction in both ARM and Thumb modes
189
         crash_probe = []() noexcept -> int {
190
            asm volatile(".word 0xe7f0def0\n");
191
            return 3;
192
         };
193

194
   #else
195
            /*
196
         PPC: "The instruction with primary opcode 0, when the instruction does not consist
197
         entirely of binary zeros"
198
         Others ?
199
         */
200
   #endif
201

202
#endif
203

204
         if(crash_probe) {
1✔
205
            const int crash_rc = Botan::OS::run_cpu_instruction_probe(crash_probe);
1✔
206
            result.confirm("Result for function executing undefined opcode", crash_rc < 0);
2✔
207
         }
208

209
         result.end_timer();
1✔
210
         return result;
1✔
211
      }
2✔
212
};
213

214
BOTAN_REGISTER_TEST("utils", "os_utils", OS_Utils_Tests);
215

216
}  // namespace
217

218
}  // namespace Botan_Tests
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc