// KEKS/CM -- KEKS-encoded cryptographic messages
// Copyright (C) 2024-2025 Sergey Matveev <stargrave@stargrave.org>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as
// published by the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this program.  If not, see <http://www.gnu.org/licenses/>.

package merkle

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

func TestEmpty(t *testing.T) {
	h0 := sha512.Sum512(nil)
	h1 := NewHasherPrefixed(sha512.New, 8, 2)
	if !bytes.Equal(h1.Sum(nil), h0[:]) {
		t.FailNow()
	}
}

func Test1ChunkShort(t *testing.T) {
	data := []byte("some")
	h0 := sha512.Sum512(append([]byte{0}, data...))
	h1 := NewHasherPrefixed(sha512.New, 8, 2)
	if _, err := h1.Write(data); err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(h1.Sum(nil), h0[:]) {
		t.FailNow()
	}
}

func Test1Chunk(t *testing.T) {
	data := []byte("somethin")
	h0 := sha512.Sum512(append([]byte{0}, data...))
	h1 := NewHasherPrefixed(sha512.New, 8, 2)
	if _, err := h1.Write(data); err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(h1.Sum(nil), h0[:]) {
		t.FailNow()
	}
}

func Test2ChunkShort(t *testing.T) {
	data := []byte("something")
	c0 := sha512.Sum512([]byte("\x00somethin"))
	c1 := sha512.Sum512([]byte("\x00g"))
	c2 := append(c0[:], c1[:]...)
	c3 := sha512.Sum512(append([]byte{0x01}, c2...))
	h1 := NewHasherPrefixed(sha512.New, 8, 2)
	if _, err := h1.Write(data); err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(h1.Sum(nil), c3[:]) {
		t.FailNow()
	}
}

func Test2Chunk(t *testing.T) {
	data := []byte("somethingsomethi")
	c0 := sha512.Sum512([]byte("\x00somethin"))
	c1 := sha512.Sum512([]byte("\x00gsomethi"))
	c2 := sha512.Sum512(append([]byte{0x01}, append(c0[:], c1[:]...)...))
	h1 := NewHasherPrefixed(sha512.New, 8, 2)
	if _, err := h1.Write(data); err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(h1.Sum(nil), c2[:]) {
		t.FailNow()
	}
}

func Test3Chunk(t *testing.T) {
	data := []byte("123456")
	c0 := sha512.Sum512([]byte("\x0012"))
	c1 := sha512.Sum512([]byte("\x0034"))
	c2 := sha512.Sum512([]byte("\x0056"))
	c3 := sha512.Sum512(append([]byte{0x01}, append(c0[:], c1[:]...)...))
	c4 := sha512.Sum512(append([]byte{0x01}, append(c3[:], c2[:]...)...))
	h1 := NewHasherPrefixed(sha512.New, 2, 2)
	if _, err := h1.Write(data); err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(h1.Sum(nil), c4[:]) {
		t.FailNow()
	}
}

func Test4Chunk(t *testing.T) {
	data := []byte("12345678")
	c0 := sha512.Sum512([]byte("\x0012"))
	c1 := sha512.Sum512([]byte("\x0034"))
	c2 := sha512.Sum512([]byte("\x0056"))
	c3 := sha512.Sum512([]byte("\x0078"))
	c4 := sha512.Sum512(append([]byte{0x01}, append(c0[:], c1[:]...)...))
	c5 := sha512.Sum512(append([]byte{0x01}, append(c2[:], c3[:]...)...))
	c6 := sha512.Sum512(append([]byte{0x01}, append(c4[:], c5[:]...)...))
	h1 := NewHasherPrefixed(sha512.New, 2, 2)
	if _, err := h1.Write(data); err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(h1.Sum(nil), c6[:]) {
		t.FailNow()
	}
}

// RFC 9162, 2.1.5 Example
func TestRFC7Leaves(t *testing.T) {
	data := []byte("d0d1d2d3d4d5d6")
	a := sha512.Sum512([]byte("\x00d0"))
	b := sha512.Sum512([]byte("\x00d1"))
	c := sha512.Sum512([]byte("\x00d2"))
	d := sha512.Sum512([]byte("\x00d3"))
	e := sha512.Sum512([]byte("\x00d4"))
	f := sha512.Sum512([]byte("\x00d5"))
	g := sha512.Sum512(append([]byte{0x01}, append(a[:], b[:]...)...))
	h := sha512.Sum512(append([]byte{0x01}, append(c[:], d[:]...)...))
	i := sha512.Sum512(append([]byte{0x01}, append(e[:], f[:]...)...))
	j := sha512.Sum512([]byte("\x00d6"))
	k := sha512.Sum512(append([]byte{0x01}, append(g[:], h[:]...)...))
	l := sha512.Sum512(append([]byte{0x01}, append(i[:], j[:]...)...))
	root := sha512.Sum512(append([]byte{0x01}, append(k[:], l[:]...)...))
	h1 := NewHasherPrefixed(sha512.New, 2, 2)
	if _, err := h1.Write(data); err != nil {
		t.Fatal(err)
	}
	if !bytes.Equal(h1.Sum(nil), root[:]) {
		t.FailNow()
	}
}
