package document import ( "bytes" "encoding/binary" "testing" "gotest.tools/v3/assert" ) func TestParsePhotoXMP(t *testing.T) { tests := []struct { name string packet string wantAlt string wantPurchase string }{ { name: "prefers x-default and first nonempty licensor URL", packet: xmpPacket(` Deutsch English Default text https://example.com/first https://example.com/second `), wantAlt: "Default text", wantPurchase: "https://example.com/first", }, { name: "uses English when x-default is missing and accepts structured attributes", packet: xmpPacket(` Français English text `), wantAlt: "English text", wantPurchase: "https://example.com/attribute", }, { name: "uses first language and ignores captions", packet: xmpPacket(` Not alt text 日本語 Français `), wantAlt: "日本語", }, { name: "accepts compact attribute alt text", packet: ` `, wantAlt: "Compact alt text", }, { name: "missing fields are valid", packet: xmpPacket(`Photographer`), }, } for _, test := range tests { t.Run(test.name, func(t *testing.T) { metadata, err := parsePhotoXMP([]byte(test.packet)) assert.NilError(t, err) assert.Equal(t, metadata.Alt, test.wantAlt) assert.Equal(t, metadata.PurchaseURL, test.wantPurchase) }) } } func TestParsePhotoXMPRejectsMalformedXML(t *testing.T) { _, err := parsePhotoXMP([]byte(`Embedded alt text https://example.com/purchase`) packetWithTerminator := append([]byte(packet), 0) metadata, err := loadPhotoXMP(bytes.NewReader(jpegWithXMP(packetWithTerminator))) assert.NilError(t, err) assert.DeepEqual(t, metadata, want) metadata, err = loadPhotoXMP(bytes.NewReader([]byte{0xff, 0xd8, 0xff, 0xd9})) assert.NilError(t, err) assert.DeepEqual(t, metadata, photoXMP{}) } func TestApplyPhotoXMP(t *testing.T) { im := &img{ configuredPurchaseURL: "https://example.com/config", hasConfiguredPurchaseURL: true, } im.applyPhotoXMP(photoXMP{ Alt: "Embedded alt", PurchaseURL: "https://example.com/embedded", }) assert.Equal(t, im.Alt, "Embedded alt") assert.Equal(t, im.PurchaseURL, "https://example.com/config") im.hasConfiguredPurchaseURL = false im.applyPhotoXMP(photoXMP{}) assert.Equal(t, im.Alt, "") assert.Equal(t, im.PurchaseURL, "") im.configuredPurchaseURL = "" im.hasConfiguredPurchaseURL = true im.applyPhotoXMP(photoXMP{PurchaseURL: "https://example.com/embedded"}) assert.Equal(t, im.PurchaseURL, "") } func xmpPacket(properties string) string { return ` ` + properties + ` ` } func jpegWithXMP(packet []byte) []byte { return addXMPToJPEG([]byte{0xff, 0xd8, 0xff, 0xd9}, packet) } func addXMPToJPEG(source, packet []byte) []byte { if len(source) < 2 || source[0] != 0xff || source[1] != 0xd8 { panic("test source is not a JPEG") } identifier := []byte("http://ns.adobe.com/xap/1.0/\x00") segmentLength := len(identifier) + len(packet) + 2 if segmentLength > int(^uint16(0)) { panic("test XMP packet is too large for a JPEG APP1 segment") } xmpSegment := []byte{0xff, 0xe1, 0, 0} binary.BigEndian.PutUint16(xmpSegment[2:4], uint16(segmentLength)) xmpSegment = append(xmpSegment, identifier...) xmpSegment = append(xmpSegment, packet...) // goexif expects the EXIF APP1 segment to precede other APP1 data. insertAt := 2 for offset := 2; offset+4 <= len(source); { if source[offset] != 0xff { break } marker := source[offset+1] if marker == 0xd9 || marker == 0xda { break } length := int(binary.BigEndian.Uint16(source[offset+2 : offset+4])) segmentEnd := offset + 2 + length if length < 2 || segmentEnd > len(source) { break } if marker == 0xe1 && offset+10 <= segmentEnd && bytes.Equal(source[offset+4:offset+10], []byte("Exif\x00\x00")) { insertAt = segmentEnd break } offset = segmentEnd } jpeg := make([]byte, 0, len(source)+len(xmpSegment)) jpeg = append(jpeg, source[:insertAt]...) jpeg = append(jpeg, xmpSegment...) return append(jpeg, source[insertAt:]...) }