Skip to content

Commit

Permalink
convert some table tests to f-tests for clarity/readability
Browse files Browse the repository at this point in the history
  • Loading branch information
dropwhile committed Jul 1, 2024
1 parent b6b7bbe commit 6d9261c
Show file tree
Hide file tree
Showing 4 changed files with 235 additions and 224 deletions.
170 changes: 76 additions & 94 deletions pkg/camo/encoding/url_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,78 +12,113 @@ import (
is "gotest.tools/v3/assert/cmp"
)

type enctesto struct {
encoder EncoderFunc
hmac string
edig string
eURL string
sURL string
}
func TestEncoder(t *testing.T) {
t.Parallel()

f := func(encoder EncoderFunc, hmac, edig, eURL, sURL string) {
t.Helper()
hmacKey := []byte(hmac)
// test specific encoder
encodedURL := encoder(hmacKey, sURL)
assert.Check(t, is.Equal(encodedURL, fmt.Sprintf("/%s/%s", edig, eURL)), "encoded url does not match")
}

var enctests = []enctesto{
// hex
{
f(
HexEncodeURL, "test", "0f6def1cb147b0e84f39cbddc5ea10c80253a6f3",
"687474703a2f2f676f6c616e672e6f72672f646f632f676f706865722f66726f6e74706167652e706e67",
"http://golang.org/doc/gopher/frontpage.png",
},
)

// base64
{
f(
B64EncodeURL, "test", "D23vHLFHsOhPOcvdxeoQyAJTpvM",
"aHR0cDovL2dvbGFuZy5vcmcvZG9jL2dvcGhlci9mcm9udHBhZ2UucG5n",
"http://golang.org/doc/gopher/frontpage.png",
},
)
}

func TestEncoder(t *testing.T) {
func TestDecoder(t *testing.T) {
t.Parallel()
for _, p := range enctests {
hmacKey := []byte(p.hmac)
// test specific encoder
encodedURL := p.encoder(hmacKey, p.sURL)
assert.Check(t, is.Equal(encodedURL, fmt.Sprintf("/%s/%s", p.edig, p.eURL)), "encoded url does not match")
}
}

type dectesto struct {
decoder DecoderFunc
hmac string
edig string
eURL string
sURL string
}
f := func(decoder DecoderFunc, hmac, edig, eURL, sURL string) {
t.Helper()
hmacKey := []byte(hmac)
// test specific decoder
encodedURL, err := decoder(hmacKey, edig, eURL)
assert.Check(t, err, "decoded url failed to verify")
assert.Check(t, is.Equal(encodedURL, sURL), "decoded url does not match")

// also test generic "guessing" decoder
encodedURL, ok := DecodeURL(hmacKey, edig, eURL)
assert.Check(t, ok, "decoded url failed to verify")
assert.Check(t, is.Equal(encodedURL, sURL), "decoded url does not match")
}

var dectests = []dectesto{
// hex
{
f(
HexDecodeURL, "test", "0f6def1cb147b0e84f39cbddc5ea10c80253a6f3",
"687474703a2f2f676f6c616e672e6f72672f646f632f676f706865722f66726f6e74706167652e706e67",
"http://golang.org/doc/gopher/frontpage.png",
},
)

// base64
{
f(
B64DecodeURL, "test", "D23vHLFHsOhPOcvdxeoQyAJTpvM",
"aHR0cDovL2dvbGFuZy5vcmcvZG9jL2dvcGhlci9mcm9udHBhZ2UucG5n",
"http://golang.org/doc/gopher/frontpage.png",
},
)
}

func TestDecoder(t *testing.T) {
func TestBadDecodes(t *testing.T) {
t.Parallel()
for _, p := range dectests {
hmacKey := []byte(p.hmac)

f := func(decoder DecoderFunc, hmac, edig, eURL string) {
t.Helper()
hmacKey := []byte(hmac)
// test specific decoder
encodedURL, err := p.decoder(hmacKey, p.edig, p.eURL)
assert.Check(t, err, "decoded url failed to verify")
assert.Check(t, is.Equal(encodedURL, p.sURL), "decoded url does not match")
encodedURL, err := decoder(hmacKey, edig, eURL)
assert.Check(t, err != nil, "decoded url verfied when it shouldn't have")
assert.Check(t, is.Equal(encodedURL, ""), "decoded url result not empty")

// also test generic "guessing" decoder
encodedURL, ok := DecodeURL(hmacKey, p.edig, p.eURL)
assert.Check(t, ok, "decoded url failed to verify")
assert.Check(t, is.Equal(encodedURL, p.sURL), "decoded url does not match")
encodedURL, ok := DecodeURL(hmacKey, edig, eURL)
assert.Check(t, !ok, "decoded url verfied when it shouldn't have")
assert.Check(t, is.Equal(encodedURL, ""), "decoded url result not empty")
}

// hex
f(
HexDecodeURL, "test", "000",
"687474703a2f2f676f6c616e672e6f72672f646f632f676f706865722f66726f6e74706167652e706e67",
)
f(
HexDecodeURL, "test", "0f6def1cb147b0e84f39cbddc5ea10c80253a6f3",
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
)

// base64
f(
B64DecodeURL, "test", "000",
"aHR0cDovL2dvbGFuZy5vcmcvZG9jL2dvcGhlci9mcm9udHBhZ2UucG5n",
)
f(
B64DecodeURL, "test", "D23vHLFHsOhPOcvdxeoQyAJTpvM",
"00000000000000000000000000000000000000000000000000000000",
)

// mixmatch
// hex
f(
HexDecodeURL, "test", "0f6def1cb147b0e84f39cbddc5ea10c80253a6f3",
"aHR0cDovL2dvbGFuZy5vcmcvZG9jL2dvcGhlci9mcm9udHBhZ2UucG5n",
)

// base64
f(
B64DecodeURL, "test", "D23vHLFHsOhPOcvdxeoQyAJTpvM",
"687474703a2f2f676f6c616e672e6f72672f646f632f676f706865722f66726f6e74706167652e706e67",
)
}

func BenchmarkHexEncoder(b *testing.B) {
Expand Down Expand Up @@ -121,56 +156,3 @@ func BenchmarkGuessingDecoderB64(b *testing.B) {
DecodeURL([]byte("test"), "D23vHLFHsOhPOcvdxeoQyAJTpvM", "aHR0cDovL2dvbGFuZy5vcmcvZG9jL2dvcGhlci9mcm9udHBhZ2UucG5n")
}
}

var baddectests = []dectesto{
// hex
{
HexDecodeURL, "test", "000",
"687474703a2f2f676f6c616e672e6f72672f646f632f676f706865722f66726f6e74706167652e706e67", "",
},
{
HexDecodeURL, "test", "0f6def1cb147b0e84f39cbddc5ea10c80253a6f3",
"000000000000000000000000000000000000000000000000000000000000000000000000000000000000", "",
},

// base64
{
B64DecodeURL, "test", "000",
"aHR0cDovL2dvbGFuZy5vcmcvZG9jL2dvcGhlci9mcm9udHBhZ2UucG5n", "",
},
{
B64DecodeURL, "test", "D23vHLFHsOhPOcvdxeoQyAJTpvM",
"00000000000000000000000000000000000000000000000000000000", "",
},

// mixmatch
// hex
{
HexDecodeURL, "test", "0f6def1cb147b0e84f39cbddc5ea10c80253a6f3",
"aHR0cDovL2dvbGFuZy5vcmcvZG9jL2dvcGhlci9mcm9udHBhZ2UucG5n",
"http://golang.org/doc/gopher/frontpage.png",
},

// base64
{
B64DecodeURL, "test", "D23vHLFHsOhPOcvdxeoQyAJTpvM",
"687474703a2f2f676f6c616e672e6f72672f646f632f676f706865722f66726f6e74706167652e706e67",
"http://golang.org/doc/gopher/frontpage.png",
},
}

func TestBadDecodes(t *testing.T) {
t.Parallel()
for _, p := range baddectests {
hmacKey := []byte(p.hmac)
// test specific decoder
encodedURL, err := p.decoder(hmacKey, p.edig, p.eURL)
assert.Check(t, err != nil, "decoded url verfied when it shouldn't have")
assert.Check(t, is.Equal(encodedURL, ""), "decoded url result not empty")

// also test generic "guessing" decoder
encodedURL, ok := DecodeURL(hmacKey, p.edig, p.eURL)
assert.Check(t, !ok, "decoded url verfied when it shouldn't have")
assert.Check(t, is.Equal(encodedURL, ""), "decoded url result not empty")
}
}
33 changes: 33 additions & 0 deletions pkg/camo/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@
package camo

import (
"encoding/json"
"fmt"
"io"
"net/http"
"net/http/httptest"
"reflect"
"testing"

"github.com/cactus/go-camo/v2/pkg/camo/encoding"
Expand Down Expand Up @@ -100,3 +102,34 @@ func statusCodeAssert(t *testing.T, expected int, resp *http.Response) {
expected, resp.StatusCode,
)
}

type Tuple[A any, B any] struct {
a A
b B
}

func (r *Tuple[A, B]) UnmarshalJSON(p []byte) error {
var tmp []json.RawMessage
if err := json.Unmarshal(p, &tmp); err != nil {
return err
}
if err := json.Unmarshal(tmp[0], &r.a); err != nil {
return err
}

if reflect.TypeOf(&r.b) == reflect.TypeFor[*B]() {
var s string
if err := json.Unmarshal(tmp[1], &s); err != nil {
return err
}
if s != "" {
err := fmt.Errorf(s)
r.b = err.(B)
}
} else {
if err := json.Unmarshal(tmp[1], &r.b); err != nil {
return err
}
}
return nil
}
Loading

0 comments on commit 6d9261c

Please sign in to comment.