debian-mirror-gitlab/workhorse/internal/upload/exif/exif_test.go

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

113 lines
2.5 KiB
Go
Raw Normal View History

2021-02-22 17:27:13 +05:30
package exif
import (
"context"
"io"
"os"
"strings"
"testing"
"github.com/stretchr/testify/require"
)
2021-04-15 22:33:27 +05:30
func TestFileTypeFromSuffix(t *testing.T) {
2021-02-22 17:27:13 +05:30
tests := []struct {
name string
2021-04-15 22:33:27 +05:30
expected FileType
2021-02-22 17:27:13 +05:30
}{
{
name: "/full/path.jpg",
2021-04-15 22:33:27 +05:30
expected: TypeJPEG,
2021-02-22 17:27:13 +05:30
},
{
name: "path.jpeg",
2021-04-15 22:33:27 +05:30
expected: TypeJPEG,
2021-02-22 17:27:13 +05:30
},
{
name: "path.tiff",
2021-04-15 22:33:27 +05:30
expected: TypeTIFF,
2021-02-22 17:27:13 +05:30
},
{
name: "path.JPG",
2021-04-15 22:33:27 +05:30
expected: TypeJPEG,
2021-02-22 17:27:13 +05:30
},
{
name: "path.tar",
2021-04-15 22:33:27 +05:30
expected: TypeUnknown,
2021-02-22 17:27:13 +05:30
},
{
name: "path",
2021-04-15 22:33:27 +05:30
expected: TypeUnknown,
},
{
name: "something.jpg.py",
expected: TypeUnknown,
},
{
name: "something.py.jpg",
expected: TypeJPEG,
},
{
name: `something.jpg
.py`,
expected: TypeUnknown,
},
{
name: `something.something
.jpg`,
expected: TypeUnknown,
2021-02-22 17:27:13 +05:30
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
2021-04-15 22:33:27 +05:30
require.Equal(t, test.expected, FileTypeFromSuffix(test.name))
2021-02-22 17:27:13 +05:30
})
}
}
func TestNewCleanerWithValidFile(t *testing.T) {
input, err := os.Open("testdata/sample_exif.jpg")
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cleaner, err := NewCleaner(ctx, input)
require.NoError(t, err, "Expected no error when creating cleaner command")
2022-07-23 23:45:48 +05:30
size, err := io.Copy(io.Discard, cleaner)
2021-02-22 17:27:13 +05:30
require.NoError(t, err, "Expected no error when reading output")
sizeAfterStrip := int64(25399)
require.Equal(t, sizeAfterStrip, size, "Different size of converted image")
}
func TestNewCleanerWithInvalidFile(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cleaner, err := NewCleaner(ctx, strings.NewReader("invalid image"))
require.NoError(t, err, "Expected no error when creating cleaner command")
2022-07-23 23:45:48 +05:30
size, err := io.Copy(io.Discard, cleaner)
2021-02-22 17:27:13 +05:30
require.Error(t, err, "Expected error when reading output")
require.Equal(t, int64(0), size, "Size of invalid image should be 0")
}
func TestNewCleanerReadingAfterEOF(t *testing.T) {
input, err := os.Open("testdata/sample_exif.jpg")
require.NoError(t, err)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cleaner, err := NewCleaner(ctx, input)
require.NoError(t, err, "Expected no error when creating cleaner command")
2022-07-23 23:45:48 +05:30
_, err = io.Copy(io.Discard, cleaner)
2021-02-22 17:27:13 +05:30
require.NoError(t, err, "Expected no error when reading output")
buf := make([]byte, 1)
size, err := cleaner.Read(buf)
require.Equal(t, 0, size, "The output was already consumed by previous reads")
require.Equal(t, io.EOF, err, "We return EOF")
}