2018-05-22 02:39:26 +02:00
|
|
|
package funcs
|
|
|
|
|
|
|
|
import (
|
2018-05-23 18:16:42 +02:00
|
|
|
"fmt"
|
2018-05-22 02:39:26 +02:00
|
|
|
"testing"
|
2018-05-23 18:16:42 +02:00
|
|
|
|
|
|
|
"github.com/zclconf/go-cty/cty"
|
|
|
|
"golang.org/x/crypto/bcrypt"
|
2018-05-22 02:39:26 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestUUID(t *testing.T) {
|
|
|
|
result, err := UUID()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resultStr := result.AsString()
|
|
|
|
if got, want := len(resultStr), 36; got != want {
|
|
|
|
t.Errorf("wrong result length %d; want %d", got, want)
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 18:16:42 +02:00
|
|
|
|
|
|
|
func TestBase64Sha256(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
String cty.Value
|
|
|
|
Want cty.Value
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cty.StringVal("test"),
|
|
|
|
cty.StringVal("n4bQgYhMfWWaL+qgxVrQFaO/TxsrC4Is0V1sFbDwCgg="),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
// This would differ because we're base64-encoding hex represantiation, not raw bytes.
|
|
|
|
// base64encode(sha256("test")) =
|
|
|
|
// "OWY4NmQwODE4ODRjN2Q2NTlhMmZlYWEwYzU1YWQwMTVhM2JmNGYxYjJiMGI4MjJjZDE1ZDZjMTViMGYwMGEwOA=="
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("base64sha256(%#v)", test.String), func(t *testing.T) {
|
|
|
|
got, err := Base64Sha256(test.String)
|
|
|
|
|
|
|
|
if test.Err {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("succeeded; want error")
|
|
|
|
}
|
|
|
|
return
|
2018-05-23 18:38:12 +02:00
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
2018-05-23 18:16:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !got.RawEquals(test.Want) {
|
|
|
|
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBase64Sha512(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
String cty.Value
|
|
|
|
Want cty.Value
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cty.StringVal("test"),
|
|
|
|
cty.StringVal("7iaw3Ur350mqGo7jwQrpkj9hiYB3Lkc/iBml1JQODbJ6wYX4oOHV+E+IvIh/1nsUNzLDBMxfqa2Ob1f1ACio/w=="),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
// This would differ because we're base64-encoding hex represantiation, not raw bytes
|
|
|
|
// base64encode(sha512("test")) =
|
|
|
|
// "OZWUyNmIwZGQ0YWY3ZTc0OWFhMWE4ZWUzYzEwYWU5OTIzZjYxODk4MDc3MmU0NzNmODgxOWE1ZDQ5NDBlMGRiMjdhYzE4NWY4YTBlMWQ1Zjg0Zjg4YmM4ODdmZDY3YjE0MzczMmMzMDRjYzVmYTlhZDhlNmY1N2Y1MDAyOGE4ZmY="
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("base64sha512(%#v)", test.String), func(t *testing.T) {
|
|
|
|
got, err := Base64Sha512(test.String)
|
|
|
|
|
|
|
|
if test.Err {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("succeeded; want error")
|
|
|
|
}
|
|
|
|
return
|
2018-05-23 18:38:12 +02:00
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
2018-05-23 18:16:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if !got.RawEquals(test.Want) {
|
|
|
|
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBcrypt(t *testing.T) {
|
|
|
|
// single variable test
|
|
|
|
p, err := Bcrypt(cty.StringVal("test"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(p.AsString()), []byte("test"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error comparing hash and password: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// testing with two parameters
|
|
|
|
p, err = Bcrypt(cty.StringVal("test"), cty.NumberIntVal(5))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = bcrypt.CompareHashAndPassword([]byte(p.AsString()), []byte("test"))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Error comparing hash and password: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Negative test for more than two parameters
|
|
|
|
_, err = Bcrypt(cty.StringVal("test"), cty.NumberIntVal(10), cty.NumberIntVal(11))
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("succeeded; want error")
|
|
|
|
}
|
|
|
|
}
|
2018-05-23 18:38:12 +02:00
|
|
|
|
|
|
|
func TestMd5(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
String cty.Value
|
|
|
|
Want cty.Value
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cty.StringVal("tada"),
|
|
|
|
cty.StringVal("ce47d07243bb6eaf5e1322c81baf9bbf"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{ // Confirm that we're not trimming any whitespaces
|
|
|
|
cty.StringVal(" tada "),
|
|
|
|
cty.StringVal("aadf191a583e53062de2d02c008141c4"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
{ // We accept empty string too
|
|
|
|
cty.StringVal(""),
|
|
|
|
cty.StringVal("d41d8cd98f00b204e9800998ecf8427e"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("md5(%#v)", test.String), func(t *testing.T) {
|
|
|
|
got, err := Md5(test.String)
|
|
|
|
|
|
|
|
if test.Err {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("succeeded; want error")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !got.RawEquals(test.Want) {
|
|
|
|
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRsaDecrypt(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
Ciphertext cty.Value
|
|
|
|
Privatekey cty.Value
|
|
|
|
Want cty.Value
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
// Base-64 encoded cipher decrypts correctly
|
|
|
|
{
|
|
|
|
cty.StringVal(CipherBase64),
|
|
|
|
cty.StringVal(PrivateKey),
|
|
|
|
cty.StringVal("message"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
// Wrong key
|
|
|
|
{
|
|
|
|
cty.StringVal(CipherBase64),
|
|
|
|
cty.StringVal(WrongPrivateKey),
|
|
|
|
cty.UnknownVal(cty.String),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
// Bad key
|
|
|
|
{
|
|
|
|
cty.StringVal(CipherBase64),
|
|
|
|
cty.StringVal("bad key"),
|
|
|
|
cty.UnknownVal(cty.String),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
// Empty key
|
|
|
|
{
|
|
|
|
cty.StringVal(CipherBase64),
|
|
|
|
cty.StringVal(""),
|
|
|
|
cty.UnknownVal(cty.String),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
// Bad cipher
|
|
|
|
{
|
|
|
|
cty.StringVal("bad cipher"),
|
|
|
|
cty.StringVal(PrivateKey),
|
|
|
|
cty.UnknownVal(cty.String),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
// Empty cipher
|
|
|
|
{
|
|
|
|
cty.StringVal(""),
|
|
|
|
cty.StringVal(PrivateKey),
|
|
|
|
cty.UnknownVal(cty.String),
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("RsaDecrypt(%#v, %#v)", test.Ciphertext, test.Privatekey), func(t *testing.T) {
|
|
|
|
got, err := RsaDecrypt(test.Ciphertext, test.Privatekey)
|
|
|
|
|
|
|
|
if test.Err {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("succeeded; want error")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !got.RawEquals(test.Want) {
|
|
|
|
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSha1(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
String cty.Value
|
|
|
|
Want cty.Value
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cty.StringVal("test"),
|
|
|
|
cty.StringVal("a94a8fe5ccb19ba61c4c0873d391e987982fbbd3"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("sha1(%#v)", test.String), func(t *testing.T) {
|
|
|
|
got, err := Sha1(test.String)
|
|
|
|
|
|
|
|
if test.Err {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("succeeded; want error")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !got.RawEquals(test.Want) {
|
|
|
|
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 21:11:25 +02:00
|
|
|
func TestSha256(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
String cty.Value
|
|
|
|
Want cty.Value
|
|
|
|
Err bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
cty.StringVal("test"),
|
|
|
|
cty.StringVal("9f86d081884c7d659a2feaa0c55ad015a3bf4f1b2b0b822cd15d6c15b0f00a08"),
|
|
|
|
false,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, test := range tests {
|
|
|
|
t.Run(fmt.Sprintf("sha256(%#v)", test.String), func(t *testing.T) {
|
|
|
|
got, err := Sha256(test.String)
|
|
|
|
|
|
|
|
if test.Err {
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("succeeded; want error")
|
|
|
|
}
|
|
|
|
return
|
|
|
|
} else if err != nil {
|
|
|
|
t.Fatalf("unexpected error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !got.RawEquals(test.Want) {
|
|
|
|
t.Errorf("wrong result\ngot: %#v\nwant: %#v", got, test.Want)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-23 18:38:12 +02:00
|
|
|
const (
|
|
|
|
CipherBase64 = "eczGaDhXDbOFRZGhjx2etVzWbRqWDlmq0bvNt284JHVbwCgObiuyX9uV0LSAMY707IEgMkExJqXmsB4OWKxvB7epRB9G/3+F+pcrQpODlDuL9oDUAsa65zEpYF0Wbn7Oh7nrMQncyUPpyr9WUlALl0gRWytOA23S+y5joa4M34KFpawFgoqTu/2EEH4Xl1zo+0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