package secretcrypt

import (
	"bytes"
	"testing"
)

func passthrough(plaintext string) {
	crypted, err := Encrypt("testphrase", []byte(plaintext))
	if err != nil {
		panic(err)
	}

	plain, err := Decrypt("testphrase", crypted)
	if err != nil {
		panic(err)
	}

	if !bytes.Equal(plain, []byte(plain)) {
		panic("expected correct plaintext")
	}
}

func TestEncryptDecryptDoesNotCorrupt(t *testing.T) {
	passthrough("test")
	passthrough("")
	passthrough("t")
}

package secretcrypt

import (
	"bytes"
	"testing"
	"math/rand"
)

func passthrough(passphrase string, plaintext []byte) {
	crypted, err := Encrypt(passphrase, plaintext)
	if err != nil {
		panic(err)
	}

	plainResult, err := Decrypt(passphrase, crypted)
	if err != nil {
		panic(err)
	}

	if !bytes.Equal(plainResult, plaintext) {
		panic("expected correct plaintext")
	}
}

func TestEncryptDecryptDoesNotCorrupt(t *testing.T) {
	rand.NewSource(0)
	rSource := rand.NewSource(0)
	r := rand.New(rSource)

	// Choose a small number of sizes for performance reasons. Because key stretching happens on every
	// call, we're slow.
	plaintextLens := []int{0, 5, 64000, 128000}
	for i := 0; i < len(plaintextLens); i++ {
		b := make([]byte, plaintextLens[i])

		r.Read(b)
		passthrough("testphrase", b)
	}
}

Fix test bug, improve test edge case coverage.
Fix test bug, improve test edge case coverage.

The bug fixed is that we were comparing the wrong byte array in
`passthrough`, thus rendering the test partially ineffective (would
not detect incorrect output).
