Improve test coverage and documentation
This commit is contained in:
parent
1c8fd87a7f
commit
94590bbc23
|
@ -9,37 +9,43 @@ import (
|
|||
"nullprogram.com/x/rng"
|
||||
)
|
||||
|
||||
// RandomStrChoice returns a random item from an input slice of strings.
|
||||
func RandomStrChoice(choice []string) string {
|
||||
strlen := len(choice)
|
||||
n := uint32(0)
|
||||
if strlen > 0 {
|
||||
n = uint32(RNG(16)) % uint32(strlen)
|
||||
if len(choice) > 0 {
|
||||
return choice[RNGUint32()%uint32(len(choice))]
|
||||
}
|
||||
return choice[n]
|
||||
return choice[0]
|
||||
}
|
||||
|
||||
// GetCryptoSeed returns a random int64 derived from crypto/rand.
|
||||
// This can be used as a seed for the math/rand package.
|
||||
func GetCryptoSeed() int64 {
|
||||
var seed int64
|
||||
_ = binary.Read(crip.Reader, binary.BigEndian, &seed)
|
||||
return seed
|
||||
}
|
||||
|
||||
// GetOptimizedRand returns a pointer to a new rand.Rand which uses crypto/rand to seed a splitmix64 rng.
|
||||
func GetOptimizedRand() *rand.Rand {
|
||||
r := new(rng.SplitMix64)
|
||||
r.Seed(GetCryptoSeed())
|
||||
return rand.New(r)
|
||||
}
|
||||
|
||||
// RNGUint32 returns a random uint32 using crypto/rand and splitmix64.
|
||||
func RNGUint32() uint32 {
|
||||
rng := GetOptimizedRand()
|
||||
return rng.Uint32()
|
||||
r := GetOptimizedRand()
|
||||
return r.Uint32()
|
||||
}
|
||||
|
||||
// RNG returns integer with a maximum amount of 'n' using crypto/rand and splitmix64.
|
||||
func RNG(n int) int {
|
||||
rng := GetOptimizedRand()
|
||||
return rng.Intn(n)
|
||||
r := GetOptimizedRand()
|
||||
return r.Intn(n)
|
||||
}
|
||||
|
||||
// OneInA generates a random number with a maximum of 'million' (input int).
|
||||
// If the resulting random number is equal to 1, then the result is true.
|
||||
func OneInA(million int) bool {
|
||||
return RNG(million) == 1
|
||||
}
|
||||
|
|
|
@ -75,5 +75,12 @@ func Test_RandStr_Entropy(t *testing.T) {
|
|||
one = ""
|
||||
}
|
||||
t.Logf("[ENTROPY] final score (lower is better): %d", totalScore)
|
||||
|
||||
}
|
||||
|
||||
func Test_RandomStrChoice(t *testing.T) {
|
||||
var slice []string
|
||||
for n := 0; n != 500; n++ {
|
||||
slice = append(slice, RandStr(555))
|
||||
}
|
||||
check(RandomStrChoice(slice), RandomStrChoice(slice), t)
|
||||
}
|
||||
|
|
|
@ -1 +1,51 @@
|
|||
package hash
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"os"
|
||||
"testing"
|
||||
|
||||
"git.tcp.direct/kayos/common/entropy"
|
||||
"git.tcp.direct/kayos/common/squish"
|
||||
)
|
||||
|
||||
const kayos = "Kr+6ONDx+cq/WvhHpQE/4LVuJYi9QHz1TztHNTWwa9KJWqHxfTNLKF3YxrcLptA3wO0KHm83Lq7gpBWgCQzPag=="
|
||||
|
||||
func TestBlake2bsum(t *testing.T) {
|
||||
og := squish.B64d(kayos)
|
||||
newc := Blake2bSum([]byte("kayos\n"))
|
||||
if !bytes.Equal(newc, og) {
|
||||
t.Fatalf("wanted: %v, got %v", kayos, squish.B64e(newc))
|
||||
}
|
||||
if !BlakeEqual([]byte("kayos\n"), []byte{107, 97, 121, 111, 115, 10}) {
|
||||
t.Fatalf("BlakeEqual should have been true. %s should == %s", []byte("kayos\n"), []byte{107, 97, 121, 111, 115, 92, 110})
|
||||
}
|
||||
}
|
||||
|
||||
func TestBlakeFileChecksum(t *testing.T) {
|
||||
path := t.TempDir() + "/blake2b.dat"
|
||||
err := os.WriteFile(path, []byte{107, 97, 121, 111, 115, 10}, os.ModePerm)
|
||||
if err != nil {
|
||||
t.Errorf("failed to write test fle for TestBlakeFileChecksum: %s", err.Error())
|
||||
}
|
||||
filecheck, err2 := BlakeFileChecksum(path)
|
||||
if err2 != nil {
|
||||
t.Errorf("failed to read test fle for TestBlakeFileChecksum: %s", err2.Error())
|
||||
}
|
||||
if len(filecheck) == 0 {
|
||||
t.Errorf("Got nil output from BlakeFileChecksum")
|
||||
}
|
||||
if !bytes.Equal(filecheck, squish.B64d(kayos)) {
|
||||
t.Fatalf("wanted: %v, got %v", kayos, squish.B64e(filecheck))
|
||||
}
|
||||
badfile, err3 := BlakeFileChecksum(t.TempDir() + "/" + entropy.RandStr(50))
|
||||
if err3 == nil {
|
||||
t.Errorf("shouldn't have been able to read phony file")
|
||||
}
|
||||
if len(badfile) != 0 {
|
||||
t.Errorf("Got non-nil output from bogus file: %v", badfile)
|
||||
}
|
||||
if !bytes.Equal(filecheck, squish.B64d(kayos)) {
|
||||
t.Fatalf("wanted: %v, got %v", kayos, squish.B64e(filecheck))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -3,6 +3,7 @@
|
|||
package linux
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"strings"
|
||||
"syscall"
|
||||
)
|
||||
|
@ -73,6 +74,11 @@ func GetUname(unameFlags string) (un string, err error) {
|
|||
targets = append(targets, &ub.Version)
|
||||
}
|
||||
}
|
||||
|
||||
if len(targets) < 2 {
|
||||
return "", errors.New("no valid uname targets in string")
|
||||
}
|
||||
|
||||
var uns []string
|
||||
for _, target := range targets {
|
||||
var sub []string
|
||||
|
|
|
@ -15,7 +15,7 @@ func TestGetUname(t *testing.T) {
|
|||
}
|
||||
|
||||
func TestGetUnameFailure(t *testing.T) {
|
||||
uname, err := GetUname("frickhole johnson")
|
||||
uname, err := GetUname("1!cch013 j0h/\\/50/\\/")
|
||||
if err == nil {
|
||||
t.Fatalf("[FAIL] We failed to fail. Wanted an error. %e", err)
|
||||
}
|
||||
|
|
|
@ -34,8 +34,6 @@ func IterateNetRange(ips interface{}) chan ipa.IP {
|
|||
for head := addrs.From(); head != addrs.To(); head = head.Next() {
|
||||
if !head.IsUnspecified() {
|
||||
ret <- head
|
||||
} else {
|
||||
close(ret)
|
||||
}
|
||||
}
|
||||
}(ch)
|
||||
|
|
|
@ -59,15 +59,25 @@ func TestIterateNetRange(t *testing.T) {
|
|||
want: test29,
|
||||
},
|
||||
{
|
||||
name: "string",
|
||||
name: "stringprefix",
|
||||
args: args{ips: test29str},
|
||||
want: test29,
|
||||
},
|
||||
{
|
||||
name: "stringrange",
|
||||
args: args{ips: test29rangestr},
|
||||
want: test29,
|
||||
},
|
||||
{
|
||||
name: "bogus",
|
||||
args: args{ips: "whatever, man. I'm just trynt'a vibe."},
|
||||
want: nil,
|
||||
},
|
||||
{
|
||||
name: "evenboguser",
|
||||
args: args{ips: int(5)},
|
||||
want: nil,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
index := 0
|
||||
|
|
Loading…
Reference in New Issue