aboutsummaryrefslogtreecommitdiff
path: root/internal/casino/hardening_casino_rng_test.go
blob: 2d444410e6320ba4ffc11d977f881cfc9626aad6 (plain)
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
package casino

import "testing"

// RNG seams: outcomes must not be replayable from a clock, while tests keep
// a deterministic, injectable seed.

func TestNewManagerSeedIsADeterministicSeam(t *testing.T) {
	cfg := MachineConfig{ID: "slots", Game: GameSlots}
	same := []*Machine{
		NewManager(7).EnsureMachine(1, cfg),
		NewManager(7).EnsureMachine(1, cfg),
	}
	other := NewManager(8).EnsureMachine(1, cfg)

	first := machineDraws(same[0])
	if got := machineDraws(same[1]); !sameDraws(first, got) {
		t.Fatalf("the same seed produced different slot streams: %v vs %v", first, got)
	}
	if got := machineDraws(other); sameDraws(first, got) {
		t.Fatalf("different seeds produced the same slot stream: %v", got)
	}
}

func TestNewManagerEntropySeedsFromCryptoRand(t *testing.T) {
	if seed := entropySeed(); seed < 0 {
		t.Fatalf("entropy seed %d is negative", seed)
	}
	seen := make(map[int64]bool)
	for i := 0; i < 4; i++ {
		m := NewManagerEntropy()
		if m.seed < 0 {
			t.Fatalf("NewManagerEntropy seed %d is negative", m.seed)
		}
		seen[m.seed] = true
	}
	if len(seen) < 2 {
		t.Fatalf("NewManagerEntropy reused one seed across %d managers", len(seen))
	}
}

// Each game gets its own stream seeded from the manager's: no two tables or
// machines may share reel results.
func TestManagerGamesKeepIndependentStreams(t *testing.T) {
	m := NewManager(3)
	first := m.EnsureMachine(1, MachineConfig{ID: "slots_a", Game: GameSlots})
	second := m.EnsureMachine(1, MachineConfig{ID: "slots_b", Game: GameSlots})
	if first == nil || second == nil {
		t.Fatal("machines were not created")
	}
	if sameDraws(machineDraws(first), machineDraws(second)) {
		t.Fatal("two machines share one RNG stream")
	}
}

// machineDraws consumes n values from a game's own stream.
func machineDraws(m *Machine) []int64 {
	draws := make([]int64, 8)
	for i := range draws {
		draws[i] = m.rng.Int63()
	}
	return draws
}

func sameDraws(a, b []int64) bool {
	if len(a) != len(b) {
		return false
	}
	for i := range a {
		if a[i] != b[i] {
			return false
		}
	}
	return true
}