How to use Get method of lib Package

Best K6 code snippet using lib.Get

contextHandle_test.go

Source:contextHandle_test.go Github

copy

Full Screen

...38 //test return/get session39 assert.Equal(t, 0, len(handle.sessions))40 handle.ReturnSession(session)41 assert.Equal(t, 1, len(handle.sessions))42 session = handle.GetSession()43 assert.Equal(t, 0, len(handle.sessions))44 handle.ReturnSession(session)45 assert.Equal(t, 1, len(handle.sessions))46 //add new 2 session to pool, externally47 session1, err := handle.OpenSession()48 assert.NoError(t, err)49 assert.True(t, session > 0)50 session2, err := handle.OpenSession()51 assert.NoError(t, err)52 assert.True(t, session > 0)53 handle.ReturnSession(session1)54 handle.ReturnSession(session2)55 assert.Equal(t, 3, len(handle.sessions))56 //empty pool57 session1 = handle.GetSession()58 session2 = handle.GetSession()59 session3 := handle.GetSession()60 assert.Equal(t, 0, len(handle.sessions))61 //even if pool is empty should be able to get session62 session4 := handle.GetSession()63 assert.Equal(t, 0, len(handle.sessions))64 //return all sessions to pool65 handle.ReturnSession(session1)66 handle.ReturnSession(session2)67 handle.ReturnSession(session3)68 handle.ReturnSession(session4)69 assert.Equal(t, 4, len(handle.sessions))70 //reset session pool after test71 handle.sessions = make(chan mPkcs11.SessionHandle, handle.opts.sessionCacheSize)72 //force reload73 handle, err = ReloadPKCS11ContextHandle(lib, label, pin)74 assert.NoError(t, err)75 assert.NotNil(t, handle)76 assert.NotNil(t, handle.ctx)77 assert.Equal(t, handle.lib, lib)78 assert.Equal(t, handle.label, label)79 assert.Equal(t, handle.pin, pin)80}81func TestMultipleContextHandleInstances(t *testing.T) {82 testSessions := func(handle *ContextHandle) {83 //Test session84 session, err := handle.OpenSession()85 assert.NoError(t, err)86 assert.True(t, session > 0)87 //Test login88 err = handle.Login(session)89 assert.NoError(t, err)90 //test return/get session91 assert.Equal(t, 0, len(handle.sessions))92 handle.ReturnSession(session)93 assert.Equal(t, 1, len(handle.sessions))94 session = handle.GetSession()95 assert.Equal(t, 0, len(handle.sessions))96 handle.ReturnSession(session)97 assert.Equal(t, 1, len(handle.sessions))98 //add new 2 session to pool, externally99 session1, err := handle.OpenSession()100 assert.NoError(t, err)101 assert.True(t, session > 0)102 session2, err := handle.OpenSession()103 assert.NoError(t, err)104 assert.True(t, session > 0)105 handle.ReturnSession(session1)106 handle.ReturnSession(session2)107 assert.Equal(t, 3, len(handle.sessions))108 //empty pool109 session1 = handle.GetSession()110 session2 = handle.GetSession()111 session3 := handle.GetSession()112 assert.Equal(t, 0, len(handle.sessions))113 //even if pool is empty should be able to get session114 session4 := handle.GetSession()115 assert.Equal(t, 0, len(handle.sessions))116 //return all sessions to pool117 handle.ReturnSession(session1)118 handle.ReturnSession(session2)119 handle.ReturnSession(session3)120 handle.ReturnSession(session4)121 assert.Equal(t, 4, len(handle.sessions))122 //reset session pool after test123 handle.sessions = make(chan mPkcs11.SessionHandle, handle.opts.sessionCacheSize)124 }125 handle1, err := LoadPKCS11ContextHandle(lib, label, pin)126 assert.NoError(t, err)127 assert.NotNil(t, handle1)128 assert.NotNil(t, handle1.ctx)129 assert.Equal(t, handle1.lib, lib)130 assert.Equal(t, handle1.label, label)131 assert.Equal(t, handle1.pin, pin)132 testSessions(handle1)133 handle2, err := LoadPKCS11ContextHandle(lib, label1, pin)134 assert.NoError(t, err)135 assert.NotNil(t, handle2)136 assert.NotNil(t, handle2.ctx)137 assert.Equal(t, handle2.lib, lib)138 assert.Equal(t, handle2.label, label1)139 assert.Equal(t, handle2.pin, pin)140 testSessions(handle2)141 //different label means different slot142 assert.NotEqual(t, handle1.slot, handle2.slot)143 //get session each from handle1 & 2144 session1 := handle1.GetSession()145 session2 := handle2.GetSession()146 //return them back to opposite handlers147 handle1.ReturnSession(session2)148 handle2.ReturnSession(session1)149 //Test if sessions are still valid(since lib/pin are same)150 assert.Equal(t, session2, handle1.GetSession())151 assert.Equal(t, session1, handle2.GetSession())152}153func TestContextHandleInstance(t *testing.T) {154 //get context handler155 handle, err := LoadPKCS11ContextHandle(lib, label, pin)156 assert.NoError(t, err)157 assert.NotNil(t, handle)158 assert.NotNil(t, handle.ctx)159 assert.Equal(t, handle.lib, lib)160 assert.Equal(t, handle.label, label)161 assert.Equal(t, handle.pin, pin)162 defer func() {163 //reload pkcs11 context for other tests to succeed164 handle, err := ReloadPKCS11ContextHandle(lib, label, pin)165 assert.NoError(t, err)166 assert.NotNil(t, handle)167 assert.NotNil(t, handle.ctx)168 assert.Equal(t, handle.lib, lib)169 assert.Equal(t, handle.label, label)170 assert.Equal(t, handle.pin, pin)171 }()172 //destroy pkcs11 ctx inside173 handle.ctx.Destroy()174 //test if this impacted other 'LoadPKCS11ContextHandle' calls175 t.Run("test corrupted context handler instance", func(t *testing.T) {176 //get it again177 handle1, err := LoadPKCS11ContextHandle(lib, label, pin)178 assert.NoError(t, err)179 assert.NotNil(t, handle1)180 //Open session should fail it is destroyed by previous instance181 err = handle1.ctx.CloseAllSessions(handle.slot)182 assert.Error(t, err, mPkcs11.CKR_CRYPTOKI_NOT_INITIALIZED)183 })184}185func TestContextHandleOpts(t *testing.T) {186 //get context handler187 handle, err := LoadPKCS11ContextHandle(lib, label, pin, WithOpenSessionRetry(10), WithSessionCacheSize(2))188 assert.NoError(t, err)189 assert.NotNil(t, handle)190 assert.NotNil(t, handle.ctx)191 assert.Equal(t, handle.lib, lib)192 assert.Equal(t, handle.label, label)193 assert.Equal(t, handle.pin, pin)194 //get 4 sessions195 session1 := handle.GetSession()196 session2 := handle.GetSession()197 session3 := handle.GetSession()198 session4 := handle.GetSession()199 //return all 4, but pool size is 2, so last 2 will sessions will be closed200 handle.ReturnSession(session1)201 handle.ReturnSession(session2)202 handle.ReturnSession(session3)203 handle.ReturnSession(session4)204 //session1 should be valid205 _, e := handle.ctx.GetSessionInfo(session1)206 assert.NoError(t, e)207 //session2 should be valid208 _, e = handle.ctx.GetSessionInfo(session2)209 assert.NoError(t, e)210 //session3 should be closed211 _, e = handle.ctx.GetSessionInfo(session3)212 assert.Equal(t, mPkcs11.Error(mPkcs11.CKR_SESSION_HANDLE_INVALID), e)213 //session4 should be closed214 _, e = handle.ctx.GetSessionInfo(session4)215 assert.Equal(t, mPkcs11.Error(mPkcs11.CKR_SESSION_HANDLE_INVALID), e)216}217func TestContextHandleCacheCollision(t *testing.T) {218 const pin1 = "22334455"219 //get context handler-1220 handle1, err := LoadPKCS11ContextHandle(lib, label2, pin, WithOpenSessionRetry(10), WithSessionCacheSize(2))221 assert.NoError(t, err)222 assert.NotNil(t, handle1)223 assert.NotNil(t, handle1.ctx)224 assert.Equal(t, handle1.lib, lib)225 assert.Equal(t, handle1.label, label2)226 assert.Equal(t, handle1.pin, pin)227 //get context handler-2228 handle2, err := LoadPKCS11ContextHandle(lib, label2, pin1, WithOpenSessionRetry(10), WithSessionCacheSize(2))229 assert.NoError(t, err)230 assert.NotNil(t, handle2)231 assert.NotNil(t, handle2.ctx)232 assert.Equal(t, handle2.lib, lib)233 assert.Equal(t, handle2.label, label2)234 //collision happens when different PINs used under same label and lib235 assert.Equal(t, handle2.pin, handle1.pin)236 //To fix, use connection name to distinguish instances in cache237 handle2, err = LoadPKCS11ContextHandle(lib, label2, pin1, WithOpenSessionRetry(10), WithSessionCacheSize(2), WithConnectionName("connection-2"))238 assert.NoError(t, err)239 assert.NotNil(t, handle2)240 assert.NotNil(t, handle2.ctx)241 assert.Equal(t, handle2.lib, lib)242 assert.Equal(t, handle2.label, label2)243 assert.Equal(t, handle2.pin, pin1)244}245func TestContextHandleCommonInstance(t *testing.T) {246 //get context handler247 handle, err := LoadPKCS11ContextHandle(lib, label, pin)248 assert.NoError(t, err)249 assert.NotNil(t, handle)250 assert.NotNil(t, handle.ctx)251 oldCtx := handle.ctx252 for i := 0; i < 20; i++ {253 handleX, err := LoadPKCS11ContextHandle(lib, label, pin)254 assert.NoError(t, err)255 assert.NotNil(t, handleX)256 //Should be same instance, for same set of lib, label, pin257 assert.Equal(t, oldCtx, handleX.ctx)258 }259}260func TestContextRefreshOnInvalidSession(t *testing.T) {261 handle, err := LoadPKCS11ContextHandle(lib, label, pin)262 assert.NoError(t, err)263 assert.NotNil(t, handle)264 assert.NotNil(t, handle.ctx)265 //get session266 session := handle.GetSession()267 //close this session and return it, validation on return session should stop it268 handle.ctx.CloseSession(session)269 handle.ReturnSession(session)270 //session pool unchanged, since returned session was invalid271 assert.Equal(t, 0, len(handle.sessions))272 //just for test manually add it into pool273 handle.sessions <- session274 assert.Equal(t, 1, len(handle.sessions))275 oldCtx := handle.ctx276 assert.Equal(t, oldCtx, handle.ctx)277 //get session again, now ctx should be refreshed278 ch := make(chan struct{}, 1)279 handle.NotifyCtxReload(ch)280 session = handle.GetSession()281 assert.NotEqual(t, oldCtx, handle.ctx)282 assert.NotNil(t, session)283 var receivedNotification bool284 select {285 case <-ch:286 receivedNotification = true287 case <-time.After(ctxReloadTimeout):288 t.Fatal("couldn't get notification on ctx update")289 }290 assert.True(t, receivedNotification)291 //reset session pool after test292 handle.sessions = make(chan mPkcs11.SessionHandle, handle.opts.sessionCacheSize)293}294func TestSessionsFromDifferentPKCS11Ctx(t *testing.T) {295 //Testing if session created by a ctx can be validated by of some other ctx created using same lib/label/pin296 ctxAndSession := func(label string) (*mPkcs11.Ctx, mPkcs11.SessionHandle) {297 ctx := mPkcs11.New(lib)298 assert.NotNil(t, ctx)299 err := ctx.Initialize()300 assert.False(t, err != nil && err != mPkcs11.Error(mPkcs11.CKR_CRYPTOKI_ALREADY_INITIALIZED))301 var found bool302 var slot uint303 //get all slots304 slots, err := ctx.GetSlotList(true)305 if err != nil {306 t.Fatal("Failed to get slot list for recreated context:", err)307 }308 //find slot matching label309 for _, s := range slots {310 info, err := ctx.GetTokenInfo(s)311 if err != nil {312 continue313 }314 if label == info.Label {315 found = true316 slot = s317 break318 }319 }320 assert.True(t, found)321 session, err := createNewSession(ctx, slot)322 assert.NoError(t, err)323 return ctx, session324 }325 ctx1, session1 := ctxAndSession(label)326 ctx2, session2 := ctxAndSession(label)327 //ctx2 validating session1 from ctx1328 sessionInfo, err := ctx2.GetSessionInfo(session1)329 assert.NoError(t, err)330 assert.NotNil(t, sessionInfo)331 //ctx1 validating session2 from ctx2332 sessionInfo, err = ctx1.GetSessionInfo(session2)333 assert.NoError(t, err)334 assert.NotNil(t, sessionInfo)335 //test between different slot/label336 ctx3, session3 := ctxAndSession(label1)337 sessionInfo, err = ctx1.GetSessionInfo(session3)338 assert.NoError(t, err)339 assert.NotNil(t, sessionInfo)340 sessionInfo, err = ctx3.GetSessionInfo(session1)341 assert.NoError(t, err)342 assert.NotNil(t, sessionInfo)343}344func TestContextHandlerConcurrency(t *testing.T) {345 handlersCount := 5346 concurrency := 5000347 var err error348 handlers := make([]*ContextHandle, handlersCount)349 for i := 0; i < handlersCount; i++ {350 handlers[i], err = LoadPKCS11ContextHandle(lib, label, pin)351 assert.NoError(t, err)352 }353 testDone := make(chan bool)354 runTest := func(handle *ContextHandle) {355 session1 := handle.GetSession()356 assert.True(t, session1 > 0)357 session2 := handle.GetSession()358 assert.True(t, session2 > 0)359 handle.ReturnSession(session1)360 handle.ReturnSession(session2)361 session1 = handle.GetSession()362 assert.True(t, session1 > 0)363 session2 = handle.GetSession()364 assert.True(t, session2 > 0)365 handle.ReturnSession(session1)366 handle.ReturnSession(session2)367 session3, err := handle.OpenSession()368 assert.NoError(t, err)369 assert.True(t, session3 > 0)370 err = handle.Login(session3)371 assert.NoError(t, err)372 testDone <- true373 }374 for i := 0; i < concurrency; i++ {375 go runTest(handlers[i%handlersCount])376 }377 testsReturned := 0378 for i := 0; i < concurrency; i++ {379 select {380 case b := <-testDone:381 assert.True(t, b)382 testsReturned++383 case <-time.After(time.Second * 10):384 t.Fatalf("Timed out waiting for test %d", i)385 }386 }387 assert.Equal(t, concurrency, testsReturned)388}389func TestSessionHandle(t *testing.T) {390 handle, err := LoadPKCS11ContextHandle(lib, label, pin)391 assert.NoError(t, err)392 assert.NotNil(t, handle)393 assert.NotNil(t, handle.ctx)394 //make sure session pool is empty395 for len(handle.sessions) > 0 {396 <-handle.sessions397 }398 //get session399 session := handle.GetSession()400 err = isEmpty(session)401 assert.NoError(t, err)402 //tamper pin, so that get session should fail403 pinBackup := handle.pin404 slotBackup := handle.slot405 handle.pin = "9999"406 handle.slot = 8888407 //get session should fail408 session = handle.GetSession()409 err = isEmpty(session)410 assert.Error(t, err)411 //try again412 session = handle.GetSession()413 err = isEmpty(session)414 assert.Error(t, err)415 //recover tampered pin and slot416 handle.pin = pinBackup417 handle.slot = slotBackup418 //try again419 session = handle.GetSession()420 err = isEmpty(session)421 assert.NoError(t, err)422}423func TestGetSessionResilience(t *testing.T) {424 handle, err := LoadPKCS11ContextHandle(lib, label, pin)425 assert.NoError(t, err)426 assert.NotNil(t, handle)427 assert.NotNil(t, handle.ctx)428 //make sure session pool is empty429 for len(handle.sessions) > 0 {430 <-handle.sessions431 }432 //get session433 session := handle.GetSession()434 err = isEmpty(session)435 assert.NoError(t, err)436 //tamper pin, so that get session should fail437 pinBackup := handle.pin438 slotBackup := handle.slot439 resetPinAndSlot := func() {440 handle.lock.Lock()441 defer handle.lock.Unlock()442 handle.pin = pinBackup443 handle.slot = slotBackup444 }445 handle.pin = "1111"446 handle.slot = 8888447 //make sure get session should fail448 session = handle.GetSession()449 err = isEmpty(session)450 assert.Error(t, err)451 const retry = 5452 interval := 200 * time.Millisecond453 done := make(chan bool)454 // launch get session with retry455 go func() {456 for i := 0; i < retry; i++ {457 session = handle.GetSession()458 if err := isEmpty(session); err == nil {459 done <- true460 break461 }462 time.Sleep(interval)463 continue464 }465 }()466 time.Sleep(500 * time.Millisecond)467 go resetPinAndSlot()468 select {469 case <-done:470 t.Log("session recovered")471 handle.pin = pinBackup472 handle.slot = slotBackup473 case <-time.After(ctxReloadTimeout):474 t.Fatal("couldn't recover session")475 }476}477func TestContextHandleInRecovery(t *testing.T) {478 handle, err := LoadPKCS11ContextHandle(lib, label, pin)479 assert.NoError(t, err)480 assert.NotNil(t, handle)481 assert.NotNil(t, handle.ctx)482 // make sure session pool is empty483 for len(handle.sessions) > 0 {484 <-handle.sessions485 }486 // get session487 session := handle.GetSession()488 err = isEmpty(session)489 assert.NoError(t, err)490 // tamper pin, so that get session should fail491 libBackup := handle.lib492 slotBackup := handle.slot493 resetLib := func() {494 handle.lock.Lock()495 defer handle.lock.Unlock()496 handle.lib = libBackup497 handle.slot = slotBackup498 }499 defer resetLib()500 // recreate failure scenario501 handle.lib = handle.lib + "_invalid"502 handle.slot = 9999503 // get session, should fail504 session = handle.GetSession()505 err = isEmpty(session)506 assert.Error(t, err)507 assert.True(t, handle.recovery)508 verifyRecoveryError := func(err error) {509 assert.Error(t, err)510 assert.Contains(t, err.Error(), "pkcs11 ctx is under recovery, try again later")511 }512 _, err = handle.OpenSession()513 verifyRecoveryError(err)514 err = handle.Login(4)515 verifyRecoveryError(err)516 lenBefore := len(handle.sessions)517 handle.ReturnSession(4)518 assert.Equal(t, lenBefore, len(handle.sessions))519 _, err = handle.GetAttributeValue(4, 4, nil)520 verifyRecoveryError(err)521 err = handle.SetAttributeValue(4, 4, nil)522 verifyRecoveryError(err)523 _, _, err = handle.GenerateKeyPair(4, nil, nil, nil)524 verifyRecoveryError(err)525}526func TestMain(m *testing.M) {527 possibilities := strings.Split(allLibs, ",")528 for _, path := range possibilities {529 trimpath := strings.TrimSpace(path)530 if _, err := os.Stat(trimpath); !os.IsNotExist(err) {531 lib = trimpath532 break533 }...

Full Screen

Full Screen

state_accessor.go

Source:state_accessor.go Github

copy

Full Screen

...15 Module: "env",16 Name: "get_balance",17 Func: func() int64 {18 account := context[ACCOUNT].(ledger.IAccount)19 return account.GetBalance().Int64()20 },21 }22}23func setBalance(context map[string]interface{}, store *wasmtime.Store) *wasmlib.ImportLib {24 return &wasmlib.ImportLib{25 Module: "env",26 Name: "set_balance",27 Func: func(balance int64) {28 account := context[ACCOUNT].(ledger.IAccount)29 account.SetBalance(new(big.Int).SetUint64(uint64(balance)))30 },31 }32}33func getState(context map[string]interface{}, store *wasmtime.Store) *wasmlib.ImportLib {34 return &wasmlib.ImportLib{35 Module: "env",36 Name: "get_state",37 Func: func(caller *wasmtime.Caller, key_ptr int32) int32 {38 mem := caller.GetExport("memory").Memory().UnsafeData(store)39 account := context[ACCOUNT].(ledger.IAccount)40 argmap := context[wasm.CONTEXT_ARGMAP].(map[int32]int32)41 key := mem[key_ptr : key_ptr+argmap[key_ptr]]42 ok, value := account.GetState(key)43 if !ok {44 context[wasm.ERROR] = fmt.Errorf("no state")45 return -146 }47 inputPointer, err := setBytes(caller, store, value)48 if err != nil {49 context[wasm.ERROR] = err50 return -151 }52 return inputPointer53 },54 }55}56func setState(context map[string]interface{}, store *wasmtime.Store) *wasmlib.ImportLib {57 return &wasmlib.ImportLib{58 Module: "env",59 Name: "set_state",60 Func: func(caller *wasmtime.Caller, key_ptr int32, value_ptr int32) {61 mem := caller.GetExport("memory").Memory().UnsafeData(store)62 account := context[ACCOUNT].(ledger.IAccount)63 argmap := context[wasm.CONTEXT_ARGMAP].(map[int32]int32)64 key := mem[key_ptr : key_ptr+argmap[key_ptr]]65 value := mem[value_ptr : value_ptr+argmap[value_ptr]]66 account.SetState(key, value)67 },68 }69}70func addState(context map[string]interface{}, store *wasmtime.Store) *wasmlib.ImportLib {71 return &wasmlib.ImportLib{72 Module: "env",73 Name: "add_state",74 Func: func(caller *wasmtime.Caller, key_ptr int32, value_ptr int32) {75 mem := caller.GetExport("memory").Memory().UnsafeData(store)76 account := context[ACCOUNT].(ledger.IAccount)77 argmap := context[wasm.CONTEXT_ARGMAP].(map[int32]int32)78 key := mem[key_ptr : key_ptr+argmap[key_ptr]]79 value := mem[value_ptr : value_ptr+argmap[value_ptr]]80 account.AddState(key, value)81 },82 }83}84func addEvent(context map[string]interface{}, store *wasmtime.Store) *wasmlib.ImportLib {85 return &wasmlib.ImportLib{86 Module: "env",87 Name: "add_event",88 Func: func(caller *wasmtime.Caller, value_ptr int32) {89 mem := caller.GetExport("memory").Memory().UnsafeData(store)90 ledger := context[LEDGER].(*ledger1.Ledger)91 txHash := context[TX_HASH].(string)92 argmap := context[wasm.CONTEXT_ARGMAP].(map[int32]int32)93 value := mem[value_ptr : value_ptr+argmap[value_ptr]]94 event := &pb.Event{95 TxHash: types.NewHashByStr(txHash),96 Data: value,97 EventType: pb.Event_WASM,98 }99 ledger.AddEvent(event)100 },101 }102}103func getCurrentHeight(context map[string]interface{}, store *wasmtime.Store) *wasmlib.ImportLib {104 return &wasmlib.ImportLib{105 Module: "env",106 Name: "get_current_height",107 Func: func(caller *wasmtime.Caller) int64 {108 return int64(context[CURRENT_HEIGHT].(uint64))109 },110 }111}112func getTxHash(context map[string]interface{}, store *wasmtime.Store) *wasmlib.ImportLib {113 return &wasmlib.ImportLib{114 Module: "env",115 Name: "get_tx_hash",116 Func: func(caller *wasmtime.Caller) int32 {117 txHash := context[TX_HASH].(string)118 inputPointer, err := setString(caller, store, txHash)119 if err != nil {120 context[wasm.ERROR] = err121 return -1122 }123 return inputPointer124 },125 }126}127func getCaller(context map[string]interface{}, store *wasmtime.Store) *wasmlib.ImportLib {128 return &wasmlib.ImportLib{129 Module: "env",130 Name: "get_caller",131 Func: func(caller *wasmtime.Caller) int32 {132 contractCaller := context[CALLER].(string)133 inputPointer, err := setString(caller, store, contractCaller)134 if err != nil {135 context[wasm.ERROR] = err136 return -1137 }138 return inputPointer139 },140 }141}142func getCurrentCaller(context map[string]interface{}, store *wasmtime.Store) *wasmlib.ImportLib {143 return &wasmlib.ImportLib{144 Module: "env",145 Name: "get_current_caller",146 Func: func(caller *wasmtime.Caller) int32 {147 currentCaller := context[CURRENT_CALLER].(string)148 inputPointer, err := setString(caller, store, currentCaller)149 if err != nil {150 context[wasm.ERROR] = err151 return -1152 }153 return inputPointer154 },155 }156}157func setString(caller *wasmtime.Caller, store *wasmtime.Store, str string) (int32, error) {158 alloc := caller.GetExport("allocate").Func()159 if alloc == nil {160 return -1, fmt.Errorf("not found allocate method")161 }162 lengthOfStr := len(str)163 allocResult, err := alloc.Call(store, lengthOfStr+1)164 if err != nil {165 return -1, err166 }167 inputPointer := allocResult.(int32)168 mem := caller.GetExport("memory").Memory().UnsafeData(store)169 memory := mem[inputPointer:]170 var i int171 for i = 0; i < lengthOfStr; i++ {172 memory[i] = str[i]173 }174 memory[i] = 0175 fmt.Println(inputPointer)176 return inputPointer, nil177}178func setBytes(caller *wasmtime.Caller, store *wasmtime.Store, str []byte) (int32, error) {179 alloc := caller.GetExport("allocate").Func()180 if alloc == nil {181 return -1, fmt.Errorf("not found allocate method")182 }183 lengthOfStr := len(str)184 allocResult, err := alloc.Call(store, lengthOfStr+1)185 if err != nil {186 return -1, err187 }188 inputPointer := allocResult.(int32)189 mem := caller.GetExport("memory").Memory().UnsafeData(store)190 memory := mem[inputPointer:]191 var i int192 for i = 0; i < lengthOfStr; i++ {193 memory[i] = str[i]194 }195 memory[i] = 0196 return inputPointer, nil197}198func ImportLedgerLib(context map[string]interface{}, store *wasmtime.Store) []*wasmlib.ImportLib {199 var libs []*wasmlib.ImportLib200 libs = append(libs, getBalance(context, store))201 libs = append(libs, setBalance(context, store))202 libs = append(libs, getState(context, store))203 libs = append(libs, setState(context, store))...

Full Screen

Full Screen

main.go

Source:main.go Github

copy

Full Screen

...81 }82 }()83}84func init() {85 activeMiruEnv.tcpListenAddress = lib.GetEnvStr("MIRU_SYSLOG_TCP_ADDR_PORT", "")86 activeMiruEnv.stumptownAddress = lib.GetEnvStr("MIRU_STUMPTOWN_ADDR_PORT", "")87 activeMiruEnv.miruStumptownIntakeURL = lib.GetEnvStr("MIRU_STUMPTOWN_INTAKE_URL", "/miru/stumptown/intake")88 activeMiruEnv.channelBufferSizeParse = lib.GetEnvInt("CHANNEL_BUFFER_SIZE_PARSE", 1024)89 activeMiruEnv.channelBufferSizeMiruAccum = lib.GetEnvInt("CHANNEL_BUFFER_SIZE_MIRU_ACCUM", 1024)90 activeMiruEnv.channelBufferSizeMiruPost = lib.GetEnvInt("CHANNEL_BUFFER_SIZE_MIRU_POST", 1024)91 activeMiruEnv.channelBufferSizeS3Accum = lib.GetEnvInt("CHANNEL_BUFFER_SIZE_S3_ACCUM", 1024)92 activeMiruEnv.channelBufferSizeS3Post = lib.GetEnvInt("CHANNEL_BUFFER_SIZE_S3_POST", 1024)93 activeMiruEnv.miruAccumBatch = lib.GetEnvInt("MIRU_ACCUM_BATCH", 1000)94 activeMiruEnv.miruAccumDelayMs = lib.GetEnvInt("MIRU_ACCUM_DELAY_MS", 1000)95 activeMiruEnv.miruDelayOnSuccessMs = lib.GetEnvInt("MIRU_DELAY_ON_SUCCESS_MS", 500)96 activeMiruEnv.miruDelayOnErrorMs = lib.GetEnvInt("MIRU_DELAY_ON_ERROR_MS", 5000)97 activeMiruEnv.s3AccumBatchBytes = lib.GetEnvInt("S3_ACCUM_BATCH_BYTES", 10*1024*1024)98 activeMiruEnv.s3AccumDelayMs = lib.GetEnvInt("S3_ACCUM_DELAY_MS", 1*60*60*1000*100)99 activeMiruEnv.s3DelayOnSuccessMs = lib.GetEnvInt("S3_DELAY_ON_SUCCESS_MS", 500)100 activeMiruEnv.s3DelayOnErrorMs = lib.GetEnvInt("S3_DELAY_ON_ERROR_MS", 5000)101 activeMiruEnv.awsInfo = lib.AWSInfo{102 AwsRegion: lib.GetEnvStr("AWS_REGION", "us-west-2"),103 S3Bucket: lib.GetEnvStr("S3_BUCKET", "miru-syslog"),104 AwsAccessKeyID: lib.GetEnvStr("AWS_ACCESS_KEY_ID", ""),105 AwsSecretAccessKey: lib.GetEnvStr("AWS_SECRET_ACCESS_KEY", "")}106}107func initChannels() {108 log.Print("Initialize channels")109 sb.MiruPostChan = lib.MiruPostChan(110 activeMiruEnv.channelBufferSizeMiruPost,111 activeMiruEnv.stumptownAddress,112 activeMiruEnv.miruStumptownIntakeURL,113 time.Millisecond*time.Duration(activeMiruEnv.miruDelayOnSuccessMs),114 time.Millisecond*time.Duration(activeMiruEnv.miruDelayOnErrorMs))115 sb.MiruAccumChan = lib.MiruAccumChan(116 activeMiruEnv.channelBufferSizeMiruAccum,117 activeMiruEnv.miruAccumBatch,118 time.Millisecond*time.Duration(activeMiruEnv.miruAccumDelayMs),119 sb.MiruPostChan)...

Full Screen

Full Screen

Get

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(lib.Get())4}5func Get() string {6}7The go get command downloads the packages named by the import paths

Full Screen

Full Screen

Get

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(lib.Get())4}5func Get() string {6}7import "testing"8func TestGet(t *testing.T) {9 if Get() != "Hello" {10 t.Error("Get() != Hello")11 }12}13import (14func TestGet(t *testing.T) {15 if Get() != "Hello" {16 t.Error("Get() != Hello")17 }18}

Full Screen

Full Screen

Get

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(lib.Get())4}5func Get() string {6}7 /usr/local/go/src/lib (from $GOROOT)8 /home/username/go/src/lib (from $GOPATH)9import (10func main() {11 fmt.Println(lib.Get())12}13func Get() string {14}15import (16func main() {17 fmt.Println(lib.Get())18}19func Get() string {20}

Full Screen

Full Screen

Get

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(stringutil.Reverse("!oG ,olleH"))4 fmt.Println(stringutil.MyName)5}6import (7func main() {8 fmt.Println(icomefromalaska.ReverseK("!oG ,olleH"))9 fmt.Println(icomefromalaska.MyName)10}11import (12func main() {13 fmt.Println(stringutil.Reverse("!oG ,olleH"))14 fmt.Println(stringutil.MyName)15}16import (17func main() {18 fmt.Println(icomefromalaska.ReverseK("!oG ,olleH"))19 fmt.Println(icomefromalaska.MyName)20}21import (22func main() {23 fmt.Println(icomefromalaska.ReverseK("!oG ,olleH"))24 fmt.Println(icomefromalaska.MyName)25}26import (27func main() {28 fmt.Println(icomefromalaska.ReverseK("!oG ,olleH"))29 fmt.Println(icomefromalaska.MyName)30}31import (32func main() {33 fmt.Println(icomefromalaska.ReverseK("!oG ,olleH"))34 fmt.Println(icomefromalaska.MyName)35}36import (

Full Screen

Full Screen

Get

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 if err != nil {4 fmt.Println(err)5 }6 fmt.Println(res)7}8import (9func Get(url string) (string, error) {10 res, err := http.Get(url)11 if err != nil {12 }13 defer res.Body.Close()14 body, err := ioutil.ReadAll(res.Body)15 if err != nil {16 }17 return string(body), nil18}

Full Screen

Full Screen

Get

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(lib.Get())4}5import (6func main() {7 fmt.Println(lib.Get())8}9import (10func main() {11 fmt.Println(lib.Get())12}13import (14func main() {15 fmt.Println(lib.Get())16}17import (18func main() {19 fmt.Println(lib.Get())20}21import (22func main() {23 fmt.Println(lib.Get())24}25import (26func main() {27 fmt.Println(lib.Get())28}29import (30func main() {31 fmt.Println(lib.Get())32}33import (34func main() {35 fmt.Println(lib.Get())36}37import (38func main() {

Full Screen

Full Screen

Get

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(lib.Get())4}5func Get() string {6}7import (8func TestGet(t *testing.T) {9 if Get() != "Hello, World!" {10 t.Errorf("Get() returned unexpected value")11 }12}13import "testing"14func BenchmarkGet(b *testing.B) {15 for i := 0; i < b.N; i++ {16 Get()17 }18}19import (20func ExampleGet() {21 fmt.Println(Get())22}23import "testing"24func BenchmarkGet(b *testing.B) {25 for i := 0; i < b.N; i++ {26 Get()27 }28}29import (30func ExampleGet() {31 fmt.Println(Get())32}33import "testing"34func BenchmarkGet(b *testing.B) {35 for i := 0; i < b.N; i++ {36 Get()37 }38}39import (40func ExampleGet() {41 fmt.Println(Get())42}43import "testing"44func BenchmarkGet(b *testing.B) {45 for i := 0; i < b.N; i++ {46 Get()47 }48}

Full Screen

Full Screen

Get

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello World")4 fmt.Println(lib.Get())5}6func Get() string {7}8import (9func TestGet(t *testing.T) {10 if Get() != "Hello World" {11 t.Error("Get() failed. Expected Hello World, Got ", Get())12 }13}14--- PASS: TestGet (0.00 seconds)15import (16func TestGet(t *testing.T) {17 if Get() != "Hello World" {18 t.Error("Get() failed. Expected Hello World, Got ", Get())19 }20}21func Get() string {22}

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 K6 automation tests on LambdaTest cloud grid

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

Most used method in

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful