| 132 | } |
| 133 | |
| 134 | func (s *ImageHashTestSuite) TestSHA256HashCalc() { |
| 135 | testCases := []struct { |
| 136 | filename string |
| 137 | expectedHash string |
| 138 | }{ |
| 139 | { |
| 140 | "test-images/png/1-bpp.png", |
| 141 | "c4e0a85ba474b6d6190d9f1d477f5d7ddab041a9951a1587f0ec8487d81cc1f9", |
| 142 | }, |
| 143 | { |
| 144 | "test-images/png/16-bpp-grayscale.png", |
| 145 | "9bc0007bfa2c6a1066e045fe84b4b8f731f0c1e0665c67ba012a8ca9d714d4f6", |
| 146 | }, |
| 147 | { |
| 148 | "test-images/png/16-bpp-linear.png", |
| 149 | "354bb65b9efce0b89990b49ff4609051186940bbbd261b13f225fa4710d19357", |
| 150 | }, |
| 151 | { |
| 152 | "test-images/png/16-bpp.png", |
| 153 | "14c680b197a3110df5ae58a0c57092de9ef10b4559b52701ce5f7ee69fbb9715", |
| 154 | }, |
| 155 | { |
| 156 | "test-images/png/8-bpp-grayscale.png", |
| 157 | "733d30d033d396f46cf2d0e99f542b5f1160129c0ca008b01903f8debb8044e0", |
| 158 | }, |
| 159 | { |
| 160 | "test-images/png/8-bpp.png", |
| 161 | "880ca38b6a98787ed85272a3f9567a29ab417afd9af8999960bd149d5029c450", |
| 162 | }, |
| 163 | { |
| 164 | "test-images/png/png.png", |
| 165 | "880ca38b6a98787ed85272a3f9567a29ab417afd9af8999960bd149d5029c450", |
| 166 | }, |
| 167 | } |
| 168 | |
| 169 | for _, tc := range testCases { |
| 170 | s.Run(tc.filename, func() { |
| 171 | // Calculate SHA-256 hash |
| 172 | hash := s.calcHash(tc.filename, testutil.HashTypeSHA256) |
| 173 | |
| 174 | // Check that calculated hash matches expected hash |
| 175 | s.Require().Equal( |
| 176 | "SHA256:"+tc.expectedHash, hash.String(), |
| 177 | "unexpected hash value for %s", tc.filename, |
| 178 | ) |
| 179 | |
| 180 | // Dump hash to buffer and load it back to check consistency |
| 181 | var buf bytes.Buffer |
| 182 | err := hash.Dump(&buf) |
| 183 | s.Require().NoError(err) |
| 184 | |
| 185 | loadedHash, err := testutil.LoadImageHash(&buf) |
| 186 | s.Require().NoError(err) |
| 187 | s.Require().NotNil(loadedHash) |
| 188 | |
| 189 | // Check that loaded hash matches original hash |
| 190 | distance, err := hash.Distance(loadedHash) |
| 191 | s.Require().NoError(err) |