2022-02-13 02:14:54 +00:00
|
|
|
package entropy
|
2022-01-02 07:34:30 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"strings"
|
2022-07-12 09:00:54 +00:00
|
|
|
"sync"
|
2022-01-02 07:34:30 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2022-12-11 20:02:46 +00:00
|
|
|
var dupCount = 0
|
|
|
|
|
2022-03-26 15:25:52 +00:00
|
|
|
func check[T comparable](zero T, one T, t *testing.T) {
|
|
|
|
if zero == one {
|
2022-12-11 20:02:46 +00:00
|
|
|
dupCount++
|
2022-03-26 15:25:52 +00:00
|
|
|
t.Errorf("hit a duplicate! %v == %v", zero, one)
|
2022-12-11 20:02:46 +00:00
|
|
|
t.Logf("duplicates so far: %d", dupCount)
|
2022-03-26 15:25:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-02 07:34:30 +00:00
|
|
|
func Test_RNG(t *testing.T) {
|
2022-10-11 08:06:20 +00:00
|
|
|
// for coverage
|
|
|
|
sharedRand = GetSharedRand()
|
2022-02-13 03:27:04 +00:00
|
|
|
RandSleepMS(5)
|
2022-10-11 08:06:20 +00:00
|
|
|
sharedRand = nil
|
|
|
|
getSharedRand = &sync.Once{}
|
|
|
|
// - - - - - -
|
2022-02-13 03:27:04 +00:00
|
|
|
if OneInA(1000000) {
|
|
|
|
println(string([]byte{
|
|
|
|
0x66, 0x75, 0x63, 0x6B, 0x68,
|
|
|
|
0x6F, 0x6C, 0x65, 0x20, 0x6A,
|
|
|
|
0x6F, 0x6E, 0x65, 0x73, 0x2E,
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
|
2022-01-02 07:34:30 +00:00
|
|
|
for n := 0; n != 500; n++ {
|
2022-03-26 15:25:52 +00:00
|
|
|
check(RNG(55555), RNG(55555), t)
|
|
|
|
check(RNGUint32(), RNGUint32(), t)
|
2022-01-02 07:34:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-12-11 20:02:46 +00:00
|
|
|
func Test_OneInA(t *testing.T) {
|
|
|
|
for n := 0; n < 100; n++ {
|
|
|
|
yes := ""
|
|
|
|
if OneInA(1) {
|
|
|
|
yes = "hello"
|
|
|
|
}
|
|
|
|
if yes != "hello" {
|
|
|
|
t.Fatalf("OneInA failed to trigger when provided '1' as an argument")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-13 12:09:07 +00:00
|
|
|
func randStrChecks(zero, one string, t *testing.T, intendedLength int) {
|
2022-01-02 07:34:30 +00:00
|
|
|
if len(zero) != len(one) {
|
|
|
|
t.Fatalf("RandStr output length inconsistency, len(zero) is %d but wanted len(one) which is %d", len(zero), len(one))
|
|
|
|
}
|
2022-07-13 12:09:07 +00:00
|
|
|
if len(zero) != intendedLength || len(one) != intendedLength {
|
2022-01-02 07:34:30 +00:00
|
|
|
t.Fatalf("RandStr output length inconsistency, len(zero) is %d and len(one) is %d, but both should have been 55", len(zero), len(one))
|
|
|
|
}
|
2022-03-26 15:25:52 +00:00
|
|
|
check(zero, one, t)
|
2022-01-02 07:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_RandStr(t *testing.T) {
|
|
|
|
for n := 0; n != 500; n++ {
|
|
|
|
zero := RandStr(55)
|
|
|
|
one := RandStr(55)
|
2022-07-13 12:09:07 +00:00
|
|
|
t.Logf("Random0: %s Random1: %s", zero, one)
|
|
|
|
randStrChecks(zero, one, t, 55)
|
|
|
|
}
|
|
|
|
t.Logf("[SUCCESS] RandStr had no collisions")
|
|
|
|
}
|
|
|
|
|
|
|
|
func Test_RandStrWithUpper(t *testing.T) {
|
|
|
|
for n := 0; n != 500; n++ {
|
|
|
|
zero := RandStrWithUpper(15)
|
|
|
|
one := RandStrWithUpper(15)
|
|
|
|
t.Logf("Random0: %s Random1: %s", zero, one)
|
|
|
|
randStrChecks(zero, one, t, 15)
|
2022-01-02 07:34:30 +00:00
|
|
|
}
|
2022-07-12 09:00:54 +00:00
|
|
|
t.Logf("[SUCCESS] RandStr had no collisions")
|
2022-01-02 07:34:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func Test_RandStr_Entropy(t *testing.T) {
|
2022-03-16 03:39:22 +00:00
|
|
|
var totalScore = 0
|
2022-01-02 07:34:30 +00:00
|
|
|
for n := 0; n != 500; n++ {
|
|
|
|
zero := RandStr(55)
|
|
|
|
one := RandStr(55)
|
2022-07-13 12:09:07 +00:00
|
|
|
randStrChecks(zero, one, t, 55)
|
2022-01-02 07:34:30 +00:00
|
|
|
zeroSplit := strings.Split(zero, "")
|
|
|
|
oneSplit := strings.Split(one, "")
|
|
|
|
var similarity = 0
|
|
|
|
for i, char := range zeroSplit {
|
|
|
|
if oneSplit[i] != char {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
similarity++
|
2022-03-16 03:39:22 +00:00
|
|
|
// t.Logf("[-] zeroSplit[%d] is the same as oneSplit[%d] (%s)", i, i, char)
|
2022-01-02 07:34:30 +00:00
|
|
|
}
|
|
|
|
if similarity*4 > 55 {
|
2022-03-16 04:07:27 +00:00
|
|
|
t.Errorf("[ENTROPY FAILURE] more than a quarter of the string is the same!\n zero: %s \n one: %s \nTotal similar: %d",
|
|
|
|
zero, one, similarity)
|
2022-01-02 07:34:30 +00:00
|
|
|
}
|
2022-07-12 09:00:54 +00:00
|
|
|
// t.Logf("[ENTROPY] Similarity score (lower is better): %d", similarity)
|
2022-07-04 07:43:03 +00:00
|
|
|
totalScore += similarity
|
2022-01-02 07:34:30 +00:00
|
|
|
}
|
2022-07-12 09:00:54 +00:00
|
|
|
t.Logf("[ENTROPY] final score (lower is better): %d (RandStr)", totalScore)
|
2022-03-27 03:19:17 +00:00
|
|
|
}
|
2022-01-02 07:34:30 +00:00
|
|
|
|
2022-09-28 07:04:16 +00:00
|
|
|
func Test_RandomStrChoice(t *testing.T) {
|
2022-07-04 07:43:03 +00:00
|
|
|
if RandomStrChoice([]string{}) != "" {
|
|
|
|
t.Fatalf("RandomStrChoice returned a value when given an empty slice")
|
|
|
|
}
|
2022-09-28 07:04:16 +00:00
|
|
|
var slice []string
|
|
|
|
for n := 0; n != 500; n++ {
|
|
|
|
slice = append(slice, RandStr(555))
|
2022-03-27 03:19:17 +00:00
|
|
|
}
|
2022-09-28 07:04:16 +00:00
|
|
|
check(RandomStrChoice(slice), RandomStrChoice(slice), t)
|
2022-01-02 07:34:30 +00:00
|
|
|
}
|
2022-07-12 09:00:54 +00:00
|
|
|
|
|
|
|
func Test_RNGUint32(t *testing.T) {
|
|
|
|
// start globals fresh, just for coverage.
|
|
|
|
sharedRand = GetOptimizedRand()
|
|
|
|
getSharedRand = &sync.Once{}
|
|
|
|
RNGUint32()
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_RandStr5(b *testing.B) {
|
|
|
|
for n := 0; n != b.N; n++ {
|
|
|
|
RandStr(5)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_RandStr25(b *testing.B) {
|
|
|
|
for n := 0; n != b.N; n++ {
|
|
|
|
RandStr(25)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_RandStr55(b *testing.B) {
|
|
|
|
for n := 0; n != b.N; n++ {
|
|
|
|
RandStr(55)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_RandStr500(b *testing.B) {
|
|
|
|
for n := 0; n != b.N; n++ {
|
|
|
|
RandStr(500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_RandStr55555(b *testing.B) {
|
|
|
|
for n := 0; n != b.N; n++ {
|
|
|
|
RandStr(55555)
|
|
|
|
}
|
|
|
|
}
|
2022-07-13 12:09:07 +00:00
|
|
|
|
|
|
|
func Benchmark_RandStrWithUpper5(b *testing.B) {
|
|
|
|
for n := 0; n != b.N; n++ {
|
|
|
|
RandStrWithUpper(5)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_RandStrWithUpper25(b *testing.B) {
|
|
|
|
for n := 0; n != b.N; n++ {
|
|
|
|
RandStrWithUpper(25)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_RandStrWithUpper55(b *testing.B) {
|
|
|
|
for n := 0; n != b.N; n++ {
|
|
|
|
RandStrWithUpper(55)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_RandStrWithUpper500(b *testing.B) {
|
|
|
|
for n := 0; n != b.N; n++ {
|
|
|
|
RandStrWithUpper(500)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func Benchmark_RandStrWithUpper55555(b *testing.B) {
|
|
|
|
for n := 0; n != b.N; n++ {
|
|
|
|
RandStrWithUpper(55555)
|
|
|
|
}
|
|
|
|
}
|