lotus/conformance/chaos/actor_test.go

276 lines
6.5 KiB
Go
Raw Normal View History

2020-09-10 13:22:25 +00:00
package chaos
import (
"context"
2020-09-10 13:30:52 +00:00
"testing"
2020-09-15 13:10:20 +00:00
"github.com/filecoin-project/go-address"
2020-09-11 14:25:59 +00:00
"github.com/filecoin-project/go-state-types/abi"
2020-09-10 13:22:25 +00:00
"github.com/filecoin-project/go-state-types/exitcode"
2020-09-15 13:10:20 +00:00
"github.com/ipfs/go-cid"
2020-10-08 01:09:33 +00:00
builtin2 "github.com/filecoin-project/specs-actors/v2/actors/builtin"
mock2 "github.com/filecoin-project/specs-actors/v2/support/mock"
atesting2 "github.com/filecoin-project/specs-actors/v2/support/testing"
2020-09-10 13:22:25 +00:00
)
func TestSingleton(t *testing.T) {
2020-10-08 01:09:33 +00:00
receiver := atesting2.NewIDAddr(t, 100)
builder := mock2.NewBuilder(context.Background(), receiver)
2020-09-10 13:22:25 +00:00
rt := builder.Build(t)
2020-09-10 13:51:30 +00:00
var a Actor
2020-09-10 13:22:25 +00:00
msg := "constructor should not be called; the Chaos actor is a singleton actor"
rt.ExpectAssertionFailure(msg, func() {
2020-09-11 14:25:59 +00:00
rt.Call(a.Constructor, abi.Empty)
2020-09-10 13:22:25 +00:00
})
rt.Verify()
}
func TestCallerValidationNone(t *testing.T) {
2020-10-08 01:09:33 +00:00
receiver := atesting2.NewIDAddr(t, 100)
builder := mock2.NewBuilder(context.Background(), receiver)
rt := builder.Build(t)
var a Actor
rt.Call(a.CallerValidation, &CallerValidationArgs{Branch: CallerValidationBranchNone})
rt.Verify()
}
func TestCallerValidationIs(t *testing.T) {
2020-10-08 01:09:33 +00:00
caller := atesting2.NewIDAddr(t, 100)
receiver := atesting2.NewIDAddr(t, 101)
builder := mock2.NewBuilder(context.Background(), receiver)
rt := builder.Build(t)
2020-10-08 01:09:33 +00:00
rt.SetCaller(caller, builtin2.AccountActorCodeID)
var a Actor
2020-10-08 01:09:33 +00:00
caddrs := []address.Address{atesting2.NewIDAddr(t, 101)}
rt.ExpectValidateCallerAddr(caddrs...)
2020-10-08 01:09:33 +00:00
// fixed in: https://github.com/filecoin-project/specs-actors/pull/1155
rt.ExpectAbort(exitcode.SysErrForbidden, func() {
rt.Call(a.CallerValidation, &CallerValidationArgs{
2020-09-15 14:47:11 +00:00
Branch: CallerValidationBranchIsAddress,
Addrs: caddrs,
})
})
rt.Verify()
rt.ExpectValidateCallerAddr(caller)
rt.Call(a.CallerValidation, &CallerValidationArgs{
2020-09-15 14:47:11 +00:00
Branch: CallerValidationBranchIsAddress,
Addrs: []address.Address{caller},
})
rt.Verify()
}
func TestCallerValidationType(t *testing.T) {
2020-10-08 01:09:33 +00:00
caller := atesting2.NewIDAddr(t, 100)
receiver := atesting2.NewIDAddr(t, 101)
builder := mock2.NewBuilder(context.Background(), receiver)
rt := builder.Build(t)
2020-10-08 01:09:33 +00:00
rt.SetCaller(caller, builtin2.AccountActorCodeID)
var a Actor
2020-10-08 01:09:33 +00:00
rt.ExpectValidateCallerType(builtin2.CronActorCodeID)
rt.ExpectAbort(exitcode.SysErrForbidden, func() {
rt.Call(a.CallerValidation, &CallerValidationArgs{
2020-09-15 14:47:11 +00:00
Branch: CallerValidationBranchIsType,
2020-10-08 01:09:33 +00:00
Types: []cid.Cid{builtin2.CronActorCodeID},
})
})
rt.Verify()
2020-10-08 01:09:33 +00:00
rt.ExpectValidateCallerType(builtin2.AccountActorCodeID)
rt.Call(a.CallerValidation, &CallerValidationArgs{
2020-09-15 14:47:11 +00:00
Branch: CallerValidationBranchIsType,
2020-10-08 01:09:33 +00:00
Types: []cid.Cid{builtin2.AccountActorCodeID},
})
rt.Verify()
}
func TestCallerValidationInvalidBranch(t *testing.T) {
2020-10-08 01:09:33 +00:00
receiver := atesting2.NewIDAddr(t, 100)
builder := mock2.NewBuilder(context.Background(), receiver)
rt := builder.Build(t)
var a Actor
rt.ExpectAssertionFailure("invalid branch passed to CallerValidation", func() {
rt.Call(a.CallerValidation, &CallerValidationArgs{Branch: -1})
})
rt.Verify()
}
2020-09-10 13:22:25 +00:00
func TestDeleteActor(t *testing.T) {
2020-10-08 01:09:33 +00:00
receiver := atesting2.NewIDAddr(t, 100)
beneficiary := atesting2.NewIDAddr(t, 101)
builder := mock2.NewBuilder(context.Background(), receiver)
2020-09-10 13:22:25 +00:00
rt := builder.Build(t)
2020-09-10 13:51:30 +00:00
var a Actor
2020-09-10 13:22:25 +00:00
rt.ExpectValidateCallerAny()
rt.ExpectDeleteActor(beneficiary)
rt.Call(a.DeleteActor, &beneficiary)
rt.Verify()
}
func TestMutateStateInTransaction(t *testing.T) {
2020-10-08 01:09:33 +00:00
receiver := atesting2.NewIDAddr(t, 100)
builder := mock2.NewBuilder(context.Background(), receiver)
2020-09-10 13:22:25 +00:00
rt := builder.Build(t)
2020-09-10 13:51:30 +00:00
var a Actor
2020-09-10 13:22:25 +00:00
rt.ExpectValidateCallerAny()
rt.StateCreate(&State{})
2020-09-10 13:22:25 +00:00
val := "__mutstat test"
rt.Call(a.MutateState, &MutateStateArgs{
Value: val,
Branch: MutateInTransaction,
})
var st State
rt.GetState(&st)
if st.Value != val {
t.Fatal("state was not updated")
}
rt.Verify()
}
func TestMutateStateAfterTransaction(t *testing.T) {
2020-10-08 01:09:33 +00:00
receiver := atesting2.NewIDAddr(t, 100)
builder := mock2.NewBuilder(context.Background(), receiver)
2020-09-10 13:22:25 +00:00
rt := builder.Build(t)
2020-09-10 13:51:30 +00:00
var a Actor
2020-09-10 13:22:25 +00:00
rt.ExpectValidateCallerAny()
rt.StateCreate(&State{})
2020-09-10 13:22:25 +00:00
val := "__mutstat test"
rt.Call(a.MutateState, &MutateStateArgs{
Value: val,
Branch: MutateAfterTransaction,
})
var st State
rt.GetState(&st)
// state should be updated successfully _in_ the transaction but not outside
if st.Value != val+"-in" {
t.Fatal("state was not updated")
}
rt.Verify()
}
func TestMutateStateReadonly(t *testing.T) {
2020-10-08 01:09:33 +00:00
receiver := atesting2.NewIDAddr(t, 100)
builder := mock2.NewBuilder(context.Background(), receiver)
2020-09-10 13:22:25 +00:00
rt := builder.Build(t)
2020-09-10 13:51:30 +00:00
var a Actor
2020-09-10 13:22:25 +00:00
rt.ExpectValidateCallerAny()
rt.StateCreate(&State{})
2020-09-10 13:22:25 +00:00
val := "__mutstat test"
rt.Call(a.MutateState, &MutateStateArgs{
Value: val,
Branch: MutateReadonly,
})
var st State
rt.GetState(&st)
if st.Value != "" {
t.Fatal("state was not expected to be updated")
}
rt.Verify()
}
func TestMutateStateInvalidBranch(t *testing.T) {
2020-10-08 01:09:33 +00:00
receiver := atesting2.NewIDAddr(t, 100)
builder := mock2.NewBuilder(context.Background(), receiver)
rt := builder.Build(t)
var a Actor
rt.ExpectValidateCallerAny()
rt.ExpectAssertionFailure("unknown mutation type", func() {
rt.Call(a.MutateState, &MutateStateArgs{Branch: -1})
})
rt.Verify()
}
2020-09-10 13:22:25 +00:00
func TestAbortWith(t *testing.T) {
2020-10-08 01:09:33 +00:00
receiver := atesting2.NewIDAddr(t, 100)
builder := mock2.NewBuilder(context.Background(), receiver)
2020-09-10 13:22:25 +00:00
rt := builder.Build(t)
2020-09-10 13:51:30 +00:00
var a Actor
2020-09-10 13:22:25 +00:00
msg := "__test forbidden"
rt.ExpectAbortContainsMessage(exitcode.ErrForbidden, msg, func() {
rt.Call(a.AbortWith, &AbortWithArgs{
Code: exitcode.ErrForbidden,
Message: msg,
Uncontrolled: false,
})
})
rt.Verify()
}
func TestAbortWithUncontrolled(t *testing.T) {
2020-10-08 01:09:33 +00:00
receiver := atesting2.NewIDAddr(t, 100)
builder := mock2.NewBuilder(context.Background(), receiver)
2020-09-10 13:22:25 +00:00
rt := builder.Build(t)
2020-09-10 13:51:30 +00:00
var a Actor
2020-09-10 13:22:25 +00:00
msg := "__test uncontrolled panic"
rt.ExpectAssertionFailure(msg, func() {
rt.Call(a.AbortWith, &AbortWithArgs{
Message: msg,
Uncontrolled: true,
})
})
rt.Verify()
}
func TestInspectRuntime(t *testing.T) {
2020-10-08 01:09:33 +00:00
caller := atesting2.NewIDAddr(t, 100)
receiver := atesting2.NewIDAddr(t, 101)
builder := mock2.NewBuilder(context.Background(), receiver)
rt := builder.Build(t)
2020-10-08 01:09:33 +00:00
rt.SetCaller(caller, builtin2.AccountActorCodeID)
rt.StateCreate(&State{})
var a Actor
rt.ExpectValidateCallerAny()
ret := rt.Call(a.InspectRuntime, abi.Empty)
rtr, ok := ret.(*InspectRuntimeReturn)
if !ok {
t.Fatal("invalid return value")
}
if rtr.Caller != caller {
t.Fatal("unexpected runtime caller")
}
if rtr.Receiver != receiver {
t.Fatal("unexpected runtime receiver")
}
rt.Verify()
}