|
| 1 | +package analyzer |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/readium/go-toolkit/pkg/manifest" |
| 8 | + "github.com/readium/go-toolkit/pkg/mediatype" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "github.com/stretchr/testify/require" |
| 11 | +) |
| 12 | + |
| 13 | +func TestInspectImage(t *testing.T) { |
| 14 | + fs := os.DirFS("testdata/") |
| 15 | + catLink := manifest.Link{ |
| 16 | + Href: manifest.MustNewHREFFromString("catsink.jpg", false), |
| 17 | + MediaType: &mediatype.JPEG, |
| 18 | + } |
| 19 | + |
| 20 | + link, err := InspectImage(fs, catLink, []manifest.HashAlgorithm{}) |
| 21 | + require.NoError(t, err) |
| 22 | + require.NotNil(t, link) |
| 23 | + assert.Equal(t, uint(615), link.Width) |
| 24 | + assert.Equal(t, uint(458), link.Height) |
| 25 | + assert.Equal(t, uint(36710), link.Size) |
| 26 | + assert.False(t, link.Properties.Get("animated").(bool)) |
| 27 | + assert.Empty(t, link.Properties.Hash()) |
| 28 | + |
| 29 | + link, err = InspectImage(fs, manifest.Link{ |
| 30 | + Href: manifest.MustNewHREFFromString("animated.webp", false), |
| 31 | + MediaType: &mediatype.WEBP, |
| 32 | + }, []manifest.HashAlgorithm{}) |
| 33 | + require.NoError(t, err) |
| 34 | + require.NotNil(t, link) |
| 35 | + assert.Equal(t, uint(1000), link.Width) |
| 36 | + assert.Equal(t, uint(1000), link.Height) |
| 37 | + assert.Equal(t, uint(5764), link.Size) |
| 38 | + assert.True(t, link.Properties.Get("animated").(bool)) |
| 39 | + |
| 40 | + link, err = InspectImage(fs, manifest.Link{ |
| 41 | + Href: manifest.MustNewHREFFromString("animated.png", false), |
| 42 | + MediaType: &mediatype.PNG, |
| 43 | + }, []manifest.HashAlgorithm{}) |
| 44 | + require.NoError(t, err) |
| 45 | + require.NotNil(t, link) |
| 46 | + assert.Equal(t, uint(1000), link.Width) |
| 47 | + assert.Equal(t, uint(1000), link.Height) |
| 48 | + assert.Equal(t, uint(2932), link.Size) |
| 49 | + assert.True(t, link.Properties.Get("animated").(bool)) |
| 50 | + |
| 51 | + _, err = InspectImage(fs, manifest.Link{ |
| 52 | + Href: manifest.MustNewHREFFromString("corrupt.png", false), |
| 53 | + MediaType: &mediatype.PNG, |
| 54 | + }, []manifest.HashAlgorithm{}) |
| 55 | + require.Error(t, err) |
| 56 | + |
| 57 | + _, err = InspectImage(fs, manifest.Link{ |
| 58 | + Href: manifest.MustNewHREFFromString("frame1.jxl", false), |
| 59 | + MediaType: &mediatype.JXL, |
| 60 | + }, []manifest.HashAlgorithm{}) |
| 61 | + require.ErrorContains(t, err, "JXL file format is currently unsupported") |
| 62 | + |
| 63 | + link, err = InspectImage(fs, catLink, []manifest.HashAlgorithm{ |
| 64 | + manifest.HashAlgorithmBlake2b, // This is expected to not to anything |
| 65 | + manifest.HashAlgorithmSHA256, |
| 66 | + }) |
| 67 | + require.NoError(t, err) |
| 68 | + require.NotNil(t, link) |
| 69 | + if assert.Len(t, link.Properties.Hash(), 1) { |
| 70 | + assert.True(t, link.Properties.Hash()[0].Equal(manifest.HashValue{ |
| 71 | + Algorithm: manifest.HashAlgorithmSHA256, |
| 72 | + Value: "nzGm6cNL7fAadGSoFdtLzg/Z3MFqe3/fiWUZF9CPAKY=", |
| 73 | + })) |
| 74 | + } |
| 75 | + |
| 76 | + link, err = InspectImage(fs, catLink, []manifest.HashAlgorithm{ |
| 77 | + manifest.HashAlgorithmPhashDCT, |
| 78 | + }) |
| 79 | + require.NoError(t, err) |
| 80 | + require.NotNil(t, link) |
| 81 | + if assert.Len(t, link.Properties.Hash(), 1) { |
| 82 | + assert.True(t, link.Properties.Hash()[0].Equal(manifest.HashValue{ |
| 83 | + Algorithm: manifest.HashAlgorithmPhashDCT, |
| 84 | + Value: "TL5pWb0AIL8=", |
| 85 | + })) |
| 86 | + } |
| 87 | +} |
| 88 | + |
| 89 | +func TestMatchImage(t *testing.T) { |
| 90 | + fs := os.DirFS("testdata/") |
| 91 | + |
| 92 | + ok, err := MatchImage(manifest.Link{ |
| 93 | + Href: manifest.MustNewHREFFromString("audio.mp3", false), |
| 94 | + MediaType: &mediatype.MP3, |
| 95 | + }, manifest.HashList{}) |
| 96 | + require.ErrorContains(t, err, "link is not to an image that can be matched") |
| 97 | + require.False(t, ok) |
| 98 | + |
| 99 | + link, err := InspectImage(fs, manifest.Link{ |
| 100 | + Href: manifest.MustNewHREFFromString("catsink.jpg", false), |
| 101 | + MediaType: &mediatype.JPEG, |
| 102 | + }, []manifest.HashAlgorithm{ |
| 103 | + manifest.HashAlgorithmSHA256, |
| 104 | + manifest.HashAlgorithmPhashDCT, |
| 105 | + }) |
| 106 | + require.NoError(t, err) |
| 107 | + require.NotNil(t, link) |
| 108 | + ok, err = MatchImage(*link, manifest.HashList{ |
| 109 | + manifest.HashValue{ |
| 110 | + Algorithm: manifest.HashAlgorithmSHA256, |
| 111 | + Value: "nzGm6cNL7fAadGSoFdtLzg/Z3MFqe3/fiWUZF9CPAKY=", |
| 112 | + }, |
| 113 | + }) |
| 114 | + require.NoError(t, err) |
| 115 | + require.True(t, ok) |
| 116 | + ok, err = MatchImage(*link, manifest.HashList{ |
| 117 | + manifest.HashValue{ |
| 118 | + Algorithm: manifest.HashAlgorithmSHA256, |
| 119 | + Value: "xxxxxxxxfAadGSoFdtLzg/Z3MFqe3/fiWUZF9CPAKY=", |
| 120 | + }, |
| 121 | + }) |
| 122 | + require.NoError(t, err) |
| 123 | + require.False(t, ok) |
| 124 | + |
| 125 | + link1, err := InspectImage(fs, manifest.Link{ |
| 126 | + Href: manifest.MustNewHREFFromString("frame1.png", false), |
| 127 | + MediaType: &mediatype.PNG, |
| 128 | + }, []manifest.HashAlgorithm{manifest.HashAlgorithmPhashDCT}) |
| 129 | + require.NoError(t, err) |
| 130 | + require.NotNil(t, link1) |
| 131 | + link2, err := InspectImage(fs, manifest.Link{ |
| 132 | + Href: manifest.MustNewHREFFromString("frame2.png", false), |
| 133 | + MediaType: &mediatype.PNG, |
| 134 | + }, []manifest.HashAlgorithm{manifest.HashAlgorithmPhashDCT}) |
| 135 | + require.NoError(t, err) |
| 136 | + require.NotNil(t, link2) |
| 137 | + if assert.Len(t, link1.Properties.Hash(), 1) && assert.Len(t, link2.Properties.Hash(), 1) { |
| 138 | + hashes1 := link1.Properties.Hash() |
| 139 | + hashes2 := link2.Properties.Hash() |
| 140 | + |
| 141 | + // Too similar, they match |
| 142 | + ok, err = MatchImage(*link1, hashes2) |
| 143 | + require.NoError(t, err) |
| 144 | + assert.True(t, ok) |
| 145 | + |
| 146 | + // Pretty different, no match |
| 147 | + ok, err = MatchImage(*link, hashes1) |
| 148 | + require.NoError(t, err) |
| 149 | + assert.False(t, ok) |
| 150 | + } |
| 151 | +} |
0 commit comments