mirror of
https://codeberg.org/forgejo/forgejo
synced 2024-11-24 10:46:10 +01:00
81467e6f35
* Change to display SVG files as images * Remove unsafe styles from SVG CSP * Add integration test to test SVG headers * Add config setting to disable SVG rendering * Add test for img tag when loading SVG image * Remove the Raw view button for svg files since we don't fully support this * Fix copyright year * Rename and move config setting * Add setting to cheat sheet in docs * Fix so that comment matches cheat sheet * Add allowing styles in CSP based on pull request feedback * Re-enable raw button since we show SVG styles now * Change so that SVG files are editable * Add UI to toggle between source and rendered image for SVGs * Change to show blame button for SVG images * Fix to update ctx data * Add test for DetectContentType when file is longer than sniffLen Co-authored-by: Jonathan Tran <jon@allspice.io> Co-authored-by: Kyle D <kdumontnu@gmail.com>
253 lines
7.5 KiB
Go
253 lines
7.5 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// Use of this source code is governed by a MIT-style
|
|
// license that can be found in the LICENSE file.
|
|
|
|
package base
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
)
|
|
|
|
func TestEncodeMD5(t *testing.T) {
|
|
assert.Equal(t,
|
|
"3858f62230ac3c915f300c664312c63f",
|
|
EncodeMD5("foobar"),
|
|
)
|
|
}
|
|
|
|
func TestEncodeSha1(t *testing.T) {
|
|
assert.Equal(t,
|
|
"8843d7f92416211de9ebb963ff4ce28125932878",
|
|
EncodeSha1("foobar"),
|
|
)
|
|
}
|
|
|
|
func TestEncodeSha256(t *testing.T) {
|
|
assert.Equal(t,
|
|
"c3ab8ff13720e8ad9047dd39466b3c8974e592c2fa383d4a3960714caef0c4f2",
|
|
EncodeSha256("foobar"),
|
|
)
|
|
}
|
|
|
|
func TestShortSha(t *testing.T) {
|
|
assert.Equal(t, "veryverylo", ShortSha("veryverylong"))
|
|
}
|
|
|
|
func TestBasicAuthDecode(t *testing.T) {
|
|
_, _, err := BasicAuthDecode("?")
|
|
assert.Equal(t, "illegal base64 data at input byte 0", err.Error())
|
|
|
|
user, pass, err := BasicAuthDecode("Zm9vOmJhcg==")
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, "foo", user)
|
|
assert.Equal(t, "bar", pass)
|
|
|
|
_, _, err = BasicAuthDecode("aW52YWxpZA==")
|
|
assert.Error(t, err)
|
|
|
|
_, _, err = BasicAuthDecode("invalid")
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestBasicAuthEncode(t *testing.T) {
|
|
assert.Equal(t, "Zm9vOmJhcg==", BasicAuthEncode("foo", "bar"))
|
|
}
|
|
|
|
// TODO: Test PBKDF2()
|
|
// TODO: Test VerifyTimeLimitCode()
|
|
// TODO: Test CreateTimeLimitCode()
|
|
|
|
func TestFileSize(t *testing.T) {
|
|
var size int64 = 512
|
|
assert.Equal(t, "512 B", FileSize(size))
|
|
size *= 1024
|
|
assert.Equal(t, "512 KiB", FileSize(size))
|
|
size *= 1024
|
|
assert.Equal(t, "512 MiB", FileSize(size))
|
|
size *= 1024
|
|
assert.Equal(t, "512 GiB", FileSize(size))
|
|
size *= 1024
|
|
assert.Equal(t, "512 TiB", FileSize(size))
|
|
size *= 1024
|
|
assert.Equal(t, "512 PiB", FileSize(size))
|
|
size *= 4
|
|
assert.Equal(t, "2.0 EiB", FileSize(size))
|
|
}
|
|
|
|
func TestSubtract(t *testing.T) {
|
|
toFloat64 := func(n interface{}) float64 {
|
|
switch v := n.(type) {
|
|
case int:
|
|
return float64(v)
|
|
case int8:
|
|
return float64(v)
|
|
case int16:
|
|
return float64(v)
|
|
case int32:
|
|
return float64(v)
|
|
case int64:
|
|
return float64(v)
|
|
case float32:
|
|
return float64(v)
|
|
case float64:
|
|
return v
|
|
default:
|
|
return 0.0
|
|
}
|
|
}
|
|
values := []interface{}{
|
|
int(-3),
|
|
int8(14),
|
|
int16(81),
|
|
int32(-156),
|
|
int64(1528),
|
|
float32(3.5),
|
|
float64(-15.348),
|
|
}
|
|
for _, left := range values {
|
|
for _, right := range values {
|
|
expected := toFloat64(left) - toFloat64(right)
|
|
sub := Subtract(left, right)
|
|
assert.InDelta(t, expected, sub, 1e-3)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestEllipsisString(t *testing.T) {
|
|
assert.Equal(t, "...", EllipsisString("foobar", 0))
|
|
assert.Equal(t, "...", EllipsisString("foobar", 1))
|
|
assert.Equal(t, "...", EllipsisString("foobar", 2))
|
|
assert.Equal(t, "...", EllipsisString("foobar", 3))
|
|
assert.Equal(t, "f...", EllipsisString("foobar", 4))
|
|
assert.Equal(t, "fo...", EllipsisString("foobar", 5))
|
|
assert.Equal(t, "foobar", EllipsisString("foobar", 6))
|
|
assert.Equal(t, "foobar", EllipsisString("foobar", 10))
|
|
}
|
|
|
|
func TestTruncateString(t *testing.T) {
|
|
assert.Equal(t, "", TruncateString("foobar", 0))
|
|
assert.Equal(t, "f", TruncateString("foobar", 1))
|
|
assert.Equal(t, "fo", TruncateString("foobar", 2))
|
|
assert.Equal(t, "foo", TruncateString("foobar", 3))
|
|
assert.Equal(t, "foob", TruncateString("foobar", 4))
|
|
assert.Equal(t, "fooba", TruncateString("foobar", 5))
|
|
assert.Equal(t, "foobar", TruncateString("foobar", 6))
|
|
assert.Equal(t, "foobar", TruncateString("foobar", 7))
|
|
}
|
|
|
|
func TestStringsToInt64s(t *testing.T) {
|
|
testSuccess := func(input []string, expected []int64) {
|
|
result, err := StringsToInt64s(input)
|
|
assert.NoError(t, err)
|
|
assert.Equal(t, expected, result)
|
|
}
|
|
testSuccess([]string{}, []int64{})
|
|
testSuccess([]string{"-1234"}, []int64{-1234})
|
|
testSuccess([]string{"1", "4", "16", "64", "256"},
|
|
[]int64{1, 4, 16, 64, 256})
|
|
|
|
_, err := StringsToInt64s([]string{"-1", "a", "$"})
|
|
assert.Error(t, err)
|
|
}
|
|
|
|
func TestInt64sToStrings(t *testing.T) {
|
|
assert.Equal(t, []string{}, Int64sToStrings([]int64{}))
|
|
assert.Equal(t,
|
|
[]string{"1", "4", "16", "64", "256"},
|
|
Int64sToStrings([]int64{1, 4, 16, 64, 256}),
|
|
)
|
|
}
|
|
|
|
func TestInt64sToMap(t *testing.T) {
|
|
assert.Equal(t, map[int64]bool{}, Int64sToMap([]int64{}))
|
|
assert.Equal(t,
|
|
map[int64]bool{1: true, 4: true, 16: true},
|
|
Int64sToMap([]int64{1, 4, 16}),
|
|
)
|
|
}
|
|
|
|
func TestIsLetter(t *testing.T) {
|
|
assert.True(t, IsLetter('a'))
|
|
assert.True(t, IsLetter('e'))
|
|
assert.True(t, IsLetter('q'))
|
|
assert.True(t, IsLetter('z'))
|
|
assert.True(t, IsLetter('A'))
|
|
assert.True(t, IsLetter('E'))
|
|
assert.True(t, IsLetter('Q'))
|
|
assert.True(t, IsLetter('Z'))
|
|
assert.True(t, IsLetter('_'))
|
|
assert.False(t, IsLetter('-'))
|
|
assert.False(t, IsLetter('1'))
|
|
assert.False(t, IsLetter('$'))
|
|
}
|
|
|
|
func TestDetectContentTypeLongerThanSniffLen(t *testing.T) {
|
|
// Pre-condition: Shorter than sniffLen detects SVG.
|
|
assert.Equal(t, "image/svg+xml", DetectContentType([]byte(`<!-- Comment --><svg></svg>`)))
|
|
// Longer than sniffLen detects something else.
|
|
assert.Equal(t, "text/plain; charset=utf-8", DetectContentType([]byte(`<!--
|
|
Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
|
|
Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
|
|
Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
|
|
Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
|
|
Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
|
|
Comment Comment Comment Comment Comment Comment Comment Comment Comment Comment
|
|
Comment Comment Comment --><svg></svg>`)))
|
|
}
|
|
|
|
func TestIsTextFile(t *testing.T) {
|
|
assert.True(t, IsTextFile([]byte{}))
|
|
assert.True(t, IsTextFile([]byte("lorem ipsum")))
|
|
}
|
|
|
|
func TestIsSVGImageFile(t *testing.T) {
|
|
assert.True(t, IsSVGImageFile([]byte("<svg></svg>")))
|
|
assert.True(t, IsSVGImageFile([]byte(" <svg></svg>")))
|
|
assert.True(t, IsSVGImageFile([]byte(`<svg width="100"></svg>`)))
|
|
assert.True(t, IsSVGImageFile([]byte("<svg/>")))
|
|
assert.True(t, IsSVGImageFile([]byte(`<?xml version="1.0" encoding="UTF-8"?><svg></svg>`)))
|
|
assert.True(t, IsSVGImageFile([]byte(`<!-- Comment -->
|
|
<svg></svg>`)))
|
|
assert.True(t, IsSVGImageFile([]byte(`<!-- Multiple -->
|
|
<!-- Comments -->
|
|
<svg></svg>`)))
|
|
assert.True(t, IsSVGImageFile([]byte(`<!-- Multiline
|
|
Comment -->
|
|
<svg></svg>`)))
|
|
assert.True(t, IsSVGImageFile([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
|
<!-- Comment -->
|
|
<svg></svg>`)))
|
|
assert.True(t, IsSVGImageFile([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
|
<!-- Multiple -->
|
|
<!-- Comments -->
|
|
<svg></svg>`)))
|
|
assert.True(t, IsSVGImageFile([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
|
<!-- Multline
|
|
Comment -->
|
|
<svg></svg>`)))
|
|
assert.False(t, IsSVGImageFile([]byte{}))
|
|
assert.False(t, IsSVGImageFile([]byte("svg")))
|
|
assert.False(t, IsSVGImageFile([]byte("<svgfoo></svgfoo>")))
|
|
assert.False(t, IsSVGImageFile([]byte("text<svg></svg>")))
|
|
assert.False(t, IsSVGImageFile([]byte("<html><body><svg></svg></body></html>")))
|
|
assert.False(t, IsSVGImageFile([]byte(`<script>"<svg></svg>"</script>`)))
|
|
assert.False(t, IsSVGImageFile([]byte(`<!-- <svg></svg> inside comment -->
|
|
<foo></foo>`)))
|
|
assert.False(t, IsSVGImageFile([]byte(`<?xml version="1.0" encoding="UTF-8"?>
|
|
<!-- <svg></svg> inside comment -->
|
|
<foo></foo>`)))
|
|
}
|
|
|
|
func TestFormatNumberSI(t *testing.T) {
|
|
assert.Equal(t, "125", FormatNumberSI(int(125)))
|
|
assert.Equal(t, "1.3k", FormatNumberSI(int64(1317)))
|
|
assert.Equal(t, "21.3M", FormatNumberSI(21317675))
|
|
assert.Equal(t, "45.7G", FormatNumberSI(45721317675))
|
|
assert.Equal(t, "", FormatNumberSI("test"))
|
|
}
|
|
|
|
// TODO: IsImageFile(), currently no idea how to test
|
|
// TODO: IsPDFFile(), currently no idea how to test
|