solana-go/nativetype_test.go

281 lines
5.3 KiB
Go
Raw Normal View History

// Copyright 2021 github.com/gagliardetto
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2020-11-24 11:39:33 -08:00
package solana
import (
2021-07-10 05:44:20 -07:00
"strconv"
"testing"
2020-11-24 11:39:33 -08:00
2021-07-10 05:44:20 -07:00
"github.com/mr-tron/base58"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
2020-11-24 11:39:33 -08:00
2021-07-10 05:44:20 -07:00
func TestMustHashFromBase58(t *testing.T) {
require.Panics(t, func() {
MustHashFromBase58("toto")
})
}
func TestHashFromBase58(t *testing.T) {
in := "uoEAQCWCKjV9ecsBvngctJ7upNBZX7hpN4SfdR6TaUz"
out, err := HashFromBase58(in)
assert.NoError(t, err)
assert.Equal(t, in, out.String())
jsonOut, err := out.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, strconv.Quote(in), string(jsonOut))
var ha Hash
assert.True(t, ha.IsZero())
err = ha.UnmarshalJSON(jsonOut)
assert.NoError(t, err)
assert.Equal(t, out, ha)
assert.True(t, out.Equals(ha))
assert.False(t, out.Equals(Hash{}))
assert.False(t, out.IsZero())
}
func TestSignatureFromBase58(t *testing.T) {
in := "gD3jeeaPNiyuJvTKXNEv1gntazWEkvpocofEmrz2rL6Fi4prWSsBH6a9SrwyZEatAozyMsnK2fnk3APXNFxD2Mq"
out, err := SignatureFromBase58(in)
assert.NoError(t, err)
assert.Equal(t, in, out.String())
jsonOut, err := out.MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, strconv.Quote(in), string(jsonOut))
var sig Signature
assert.True(t, sig.IsZero())
err = sig.UnmarshalJSON(jsonOut)
assert.NoError(t, err)
assert.Equal(t, out, sig)
assert.True(t, out.Equals(sig))
assert.False(t, out.Equals(Signature{}))
assert.False(t, out.IsZero())
}
func TestMustSignatureFromBase58(t *testing.T) {
require.Panics(t, func() {
MustSignatureFromBase58("toto")
})
}
func TestBase58(t *testing.T) {
in := "RYcCwZg97M2jet84ttG8"
out, err := base58.Decode(in)
assert.NoError(t, err)
assert.Equal(t, in, Base58(out).String())
jsonOut, err := Base58(out).MarshalJSON()
assert.NoError(t, err)
assert.Equal(t, strconv.Quote(in), string(jsonOut))
var b58 Base58
err = b58.UnmarshalJSON(jsonOut)
assert.NoError(t, err)
}
2021-07-20 08:29:06 -07:00
func TestData_base64(t *testing.T) {
2021-07-10 05:44:20 -07:00
val := "dGVzdA=="
in := `["` + val + `", "base64"]`
var data Data
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
2021-07-20 08:29:06 -07:00
assert.Equal(t,
[]byte("test"),
data.Content,
)
assert.Equal(t,
EncodingBase64,
data.Encoding,
)
assert.Equal(t,
[]interface{}{
val,
"base64",
},
mustJSONToInterface(mustAnyToJSON(data)),
)
}
func TestData_base64_empty(t *testing.T) {
val := ""
in := `["", "base64"]`
var data Data
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
[]byte(""),
data.Content,
)
assert.Equal(t,
EncodingBase64,
data.Encoding,
)
2021-07-10 05:44:20 -07:00
assert.Equal(t,
[]interface{}{
val,
"base64",
2021-07-10 05:44:20 -07:00
},
mustJSONToInterface(mustAnyToJSON(data)),
)
}
2021-07-20 08:29:06 -07:00
func TestData_base64_zstd(t *testing.T) {
val := "KLUv/QQAWQAAaGVsbG8td29ybGTcLcaB"
in := `["` + val + `", "base64+zstd"]`
var data Data
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
[]byte("hello-world"),
data.Content,
)
assert.Equal(t,
EncodingBase64Zstd,
data.Encoding,
)
assert.Equal(t,
[]interface{}{
val,
"base64+zstd",
},
mustJSONToInterface(mustAnyToJSON(data)),
)
}
func TestData_base64_zstd_empty(t *testing.T) {
in := `["", "base64+zstd"]`
var data Data
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
[]byte(""),
data.Content,
)
assert.Equal(t,
EncodingBase64Zstd,
data.Encoding,
)
assert.Equal(t,
[]interface{}{
"",
"base64+zstd",
},
mustJSONToInterface(mustAnyToJSON(data)),
)
}
func TestData_base58(t *testing.T) {
val := "3yZe7d"
in := `["` + val + `", "base58"]`
var data Data
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
[]byte("test"),
data.Content,
)
assert.Equal(t,
EncodingBase58,
data.Encoding,
)
assert.Equal(t,
[]interface{}{
val,
"base58",
},
mustJSONToInterface(mustAnyToJSON(data)),
)
}
func TestData_base58_empty(t *testing.T) {
val := ""
in := `["", "base58"]`
var data Data
err := data.UnmarshalJSON([]byte(in))
assert.NoError(t, err)
assert.Equal(t,
[]byte(""),
data.Content,
)
assert.Equal(t,
EncodingBase58,
data.Encoding,
)
assert.Equal(t,
[]interface{}{
val,
"base58",
},
mustJSONToInterface(mustAnyToJSON(data)),
)
}
2021-07-10 05:44:20 -07:00
// mustAnyToJSON marshals the provided variable
// to JSON bytes.
func mustAnyToJSON(raw interface{}) []byte {
out, err := json.Marshal(raw)
if err != nil {
panic(err)
}
return out
}
// mustJSONToInterface unmarshals the provided JSON bytes
// into an `interface{}` type variable, and returns it.
func mustJSONToInterface(rawJSON []byte) interface{} {
var out interface{}
err := json.Unmarshal(rawJSON, &out)
if err != nil {
panic(err)
}
return out
}
func TestMustPublicKeyFromBase58(t *testing.T) {
require.Panics(t, func() {
MustPublicKeyFromBase58("toto")
})
2020-11-24 11:39:33 -08:00
}