grafana/pkg/util/encoding_test.go
Sergey Kostrukov 1dca39fb91
Auth Proxy: encoding of non-ASCII headers (#44797)
* Decode auth proxy headers using URL encoding

* Header encoding configuration via settings file

* Rename configuration setting to headers_encoded

* Quoted-printable encoding

* Tests for AuthProxy

* Fix encoding name

* Remove authproxy init
2022-03-04 04:58:27 -05:00

105 lines
2.5 KiB
Go

package util
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetBasicAuthHeader_Encoding(t *testing.T) {
t.Run("generating base64 header", func(t *testing.T) {
result := GetBasicAuthHeader("grafana", "1234")
assert.Equal(t, "Basic Z3JhZmFuYToxMjM0", result)
})
t.Run("decoding basic auth header", func(t *testing.T) {
header := GetBasicAuthHeader("grafana", "1234")
username, password, err := DecodeBasicAuthHeader(header)
require.NoError(t, err)
assert.Equal(t, "grafana", username)
assert.Equal(t, "1234", password)
})
}
func TestEncodePassword(t *testing.T) {
encodedPassword, err := EncodePassword("iamgod", "pepper")
require.NoError(t, err)
assert.Equal(
t,
"e59c568621e57756495a468f47c74e07c911b037084dd464bb2ed72410970dc849cabd71b48c394faf08a5405dae53741ce9",
encodedPassword,
)
}
func TestDecodeQuotedPrintable(t *testing.T) {
t.Run("should return not encoded string as is", func(t *testing.T) {
testStrings := []struct {
in string
out string
}{
{"", ""},
{" ", ""},
{"munich", "munich"},
{" munich", " munich"},
{"munich gothenburg", "munich gothenburg"},
{"München", "München"},
{"München Göteborg", "München Göteborg"},
}
for _, str := range testStrings {
val := DecodeQuotedPrintable(str.in)
assert.Equal(t, str.out, val)
}
})
t.Run("should decode encoded string", func(t *testing.T) {
testStrings := []struct {
in string
out string
}{
{"M=C3=BCnchen", "München"},
{"M=C3=BCnchen G=C3=B6teborg", "München Göteborg"},
{"=E5=85=AC=E5=8F=B8", "公司"},
}
for _, str := range testStrings {
val := DecodeQuotedPrintable(str.in)
assert.Equal(t, str.out, val)
}
})
t.Run("should gracefully ignore invalid encoding sequences", func(t *testing.T) {
testStrings := []struct {
in string
out string
}{
{"=XY=ZZ", "=XY=ZZ"},
{"==58", "=X"},
{"munich = gothenburg", "munich = gothenburg"},
{"munich == tromso", "munich == tromso"},
}
for _, str := range testStrings {
val := DecodeQuotedPrintable(str.in)
assert.Equal(t, str.out, val)
}
})
t.Run("should return invalid UTF-8 sequences as is", func(t *testing.T) {
testStrings := []struct {
in string
out string
}{
{"=E5 =85=AC =E5=8F =B8", "\xE5 \x85\xAC \xE5\x8F \xB8"},
{"=00=00munich=FF=FF", "\x00\x00munich\xFF\xFF"},
}
for _, str := range testStrings {
val := DecodeQuotedPrintable(str.in)
assert.Equal(t, str.out, val)
}
})
}