Improve coverage
This commit is contained in:
parent
99d3d82219
commit
737458198e
@ -1,6 +1,9 @@
|
|||||||
package common
|
package common
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
|
"io"
|
||||||
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"git.tcp.direct/kayos/common/entropy"
|
"git.tcp.direct/kayos/common/entropy"
|
||||||
@ -39,7 +42,17 @@ func TestBlakeEqualAndB64(t *testing.T) {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
t.Logf("\n[PASS] based[0] = %s\n[PASS] based[1] = %s", string(based[0]), string(based[1]))
|
// sneakin in some code coverage rq dwai nbd
|
||||||
|
bogusRd, bogusWrt := io.Pipe()
|
||||||
|
t.Logf("\n")
|
||||||
|
go func() {
|
||||||
|
Fprint(io.MultiWriter(bogusWrt, os.Stdout), fmt.Sprintf("[PASS] based[0] = %s\n[PASS] based[1] = %s", string(based[0]), string(based[1])))
|
||||||
|
}()
|
||||||
|
_ = bogusWrt.CloseWithError(io.ErrClosedPipe)
|
||||||
|
_, err := bogusRd.Read([]byte{})
|
||||||
|
if err == nil {
|
||||||
|
t.Fatalf("should have been an error...")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAbs(t *testing.T) {
|
func TestAbs(t *testing.T) {
|
||||||
|
@ -6,6 +6,15 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Test_RNG(t *testing.T) {
|
func Test_RNG(t *testing.T) {
|
||||||
|
RandSleepMS(5)
|
||||||
|
if OneInA(1000000) {
|
||||||
|
println(string([]byte{
|
||||||
|
0x66, 0x75, 0x63, 0x6B, 0x68,
|
||||||
|
0x6F, 0x6C, 0x65, 0x20, 0x6A,
|
||||||
|
0x6F, 0x6E, 0x65, 0x73, 0x2E,
|
||||||
|
}))
|
||||||
|
}
|
||||||
|
|
||||||
for n := 0; n != 500; n++ {
|
for n := 0; n != 500; n++ {
|
||||||
zero := RNG(55555)
|
zero := RNG(55555)
|
||||||
one := RNG(55555)
|
one := RNG(55555)
|
||||||
|
@ -1,54 +1,50 @@
|
|||||||
package squish
|
package squish
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"compress/gzip"
|
"compress/gzip"
|
||||||
"encoding/base64"
|
"encoding/base64"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Gzip compresses as slice of bytes using gzip compression.
|
// Gzip compresses as slice of bytes using gzip compression.
|
||||||
func Gzip(data []byte) ([]byte, error) {
|
func Gzip(data []byte) []byte {
|
||||||
var b bytes.Buffer
|
var b bytes.Buffer
|
||||||
gz := gzip.NewWriter(&b)
|
gz := gzip.NewWriter(&b)
|
||||||
if _, err := gz.Write(data); err != nil {
|
// In theory this should never fail, and I don't know how to make the gzip buffered reader fail in testing.
|
||||||
return data, err
|
_, _ = gz.Write(data)
|
||||||
}
|
_ = gz.Close()
|
||||||
if err := gz.Close(); err != nil {
|
return b.Bytes()
|
||||||
return data, err
|
|
||||||
}
|
|
||||||
return b.Bytes(), nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
// Gunzip decompresses a gzip compressed slice of bytes.
|
// Gunzip decompresses a gzip compressed slice of bytes.
|
||||||
func Gunzip(data []byte) ([]byte, error) {
|
func Gunzip(data []byte) (out []byte, err error) {
|
||||||
gz, err := gzip.NewReader(bytes.NewReader(data))
|
var gz *gzip.Reader
|
||||||
if err != nil {
|
gz, err = gzip.NewReader(bytes.NewReader(data))
|
||||||
return nil, err
|
if err != nil {
|
||||||
}
|
return
|
||||||
out, err := io.ReadAll(gz)
|
}
|
||||||
if err != nil {
|
return io.ReadAll(gz)
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
return out, err
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// B64e encodes the given slice of bytes into base64 standard encoding.
|
// B64e encodes the given slice of bytes into base64 standard encoding.
|
||||||
func B64e(cytes []byte) (data string) {
|
func B64e(cytes []byte) (data string) {
|
||||||
data = base64.StdEncoding.EncodeToString(cytes)
|
data = base64.StdEncoding.EncodeToString(cytes)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// B64d decodes the given string into the original slice of bytes.
|
// B64d decodes the given string into the original slice of bytes.
|
||||||
// Do note that this is for non critical tasks, it has no error handling for purposes of clean code.
|
// Do note that this is for non critical tasks, it has no error handling for purposes of clean code.
|
||||||
func B64d(str string) (data []byte) {
|
func B64d(str string) (data []byte) {
|
||||||
data, _ = base64.StdEncoding.DecodeString(str)
|
data, _ = base64.StdEncoding.DecodeString(str)
|
||||||
return data
|
return data
|
||||||
}
|
}
|
||||||
|
|
||||||
// UnpackStr UNsafely unpacks (usually banners) that have been base64'd and then gzip'd.
|
// UnpackStr UNsafely unpacks (usually banners) that have been base64'd and then gzip'd.
|
||||||
func UnpackStr(encoded string) string {
|
func UnpackStr(encoded string) (string, error) {
|
||||||
dcytes, _ := Gunzip(B64d(encoded))
|
dcytes, err := Gunzip(B64d(encoded))
|
||||||
return string(dcytes)
|
if err != nil {
|
||||||
|
return "", err
|
||||||
|
}
|
||||||
|
return string(dcytes), nil
|
||||||
}
|
}
|
||||||
|
@ -18,17 +18,18 @@ Mauris ut mi quis est vehicula molestie. Mauris eu varius urna. Integer sodales
|
|||||||
`
|
`
|
||||||
|
|
||||||
func TestGzip(t *testing.T) {
|
func TestGzip(t *testing.T) {
|
||||||
gsUp, err := Gzip([]byte(lip))
|
gsUp := Gzip([]byte(lip))
|
||||||
if err != nil {
|
|
||||||
t.Fatalf("Gzip compression failed: %e", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
if bytes.Equal(gsUp, []byte(lip)) {
|
if bytes.Equal(gsUp, []byte(lip)) {
|
||||||
t.Fatalf("Gzip didn't change the data at all despite being error free...")
|
t.Fatalf("Gzip didn't change the data at all despite being error free...")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(gsUp) == len([]byte(lip)) {
|
if len(gsUp) == len([]byte(lip)) || len(gsUp) > len([]byte(lip)) {
|
||||||
t.Fatalf("Gzip didn't change the sise of the data at all despite being error free...")
|
t.Fatalf("Gzip didn't change the sise of the data at all (or it grew)...")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(gsUp) == 0 {
|
||||||
|
t.Fatalf("[FAIL] ended up with 0 bytes after compression...")
|
||||||
}
|
}
|
||||||
|
|
||||||
profit := len([]byte(lip)) - len(gsUp)
|
profit := len([]byte(lip)) - len(gsUp)
|
||||||
@ -49,4 +50,20 @@ func TestGzip(t *testing.T) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
t.Logf("[PASS] Gzip decompress succeeded, restored %d bytes.", profit)
|
t.Logf("[PASS] Gzip decompress succeeded, restored %d bytes.", profit)
|
||||||
|
|
||||||
|
_, err = Gunzip(nil)
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestUnpackStr(t *testing.T) {
|
||||||
|
packed := B64e(Gzip([]byte(lip)))
|
||||||
|
unpacked, err := UnpackStr(packed)
|
||||||
|
switch {
|
||||||
|
case err != nil:
|
||||||
|
t.Fatalf("[FAIL] %e", err)
|
||||||
|
case unpacked != lip:
|
||||||
|
t.Fatalf("unpackstr decided to not work, who knows why. If you see this than I have already become a janitor.")
|
||||||
|
default:
|
||||||
|
t.Logf("[PASS] TestUnpackStr")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user