package main

import (
	"fmt"
	"testing"
	"time"

	"github.com/awnumar/memguard"
)

func TestGetPocket(t *testing.T) {
	key := memguard.NewBufferFromBytes([]byte("yellow submarine"))

	start := time.Now()
	pocket := GetPocket(key)
	if key.IsAlive() {
		t.Error("key not destroyed")
	}
	fmt.Println(time.Since(start))

	id, err := pocket.ID.Open()
	if err != nil {
		t.Error(err)
	}
	defer id.Destroy()
	if id.Size() != 32 {
		t.Error("invalid size")
	}
	if !id.EqualTo([]byte{0x6b, 0x3a, 0x90, 0x3a, 0xf9, 0xc1, 0x7e, 0xfd, 0x8e, 0xc6, 0x52, 0x78, 0xd4, 0x85, 0xec, 0xc6, 0x9b, 0x1, 0x5e, 0x0, 0x7f, 0xfc, 0x79, 0xb9, 0x58, 0xa0, 0x8, 0xd2, 0x7f, 0x50, 0x4e, 0xb9}) {
		fmt.Printf("%#v\n", id.Bytes())
		t.Error("unexpected id")
	}

	key, err = pocket.Key.Open()
	if err != nil {
		t.Error(err)
	}
	defer key.Destroy()
	if key.Size() != 32 {
		t.Error("invalid size")
	}
	if !key.EqualTo([]byte{0x74, 0xe5, 0x81, 0xd8, 0x47, 0x41, 0x7e, 0x88, 0x98, 0x55, 0x68, 0x53, 0x9b, 0xc5, 0xd6, 0x4a, 0x7b, 0xeb, 0x82, 0xe3, 0x56, 0x63, 0x17, 0x61, 0xaa, 0xc0, 0x28, 0xf8, 0x70, 0x87, 0x82, 0xbb}) {
		fmt.Printf("%#v\n", key.Bytes())
		t.Error("unexpected key")
	}
}
