// Copyright 2025 The zb Authors
// SPDX-License-Identifier: MIT

package macho

import (
	"testing"

	"github.com/google/go-cmp/cmp"
)

func TestSuperBlobUnmarshalBinary(t *testing.T) {
	data, err := openTestFile("macho-program-aarch64-apple-macos", 16544, 274)
	if err != nil {
		t.Fatal(err)
	}

	got := new(SuperBlob)
	if err := got.UnmarshalBinary(data); err != nil {
		t.Error("new(SuperBlob).UnmarshalBinary(...):", err)
	}
	want := &SuperBlob{
		Magic: CodeSignatureMagicEmbeddedSignature,
		Blobs: []SuperBlobEntry{
			{
				Type: SuperBlobCodeDirectorySlot,
				Blob: CodeSignatureBlob{
					Magic: CodeSignatureMagicCodeDirectory,
					Data: []byte{
						0x00, 0x02, 0x04, 0x00, 0x00, 0x02, 0x00, 0x02,
						0x00, 0x00, 0x00, 0x5e, 0x00, 0x00, 0x00, 0x58,
						0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
						0x00, 0x00, 0x40, 0xa0, 0x20, 0x02, 0x00, 0x0c,
						0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
						0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
						0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
						0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
						0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14,
						0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
						0x61, 0x2e, 0x6f, 0x75, 0x74, 0x00, 0x55, 0x5f,
						0xa9, 0xfe, 0x6a, 0x5a, 0x0a, 0x79, 0xb8, 0xbd,
						0xd1, 0xcf, 0x0a, 0xa6, 0x3f, 0xd0, 0x66, 0x2d,
						0x08, 0x1f, 0x1f, 0xbc, 0xe1, 0x79, 0x04, 0xa5,
						0x5d, 0x9c, 0x31, 0x5d, 0x3a, 0x20, 0xad, 0x7f,
						0xac, 0xb2, 0x58, 0x6f, 0xc6, 0xe9, 0x66, 0xc0,
						0x04, 0xd7, 0xd1, 0xd1, 0x6b, 0x02, 0x4f, 0x58,
						0x05, 0xff, 0x7c, 0xb4, 0x7c, 0x7a, 0x85, 0xda,
						0xbd, 0x8b, 0x48, 0x89, 0x2c, 0xa7, 0xad, 0x7f,
						0xac, 0xb2, 0x58, 0x6f, 0xc6, 0xe9, 0x66, 0xc0,
						0x04, 0xd7, 0xd1, 0xd1, 0x6b, 0x02, 0x4f, 0x58,
						0x05, 0xff, 0x7c, 0xb4, 0x7c, 0x7a, 0x85, 0xda,
						0xbd, 0x8b, 0x48, 0x89, 0x2c, 0xa7, 0x4c, 0x71,
						0x07, 0x05, 0x56, 0x1c, 0x1f, 0xb7, 0x7c, 0x0f,
						0xc5, 0x76, 0x64, 0xf6, 0x78, 0xf0, 0xd3, 0xf3,
						0x3e, 0xd7, 0x63, 0x2a, 0xe4, 0x08, 0xb6, 0xb8,
						0x78, 0x97, 0x46, 0x42, 0x74, 0x8b, 0xf5, 0x8e,
						0x3b, 0x55, 0xe9, 0xf9, 0xa2, 0x8a, 0xd4, 0x81,
						0xc0, 0x77, 0x24, 0xa5, 0x24, 0x12, 0x13, 0x3f,
						0x0c, 0x36, 0xc4, 0xac, 0x89, 0x3a, 0xba, 0xba,
						0xd6, 0x5b, 0xbf, 0x41, 0xfc, 0x02,
					},
				},
			},
		},
	}
	if diff := cmp.Diff(want, got); diff != "" {
		t.Errorf("-want +got:\n%s", diff)
	}
}

func TestCodeDirectoryUnmarshalBinary(t *testing.T) {
	tests := []struct {
		name string
		data []byte
		want CodeDirectory
	}{
		{
			name: "AdHoc",
			data: []byte{
				0xfa, 0xde, 0x0c, 0x02, 0x00, 0x00, 0x00, 0xfe,
				0x00, 0x02, 0x04, 0x00, 0x00, 0x02, 0x00, 0x02,
				0x00, 0x00, 0x00, 0x5e, 0x00, 0x00, 0x00, 0x58,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x05,
				0x00, 0x00, 0x40, 0xa0, 0x20, 0x02, 0x00, 0x0c,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
				0x61, 0x2e, 0x6f, 0x75, 0x74, 0x00, 0x55, 0x5f,
				0xa9, 0xfe, 0x6a, 0x5a, 0x0a, 0x79, 0xb8, 0xbd,
				0xd1, 0xcf, 0x0a, 0xa6, 0x3f, 0xd0, 0x66, 0x2d,
				0x08, 0x1f, 0x1f, 0xbc, 0xe1, 0x79, 0x04, 0xa5,
				0x5d, 0x9c, 0x31, 0x5d, 0x3a, 0x20, 0xad, 0x7f,
				0xac, 0xb2, 0x58, 0x6f, 0xc6, 0xe9, 0x66, 0xc0,
				0x04, 0xd7, 0xd1, 0xd1, 0x6b, 0x02, 0x4f, 0x58,
				0x05, 0xff, 0x7c, 0xb4, 0x7c, 0x7a, 0x85, 0xda,
				0xbd, 0x8b, 0x48, 0x89, 0x2c, 0xa7, 0xad, 0x7f,
				0xac, 0xb2, 0x58, 0x6f, 0xc6, 0xe9, 0x66, 0xc0,
				0x04, 0xd7, 0xd1, 0xd1, 0x6b, 0x02, 0x4f, 0x58,
				0x05, 0xff, 0x7c, 0xb4, 0x7c, 0x7a, 0x85, 0xda,
				0xbd, 0x8b, 0x48, 0x89, 0x2c, 0xa7, 0x4c, 0x71,
				0x07, 0x05, 0x56, 0x1c, 0x1f, 0xb7, 0x7c, 0x0f,
				0xc5, 0x76, 0x64, 0xf6, 0x78, 0xf0, 0xd3, 0xf3,
				0x3e, 0xd7, 0x63, 0x2a, 0xe4, 0x08, 0xb6, 0xb8,
				0x78, 0x97, 0x46, 0x42, 0x74, 0x8b, 0xf5, 0x8e,
				0x3b, 0x55, 0xe9, 0xf9, 0xa2, 0x8a, 0xd4, 0x81,
				0xc0, 0x77, 0x24, 0xa5, 0x24, 0x12, 0x13, 0x3f,
				0x0c, 0x36, 0xc4, 0xac, 0x89, 0x3a, 0xba, 0xba,
				0xd6, 0x5b, 0xbf, 0x41, 0xfc, 0x02,
			},
			want: CodeDirectory{
				Identifier:             "a.out",
				Flags:                  CodeSignatureAdHoc | CodeSignatureLinkerSigned,
				CodeLimit:              16544,
				ExecutableSegmentBase:  0,
				ExecutableSegmentLimit: 20,
				ExecutableSegmentFlags: 0x1,
				PageSize:               12,

				HashType: HashTypeSHA256,
				HashData: []byte{
					0x55, 0x5f, 0xa9, 0xfe, 0x6a, 0x5a, 0x0a, 0x79, 0xb8, 0xbd, 0xd1, 0xcf, 0x0a, 0xa6, 0x3f, 0xd0, 0x66, 0x2d, 0x08, 0x1f, 0x1f, 0xbc, 0xe1, 0x79, 0x04, 0xa5, 0x5d, 0x9c, 0x31, 0x5d, 0x3a, 0x20,
					0xad, 0x7f, 0xac, 0xb2, 0x58, 0x6f, 0xc6, 0xe9, 0x66, 0xc0, 0x04, 0xd7, 0xd1, 0xd1, 0x6b, 0x02, 0x4f, 0x58, 0x05, 0xff, 0x7c, 0xb4, 0x7c, 0x7a, 0x85, 0xda, 0xbd, 0x8b, 0x48, 0x89, 0x2c, 0xa7,
					0xad, 0x7f, 0xac, 0xb2, 0x58, 0x6f, 0xc6, 0xe9, 0x66, 0xc0, 0x04, 0xd7, 0xd1, 0xd1, 0x6b, 0x02, 0x4f, 0x58, 0x05, 0xff, 0x7c, 0xb4, 0x7c, 0x7a, 0x85, 0xda, 0xbd, 0x8b, 0x48, 0x89, 0x2c, 0xa7,
					0x4c, 0x71, 0x07, 0x05, 0x56, 0x1c, 0x1f, 0xb7, 0x7c, 0x0f, 0xc5, 0x76, 0x64, 0xf6, 0x78, 0xf0, 0xd3, 0xf3, 0x3e, 0xd7, 0x63, 0x2a, 0xe4, 0x08, 0xb6, 0xb8, 0x78, 0x97, 0x46, 0x42, 0x74, 0x8b,
					0xf5, 0x8e, 0x3b, 0x55, 0xe9, 0xf9, 0xa2, 0x8a, 0xd4, 0x81, 0xc0, 0x77, 0x24, 0xa5, 0x24, 0x12, 0x13, 0x3f, 0x0c, 0x36, 0xc4, 0xac, 0x89, 0x3a, 0xba, 0xba, 0xd6, 0x5b, 0xbf, 0x41, 0xfc, 0x02,
				},
			},
		},
	}

	for _, test := range tests {
		t.Run(test.name, func(t *testing.T) {
			var got CodeDirectory
			if err := got.UnmarshalBinary(test.data); err != nil {
				t.Error("UnmarshalBinary:", err)
			}
			if diff := cmp.Diff(&test.want, &got); diff != "" {
				t.Errorf("-want +got:\n%s", diff)
			}
		})
	}
}
