opentofu/lang/funcs/encoding_test.go

166 lines
3.2 KiB
Go
Raw Normal View History

2018-05-22 18:29:45 -05:00
package funcs
import (
"fmt"
"testing"
"github.com/zclconf/go-cty/cty"
)
func TestBase64Decode(t *testing.T) {
tests := []struct {
String cty.Value
Want cty.Value
Err bool
}{
{
cty.StringVal("YWJjMTIzIT8kKiYoKSctPUB+"),
cty.StringVal("abc123!?$*&()'-=@~"),
false,
},
{ // Invalid base64 data decoding
cty.StringVal("this-is-an-invalid-base64-data"),
cty.UnknownVal(cty.String),
true,
},
{ // Invalid utf-8
cty.StringVal("\xc3\x28"),
cty.UnknownVal(cty.String),
true,
},
2018-05-22 18:29:45 -05:00
}
for _, test := range tests {
t.Run(fmt.Sprintf("base64decode(%#v)", test.String), func(t *testing.T) {
got, err := Base64Decode(test.String)
if test.Err {
if err == nil {
t.Fatal("succeeded; want error")
}
return
2018-05-23 11:38:12 -05:00
} else if err != nil {
t.Fatalf("unexpected error: %s", err)
2018-05-22 18:29:45 -05:00
}
if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}
2018-05-23 09:27:28 -05:00
func TestBase64Encode(t *testing.T) {
tests := []struct {
String cty.Value
Want cty.Value
Err bool
}{
{
cty.StringVal("abc123!?$*&()'-=@~"),
cty.StringVal("YWJjMTIzIT8kKiYoKSctPUB+"),
false,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("base64encode(%#v)", test.String), func(t *testing.T) {
got, err := Base64Encode(test.String)
if test.Err {
if err == nil {
t.Fatal("succeeded; want error")
}
return
2018-05-23 11:38:12 -05:00
} else if err != nil {
t.Fatalf("unexpected error: %s", err)
2018-05-23 09:27:28 -05:00
}
if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}
func TestBase64Gzip(t *testing.T) {
tests := []struct {
String cty.Value
Want cty.Value
Err bool
}{
{
cty.StringVal("test"),
cty.StringVal("H4sIAAAAAAAA/ypJLS4BAAAA//8BAAD//wx+f9gEAAAA"),
false,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("base64gzip(%#v)", test.String), func(t *testing.T) {
got, err := Base64Gzip(test.String)
if test.Err {
if err == nil {
t.Fatal("succeeded; want error")
}
return
2018-05-23 11:38:12 -05:00
} else if err != nil {
t.Fatalf("unexpected error: %s", err)
2018-05-23 09:27:28 -05:00
}
if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}
func TestURLEncode(t *testing.T) {
tests := []struct {
String cty.Value
Want cty.Value
Err bool
}{
{
cty.StringVal("abc123-_"),
cty.StringVal("abc123-_"),
false,
},
{
cty.StringVal("foo:bar@localhost?foo=bar&bar=baz"),
cty.StringVal("foo%3Abar%40localhost%3Ffoo%3Dbar%26bar%3Dbaz"),
false,
},
{
cty.StringVal("mailto:email?subject=this+is+my+subject"),
cty.StringVal("mailto%3Aemail%3Fsubject%3Dthis%2Bis%2Bmy%2Bsubject"),
false,
},
{
cty.StringVal("foo/bar"),
cty.StringVal("foo%2Fbar"),
false,
},
}
for _, test := range tests {
t.Run(fmt.Sprintf("urlencode(%#v)", test.String), func(t *testing.T) {
got, err := URLEncode(test.String)
if test.Err {
if err == nil {
t.Fatal("succeeded; want error")
}
return
2018-05-23 11:38:12 -05:00
} else if err != nil {
t.Fatalf("unexpected error: %s", err)
2018-05-23 09:27:28 -05:00
}
if !got.RawEquals(test.Want) {
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
}
})
}
}