This repository has been archived by the owner on Oct 23, 2024. It is now read-only.
forked from arrikto/oidc-authservice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
util_test.go
86 lines (74 loc) · 2.39 KB
/
util_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package main
import (
"fmt"
"testing"
"gonum.org/v1/gonum/stat"
"gonum.org/v1/gonum/stat/distuv"
)
func TestCreateNonce_Simple(t *testing.T) {
tests := []struct {
name string
length int
}{
{name: "0", length: 0},
{name: "32", length: 32},
{name: "63", length: 63},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
nonce, err := createNonce(test.length)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
t.Logf("Length: %d, Nonce: %s", test.length, nonce)
})
}
}
// TestCreateNonce_Distribution performs a statistical fitness test. Essentially,
// it tests that the nonce characters follow a uniform distribution.
// See: https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test
func TestCreateNonce_Distribution(t *testing.T) {
nonce, err := createNonce(100000000)
if err != nil {
t.Fatalf("Unexpected error: %v", err)
}
distribution := map[rune]int{}
for _, nonceChar := range nonce {
distribution[nonceChar]++
}
for k, v := range distribution {
fmt.Printf("%v: %v\n", k, v)
}
// 1. Calculate the χ² statistic, a normalized sum of squared deviations
// between observed and expected frequencies.
var observed, expected []float64
for _, v := range distribution {
observed = append(observed, float64(v))
}
mean := stat.Mean(observed, nil)
for range observed {
expected = append(expected, mean)
}
chiSquare := stat.ChiSquare(observed, expected)
// 2. Determine the degrees of freedom.
// df = Cats − Parms, where Cats is the number of observation categories
// recognized by the model, and Parms is the number of parameters in the
// model adjusted to make the model best fit the observations.
// In this case, Cats=len(observed) and Parms=1
// See: https://en.wikipedia.org/wiki/Pearson%27s_chi-squared_test#Discrete_uniform_distribution
df := len(observed) - 1
// 3. Select a level of confidence, p.
p := 0.95
// 4. Compare χ² to the critical value from the chi-squared distribution
// with df degrees of freedom and the selected confidence level.
chiSquaredDist := distuv.ChiSquared{
K: float64(df),
}
// 5. Test null hypothesis, that observed follows the uniform distribution.
threshold := chiSquaredDist.Quantile(p)
t.Logf("Test: %v, Threshold: %v", chiSquare, threshold)
nullHypothesis := chiSquare < threshold
if !nullHypothesis {
t.Fatalf("Nonce characters don't seem to follow a uniform distribution")
}
}