<commit_before>package fernet

import (
	"crypto/aes"
	"reflect"
	"testing"
	"time"
)

type test struct {
	secret string
	src    []byte
	iv     [aes.BlockSize]byte
	now    time.Time
	ttl    time.Duration
	token  []byte
	desc   string
}

var genTokens = []*test{
	{
		secret: "<KEY>
		src:    []byte("hello"),
		iv:     [...]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf},
		now:    time.Date(1985, time.October, 26, 1, 20, 0, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
	},
}

var genErrTokens = []*test{
	{
		secret: "AAAAAAAAAAAAAAAA<KEY>=",
		src:    []byte("hello"),
		now:    time.Date(1985, time.October, 26, 1, 20, 0, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "zero-value key",
	},
}

var verifyTokens = []*test{
	{
		secret: "<KEY>
		src:    []byte("hello"),
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "plain style",
	},
}

var verifyBadTokens = []*test{
	{
		secret: "<KEY>
		src:    []byte("hello"),
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "incorrect mac",
	},
	{
		secret: "<KEY>
		src:    []byte("hello"),
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>),
		desc:   "too short",
	},
	{
		secret: "<KEY>
		src:    []byte("hello"),
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "invalid base64",
	},
	{
		secret: "<KEY>
		src:    []byte("hello"),
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>),
		desc:   "payload size not multiple of block size",
	},
	{
		secret: "<KEY>
		src:    []byte("hello"),
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "payload padding error",
	},
	{
		secret: "<KEY>
		src:    []byte("hello"),
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "far-future TS",
	},
	{
		secret: "<KEY>
		src:    []byte("hello"),
		now:    time.Date(1985, time.October, 26, 1, 21, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "expired ttl",
	},
	{
		secret: "<KEY>
		src:    []byte("hello"),
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "incorrect IV (leads to padding error)",
	},
	{
		secret: "<KEY>
		src:    []byte("hello"),
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "zero-value key",
	},
}

func TestGenerate(t *testing.T) {
	for _, tok := range genTokens {
		k := MustDecodeKey(tok.secret)
		g, err := gen(tok.src, tok.iv[:], tok.now, k)
		if !reflect.DeepEqual(g, tok.token) {
			t.Errorf("%#v", string(g))
		}
		if err != nil {
			t.Errorf("err %v", err)
		}
	}
}

func TestGenerateErr(t *testing.T) {
	for _, tok := range genErrTokens {
		k := MustDecodeKey(tok.secret)
		g, err := gen(tok.src, tok.iv[:], tok.now, k)
		if err == nil || err == nil || g != nil {<commit_msg>remove unused test fixture fields<commit_after>package fernet

import (
	"crypto/aes"
	"reflect"
	"testing"
	"time"
)

type test struct {
	secret string
	src    []byte
	iv     [aes.BlockSize]byte
	now    time.Time
	ttl    time.Duration
	token  []byte
	desc   string
}

var genTokens = []*test{
	{
		secret: "<KEY>
		src:    []byte("hello"),
		iv:     [...]byte{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf},
		now:    time.Date(1985, time.October, 26, 1, 20, 0, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
	},
}

var genErrTokens = []*test{
	{
		secret: "<KEY>AAAAAAAAAAA=",
		src:    []byte("hello"),
		now:    time.Date(1985, time.October, 26, 1, 20, 0, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "zero-value key",
	},
}

var verifyTokens = []*test{
	{
		secret: "<KEY>
		src:    []byte("hello"),
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "plain style",
	},
}

var verifyBadTokens = []*test{
	{
		secret: "<KEY>
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "incorrect mac",
	},
	{
		secret: "<KEY>
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>),
		desc:   "too short",
	},
	{
		secret: "<KEY>
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "invalid base64",
	},
	{
		secret: "<KEY>
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>),
		desc:   "payload size not multiple of block size",
	},
	{
		secret: "<KEY>
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "payload padding error",
	},
	{
		secret: "<KEY>
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "far-future TS",
	},
	{
		secret: "cw_0x689RpI-jtRR7oE8h_eQsKImvJapLeSbXpwF4e4=",
		now:    time.Date(1985, time.October, 26, 1, 21, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "expired ttl",
	},
	{
		secret: "<KEY>
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "incorrect IV (leads to padding error)",
	},
	{
		secret: "<KEY>
		now:    time.Date(1985, time.October, 26, 1, 20, 1, 0, time.FixedZone("PDT", -7*3600)),
		ttl:    60 * time.Second,
		token:  []byte("<KEY>"),
		desc:   "zero-value key",
	},
}

func TestGenerate(t *testing.T) {
	for _, tok := range genTokens {
		k := MustDecodeKey(tok.secret)
		g, err := gen(tok.src, tok.iv[:], tok.now, k)
		if !reflect.DeepEqual(g, tok.token) {
			t.Errorf("%#v", string(g))
		}
		if err != nil {
			t.Errorf("err %v", err)
		}
	}
}

func TestGenerateErr(t *testing.T) {
	for _, tok := range genErrTokens {
		k := MustDecodeKey(tok.secret)
		g, err := gen(tok.src, tok.iv[:], tok.now, k)
		if err == nil || err == nil || g != nil {
{'fraction_non_alphanumeric': 0.22119037568594344, 'fraction_numerical': 0.0669762206275503, 'mean_word_length': 3.0593946316390634, 'pattern_counts': {'":': 0, '<': 50, '<?xml version=': 0, '>': 50, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3045432', 'n_tokens_mistral': 3551, 'n_tokens_neox': 3024, 'n_words': 730}