forgejo/modules/avatar/avatar_test.go
Mathieu Fenniak bd4201ed25 chore: remove EXIF stripping capability due to usage of AGPL licensed exif-terminator library (#13105)
In #9638, repository and user avatars had an EXIF removal capability added to them.  Unfortunately, this capability was added with an AGPL licensed library, exif-terminator, which is incompatible with Forgejo's license.  This was not detected by license check automation at the time.

This PR removes the capability in order to fix the license compatibility.  The `forgejo doctor avatar-strip-exif` is retained with a warning output that it is not supported.

Reopens: forgejo/forgejo#9608

## Checklist

The [contributor guide](https://forgejo.org/docs/next/contributor/) contains information that will be helpful to first time contributors. All work and communication must conform to Forgejo's [AI Agreement](https://codeberg.org/forgejo/governance/src/branch/main/AIAgreement.md). There also are a few [conditions for merging Pull Requests in Forgejo repositories](https://codeberg.org/forgejo/governance/src/branch/main/PullRequestsAgreement.md). You are also welcome to join the [Forgejo development chatroom](https://matrix.to/#/#forgejo-development:matrix.org).

### Tests for Go changes

- I added test coverage for Go changes...
  - [x] in their respective `*_test.go` for unit tests.
  - [ ] in the `tests/integration` directory if it involves interactions with a live Forgejo server.
- I ran...
  - [x] `make pr-go` before pushing

### Documentation

- [ ] I created a pull request [to the documentation](https://codeberg.org/forgejo/docs) to explain to Forgejo users how to use this change.
- [x] I did not document these changes and I do not expect someone else to do it.

### Release notes

- [x] This change will be noticed by a Forgejo user or admin (feature, bug fix, performance, etc.). I suggest to include a release note for this change.
- [ ] This change is not visible to a Forgejo user or admin (refactor, dependency upgrade, etc.). I think there is no need to add a release note for this change.

Reviewed-on: https://codeberg.org/forgejo/forgejo/pulls/13105
Reviewed-by: Andreas Ahlenstorf <aahlenst@noreply.codeberg.org>
Reviewed-by: 0ko <0ko@noreply.codeberg.org>
2026-06-17 16:16:35 +02:00

156 lines
4.6 KiB
Go

// Copyright 2016 The Gogs Authors. All rights reserved.
// SPDX-License-Identifier: MIT
package avatar
import (
"bytes"
"image"
"image/png"
"os"
"testing"
"forgejo.org/modules/setting"
"forgejo.org/modules/test"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_RandomImageSize(t *testing.T) {
_, err := RandomImageSize(0, []byte("gitea@local"))
require.Error(t, err)
_, err = RandomImageSize(64, []byte("gitea@local"))
require.NoError(t, err)
}
func Test_RandomImage(t *testing.T) {
_, err := RandomImage([]byte("gitea@local"))
require.NoError(t, err)
}
func Test_ProcessAvatarPNG(t *testing.T) {
defer test.MockVariableValue(&setting.Avatar.MaxWidth, 4096)()
defer test.MockVariableValue(&setting.Avatar.MaxHeight, 4096)()
data, err := os.ReadFile("testdata/avatar.png")
require.NoError(t, err)
_, img, err := processAvatarImage(data, 262144)
require.NoError(t, err)
require.Nil(t, img)
}
func Test_ProcessAvatarJPEG(t *testing.T) {
defer test.MockVariableValue(&setting.Avatar.MaxWidth, 4096)()
defer test.MockVariableValue(&setting.Avatar.MaxHeight, 4096)()
data, err := os.ReadFile("testdata/avatar.jpeg")
require.NoError(t, err)
_, img, err := processAvatarImage(data, 262144)
require.NoError(t, err)
require.Nil(t, img)
}
func Test_ProcessAvatarGIF(t *testing.T) {
defer test.MockVariableValue(&setting.Avatar.MaxWidth, 4096)()
defer test.MockVariableValue(&setting.Avatar.MaxHeight, 4096)()
data, err := os.ReadFile("testdata/avatar.gif")
require.NoError(t, err)
_, img, err := processAvatarImage(data, 262144)
require.NoError(t, err)
require.Nil(t, img)
}
func Test_ProcessAvatarInvalidData(t *testing.T) {
defer test.MockVariableValue(&setting.Avatar.MaxWidth, 5)()
defer test.MockVariableValue(&setting.Avatar.MaxHeight, 5)()
_, _, err := processAvatarImage([]byte{}, 12800)
assert.EqualError(t, err, "image.DecodeConfig: image: unknown format")
}
func Test_ProcessAvatarInvalidImageSize(t *testing.T) {
defer test.MockVariableValue(&setting.Avatar.MaxWidth, 5)()
defer test.MockVariableValue(&setting.Avatar.MaxHeight, 5)()
data, err := os.ReadFile("testdata/avatar.png")
require.NoError(t, err)
_, _, err = processAvatarImage(data, 12800)
assert.EqualError(t, err, "image width is too large: 10 > 5")
}
func Test_ProcessAvatarImage(t *testing.T) {
defer test.MockVariableValue(&setting.Avatar.MaxWidth, 4096)()
defer test.MockVariableValue(&setting.Avatar.MaxHeight, 4096)()
scaledSize := DefaultAvatarSize * setting.Avatar.RenderedSizeFactor
newImgData := func(size int, optHeight ...int) []byte {
width := size
height := size
if len(optHeight) == 1 {
height = optHeight[0]
}
img := image.NewRGBA(image.Rect(0, 0, width, height))
bs := bytes.Buffer{}
err := png.Encode(&bs, img)
require.NoError(t, err)
return bs.Bytes()
}
// if origin image canvas is too large, crop and resize it
origin := newImgData(500, 600)
result, img, err := processAvatarImage(origin, 0)
require.NoError(t, err)
assert.NotEqual(t, origin, result)
decoded, err := png.Decode(bytes.NewReader(result))
require.NoError(t, err)
assert.Equal(t, scaledSize, decoded.Bounds().Max.X)
assert.Equal(t, scaledSize, decoded.Bounds().Max.Y)
assert.Equal(t, scaledSize, img.Bounds().Max.X)
assert.Equal(t, scaledSize, img.Bounds().Max.Y)
// if origin image is smaller than the default size, use the origin image
origin = newImgData(1)
result, _, err = processAvatarImage(origin, 0)
require.NoError(t, err)
assert.Equal(t, origin, result)
// use the origin image if the origin is smaller
origin = newImgData(scaledSize + 100)
result, _, err = processAvatarImage(origin, 0)
require.NoError(t, err)
assert.Less(t, len(result), len(origin))
// still use the origin image if the origin doesn't exceed the max-origin-size
origin = newImgData(scaledSize + 100)
result, img, err = processAvatarImage(origin, 262144)
require.NoError(t, err)
require.Nil(t, img)
assert.Equal(t, origin, result)
// allow to use known image format (eg: webp) if it is small enough
origin, err = os.ReadFile("testdata/animated.webp")
require.NoError(t, err)
result, img, err = processAvatarImage(origin, 262144)
require.NoError(t, err)
require.Nil(t, img)
assert.Equal(t, origin, result)
// do not support unknown image formats, eg: SVG may contain embedded JS
origin = []byte("<svg></svg>")
_, _, err = processAvatarImage(origin, 262144)
require.ErrorContains(t, err, "image: unknown format")
// make sure the canvas size limit works
setting.Avatar.MaxWidth = 5
setting.Avatar.MaxHeight = 5
origin = newImgData(10)
_, _, err = processAvatarImage(origin, 262144)
require.ErrorContains(t, err, "image width is too large: 10 > 5")
}