1. Eric Roshan Eisner
  2. stego

Source

stego / bytestream_test.go

package stego

import (
	"bytes"
	"crypto/aes"
	"testing"
)

const (
	compressible = "Reeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeally repetitive"
)

func bytestreamRoundTrip(t *testing.T, s string) {
	start := []byte(s)
	enc := EncodeStream(start)
	end, err := DecodeStream(enc)
	if err != nil {
		t.Errorf("Error: %v", err)
	}
	if !bytes.Equal(start, end) {
		t.Errorf("Input and output differ: %s, %s", start, end)
	}
}

func TestBytestreamRoundTrip(t *testing.T) {
	bytestreamRoundTrip(t, "")
	bytestreamRoundTrip(t, "short")
	bytestreamRoundTrip(t, compressible)
}

func TestBytestreamEncoding(t *testing.T) {
	enc := EncodeStream([]byte("short"))
	guess := []byte{5 << 1, 's', 'h', 'o', 'r', 't'}
	guess = append(guess, checksum([]byte("short"))...)
	if !bytes.Equal(enc, guess) {
		t.Errorf("%q != %q", enc, guess)
	}

	// toggle one data bit
	enc[3] ^= 0x1
	_, err := DecodeStream(enc)
	if err == nil {
		t.Errorf("Checksum did not detect data corruption.")
	}

	enc = EncodeStream([]byte(compressible))
	if enc[0]&0x1 != 1 || len(enc) > len(compressible) {
		t.Errorf("compression failed")
	}
}

func cryptoRoundTrip(t *testing.T, s string) {
	key := []byte("keykeykeykeykey!")
	c, _ := aes.NewCipher(key)
	start := []byte(s)
	enc := CryptoEncodeStream(start, c)
	end, err := CryptoDecodeStream(enc, c)
	if err != nil {
		t.Errorf("Error: %v", err)
	}
	if !bytes.Equal(start, end) {
		t.Errorf("Input and output differ: %s, %s", start, end)
	}
}

func TestCryptoRoundTrip(t *testing.T) {
	cryptoRoundTrip(t, "")
	cryptoRoundTrip(t, "short")
	cryptoRoundTrip(t, compressible)
}

func passphraseRoundTrip(t *testing.T, s string) {
	passphrase := []byte("setec astronomy")
	start := []byte(s)
	enc := PassphraseEncodeStream(start, passphrase)
	end, err := PassphraseDecodeStream(enc, passphrase)
	if err != nil {
		t.Errorf("Error: %v", err)
	}
	if !bytes.Equal(start, end) {
		t.Errorf("Input and output differ: %s, %s", start, end)
	}
}

func TestPassphraseRoundTrip(t *testing.T) {
	// Limited tests, as this is intentionally computationally expensive.
	if testing.Short() {
		return
	}
	passphraseRoundTrip(t, "short")
}