/*
Copyright (c) Edgeless Systems GmbH

SPDX-License-Identifier: AGPL-3.0-only
*/

package extract

import (
	"bytes"
	"io"
	"testing"

	"github.com/edgelesssys/constellation/v2/image/measured-boot/fixtures"
	"github.com/edgelesssys/constellation/v2/image/measured-boot/pesection"

	"github.com/stretchr/testify/assert"
	"go.uber.org/goleak"
)

func TestMain(m *testing.M) {
	goleak.VerifyTestMain(m, goleak.IgnoreAnyFunction("github.com/bazelbuild/rules_go/go/tools/bzltestutil.RegisterTimeoutHandler.func1"))
}

func TestPeSectionReader(t *testing.T) {
	assert := assert.New(t)

	// can read existing section ".uname"
	peReader := bytes.NewReader(fixtures.UKI())
	unameSectionReader, err := PeSectionReader(peReader, ".uname")
	assert.NoError(err)
	uname, err := io.ReadAll(unameSectionReader)
	assert.NoError(err)
	assert.Equal("0.0.0-100.constellation.fc0.x86_64", string(uname))

	// fails to read non-existing section
	_, err = PeSectionReader(peReader, ".non-existing")
	assert.Error(err)

	// fails to read non-PE file
	_, err = PeSectionReader(bytes.NewReader([]byte("not a PE file")), ".uname")
	assert.Error(err)
}

func TestPeFileSectionDigests(t *testing.T) {
	assert := assert.New(t)

	// can calculate section digests
	peReader := bytes.NewReader(fixtures.UKI())
	sectionDigests, err := PeFileSectionDigests(peReader)
	assert.NoError(err)
	assert.Equal([]pesection.PESection{
		{
			Name: ".linux", Size: 0x1f8,
			Digest: [32]uint8{
				0x01, 0xe5, 0xce, 0xe2, 0xd1, 0x8e, 0xaa, 0xce,
				0x36, 0xb5, 0xbc, 0x39, 0x4f, 0x70, 0x31, 0xaa,
				0xe1, 0x66, 0x8e, 0x4a, 0x7f, 0x7c, 0xc0, 0xe9,
				0x49, 0x52, 0x5e, 0xa6, 0x5c, 0x40, 0xf7, 0x95,
			},
			Measure: true, MeasureOrder: 0,
		},
		{
			Name: ".osrel", Size: 0x2b0,
			Digest: [32]uint8{
				0x65, 0x83, 0x80, 0x1d, 0xa2, 0x9b, 0x3b, 0x74,
				0x0f, 0x0e, 0xb0, 0xc4, 0x27, 0xd5, 0xb8, 0x52,
				0x0b, 0xfb, 0xf7, 0xff, 0x63, 0x69, 0xc2, 0x2e,
				0xf2, 0xf4, 0xc4, 0x80, 0xf0, 0xea, 0x99, 0xfc,
			},
			Measure: true, MeasureOrder: 1,
		},
		{
			Name: ".cmdline",
			Size: 0x94,
			Digest: [32]uint8{
				0xf0, 0x47, 0xd0, 0x3a, 0x36, 0xf0, 0xde, 0x1f,
				0x77, 0x91, 0x6c, 0x2a, 0xab, 0x88, 0x77, 0xa9,
				0xd8, 0x80, 0xac, 0xf9, 0x17, 0x68, 0x3c, 0xc7,
				0x7b, 0x7c, 0x01, 0xdf, 0x18, 0xb1, 0x31, 0xc7,
			},
			Measure: true, MeasureOrder: 2,
		},
		{
			Name: ".initrd",
			Size: 0x12,
			Digest: [32]uint8{
				0x4e, 0x50, 0x30, 0x6a, 0x07, 0x84, 0x47, 0x1f,
				0x02, 0xde, 0x7e, 0x54, 0xd9, 0x0f, 0xdc, 0xa1,
				0x0e, 0x8e, 0x12, 0xec, 0xcc, 0x2d, 0x7a, 0x9d,
				0x97, 0x02, 0xf6, 0xe7, 0x38, 0xe1, 0xc2, 0xca,
			},
			Measure: true, MeasureOrder: 3,
		},
		{
			Name: ".splash",
			Size: 0x12,
			Digest: [32]uint8{
				0x36, 0xb5, 0xf4, 0x82, 0x37, 0x2e, 0x50, 0x49,
				0x83, 0x9d, 0x17, 0x6c, 0xf4, 0xd1, 0x4a, 0xcb,
				0xfd, 0xfe, 0xda, 0xc1, 0xbf, 0x77, 0xea, 0x0e,
				0xa4, 0xb1, 0x72, 0xa8, 0x76, 0xae, 0x2d, 0x2e,
			},
			Measure: true, MeasureOrder: 4,
		},
		{
			Name: ".dtb",
			Size: 0xf,
			Digest: [32]uint8{
				0x46, 0xa0, 0x01, 0x53, 0xca, 0xd9, 0x9d, 0x19,
				0x4a, 0xf1, 0x14, 0x48, 0x30, 0x5c, 0x8c, 0xa1,
				0x87, 0x2a, 0xba, 0xe9, 0x20, 0xee, 0x42, 0x3c,
				0x19, 0x35, 0x01, 0x05, 0x0f, 0x36, 0xe7, 0x8d,
			},
			Measure: true, MeasureOrder: 5,
		},
		{
			Name: ".pcrkey",
			Size: 0x12,
			Digest: [32]uint8{
				0x35, 0x4b, 0x67, 0xd5, 0xa3, 0xef, 0x2a, 0xff,
				0xda, 0xdb, 0x3d, 0xfc, 0x1f, 0x8b, 0xd0, 0xf6,
				0x69, 0xd0, 0x86, 0xa6, 0xd6, 0x7d, 0x5f, 0xee,
				0x88, 0xdb, 0x21, 0x90, 0xc4, 0xa7, 0x07, 0x26,
			},
			Measure: true, MeasureOrder: 7,
		},
		{
			Name: ".data",
			Size: 0x10,
			Digest: [32]uint8{
				0xc3, 0xde, 0x14, 0xca, 0x16, 0x45, 0x87, 0x5e,
				0x3b, 0xb0, 0xdd, 0xab, 0x9f, 0x60, 0x91, 0x46,
				0xf2, 0x1c, 0xc0, 0xeb, 0xd0, 0xea, 0x9b, 0x4f,
				0x22, 0xd3, 0x98, 0x40, 0xc0, 0xea, 0x29, 0xc5,
			},
			Measure: false, MeasureOrder: -1,
		},
		{
			Name: ".dynamic",
			Size: 0x13,
			Digest: [32]uint8{
				0x2b, 0x75, 0x29, 0xc8, 0x3a, 0x74, 0xbc, 0xb0,
				0xac, 0x63, 0x15, 0x18, 0xa1, 0x14, 0x95, 0x10,
				0x1a, 0x8d, 0x8e, 0x40, 0x69, 0x93, 0xed, 0x05,
				0xed, 0x8a, 0xcc, 0x2d, 0x88, 0xec, 0x13, 0x79,
			},
			Measure: false, MeasureOrder: -1,
		},
		{
			Name: ".dynsym",
			Size: 0x12,
			Digest: [32]uint8{
				0xb6, 0x0a, 0x7d, 0x65, 0x69, 0xeb, 0xa3, 0xd9,
				0x9e, 0xec, 0x13, 0x32, 0x57, 0x2b, 0x61, 0x19,
				0x32, 0x0b, 0x57, 0x1b, 0x43, 0xc1, 0x96, 0x75,
				0x37, 0x5a, 0x85, 0x76, 0xda, 0xf7, 0x81, 0x24,
			},
			Measure: false, MeasureOrder: -1,
		},
		{
			//nolint:misspell
			Name: ".rela",
			Size: 0x10,
			Digest: [32]uint8{
				0x1c, 0xd6, 0xfb, 0x4f, 0xb8, 0x74, 0xfd, 0xb2,
				0xf3, 0xb7, 0xf5, 0x3d, 0xc1, 0x8c, 0x5b, 0x8e,
				0x5b, 0xa1, 0x4d, 0x00, 0x6c, 0x56, 0x41, 0x5e,
				0x9b, 0x8e, 0x22, 0x1d, 0xbf, 0x59, 0xdd, 0x9d,
			},
			Measure: false, MeasureOrder: -1,
		},
		{
			Name: ".reloc",
			Size: 0xa,
			Digest: [32]uint8{
				0x4f, 0xfa, 0xdb, 0x1d, 0xbd, 0xe9, 0x2d, 0xce,
				0x21, 0x37, 0xae, 0x1e, 0x24, 0x74, 0xad, 0x09,
				0xf2, 0x7b, 0x62, 0xe4, 0xbb, 0xa5, 0xcc, 0xc6,
				0x49, 0x0a, 0xb0, 0xda, 0x45, 0xfa, 0x45, 0xc3,
			},
			Measure: false, MeasureOrder: -1,
		},
		{
			Name: ".sbat", Size: 0x10,
			Digest: [32]uint8{
				0x66, 0x30, 0xfb, 0x7d, 0x5b, 0xaf, 0x9d, 0x6c,
				0xd5, 0x1c, 0x9a, 0xc9, 0x54, 0x10, 0xe6, 0x8a,
				0xa3, 0xfe, 0xdb, 0x4a, 0xdd, 0xd4, 0x2b, 0x34,
				0x0e, 0x47, 0x11, 0xe2, 0x3c, 0xcc, 0xd4, 0xb2,
			},
			Measure: false, MeasureOrder: -1,
		},
		{
			Name: ".sdmagic", Size: 0x2d,
			Digest: [32]uint8{
				0xc1, 0x02, 0x12, 0x0d, 0xe9, 0xfa, 0x62, 0x43,
				0xf2, 0x16, 0xdd, 0xb4, 0x58, 0x28, 0xe2, 0xa2,
				0xb6, 0x4a, 0x65, 0x82, 0x30, 0xd0, 0xca, 0xe6,
				0xc2, 0xf2, 0x98, 0x39, 0x67, 0xba, 0xbe, 0x95,
			},
			Measure: false, MeasureOrder: -1,
		},
		{
			Name: ".text", Size: 0x10,
			Digest: [32]uint8{
				0xaf, 0x54, 0x41, 0x9a, 0x3f, 0xbe, 0x76, 0x0c,
				0xf7, 0xd3, 0x6a, 0x86, 0x37, 0xf0, 0x1d, 0x13,
				0xd4, 0x4b, 0xb5, 0xf3, 0x92, 0x15, 0xe2, 0x2e,
				0xad, 0x52, 0x15, 0x51, 0xfa, 0xe4, 0x2f, 0x2d,
			},
			Measure: false, MeasureOrder: -1,
		},
		{
			Name: ".uname", Size: 0x22,
			Digest: [32]uint8{
				0x32, 0xd5, 0x9d, 0x99, 0x0e, 0x9c, 0x1f, 0x7d,
				0xa5, 0x54, 0xcb, 0x88, 0x8e, 0x32, 0x38, 0xac,
				0x61, 0x93, 0xe5, 0xe7, 0x23, 0x0f, 0x99, 0xb1,
				0x97, 0x13, 0x8d, 0xd7, 0x23, 0xc0, 0xeb, 0xb6,
			},
			Measure: false, MeasureOrder: -1,
		},
		{
			Name: ".pcrsig", Size: 0x216,
			Digest: [32]uint8{
				0xcc, 0x41, 0xa5, 0x48, 0xbd, 0x02, 0x03, 0x17,
				0x49, 0x39, 0xf5, 0x0c, 0x3d, 0xf1, 0x77, 0x59,
				0xb8, 0x13, 0xb5, 0x31, 0xb0, 0x56, 0x3e, 0x91,
				0x20, 0x55, 0x6c, 0xf7, 0x25, 0x01, 0xa3, 0x26,
			},
			Measure: false, MeasureOrder: 6,
		},
	}, sectionDigests)

	// fails to read non-PE file
	_, err = PeFileSectionDigests(bytes.NewReader([]byte("not a PE file")))
	assert.Error(err)
}
