How to use Create method of vm Package

Best Syzkaller code snippet using vm.Create

create_chain_tx_test.go

Source:create_chain_tx_test.go Github

copy

Full Screen

...8 "github.com/ava-labs/avalanchego/utils/hashing"9 "github.com/ava-labs/avalanchego/vms/avm"10 "github.com/ava-labs/avalanchego/vms/secp256k1fx"11)12func TestUnsignedCreateChainTxVerify(t *testing.T) {13 vm, _ := defaultVM()14 vm.Ctx.Lock.Lock()15 defer func() {16 if err := vm.Shutdown(); err != nil {17 t.Fatal(err)18 }19 vm.Ctx.Lock.Unlock()20 }()21 type test struct {22 description string23 shouldErr bool24 subnetID ids.ID25 genesisData []byte26 vmID ids.ID27 fxIDs []ids.ID28 chainName string29 keys []*crypto.PrivateKeySECP256K1R30 setup func(*UnsignedCreateChainTx) *UnsignedCreateChainTx31 }32 tests := []test{33 {34 description: "tx is nil",35 shouldErr: true,36 subnetID: testSubnet1.ID(),37 genesisData: nil,38 vmID: avm.ID,39 fxIDs: nil,40 chainName: "yeet",41 keys: []*crypto.PrivateKeySECP256K1R{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},42 setup: func(*UnsignedCreateChainTx) *UnsignedCreateChainTx { return nil },43 },44 {45 description: "vm ID is empty",46 shouldErr: true,47 subnetID: testSubnet1.ID(),48 genesisData: nil,49 vmID: avm.ID,50 fxIDs: nil,51 chainName: "yeet",52 keys: []*crypto.PrivateKeySECP256K1R{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},53 setup: func(tx *UnsignedCreateChainTx) *UnsignedCreateChainTx { tx.VMID = ids.ID{}; return tx },54 },55 {56 description: "subnet ID is empty",57 shouldErr: true,58 subnetID: testSubnet1.ID(),59 genesisData: nil,60 vmID: avm.ID,61 fxIDs: nil,62 chainName: "yeet",63 keys: []*crypto.PrivateKeySECP256K1R{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},64 setup: func(tx *UnsignedCreateChainTx) *UnsignedCreateChainTx { tx.SubnetID = ids.ID{}; return tx },65 },66 {67 description: "subnet ID is platform chain's ID",68 shouldErr: true,69 subnetID: testSubnet1.ID(),70 genesisData: nil,71 vmID: avm.ID,72 fxIDs: nil,73 chainName: "yeet",74 keys: []*crypto.PrivateKeySECP256K1R{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},75 setup: func(tx *UnsignedCreateChainTx) *UnsignedCreateChainTx { tx.SubnetID = vm.Ctx.ChainID; return tx },76 },77 {78 description: "chain name is too long",79 shouldErr: true,80 subnetID: testSubnet1.ID(),81 genesisData: nil,82 vmID: avm.ID,83 fxIDs: nil,84 chainName: "yeet",85 keys: []*crypto.PrivateKeySECP256K1R{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},86 setup: func(tx *UnsignedCreateChainTx) *UnsignedCreateChainTx {87 tx.ChainName = string(make([]byte, maxNameLen+1))88 return tx89 },90 },91 {92 description: "chain name has invalid character",93 shouldErr: true,94 subnetID: testSubnet1.ID(),95 genesisData: nil,96 vmID: avm.ID,97 fxIDs: nil,98 chainName: "yeet",99 keys: []*crypto.PrivateKeySECP256K1R{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},100 setup: func(tx *UnsignedCreateChainTx) *UnsignedCreateChainTx {101 tx.ChainName = "⌘"102 return tx103 },104 },105 {106 description: "genesis data is too long",107 shouldErr: true,108 subnetID: testSubnet1.ID(),109 genesisData: nil,110 vmID: avm.ID,111 fxIDs: nil,112 chainName: "yeet",113 keys: []*crypto.PrivateKeySECP256K1R{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},114 setup: func(tx *UnsignedCreateChainTx) *UnsignedCreateChainTx {115 tx.GenesisData = make([]byte, maxGenesisLen+1)116 return tx117 },118 },119 }120 for _, test := range tests {121 tx, err := vm.newCreateChainTx(122 test.subnetID,123 test.genesisData,124 test.vmID,125 test.fxIDs,126 test.chainName,127 test.keys,128 ids.ShortEmpty, // change addr129 )130 if err != nil {131 t.Fatal(err)132 }133 tx.UnsignedTx.(*UnsignedCreateChainTx).syntacticallyVerified = false134 tx.UnsignedTx = test.setup(tx.UnsignedTx.(*UnsignedCreateChainTx))135 if err := tx.UnsignedTx.(*UnsignedCreateChainTx).Verify(vm.Ctx, vm.codec, vm.txFee, vm.Ctx.AVAXAssetID); err != nil && !test.shouldErr {136 t.Fatalf("test '%s' shouldn't have errored but got: %s", test.description, err)137 } else if err == nil && test.shouldErr {138 t.Fatalf("test '%s' didn't error but should have", test.description)139 }140 }141}142// Ensure SemanticVerify fails when there are not enough control sigs143func TestCreateChainTxInsufficientControlSigs(t *testing.T) {144 vm, _ := defaultVM()145 vm.Ctx.Lock.Lock()146 defer func() {147 if err := vm.Shutdown(); err != nil {148 t.Fatal(err)149 }150 vm.Ctx.Lock.Unlock()151 }()152 tx, err := vm.newCreateChainTx(153 testSubnet1.ID(),154 nil,155 avm.ID,156 nil,157 "chain name",158 []*crypto.PrivateKeySECP256K1R{keys[0], keys[1]},159 ids.ShortEmpty, // change addr160 )161 if err != nil {162 t.Fatal(err)163 }164 // Remove a signature165 tx.Creds[0].(*secp256k1fx.Credential).Sigs = tx.Creds[0].(*secp256k1fx.Credential).Sigs[1:]166 if _, err := tx.UnsignedTx.(UnsignedDecisionTx).SemanticVerify(vm, vm.DB, tx); err == nil {167 t.Fatal("should have errored because a sig is missing")168 }169}170// Ensure SemanticVerify fails when an incorrect control signature is given171func TestCreateChainTxWrongControlSig(t *testing.T) {172 vm, _ := defaultVM()173 vm.Ctx.Lock.Lock()174 defer func() {175 if err := vm.Shutdown(); err != nil {176 t.Fatal(err)177 }178 vm.Ctx.Lock.Unlock()179 }()180 tx, err := vm.newCreateChainTx( // create a tx181 testSubnet1.ID(),182 nil,183 avm.ID,184 nil,185 "chain name",186 []*crypto.PrivateKeySECP256K1R{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},187 ids.ShortEmpty, // change addr188 )189 if err != nil {190 t.Fatal(err)191 }192 // Generate new, random key to sign tx with193 factory := crypto.FactorySECP256K1R{}194 key, err := factory.NewPrivateKey()195 if err != nil {196 t.Fatal(err)197 }198 // Replace a valid signature with one from another key199 sig, err := key.SignHash(hashing.ComputeHash256(tx.UnsignedBytes()))200 if err != nil {201 t.Fatal(err)202 }203 copy(tx.Creds[0].(*secp256k1fx.Credential).Sigs[0][:], sig)204 if _, err = tx.UnsignedTx.(UnsignedDecisionTx).SemanticVerify(vm, vm.DB, tx); err == nil {205 t.Fatal("should have failed verification because a sig is invalid")206 }207}208// Ensure SemanticVerify fails when the Subnet the blockchain specifies as209// its validator set doesn't exist210func TestCreateChainTxNoSuchSubnet(t *testing.T) {211 vm, _ := defaultVM()212 vm.Ctx.Lock.Lock()213 defer func() {214 if err := vm.Shutdown(); err != nil {215 t.Fatal(err)216 }217 vm.Ctx.Lock.Unlock()218 }()219 tx, err := vm.newCreateChainTx(220 testSubnet1.ID(),221 nil,222 avm.ID,223 nil,224 "chain name",225 []*crypto.PrivateKeySECP256K1R{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},226 ids.ShortEmpty, // change addr227 )228 if err != nil {229 t.Fatal(err)230 }231 tx.UnsignedTx.(*UnsignedCreateChainTx).SubnetID = ids.GenerateTestID()232 if _, err := tx.UnsignedTx.(UnsignedDecisionTx).SemanticVerify(vm, vm.DB, tx); err == nil {233 t.Fatal("should have failed because subent doesn't exist")234 }235}236func TestCreateChainTxAlreadyExists(t *testing.T) {237 vm, _ := defaultVM()238 vm.Ctx.Lock.Lock()239 defer func() {240 if err := vm.Shutdown(); err != nil {241 t.Fatal(err)242 }243 vm.Ctx.Lock.Unlock()244 }()245 // create a tx246 tx, err := vm.newCreateChainTx(247 testSubnet1.ID(),248 nil,249 avm.ID,250 nil,251 "chain name",252 []*crypto.PrivateKeySECP256K1R{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},253 ids.ShortEmpty, // change addr254 )255 if err != nil {256 t.Fatal(err)257 }258 // put the chain in existing chain list259 if err := vm.putChains(vm.DB, []*Tx{tx}); err != nil {260 t.Fatal(err)261 }262 _, err = tx.UnsignedTx.(UnsignedDecisionTx).SemanticVerify(vm, vm.DB, tx)263 if err == nil {264 t.Fatalf("should have failed because the chain already exists")265 }266}267// Ensure valid tx passes semanticVerify268func TestCreateChainTxValid(t *testing.T) {269 vm, _ := defaultVM()270 vm.Ctx.Lock.Lock()271 defer func() {272 if err := vm.Shutdown(); err != nil {273 t.Fatal(err)274 }275 vm.Ctx.Lock.Unlock()276 }()277 // create a valid tx278 tx, err := vm.newCreateChainTx(279 testSubnet1.ID(),280 nil,281 avm.ID,282 nil,283 "chain name",284 []*crypto.PrivateKeySECP256K1R{testSubnet1ControlKeys[0], testSubnet1ControlKeys[1]},285 ids.ShortEmpty, // change addr286 )287 if err != nil {288 t.Fatal(err)289 }290 _, err = tx.UnsignedTx.(UnsignedDecisionTx).SemanticVerify(vm, vm.DB, tx)291 if err != nil {292 t.Fatalf("expected tx to pass verification but got error: %v", err)...

Full Screen

Full Screen

test_vm.go

Source:test_vm.go Github

copy

Full Screen

...13// TestVM is a test vm14type TestVM struct {15 T *testing.T16 CantInitialize, CantBootstrapping, CantBootstrapped,17 CantShutdown, CantCreateHandlers, CantCreateStaticHandlers,18 CantHealth bool19 InitializeF func(*snow.Context, database.Database, []byte, chan<- Message, []*Fx) error20 BootstrappingF, BootstrappedF, ShutdownF func() error21 CreateHandlersF func() map[string]*HTTPHandler22 CreateStaticHandlersF func() map[string]*HTTPHandler23 HealthF func() (interface{}, error)24}25// Default ...26func (vm *TestVM) Default(cant bool) {27 vm.CantInitialize = cant28 vm.CantBootstrapping = cant29 vm.CantBootstrapped = cant30 vm.CantShutdown = cant31 vm.CantCreateHandlers = cant32 vm.CantCreateStaticHandlers = cant33 vm.CantHealth = cant34}35// Initialize ...36func (vm *TestVM) Initialize(ctx *snow.Context, db database.Database, initState []byte, msgChan chan<- Message, fxs []*Fx) error {37 if vm.InitializeF != nil {38 return vm.InitializeF(ctx, db, initState, msgChan, fxs)39 }40 if vm.CantInitialize && vm.T != nil {41 vm.T.Fatal(errInitialize)42 }43 return errInitialize44}45// Bootstrapping ...46func (vm *TestVM) Bootstrapping() error {47 if vm.BootstrappingF != nil {48 return vm.BootstrappingF()49 } else if vm.CantBootstrapping {50 if vm.T != nil {51 vm.T.Fatalf("Unexpectedly called Bootstrapping")52 }53 return errors.New("unexpectedly called Bootstrapping")54 }55 return nil56}57// Bootstrapped ...58func (vm *TestVM) Bootstrapped() error {59 if vm.BootstrappedF != nil {60 return vm.BootstrappedF()61 } else if vm.CantBootstrapped {62 if vm.T != nil {63 vm.T.Fatalf("Unexpectedly called Bootstrapped")64 }65 return errors.New("unexpectedly called Bootstrapped")66 }67 return nil68}69// Shutdown ...70func (vm *TestVM) Shutdown() error {71 if vm.ShutdownF != nil {72 return vm.ShutdownF()73 } else if vm.CantShutdown {74 if vm.T != nil {75 vm.T.Fatalf("Unexpectedly called Shutdown")76 }77 return errors.New("unexpectedly called Shutdown")78 }79 return nil80}81// CreateHandlers ...82func (vm *TestVM) CreateHandlers() map[string]*HTTPHandler {83 if vm.CreateHandlersF != nil {84 return vm.CreateHandlersF()85 }86 if vm.CantCreateHandlers && vm.T != nil {87 vm.T.Fatalf("Unexpectedly called CreateHandlers")88 }89 return nil90}91// CreateStaticHandlers ...92func (vm *TestVM) CreateStaticHandlers() map[string]*HTTPHandler {93 if vm.CreateStaticHandlersF != nil {94 return vm.CreateStaticHandlersF()95 }96 if vm.CantCreateStaticHandlers && vm.T != nil {97 vm.T.Fatalf("Unexpectedly called CreateStaticHandlers")98 }99 return nil100}101// Health ...102func (vm *TestVM) Health() (interface{}, error) {103 if vm.HealthF != nil {104 return vm.HealthF()105 }106 if vm.CantHealth && vm.T != nil {107 vm.T.Fatalf("Unexpectedly called Health")108 }109 return nil, errors.New("Unexpectedly called Health")110}...

Full Screen

Full Screen

Create

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx, cancel := context.WithCancel(context.Background())4 defer cancel()5 if err != nil {6 log.Fatal(err)7 }8 u.User = url.UserPassword("

Full Screen

Full Screen

Create

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx, cancel := context.WithCancel(context.Background())4 defer cancel()5 u, err := url.Parse(os.Getenv("GOVC_URL"))6 if err != nil {7 panic(err)8 }9 u.User = url.UserPassword(os.Getenv("GOVC_USERNAME"), os.Getenv("GOVC_PASSWORD"))10 c, err := govmomi.NewClient(ctx, u, true)11 if err != nil {12 panic(err)13 }14 finder := find.NewFinder(c.Client, true)15 dc, err := finder.Datacenter(ctx, os.Getenv("GOVC_DATACENTER"))16 if err != nil {17 panic(err)18 }19 finder.SetDatacenter(dc)20 folder, err := finder.Folder(ctx, os.Getenv("GOVC_FOLDER"))21 if err != nil {22 panic(err)23 }24 pool, err := finder.ResourcePool(ctx, os.Getenv("GOVC_RESOURCE_POOL"))25 if err != nil {26 panic(err)27 }28 net, err = finder.Network(ctx, os.Getenv("GOVC_NETWORK"))29 if err != nil {30 panic(err)31 }32 ds, err = finder.Datastore(ctx, os.Getenv("GOVC_DATASTORE"))33 if err != nil {34 panic(err)35 }36 vm, err := finder.VirtualMachine(ctx, os.Getenv("GOVC_VM"))37 if err != nil {38 panic(err)39 }40 host, err := finder.HostSystem(ctx, os.Getenv("GOVC_HOST"))41 if err != nil {42 panic(err)43 }44 configSpec := types.VirtualMachineConfigSpec{45 Name: os.Getenv("GOVC_VM_NAME"),46 GuestId: os.Getenv("GOVC_VM_GUEST_ID"),47 Files: &types.VirtualMachineFileInfo{VmPathName: fmt.Sprintf("[%s]", ds.Name())},48 }

Full Screen

Full Screen

Create

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 ctx, cancel := context.WithCancel(context.Background())4 defer cancel()5 u, err := url.Parse(os.Args[1])6 if err != nil {7 log.Fatal(err)8 }9 c, err := govmomi.NewClient(ctx, u, true)10 if err != nil {11 log.Fatal(err)12 }13 f := find.NewFinder(c.Client, true)14 dc, err := f.DefaultDatacenter(ctx)15 if err != nil {16 log.Fatal(err)17 }18 f.SetDatacenter(dc)19 vm, err := f.VirtualMachine(ctx, os.Args[2])20 if err != nil {21 log.Fatal(err)22 }23 pool, err := f.ResourcePool(ctx, os.Args[3])24 if err != nil {25 log.Fatal(err)26 }27 ds, err := f.Datastore(ctx, os.Args[4])28 if err != nil {29 log.Fatal(err)30 }31 net, err := f.Network(ctx, os.Args[5])32 if err != nil {33 log.Fatal(err)34 }35 folder := object.NewFolder(c.Client, dc.VmFolder)36 err = vm.Properties(ctx, vm.Reference(), []string{"config.files"}, &mvm)37 if err != nil {38 log.Fatal(err)39 }

Full Screen

Full Screen

Create

Using AI Code Generation

copy

Full Screen

1import (2type vm struct {3}4func (v vm) Create() {5 fmt.Println("VM", v.name, "created")6}7func (v vm) Start() {8 fmt.Println("VM", v.name, "started")9}10func (v vm) Stop() {11 fmt.Println("VM", v.name, "stopped")12}13func main() {14 v1 := vm{"vm1", 2, 2}15 v2 := vm{"vm2", 4, 4}16 v3 := vm{"vm3", 8, 8}17 vms := []interface{}{v1, v2, v3}18 for _, v := range vms {19 value := reflect.ValueOf(v)20 method := value.MethodByName("Create")21 method.Call(nil)22 }23}24import (25type vm struct {26}27func (v vm) Create() {28 fmt.Println("VM", v.name, "created")29}30func (v vm) Start() {31 fmt.Println("VM", v.name, "started")32}33func (v vm) Stop() {34 fmt.Println("VM", v.name, "stopped")35}36func main() {37 v1 := vm{"vm1", 2, 2}38 v2 := vm{"vm2", 4, 4}39 v3 := vm{"vm3", 8, 8}40 vms := []interface{}{v1, v2, v3}41 for _, v := range vms {42 value := reflect.ValueOf(v)43 method := value.MethodByName("Start")44 method.Call(nil)45 }46}47import (48type vm struct {49}50func (v vm) Create() {51 fmt.Println("VM", v.name, "created")52}53func (v vm) Start() {54 fmt.Println("VM", v.name, "started")55}56func (v vm) Stop() {57 fmt.Println("VM", v.name, "stopped")58}59func main() {60 v1 := vm{"vm

Full Screen

Full Screen

Create

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if len(os.Args) != 4 {4 fmt.Println("Usage: go run 2.go <vc ip> <vc username> <vc password>")5 }6 ctx := context.Background()7 if err != nil {8 fmt.Println(err)9 }10 c, err := govmomi.NewClient(ctx, u, true)11 if err != nil {12 fmt.Println(err)13 }14 finder := find.NewFinder(c.Client, true)15 datastore, err := finder.Datastore(ctx, "datastore1")16 if err != nil {17 fmt.Println(err)18 }19 folder, err := finder.Folder(ctx, "vm")20 if err != nil {21 fmt.Println(err)22 }23 resourcePool, err := finder.ResourcePool(ctx, "Resources")24 if err != nil {25 fmt.Println(err)26 }27 network, err := finder.Network(ctx, "VM Network")28 if err != nil {29 fmt.Println(err)30 }31 vmMoRef, err = c.ServiceContent.VmFolder.CreateVM(ctx, types.CreateVM{32 This: folder.Reference(),33 Config: &types.VMConfigSpec{34 Files: &types.VirtualMachineFileInfo{35 VmPathName: fmt.Sprintf("[%s]", datastore.Name()),36 },

Full Screen

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run Syzkaller automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful