File: cryptorandomstringutils_test.go

package info (click to toggle)
golang-github-masterminds-goutils 1.1.1-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, forky, sid, trixie
  • size: 148 kB
  • sloc: makefile: 2
file content (112 lines) | stat: -rw-r--r-- 3,664 bytes parent folder | download
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
package goutils

import (
	"regexp"
	"strconv"
	"testing"
	"unicode/utf8"
)

func TestCryptoRandomNonAlphaNumeric(t *testing.T) {
	// If asked for a string 0 characters long, CryptoRandomNonAlphaNumeric should provide an empty string.
	if x, _ := CryptoRandomNonAlphaNumeric(0); utf8.RuneCountInString(x) != 0 {
		t.Errorf("String should be 0 characters; string was %v characters", utf8.RuneCountInString(x))
	}

	// Test CryptoRandomNonAlphaNumeric's ability to generate strings 1 through 100 characters in length.
	for i := 1; i < 101; i++ {
		if x, _ := CryptoRandomNonAlphaNumeric(i); utf8.RuneCountInString(x) != i {
			t.Errorf("String should be %v characters; string was %v characters", i, utf8.RuneCountInString(x))
		}
	}
}

func TestCryptoRandomAscii(t *testing.T) {
	// If asked for a string 0 characters long, CryptoRandomAscii should provide an empty string.
	if x, _ := CryptoRandomAscii(0); len(x) != 0 {
		t.Errorf("String should be 0 characters; string was %v characters", len(x))
	}

	// Test CryptoRandomAscii's ability to generate strings 1 through 100 characters in length.
	for i := 1; i < 101; i++ {
		if x, _ := CryptoRandomAscii(i); len(x) != i {
			t.Errorf("String should be %v characters; string was %v characters", i, len(x))
		}
	}
}

func TestCryptoRandomNumeric(t *testing.T) {
	// If asked for a string 0 characters long, CryptoRandomNumeric should provide an empty string.
	if x, _ := CryptoRandomNumeric(0); len(x) != 0 {
		t.Errorf("String should be 0 characters; string was %v characters", len(x))
	}

	// Test CryptoRandomNumeric's ability to generate strings 1 through 100 characters in length.
	for i := 1; i < 101; i++ {
		if x, _ := CryptoRandomNumeric(i); len(x) != i {
			t.Errorf("String should be %v characters; string was %v characters", i, len(x))
		}
	}
}

func TestCryptoRandomAlphabetic(t *testing.T) {
	// If asked for a string 0 characters long, CryptoRandomAlphabetic should provide an empty string.
	if x, _ := CryptoRandomAlphabetic(0); len(x) != 0 {
		t.Errorf("String should be 0 characters; string was %v characters", len(x))
	}

	// Test CryptoRandomAlphabetic's ability to generate strings 1 through 100 characters in length.
	for i := 1; i < 101; i++ {
		if x, _ := CryptoRandomAlphabetic(i); len(x) != i {
			t.Errorf("String should be %v characters; string was %v characters", i, len(x))
		}
	}
}

func TestCryptoRandomAlphaNumeric(t *testing.T) {
	// If asked for a string 0 characters long, CryptoRandomAlphaNumeric should provide an empty string.
	if x, _ := CryptoRandomAlphaNumeric(0); len(x) != 0 {
		t.Errorf("String should be 0 characters; string was %v characters", len(x))
	}

	// Test CryptoRandomAlphaNumeric's ability to generate strings 1 through 100 characters in length.
	for i := 1; i < 101; i++ {
		if x, _ := CryptoRandomAlphaNumeric(i); len(x) != i {
			t.Errorf("String should be %v characters; string was %v characters", i, len(x))
		}
	}
}

func TestCryptoRandAlphaNumeric_FuzzOnlyNumeric(t *testing.T) {

	// Testing for a reported regression in which some versions produced
	// a predictably small set of chars.
	iters := 1000
	charlen := 0
	for i := 0; i < 16; i++ {
		numOnly := 0
		charlen++
		for i := 0; i < iters; i++ {
			out, err := CryptoRandomAlphaNumeric(charlen)
			if err != nil {
				t.Fatal("func failed to produce a random thinger")
			}
			if _, err := strconv.Atoi(out); err == nil {
				numOnly++
			}

			m, err := regexp.MatchString("^[0-9a-zA-Z]+$", out)
			if err != nil {
				t.Fatal(err)
			}
			if !m {
				t.Fatal("Character is not alphanum")
			}
		}

		if numOnly == iters {
			t.Fatalf("Got %d numeric-only random sequences", numOnly)
		}
	}

}