diff --git a/common_test.go b/common_test.go new file mode 100644 index 0000000..aa121aa --- /dev/null +++ b/common_test.go @@ -0,0 +1,62 @@ +package common + +import ( + "testing" + + "git.tcp.direct/kayos/common/entropy" + "git.tcp.direct/kayos/common/hash" + "git.tcp.direct/kayos/common/squish" +) + +var needle = []byte(entropy.RandStr(16)) + +func TestBlakeEqualAndB64(t *testing.T) { + var clone = make([]byte, len(needle)) + for i, c := range needle { + clone[i] = c + } + if !hash.BlakeEqual(needle, clone) { + t.Fatalf("BlakeEqual failed! Values %v and %v should have been equal.\n|---->Lengths: %d and %d", + needle, clone, len(needle), len(clone), + ) + } + clone = make([]byte, len(needle)) + clone = []byte(entropy.RandStr(16)) + if hash.BlakeEqual(needle, clone) { + t.Fatalf("BlakeEqual failed! Values %v and %v should NOT have been equal.\n|---->Lengths: %d and %d", + needle, clone, len(needle), len(clone), + ) + } + + var based = [2][]byte{needle, clone} + + based[0] = []byte(squish.B64e(based[0])) + based[1] = []byte(squish.B64e(based[0])) + + if hash.BlakeEqual(based[0], based[1]) { + t.Fatalf("Base64 encoding failed! Values %v and %v should NOT have been equal.\n|---->Lengths: %d and %d", + based[0], based[1], len(based[0]), len(based[1]), + ) + } + + t.Logf("\n[PASS] based[0] = %s\n[PASS] based[1] = %s", string(based[0]), string(based[1])) +} + +func TestAbs(t *testing.T) { + var start = int32(entropy.RNG(5)) + for start < 1 { + t.Logf("Re-rolling for a non-zero value... %d", start) + start = int32(entropy.RNG(5)) + } + + less := start * 2 + negged := start - less + + if negged == start { + t.Fatalf("the sky is falling. up is down: %d should not equal %d.", start, negged) + } + + if Abs(int(negged)) != int(start) { + t.Fatalf("Abs failed! values %d and %d should have been equal.", start, negged) + } +} diff --git a/entropy/entropy.go b/entropy/entropy.go index 392f54f..482cf26 100644 --- a/entropy/entropy.go +++ b/entropy/entropy.go @@ -11,19 +11,35 @@ func RandomStrChoice(choice []string) string { strlen := len(choice) n := uint32(0) if strlen > 0 { - n = getRandomUint32() % uint32(strlen) + n = uint32(RNG(16)) % uint32(strlen) } return choice[n] } -func getRandomUint32() uint32 { - x := time.Now().UnixNano() - return uint32((x >> 32) ^ x) -} - func RNG(n int) int { var seed int64 binary.Read(crip.Reader, binary.BigEndian, &seed) rng := rand.New(rand.NewSource(seed)) return rng.Intn(n) } + +func OneInA(million int) bool { + return RNG(million) == 1 +} + +// RandSleepMS sleeps for a random period of time with a maximum of n milliseconds. +func RandSleepMS(n int) { + time.Sleep(time.Duration(RNG(n)) * time.Millisecond) +} + +// characters used for the gerneration of random strings. +const charset = "abcdefghijklmnopqrstuvwxyz1234567890" + +// RandStr generates a random alphanumeric string with a max length of size. Charset used is all lowercase. +func RandStr(size int) string { + buf := make([]byte, size) + for i := 0; i != size; i++ { + buf[i] = charset[uint32(RNG(32))%uint32(len(charset))] + } + return string(buf) +} diff --git a/entropy_test.go b/entropy/entropy_test.go similarity index 95% rename from entropy_test.go rename to entropy/entropy_test.go index 5222fbe..3004182 100644 --- a/entropy_test.go +++ b/entropy/entropy_test.go @@ -1,4 +1,4 @@ -package common +package entropy import ( "strings" @@ -11,7 +11,7 @@ func Test_RNG(t *testing.T) { one := RNG(55555) t.Logf("Random0: %d Random1: %d", zero, one) if zero == one { - t.Errorf("RNG hit a duplicate! %s == %s", zero, one) + t.Errorf("RNG hit a duplicate! %d == %d", zero, one) } zero = 0 one = 0 diff --git a/entropy/go.mod b/entropy/go.mod deleted file mode 100644 index 6f81440..0000000 --- a/entropy/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module git.tcp.direct/kayos/common/entropy - -go 1.17 diff --git a/hash/go.mod b/hash/go.mod deleted file mode 100644 index f6eaa72..0000000 --- a/hash/go.mod +++ /dev/null @@ -1,7 +0,0 @@ -module git.tcp.direct/kayos/common/hash - -go 1.17 - -require golang.org/x/crypto v0.0.0-20220209195652-db638375bc3a - -require golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 // indirect diff --git a/hash/go.sum b/hash/go.sum deleted file mode 100644 index 9d43f5c..0000000 --- a/hash/go.sum +++ /dev/null @@ -1,10 +0,0 @@ -golang.org/x/crypto v0.0.0-20220209195652-db638375bc3a h1:atOEWVSedO4ksXBe/UrlbSLVxQQ9RxM/tT2Jy10IaHo= -golang.org/x/crypto v0.0.0-20220209195652-db638375bc3a/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4= -golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y= -golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1 h1:SrN+KX8Art/Sf4HNj6Zcz06G7VEz+7w9tdXTPOZ7+l4= -golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= -golang.org/x/term v0.0.0-20201126162022-7de9c90e9dd1/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo= -golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ= -golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= diff --git a/hash/hash_test.go b/hash/hash_test.go new file mode 100644 index 0000000..7adc22f --- /dev/null +++ b/hash/hash_test.go @@ -0,0 +1 @@ +package hash diff --git a/linux/uname_test.go b/linux/uname_test.go index 6f2ee12..685f6ed 100644 --- a/linux/uname_test.go +++ b/linux/uname_test.go @@ -13,3 +13,13 @@ func TestGetUname(t *testing.T) { t.Logf("%s", uname) } } + +func TestGetUnameFailure(t *testing.T) { + uname, err := GetUname("frickhole johnson") + if err == nil { + t.Fatalf("[FAIL] We failed to fail. Wanted an error. %e", err) + } + if len(uname) > 1 { + t.Fatalf("[FAIL] Despite erroring out we still received a value: %v", uname) + } +} diff --git a/range.go b/network/range.go similarity index 85% rename from range.go rename to network/range.go index 9285838..9f451fd 100644 --- a/range.go +++ b/network/range.go @@ -1,8 +1,8 @@ -package common +package network import ipa "inet.af/netaddr" -func RangeIterate(ips interface{}) chan *ipa.IP { +func IterateNetRange(ips interface{}) chan *ipa.IP { var addrs ipa.IPRange switch ips.(type) { diff --git a/squish/go.mod b/squish/go.mod deleted file mode 100644 index 20824ff..0000000 --- a/squish/go.mod +++ /dev/null @@ -1,3 +0,0 @@ -module git.tcp.direct/kayos/common/squish - -go 1.17 diff --git a/squish/squish_test.go b/squish/squish_test.go new file mode 100644 index 0000000..37abe1b --- /dev/null +++ b/squish/squish_test.go @@ -0,0 +1,52 @@ +package squish + +import ( + "bytes" + "testing" +) + +const lip string = ` +Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed a ante sit amet purus blandit auctor. Nullam ornare enim sed nibh consequat molestie. Duis est lectus, vestibulum vel felis vel, convallis cursus ex. Morbi nec placerat orci. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Praesent a erat sit amet libero convallis ornare a venenatis dolor. Pellentesque euismod risus et metus porttitor, vel consectetur lacus tempus. Integer elit arcu, condimentum quis nisi eget, dapibus imperdiet nulla. Cras sit amet ante in urna varius tempus. Integer tristique sagittis nunc vel tincidunt. + +Integer non suscipit ligula, et fermentum sem. Duis id odio lorem. Sed id placerat urna, eu vehicula risus. Duis porttitor hendrerit risus. Curabitur id tellus ac arcu aliquet finibus. Pellentesque et nisl ante. Mauris sapien nisl, pretium in ligula tempus, posuere mattis turpis. + +Proin et tempus enim. Nullam at diam est. Vivamus ut lectus hendrerit, interdum ex id, ultricies sapien. Praesent rhoncus turpis dolor, quis lobortis tortor pellentesque id. Pellentesque eget nisi laoreet, fringilla augue eu, cursus risus. Integer consectetur ornare laoreet. Praesent ligula sem, tincidunt at ligula at, condimentum venenatis tortor. + +Nam laoreet enim leo, sed finibus lorem egestas vel. Maecenas varius a leo non placerat. Donec scelerisque, risus vel finibus ornare, arcu ligula interdum justo, in ultricies urna mi et neque. Curabitur sed sem dui. Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Maecenas eget laoreet nisi. Nam rhoncus sapien ac interdum sagittis. Nulla fermentum sem nec tellus dignissim lacinia. Curabitur ornare lectus non dictum laoreet. Praesent tempor risus at tortor tempor finibus. Cras id dolor mi. + +Mauris ut mi quis est vehicula molestie. Mauris eu varius urna. Integer sodales nunc at risus rutrum eleifend. In sed bibendum lectus. Morbi ipsum sapien, blandit in dignissim eu, ultrices non odio. Vestibulum ante ipsum primis in faucibus orci luctus et ultrices posuere cubilia curae; Nulla eget volutpat ligula, at elementum dui. Aliquam sed enim scelerisque, facilisis magna vitae, dignissim enim. Pellentesque non ultricies urna. Proin fermentum erat semper efficitur auctor. Vestibulum posuere non tortor vitae tincidunt. +` + +func TestGzip(t *testing.T) { + gsUp, err := Gzip([]byte(lip)) + if err != nil { + t.Fatalf("Gzip compression failed: %e", err) + } + + if bytes.Equal(gsUp, []byte(lip)) { + t.Fatalf("Gzip didn't change the data at all despite being error free...") + } + + if len(gsUp) == len([]byte(lip)) { + t.Fatalf("Gzip didn't change the sise of the data at all despite being error free...") + } + + profit := len([]byte(lip)) - len(gsUp) + t.Logf("[PASS] Gzip compress succeeded, squished %d bytes.", profit) + + hosDown, err := Gunzip(gsUp) + + if err != nil { + t.Fatalf("Gzip decompression failed: %e", err) + } + + if !bytes.Equal(hosDown, []byte(lip)) { + t.Fatalf("Gzip decompression failed, data does not appear to be the same after decompression") + } + + if len(hosDown) != len([]byte(lip)) { + t.Fatalf("Gzip decompression failed, data [%d] does not appear to be the same [%d] length after decompression", hosDown, len([]byte(lip))) + } + + t.Logf("[PASS] Gzip decompress succeeded, restored %d bytes.", profit) +} diff --git a/util.go b/util.go index 0b57001..b8f2216 100644 --- a/util.go +++ b/util.go @@ -1,12 +1,8 @@ package common import ( - crip "crypto/rand" - "encoding/binary" "fmt" "io" - "math/rand" - "time" "github.com/rs/zerolog/log" ) @@ -21,31 +17,6 @@ func Fprint(w io.Writer, s string) { } } -// RNG is a Random Number Generator -func RNG(n int) int { - var seed int64 - err := binary.Read(crip.Reader, binary.BigEndian, &seed) - if err != nil { - panic(err) - } - rng := rand.New(rand.NewSource(seed)) - return rng.Intn(n) -} - -// RandStr generates a random alphanumeric string with a max length of size. -func RandStr(size int) string { - buf := make([]byte, size) - for i := 0; i != size; i++ { - buf[i] = charset[uint32(RNG(32))%uint32(len(charset))] - } - return string(buf) -} - -// RandSleepMS sleeps for a random period of time with a maximum of n milliseconds. -func RandSleepMS(n int) { - time.Sleep(time.Duration(RNG(n)) * time.Millisecond) -} - // Abs will give you the positive version of a negative integer, quickly. func Abs(n int) int { // ayyee smash 6ros @@ -54,7 +25,3 @@ func Abs(n int) int { n64 = (n64 ^ y) - y return int(n64) } - -func OneInA(million int) bool { - return RNG(million) == 1 -}