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