2016-02-15 07:14:55 +03:00
// Copyright 2016 The Gogs Authors. All rights reserved.
2022-11-27 21:20:29 +03:00
// SPDX-License-Identifier: MIT
2016-02-15 07:14:55 +03:00
package avatar
2014-03-23 08:24:09 +04:00
import (
2023-05-13 21:59:11 +03:00
"bytes"
"image"
"image/png"
2021-09-22 08:38:34 +03:00
"os"
2014-03-23 08:24:09 +04:00
"testing"
2014-03-23 14:13:23 +04:00
2019-05-25 14:46:14 +03:00
"code.gitea.io/gitea/modules/setting"
2017-02-08 09:29:07 +03:00
"github.com/stretchr/testify/assert"
2014-03-23 08:24:09 +04:00
)
2021-05-10 09:45:17 +03:00
func Test_RandomImageSize ( t * testing . T ) {
_ , err := RandomImageSize ( 0 , [ ] byte ( "gitea@local" ) )
assert . Error ( t , err )
_ , err = RandomImageSize ( 64 , [ ] byte ( "gitea@local" ) )
2017-02-08 09:29:07 +03:00
assert . NoError ( t , err )
2021-05-10 09:45:17 +03:00
}
2014-03-24 17:16:00 +04:00
2021-05-10 09:45:17 +03:00
func Test_RandomImage ( t * testing . T ) {
_ , err := RandomImage ( [ ] byte ( "gitea@local" ) )
assert . NoError ( t , err )
2014-03-24 17:16:00 +04:00
}
2019-05-25 14:46:14 +03:00
2023-05-13 21:59:11 +03:00
func Test_ProcessAvatarPNG ( t * testing . T ) {
2020-10-14 16:07:51 +03:00
setting . Avatar . MaxWidth = 4096
setting . Avatar . MaxHeight = 4096
2019-05-25 14:46:14 +03:00
2021-09-22 08:38:34 +03:00
data , err := os . ReadFile ( "testdata/avatar.png" )
2019-05-25 14:46:14 +03:00
assert . NoError ( t , err )
2023-05-13 21:59:11 +03:00
_ , err = processAvatarImage ( data , 262144 )
2019-05-25 14:46:14 +03:00
assert . NoError ( t , err )
}
2023-05-13 21:59:11 +03:00
func Test_ProcessAvatarJPEG ( t * testing . T ) {
2020-10-14 16:07:51 +03:00
setting . Avatar . MaxWidth = 4096
setting . Avatar . MaxHeight = 4096
2019-05-25 14:46:14 +03:00
2021-09-22 08:38:34 +03:00
data , err := os . ReadFile ( "testdata/avatar.jpeg" )
2019-05-25 14:46:14 +03:00
assert . NoError ( t , err )
2023-05-13 21:59:11 +03:00
_ , err = processAvatarImage ( data , 262144 )
2019-05-25 14:46:14 +03:00
assert . NoError ( t , err )
}
2023-05-13 21:59:11 +03:00
func Test_ProcessAvatarInvalidData ( t * testing . T ) {
2020-10-14 16:07:51 +03:00
setting . Avatar . MaxWidth = 5
setting . Avatar . MaxHeight = 5
2019-05-25 14:46:14 +03:00
2023-05-13 21:59:11 +03:00
_ , err := processAvatarImage ( [ ] byte { } , 12800 )
assert . EqualError ( t , err , "image.DecodeConfig: image: unknown format" )
2019-05-25 14:46:14 +03:00
}
2022-01-20 20:46:10 +03:00
2023-05-13 21:59:11 +03:00
func Test_ProcessAvatarInvalidImageSize ( t * testing . T ) {
2020-10-14 16:07:51 +03:00
setting . Avatar . MaxWidth = 5
setting . Avatar . MaxHeight = 5
2019-05-25 14:46:14 +03:00
2021-09-22 08:38:34 +03:00
data , err := os . ReadFile ( "testdata/avatar.png" )
2019-05-25 14:46:14 +03:00
assert . NoError ( t , err )
2023-05-13 21:59:11 +03:00
_ , err = processAvatarImage ( data , 12800 )
assert . EqualError ( t , err , "image width is too large: 10 > 5" )
}
func Test_ProcessAvatarImage ( t * testing . T ) {
setting . Avatar . MaxWidth = 4096
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 )
assert . NoError ( t , err )
return bs . Bytes ( )
}
// if origin image canvas is too large, crop and resize it
origin := newImgData ( 500 , 600 )
result , err := processAvatarImage ( origin , 0 )
assert . NoError ( t , err )
assert . NotEqual ( t , origin , result )
decoded , err := png . Decode ( bytes . NewReader ( result ) )
assert . NoError ( t , err )
assert . EqualValues ( t , scaledSize , decoded . Bounds ( ) . Max . X )
assert . EqualValues ( t , scaledSize , decoded . Bounds ( ) . Max . Y )
// if origin image is smaller than the default size, use the origin image
origin = newImgData ( 1 )
result , err = processAvatarImage ( origin , 0 )
assert . 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 )
assert . 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 , err = processAvatarImage ( origin , 262144 )
assert . NoError ( t , err )
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" )
assert . NoError ( t , err )
result , err = processAvatarImage ( origin , 262144 )
assert . NoError ( t , err )
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 )
assert . 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 )
assert . ErrorContains ( t , err , "image width is too large: 10 > 5" )
2019-05-25 14:46:14 +03:00
}