package winpwn

import (
	"bytes"
	"testing"
)

func TestEnhexUnhexRoundTrip(t *testing.T) {
	data := []byte("Hello, world!")
	h := Enhex(data)
	if h != "48656c6c6f2c20776f726c6421" {
		t.Errorf("Enhex = %q", h)
	}
	back, err := Unhex(h)
	if err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(back, data) {
		t.Errorf("Unhex(Enhex(x)) = %q, want %q", back, data)
	}
}

func TestUnhexInvalid(t *testing.T) {
	if _, err := Unhex("zz"); err == nil {
		t.Error("expected error for invalid hex string")
	}
}

func TestXorRoundTrip(t *testing.T) {
	data := []byte("attack at dawn")
	key := []byte{0x42}
	if got := Xor(Xor(data, key), key); !bytes.Equal(got, data) {
		t.Errorf("Xor(Xor(x,k),k) = %q, want %q", got, data)
	}
}

func TestXorCyclesKey(t *testing.T) {
	data := []byte{1, 2, 3, 4}
	key := []byte{0xff, 0xff}
	got := Xor(data, key)
	want := []byte{0xfe, 0xfd, 0xfc, 0xfb}
	if !bytes.Equal(got, want) {
		t.Errorf("Xor = %v, want %v", got, want)
	}
}

func TestHexdumpFormat(t *testing.T) {
	out := Hexdump([]byte("Hello, world!"))
	if !bytes.Contains([]byte(out), []byte("48 65 6c 6c 6f")) {
		t.Errorf("Hexdump missing expected hex bytes: %q", out)
	}
	if !bytes.Contains([]byte(out), []byte("Hello, world!")) {
		t.Errorf("Hexdump missing ASCII gutter: %q", out)
	}
}
