github.com/pafomin-at-avito/mattermost-server@v5.11.1+incompatible/utils/imgutils/gif_test.go (about) 1 // Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package imgutils 5 6 import ( 7 "bytes" 8 "testing" 9 10 "github.com/mattermost/mattermost-server/utils/testutils" 11 "github.com/stretchr/testify/assert" 12 "github.com/stretchr/testify/require" 13 ) 14 15 func TestCountFrames(t *testing.T) { 16 header := []byte{ 17 'G', 'I', 'F', '8', '9', 'a', // header 18 1, 0, 1, 0, // width and height of 1 by 1 19 128, 0, 0, // other header information 20 0, 0, 0, 1, 1, 1, // color table 21 } 22 frame := []byte{ 23 0x2c, // block introducer 24 0, 0, 0, 0, 1, 0, 1, 0, // position and dimensions of the frame 25 0, // other frame information 26 0x2, 0x2, 0x4c, 0x1, 0, // encoded pixel data 27 } 28 trailer := []byte{0x3b} 29 30 t.Run("should count the frames of a static gif", func(t *testing.T) { 31 var b []byte 32 b = append(b, header...) 33 b = append(b, frame...) 34 b = append(b, trailer...) 35 36 count, err := CountFrames(bytes.NewReader(b)) 37 38 assert.Nil(t, err) 39 assert.Equal(t, 1, count) 40 }) 41 42 t.Run("should count the frames of an animated gif", func(t *testing.T) { 43 var b []byte 44 b = append(b, header...) 45 for i := 0; i < 100; i++ { 46 b = append(b, frame...) 47 } 48 b = append(b, trailer...) 49 50 count, err := CountFrames(bytes.NewReader(b)) 51 52 assert.Nil(t, err) 53 assert.Equal(t, 100, count) 54 }) 55 56 t.Run("should count the frames of an actual animated gif", func(t *testing.T) { 57 b, err := testutils.ReadTestFile("testgif.gif") 58 require.Nil(t, err) 59 60 count, err := CountFrames(bytes.NewReader(b)) 61 62 assert.Nil(t, err) 63 assert.Equal(t, 4, count) 64 }) 65 66 t.Run("should return an error for a non-gif image", func(t *testing.T) { 67 b, err := testutils.ReadTestFile("test.png") 68 require.Nil(t, err) 69 70 _, err = CountFrames(bytes.NewReader(b)) 71 72 assert.NotNil(t, err) 73 }) 74 75 t.Run("should return an error for garbage data", func(t *testing.T) { 76 _, err := CountFrames(bytes.NewReader([]byte("garbage data"))) 77 78 assert.NotNil(t, err) 79 }) 80 }