lbcd/txscript/nameclaim_test.go
Roy Lee 9caab1e2e1 [lbry] txscript: initial porting of claim script
Co-authored-by: Brannon King <countprimes@gmail.com>
2021-08-19 14:19:20 -04:00

87 lines
2.1 KiB
Go

package txscript
import (
"testing"
"github.com/stretchr/testify/require"
)
func TestCreationParseLoopClaim(t *testing.T) {
r := require.New(t)
claim, err := ClaimNameScript("tester", "value")
r.NoError(err)
parsed, err := parseScript(claim)
r.NoError(err)
r.True(isClaimName(parsed))
r.False(isSupportClaim(parsed))
r.False(isUpdateClaim(parsed))
script, err := DecodeClaimScript(claim)
r.NoError(err)
r.Equal([]byte("tester"), script.Name())
r.Equal([]byte("value"), script.Value())
}
func TestCreationParseLoopUpdate(t *testing.T) {
r := require.New(t)
claimID := []byte("12345123451234512345")
claim, err := UpdateClaimScript("tester", claimID, "value")
r.NoError(err)
parsed, err := parseScript(claim)
r.NoError(err)
r.False(isSupportClaim(parsed))
r.False(isClaimName(parsed))
r.True(isUpdateClaim(parsed))
script, err := DecodeClaimScript(claim)
r.NoError(err)
r.Equal([]byte("tester"), script.Name())
r.Equal(claimID, script.ClaimID())
r.Equal([]byte("value"), script.Value())
}
func TestCreationParseLoopSupport(t *testing.T) {
r := require.New(t)
claimID := []byte("12345123451234512345")
claim, err := SupportClaimScript("tester", claimID, []byte("value"))
r.NoError(err)
parsed, err := parseScript(claim)
r.NoError(err)
r.True(isSupportClaim(parsed))
r.False(isClaimName(parsed))
r.False(isUpdateClaim(parsed))
script, err := DecodeClaimScript(claim)
r.NoError(err)
r.Equal([]byte("tester"), script.Name())
r.Equal(claimID, script.ClaimID())
r.Equal([]byte("value"), script.Value())
claim, err = SupportClaimScript("tester", claimID, nil)
r.NoError(err)
script, err = DecodeClaimScript(claim)
r.NoError(err)
r.Equal([]byte("tester"), script.Name())
r.Equal(claimID, script.ClaimID())
r.Nil(script.Value())
}
func TestInvalidChars(t *testing.T) {
r := require.New(t)
script, err := ClaimNameScript("tester", "value")
r.NoError(err)
r.NoError(AllClaimsAreSane(script, true))
for i := range []byte(illegalChars) {
script, err := ClaimNameScript("a"+illegalChars[i:i+1], "value")
r.NoError(err)
r.Error(AllClaimsAreSane(script, true))
}
}