2020-02-03 05:00:36 +01:00
|
|
|
// Copyright (c) 2019-2020 Alexander Medvednikov. All rights reserved.
|
2019-07-31 03:24:12 +02:00
|
|
|
// Use of this source code is governed by an MIT license
|
|
|
|
// that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
import crypto.rand
|
|
|
|
|
2019-09-17 21:03:54 +02:00
|
|
|
fn test_crypto_rand_read() {
|
2019-09-03 15:10:26 +02:00
|
|
|
no_bytes := 100
|
|
|
|
max_percentage_diff := 20
|
|
|
|
|
|
|
|
r1 := rand.read(no_bytes) or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
assert r1.len == no_bytes
|
|
|
|
r2 := rand.read(no_bytes) or {
|
2019-07-31 03:24:12 +02:00
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
2019-09-03 15:10:26 +02:00
|
|
|
assert r2.len == no_bytes
|
2020-04-26 06:39:23 +02:00
|
|
|
|
2019-09-03 15:10:26 +02:00
|
|
|
mut difference := 0
|
|
|
|
for i, _ in r1 {
|
|
|
|
difference += if r1[i] == r2[i] {0} else {1}
|
|
|
|
}
|
|
|
|
|
|
|
|
diff_percentage := f32(100) - (f32(difference)/f32(no_bytes)*100)
|
|
|
|
|
|
|
|
assert diff_percentage <= max_percentage_diff
|
2019-07-31 03:24:12 +02:00
|
|
|
}
|
2019-09-17 21:03:54 +02:00
|
|
|
|
2019-09-18 15:12:16 +02:00
|
|
|
fn test_crypto_rand_int_u64() {
|
2019-09-19 12:12:40 +02:00
|
|
|
max := u64(160)
|
2020-04-26 11:56:27 +02:00
|
|
|
mut unique := []int{}
|
2019-09-19 12:12:40 +02:00
|
|
|
for _ in 0..80 {
|
|
|
|
r := rand.int_u64(max) or {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if r >= max {
|
|
|
|
assert false
|
|
|
|
return
|
|
|
|
}
|
|
|
|
n := int(r)
|
2020-04-26 06:39:23 +02:00
|
|
|
if n !in unique {
|
2019-09-19 12:12:40 +02:00
|
|
|
unique << n
|
|
|
|
}
|
2019-09-17 21:03:54 +02:00
|
|
|
}
|
2019-09-19 12:12:40 +02:00
|
|
|
assert unique.len >= 40
|
2019-09-17 21:03:54 +02:00
|
|
|
}
|