Skip to content

Commit

Permalink
[Testing staking_verifier] Fix the error of CV5 ~ CV8 (#2377)
Browse files Browse the repository at this point in the history
* Fix the error of CV5 ~ CV8

* Use ctxerror to rebuild unit test: CV5 ~ CV8
  • Loading branch information
fxfactorial authored Mar 5, 2020
1 parent a53d8d3 commit 51b3e1b
Showing 1 changed file with 49 additions and 17 deletions.
66 changes: 49 additions & 17 deletions core/staking_verifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,37 +100,61 @@ func TestCV3(t *testing.T) {
func TestCV5(t *testing.T) {
statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
msg := createValidator()
statedb.AddBalance(msg.ValidatorAddress, big.NewInt(5e18))
// identity length: 200 characters
msg.Identity = "adsfwryuiwfhwifbwfbcerghveugbviuscbhwiefbcusidbcifwefhgciwefherhbfiwuehfciwiuebfcuyiewfhwieufwiweifhcwefhwefhwiewwerfhuwefiuewfhuewhfiuewhfefhshfrhfhifhwbfvberhbvihfwuoefhusioehfeuwiafhaiobcfwfhceirui"
statedb.AddBalance(msg.ValidatorAddress, big.NewInt(5e18))
identitylengthError := ctxerror.New("[EnsureLength] Exceed Maximum Length", "have", len(msg.Identity), "maxIdentityLen", staking.MaxIdentityLength)
if _, err := VerifyAndCreateValidatorFromMsg(statedb, big.NewInt(0), big.NewInt(0), msg); err.Error() != identitylengthError.Error() {
t.Error("expected", identitylengthError, "got", err)
identitylengthCtxError := ctxerror.New("[EnsureLength] Exceed Maximum Length", "have", len(msg.Identity), "maxIdentityLen", staking.MaxIdentityLength)
_, err := VerifyAndCreateValidatorFromMsg(statedb, big.NewInt(0), big.NewInt(0), msg)
if err == nil {
t.Errorf("expected non null error")
}
ctxerr, ok := err.(ctxerror.CtxError)
if !ok {
t.Errorf("expected context aware error")
}
if ctxerr.Message() != identitylengthCtxError.Message() {
t.Error("expected", identitylengthCtxError, "got", err)
}
}

// Test CV6: website longer than 140 characters
func TestCV6(t *testing.T) {
statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
msg := createValidator()
statedb.AddBalance(msg.ValidatorAddress, big.NewInt(5e18))
// Website length: 200 characters
msg.Website = "https://www.iwfhwifbwfbcerghveugbviuscbhwiefbcusidbcifwefhgciwefherhbfiwuehfciwiuebfcuyiewfhwieufwiweifhcwefhwefhwiewwerfhuwefiuewfwwwwwfiuewhfefhshfrheterhbvihfwuoefhusioehfeuwiafhaiobcfwfhceirui.com"
statedb.AddBalance(msg.ValidatorAddress, big.NewInt(5e18))
websiteLengthError := ctxerror.New("[EnsureLength] Exceed Maximum Length", "have", len(msg.Website), "maxWebsiteLen", staking.MaxWebsiteLength)
if _, err := VerifyAndCreateValidatorFromMsg(statedb, big.NewInt(0), big.NewInt(0), msg); err.Error() != websiteLengthError.Error() {
t.Error("expected", websiteLengthError, "got", err)
websiteLengthCtxError := ctxerror.New("[EnsureLength] Exceed Maximum Length", "have", len(msg.Website), "maxWebsiteLen", staking.MaxWebsiteLength)
_, err := VerifyAndCreateValidatorFromMsg(statedb, big.NewInt(0), big.NewInt(0), msg)
if err == nil {
t.Errorf("expected non null error")
}
ctxerr, ok := err.(ctxerror.CtxError)
if !ok {
t.Errorf("expected context aware error")
}
if ctxerr.Message() != websiteLengthCtxError.Message() {
t.Error("expected", websiteLengthCtxError, "got", err)
}
}

// Test CV7: security contact longer than 140 characters
func TestCV7(t *testing.T) {
statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
msg := createValidator()
// Website length: 200 characters
msg.SecurityContact = "HelloiwfhwifbwfbcerghveugbviuscbhwiefbcusidbcifwefhgciwefherhbfiwuehfciwiuebfcuyiewfhwieufwiweifhcwefhwefhwiewwerfhuwefiuewfwwwwwfiuewhfefhshfrheterhbvihfwuoefhusioehfeuwiafhaiobcfwfhceiruiHellodfdfdf"
statedb.AddBalance(msg.ValidatorAddress, big.NewInt(5e18))
securityContactLengthError := ctxerror.New("[EnsureLength] Exceed Maximum Length", "have", len(msg.SecurityContact), "maxSecurityContactLen", staking.MaxWebsiteLength)
if _, err := VerifyAndCreateValidatorFromMsg(statedb, big.NewInt(0), big.NewInt(0), msg); err.Error() != securityContactLengthError.Error() {
// Security Contact length: 200 characters
msg.SecurityContact = "HelloiwfhwifbwfbcerghveugbviuscbhwiefbcusidbcifwefhgciwefherhbfiwuehfciwiuebfcuyiewfhwieufwiweifhcwefhwefhwiewwerfhuwefiuewfwwwwwfiuewhfefhshfrheterhbvihfwuoefhusioehfeuwiafhaiobcfwfhceiruiHellodfdfdf"
securityContactLengthError := ctxerror.New("[EnsureLength] Exceed Maximum Length", "have", len(msg.SecurityContact), "maxSecurityContactLen", staking.MaxSecurityContactLength)
_, err := VerifyAndCreateValidatorFromMsg(statedb, big.NewInt(0), big.NewInt(0), msg)
if err == nil {
t.Errorf("expected non null error")
}
ctxerr, ok := err.(ctxerror.CtxError)
if !ok {
t.Errorf("expected context aware error")
}
if ctxerr.Message() != securityContactLengthError.Message() {
t.Error("expected", securityContactLengthError, "got", err)
}
}
Expand All @@ -139,12 +163,20 @@ func TestCV7(t *testing.T) {
func TestCV8(t *testing.T) {
statedb, _ := state.New(common.Hash{}, state.NewDatabase(ethdb.NewMemDatabase()))
msg := createValidator()
// Website length: 300 characters
msg.Details = "HelloiwfhwifbwfbcerghveugbviuscbhwiefbcusidbcifwefhgciwefherhbfiwuehfciwiuebfcuyiewfhwieufwiweifhcwefhwefhwiewwerfhuwefiuewfwwwwwfiuewhfefhshfrheterhbvihfwuoefhusioehfeuwiafhaiobcfwfhceiruiHellodfdfdfjiusngognoherugbounviesrbgufhuoshcofwevusguahferhgvuervurehniwjvseivusehvsghjvorsugjvsiovjpsevsvvvvv"
statedb.AddBalance(msg.ValidatorAddress, big.NewInt(5e18))
detailsLenError := ctxerror.New("[EnsureLength] Exceed Maximum Length", "have", len(msg.Details), "maxDetailsLen", staking.MaxDetailsLength)
if _, err := VerifyAndCreateValidatorFromMsg(statedb, big.NewInt(0), big.NewInt(0), msg); err.Error() != detailsLenError.Error() {
t.Error("expected", detailsLenError, "got", err)
// Details length: 300 characters
msg.Details = "HelloiwfhwifbwfbcerghveugbviuscbhwiefbcusidbcifwefhgciwefherhbfiwuehfciwiuebfcuyiewfhwieufwiweifhcwefhwefhwiewwerfhuwefiuewfwwwwwfiuewhfefhshfrheterhbvihfwuoefhusioehfeuwiafhaiobcfwfhceiruiHellodfdfdfjiusngognoherugbounviesrbgufhuoshcofwevusguahferhgvuervurehniwjvseivusehvsghjvorsugjvsiovjpsevsvvvvv"
detailsLenCtxError := ctxerror.New("[EnsureLength] Exceed Maximum Length", "have", len(msg.Details), "maxDetailsLen", staking.MaxDetailsLength)
_, err := VerifyAndCreateValidatorFromMsg(statedb, big.NewInt(0), big.NewInt(0), msg)
if err == nil {
t.Errorf("Expected non null error")
}
ctxerr, ok := err.(ctxerror.CtxError)
if !ok {
t.Errorf("expected context aware error")
}
if ctxerr.Message() != detailsLenCtxError.Message() {
t.Error("expected", detailsLenCtxError, "got", err)
}
}

Expand Down

0 comments on commit 51b3e1b

Please sign in to comment.