// Copyright (c) 2026 Alibaba Group and its affiliates

// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at

//     http://www.apache.org/licenses/LICENSE-2.0

// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package attestation

import (
	"encoding/base64"
	"encoding/json"
	"testing"

	"github.com/stretchr/testify/assert"
	"github.com/stretchr/testify/require"
)

func TestUnmarshalTdxEvidence(t *testing.T) {
	const quoteB64 = "BAACAIEAAAA="    // arbitrary non-empty bytes
	const eventLogB64 = "AAAAAAMAAAA=" // arbitrary non-empty bytes

	raw := []byte(`{
		"quote":        "` + quoteB64 + `",
		"cc_eventlog":  "` + eventLogB64 + `",
		"gpu_evidence": null
	}`)

	var ev TdxEvidence
	require.NoError(t, json.Unmarshal(raw, &ev))

	assert.Equal(t, quoteB64, ev.Quote)
	assert.Equal(t, eventLogB64, ev.CCEventLog)

	q, err := base64.StdEncoding.DecodeString(ev.Quote)
	require.NoError(t, err)
	assert.NotEmpty(t, q)

	el, err := base64.StdEncoding.DecodeString(ev.CCEventLog)
	require.NoError(t, err)
	assert.NotEmpty(t, el)

	// Same payload but gpu_evidence carries an object instead of null —
	// future-proof against AA starting to populate the field.
	rawWithObj := []byte(`{
		"quote":        "` + quoteB64 + `",
		"cc_eventlog":  "` + eventLogB64 + `",
		"gpu_evidence": {"some_field": "ignored"}
	}`)
	var ev2 TdxEvidence
	require.NoError(t, json.Unmarshal(rawWithObj, &ev2))
	assert.Equal(t, quoteB64, ev2.Quote)
	assert.Equal(t, eventLogB64, ev2.CCEventLog)
}
