github.com/gitbundle/modules@v0.0.0-20231025071548-85b91c5c3b01/typesniffer/typesniffer_test.go (about) 1 // Copyright 2023 The GitBundle Inc. All rights reserved. 2 // Copyright 2017 The Gitea Authors. All rights reserved. 3 // Use of this source code is governed by a MIT-style 4 // license that can be found in the LICENSE file. 5 6 package typesniffer 7 8 import ( 9 "bytes" 10 "encoding/base64" 11 "strings" 12 "testing" 13 14 "github.com/stretchr/testify/assert" 15 ) 16 17 func TestDetectContentTypeLongerThanSniffLen(t *testing.T) { 18 // Pre-condition: Shorter than sniffLen detects SVG. 19 assert.Equal(t, "image/svg+xml", DetectContentType([]byte(`<!-- Comment --><svg></svg>`)).contentType) 20 // Longer than sniffLen detects something else. 21 assert.NotEqual(t, "image/svg+xml", DetectContentType([]byte(`<!-- `+strings.Repeat("x", sniffLen)+` --><svg></svg>`)).contentType) 22 } 23 24 func TestIsTextFile(t *testing.T) { 25 assert.True(t, DetectContentType([]byte{}).IsText()) 26 assert.True(t, DetectContentType([]byte("lorem ipsum")).IsText()) 27 } 28 29 func TestIsSvgImage(t *testing.T) { 30 assert.True(t, DetectContentType([]byte("<svg></svg>")).IsSvgImage()) 31 assert.True(t, DetectContentType([]byte(" <svg></svg>")).IsSvgImage()) 32 assert.True(t, DetectContentType([]byte(`<svg width="100"></svg>`)).IsSvgImage()) 33 assert.True(t, DetectContentType([]byte("<svg/>")).IsSvgImage()) 34 assert.True(t, DetectContentType([]byte(`<?xml version="1.0" encoding="UTF-8"?><svg></svg>`)).IsSvgImage()) 35 assert.True(t, DetectContentType([]byte(`<!-- Comment --> 36 <svg></svg>`)).IsSvgImage()) 37 assert.True(t, DetectContentType([]byte(`<!-- Multiple --> 38 <!-- Comments --> 39 <svg></svg>`)).IsSvgImage()) 40 assert.True(t, DetectContentType([]byte(`<!-- Multiline 41 Comment --> 42 <svg></svg>`)).IsSvgImage()) 43 assert.True(t, DetectContentType([]byte(`<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1 Basic//EN" 44 "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11-basic.dtd"> 45 <svg></svg>`)).IsSvgImage()) 46 assert.True(t, DetectContentType([]byte(`<?xml version="1.0" encoding="UTF-8"?> 47 <!-- Comment --> 48 <svg></svg>`)).IsSvgImage()) 49 assert.True(t, DetectContentType([]byte(`<?xml version="1.0" encoding="UTF-8"?> 50 <!-- Multiple --> 51 <!-- Comments --> 52 <svg></svg>`)).IsSvgImage()) 53 assert.True(t, DetectContentType([]byte(`<?xml version="1.0" encoding="UTF-8"?> 54 <!-- Multline 55 Comment --> 56 <svg></svg>`)).IsSvgImage()) 57 assert.True(t, DetectContentType([]byte(`<?xml version="1.0" encoding="UTF-8"?> 58 <!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 59 <!-- Multline 60 Comment --> 61 <svg></svg>`)).IsSvgImage()) 62 assert.False(t, DetectContentType([]byte{}).IsSvgImage()) 63 assert.False(t, DetectContentType([]byte("svg")).IsSvgImage()) 64 assert.False(t, DetectContentType([]byte("<svgfoo></svgfoo>")).IsSvgImage()) 65 assert.False(t, DetectContentType([]byte("text<svg></svg>")).IsSvgImage()) 66 assert.False(t, DetectContentType([]byte("<html><body><svg></svg></body></html>")).IsSvgImage()) 67 assert.False(t, DetectContentType([]byte(`<script>"<svg></svg>"</script>`)).IsSvgImage()) 68 assert.False(t, DetectContentType([]byte(`<!-- <svg></svg> inside comment --> 69 <foo></foo>`)).IsSvgImage()) 70 assert.False(t, DetectContentType([]byte(`<?xml version="1.0" encoding="UTF-8"?> 71 <!-- <svg></svg> inside comment --> 72 <foo></foo>`)).IsSvgImage()) 73 } 74 75 func TestIsPDF(t *testing.T) { 76 pdf, _ := base64.StdEncoding.DecodeString("JVBERi0xLjYKJcOkw7zDtsOfCjIgMCBvYmoKPDwvTGVuZ3RoIDMgMCBSL0ZpbHRlci9GbGF0ZURlY29kZT4+CnN0cmVhbQp4nF3NPwsCMQwF8D2f4s2CNYk1baF0EHRwOwg4iJt/NsFb/PpevUE4Mjwe") 77 assert.True(t, DetectContentType(pdf).IsPDF()) 78 assert.False(t, DetectContentType([]byte("plain text")).IsPDF()) 79 } 80 81 func TestIsVideo(t *testing.T) { 82 mp4, _ := base64.StdEncoding.DecodeString("AAAAGGZ0eXBtcDQyAAAAAGlzb21tcDQyAAEI721vb3YAAABsbXZoZAAAAADaBlwX2gZcFwAAA+gA") 83 assert.True(t, DetectContentType(mp4).IsVideo()) 84 assert.False(t, DetectContentType([]byte("plain text")).IsVideo()) 85 } 86 87 func TestIsAudio(t *testing.T) { 88 mp3, _ := base64.StdEncoding.DecodeString("SUQzBAAAAAABAFRYWFgAAAASAAADbWFqb3JfYnJhbmQAbXA0MgBUWFhYAAAAEQAAA21pbm9yX3Zl") 89 assert.True(t, DetectContentType(mp3).IsAudio()) 90 assert.False(t, DetectContentType([]byte("plain text")).IsAudio()) 91 } 92 93 func TestDetectContentTypeFromReader(t *testing.T) { 94 mp3, _ := base64.StdEncoding.DecodeString("SUQzBAAAAAABAFRYWFgAAAASAAADbWFqb3JfYnJhbmQAbXA0MgBUWFhYAAAAEQAAA21pbm9yX3Zl") 95 st, err := DetectContentTypeFromReader(bytes.NewReader(mp3)) 96 assert.NoError(t, err) 97 assert.True(t, st.IsAudio()) 98 }