How to use Equal method of lib Package

Best K6 code snippet using lib.Equal

contextHandle_test.go

Source:contextHandle_test.go Github

copy

Full Screen

...24 handle, err := LoadPKCS11ContextHandle(lib, label, pin)25 assert.NoError(t, err)26 assert.NotNil(t, handle)27 assert.NotNil(t, handle.ctx)28 assert.Equal(t, handle.lib, lib)29 assert.Equal(t, handle.label, label)30 assert.Equal(t, handle.pin, pin)31 //Test session32 session, err := handle.OpenSession()33 assert.NoError(t, err)34 assert.True(t, session > 0)35 //Test login36 err = handle.Login(session)37 assert.NoError(t, err)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 break...

Full Screen

Full Screen

parse_test.go

Source:parse_test.go Github

copy

Full Screen

...10)11var env, _ = CreateENV()12func TestEnvironment(t *testing.T) {13 // no parallel14 require.Equal(t, Development, Environment())15 environmentList := []string{Production, Staging, Testing, Development}16 for _, environment := range environmentList {17 os.Setenv(envKey, environment)18 require.Equal(t, environment, Environment())19 }20}21func TestEVString(t *testing.T) {22 t.Parallel()23 key := "LIB_NAME"24 val := "tres lib"25 os.Setenv(key, val)26 testCases := []struct {27 Name string28 Key string29 Fallback string30 Value string31 }{32 {"not_exist", "NOT_EXIST_KEY", "none", "none"},33 {"exist", key, "none", val},34 }35 for _, testCase := range testCases {36 tc := testCase37 t.Run(tc.Name, func(t *testing.T) {38 t.Parallel()39 value := env.EVString(tc.Key, tc.Fallback)40 require.Equal(t, tc.Value, value)41 })42 }43}44func TestEVInt64(t *testing.T) {45 t.Parallel()46 key := "LIB_INT64"47 val := int64(999999999999999)48 os.Setenv(key, fmt.Sprintf("%d", val))49 testCases := []struct {50 Name string51 Key string52 Fallback int6453 Value int6454 }{55 {"not_exist", "NOT_EXIST_KEY", -1, -1},56 {"exist", key, -1, val},57 }58 for _, testCase := range testCases {59 tc := testCase60 t.Run(tc.Name, func(t *testing.T) {61 t.Parallel()62 value, err := env.EVInt64(tc.Key, tc.Fallback)63 require.Nil(t, err)64 require.Equal(t, tc.Value, value)65 })66 }67}68func TestEVInt(t *testing.T) {69 t.Parallel()70 key := "LIB_INT"71 val := -99972 os.Setenv(key, fmt.Sprintf("%d", val))73 testCases := []struct {74 Name string75 Key string76 Fallback int77 Value int78 }{79 {"not_exist", "NOT_EXIST_KEY", -1, -1},80 {"exist", key, -1, val},81 }82 for _, testCase := range testCases {83 tc := testCase84 t.Run(tc.Name, func(t *testing.T) {85 t.Parallel()86 value, err := env.EVInt(tc.Key, tc.Fallback)87 require.Nil(t, err)88 require.Equal(t, tc.Value, value)89 })90 }91}92func TestEVUInt64(t *testing.T) {93 t.Parallel()94 key := "LIB_UINT64"95 val := uint64(999)96 os.Setenv(key, fmt.Sprintf("%d", val))97 testCases := []struct {98 Name string99 Key string100 Fallback uint64101 Value uint64102 }{103 {"not_exist", "NOT_EXIST_KEY", 0, 0},104 {"exist", key, 0, val},105 }106 for _, testCase := range testCases {107 tc := testCase108 t.Run(tc.Name, func(t *testing.T) {109 t.Parallel()110 value, err := env.EVUInt64(tc.Key, tc.Fallback)111 require.Nil(t, err)112 require.Equal(t, tc.Value, value)113 })114 }115}116func TestEVBool(t *testing.T) {117 t.Parallel()118 testCases := []struct {119 Name string120 Key string121 Val string122 Fallback bool123 Value bool124 Error bool125 }{126 {"not_exist", "NOT_EXIST_KEY", "true", false, false, false},127 {"invalid", "INVALID", "invalid", false, false, true},128 {"true", "LIB_BOOL_1", "true", false, true, false},129 {"false", "LIB_BOOL_2", "false", true, false, false},130 {"True", "LIB_BOOL_3", "True", false, true, false},131 {"False", "LIB_BOOL_4", "False", false, false, false},132 {"TRUE", "LIB_BOOL_5", "TRUE", true, true, false},133 {"FALSE", "LIB_BOOL_6", "FALSE", true, false, false},134 }135 for _, testCase := range testCases {136 tc := testCase137 t.Run(tc.Name, func(t *testing.T) {138 t.Parallel()139 if tc.Name != "not_exist" {140 os.Setenv(tc.Key, tc.Val)141 }142 value, err := env.EVBool(tc.Key, tc.Fallback)143 if tc.Error {144 require.Error(t, err)145 } else {146 require.Nil(t, err)147 require.Equal(t, tc.Value, value)148 }149 })150 }151}152func TestParse(t *testing.T) {153 t.Parallel()154 type validSubStruct struct {155 A1 int `env:"PA1,validator=a1"`156 B1 int64 `env:"PB1,validator=b1"`157 C1 uint `env:"PC1,validator=c1"`158 D1 uint64 `env:"PD1,validator=d1"`159 E1 float64 `env:"PE1,validator=e1"`160 F1 string `env:"PF1,validator=f1"`161 G1 bool `env:"PG1,validator=g1"`162 }163 type validSubStruct1 struct {164 A2 int `env:"PA2,validator=a2"`165 B2 int64 `env:"PB2,validator=b2"`166 C2 uint `env:"PC2,validator=c2"`167 D2 uint64 `env:"PD2,validator=d2"`168 E2 float64 `env:"PE2,validator=e2"`169 F2 string `env:"PF2,validator=f2"`170 G2 bool `env:"PG2,validator=g2"`171 }172 type validStruct struct {173 A int `env:"PA,validator=a"`174 B int64 `env:"PB,validator=b"`175 C uint `env:"PC,validator=c"`176 D uint64 `env:"PD,validator=d"`177 E float64 `env:"PE,validator=e"`178 F string `env:"PF,validator=f"`179 G bool `env:"PG,validator=g"`180 H *validSubStruct181 I validSubStruct1182 }183 t.Run("error_not_pointer", func(t *testing.T) {184 t.Parallel()185 data := validStruct{186 A: 1,187 }188 err := env.Parse(data)189 require.Error(t, err)190 require.Equal(t, 1, data.A)191 })192 t.Run("error_not_truct", func(t *testing.T) {193 t.Parallel()194 data := 1195 err := env.Parse(&data)196 require.Error(t, err)197 require.Equal(t, 1, data)198 })199 t.Run("error_scan_struct", func(t *testing.T) {200 t.Parallel()201 data := struct {202 A int `env:"TA"`203 }{204 A: 1,205 }206 os.Setenv("TA", "invalid")207 err := env.Parse(&data, func(interface{}) error {208 return errors.New("test failed scan struct")209 })210 require.Error(t, err)211 require.Equal(t, 1, data.A)212 })213 t.Run("success", func(t *testing.T) {214 t.Parallel()215 data := validStruct{216 A: -1,217 B: -10,218 C: 1,219 D: 1000,220 E: 1.8,221 F: "test",222 G: true,223 H: &validSubStruct{224 A1: -2,225 B1: -20,226 C1: 2,227 D1: 2000,228 E1: 2.8,229 F1: "2test",230 G1: true,231 },232 I: validSubStruct1{233 A2: -3,234 B2: -30,235 C2: 3,236 D2: 3000,237 E2: 3.8,238 F2: "3test",239 G2: true,240 },241 }242 clone := validStruct{243 A: -1,244 B: -10,245 C: 1,246 D: 1000,247 E: 1.8,248 F: "test",249 G: true,250 H: &validSubStruct{251 A1: -2,252 B1: -20,253 C1: 2,254 D1: 2000,255 E1: 2.8,256 F1: "2test",257 G1: true,258 },259 I: validSubStruct1{260 A2: -3,261 B2: -30,262 C2: 3,263 D2: 3000,264 E2: 3.8,265 F2: "3test",266 G2: true,267 },268 }269 os.Setenv("PA", lib.ToString(data.A+1111))270 os.Setenv("PB", lib.ToString(data.B+1111))271 os.Setenv("PC", lib.ToString(data.C+1111))272 os.Setenv("PD", lib.ToString(data.D+1111))273 os.Setenv("PE", lib.ToString(data.E+1111))274 os.Setenv("PF", data.F+lib.ToString(1111))275 os.Setenv("PG", lib.ToString(!data.G))276 os.Setenv("PA1", lib.ToString(data.H.A1+1111))277 os.Setenv("PB1", lib.ToString(data.H.B1+1111))278 os.Setenv("PC1", lib.ToString(data.H.C1+1111))279 os.Setenv("PD1", lib.ToString(data.H.D1+1111))280 os.Setenv("PE1", lib.ToString(data.H.E1+1111))281 os.Setenv("PF1", data.H.F1+lib.ToString(1111))282 os.Setenv("PG1", lib.ToString(!data.H.G1))283 os.Setenv("PA2", lib.ToString(data.I.A2+1111))284 os.Setenv("PB2", lib.ToString(data.I.B2+1111))285 os.Setenv("PC2", lib.ToString(data.I.C2+1111))286 os.Setenv("PD2", lib.ToString(data.I.D2+1111))287 os.Setenv("PE2", lib.ToString(data.I.E2+1111))288 os.Setenv("PF2", data.I.F2+lib.ToString(1111))289 os.Setenv("PG2", lib.ToString(!data.I.G2))290 err := env.Parse(&data,291 func(v interface{}) error {292 obj, ok := v.(*validStruct)293 if !ok {294 return errors.New("can convert back to object")295 }296 obj.A += 999297 return nil298 },299 func(v interface{}) error {300 obj, ok := v.(*validStruct)301 if !ok {302 return errors.New("can convert back to object")303 }304 obj.B += 999305 return nil306 },307 func(v interface{}) error {308 obj, ok := v.(*validStruct)309 if !ok {310 return errors.New("can convert back to object")311 }312 obj.C += 999313 return nil314 })315 require.Nil(t, err)316 require.Equal(t, clone.A+1111+999, data.A)317 require.Equal(t, clone.B+1111+999, data.B)318 require.Equal(t, clone.C+1111+999, data.C)319 require.Equal(t, clone.D+1111, data.D)320 require.Equal(t, clone.E+1111, data.E)321 require.Equal(t, clone.F+"1111", data.F)322 require.Equal(t, !clone.G, data.G)323 require.Equal(t, clone.H.A1+1111, data.H.A1)324 require.Equal(t, clone.H.B1+1111, data.H.B1)325 require.Equal(t, clone.H.C1+1111, data.H.C1)326 require.Equal(t, clone.H.D1+1111, data.H.D1)327 require.Equal(t, clone.H.E1+1111, data.H.E1)328 require.Equal(t, clone.H.F1+"1111", data.H.F1)329 require.Equal(t, !clone.H.G1, data.H.G1)330 require.Equal(t, clone.I.A2+1111, data.I.A2)331 require.Equal(t, clone.I.B2+1111, data.I.B2)332 require.Equal(t, clone.I.C2+1111, data.I.C2)333 require.Equal(t, clone.I.D2+1111, data.I.D2)334 require.Equal(t, clone.I.E2+1111, data.I.E2)335 require.Equal(t, clone.I.F2+"1111", data.I.F2)336 require.Equal(t, !clone.I.G2, data.I.G2)337 })338}339func TestScanStructENV(t *testing.T) {340 t.Parallel()341 type validSubStruct struct {342 A1 int `env:"A1"`343 B1 int64 `env:"B1"`344 C1 uint `env:"C1"`345 D1 uint64 `env:"D1"`346 E1 float64 `env:"E1"`347 F1 string `env:"F1"`348 G1 bool `env:"G1"`349 }350 type validSubStruct1 struct {351 A3 int `env:"A3"`352 B3 int64 `env:"B3"`353 C3 uint `env:"C3"`354 D3 uint64 `env:"D3"`355 E3 float64 `env:"E3"`356 F3 string `env:"F3"`357 G3 bool `env:"G3"`358 }359 type validStruct struct {360 A int `env:"A"`361 B int64 `env:"B"`362 C uint `env:"C"`363 D uint64 `env:"D"`364 E float64 `env:"E"`365 F string `env:"F"`366 G bool `env:"G"`367 H *validSubStruct368 I validSubStruct1369 }370 type invalidSubStruct struct {371 A2 int `env:"A2"`372 }373 t.Run("error_sub_struct_pointer", func(t *testing.T) {374 t.Parallel()375 type invalidSubStruct struct {376 A2 int `env:"ABCA1"`377 }378 type invalid struct {379 A int `env:"IA"`380 B *invalidSubStruct381 }382 data := invalid{383 A: 1,384 B: &invalidSubStruct{385 A2: 2,386 },387 }388 os.Setenv("ABCA1", "invalid")389 rv := reflect.ValueOf(&data)390 err := env.scanStructENV(rv.Elem())391 require.Error(t, err)392 require.Equal(t, 1, data.A)393 require.Equal(t, 2, data.B.A2)394 })395 t.Run("error_sub_struct", func(t *testing.T) {396 t.Parallel()397 type invalidSubStruct struct {398 A2 int `env:"ABCA2"`399 }400 type invalid struct {401 A int `env:"I1A"`402 B invalidSubStruct403 }404 data := invalid{405 A: 1,406 B: invalidSubStruct{407 A2: 2,408 },409 }410 os.Setenv("ABCA2", "invalid")411 rv := reflect.ValueOf(&data)412 err := env.scanStructENV(rv.Elem())413 require.Error(t, err)414 require.Equal(t, 1, data.A)415 require.Equal(t, 2, data.B.A2)416 })417 t.Run("error_struct", func(t *testing.T) {418 t.Parallel()419 data := struct {420 A2 int `env:"ABCA3"`421 }{422 A2: 1,423 }424 os.Setenv("ABCA3", "invalid")425 rv := reflect.ValueOf(&data)426 err := env.scanStructENV(rv.Elem())427 require.Error(t, err)428 require.Equal(t, 1, data.A2)429 })430 t.Run("success", func(t *testing.T) {431 t.Parallel()432 data := validStruct{433 A: -1,434 B: -10,435 C: 1,436 D: 1000,437 E: 1.8,438 F: "test",439 G: true,440 H: &validSubStruct{441 A1: -2,442 B1: -20,443 C1: 2,444 D1: 2000,445 E1: 2.8,446 F1: "2test",447 G1: true,448 },449 I: validSubStruct1{450 A3: -3,451 B3: -30,452 C3: 3,453 D3: 3000,454 E3: 3.8,455 F3: "3test",456 G3: true,457 },458 }459 clone := validStruct{460 A: -1,461 B: -10,462 C: 1,463 D: 1000,464 E: 1.8,465 F: "test",466 G: true,467 H: &validSubStruct{468 A1: -2,469 B1: -20,470 C1: 2,471 D1: 2000,472 E1: 2.8,473 F1: "2test",474 G1: true,475 },476 I: validSubStruct1{477 A3: -3,478 B3: -30,479 C3: 3,480 D3: 3000,481 E3: 3.8,482 F3: "3test",483 G3: true,484 },485 }486 os.Setenv("A", lib.ToString(data.A+1111))487 os.Setenv("B", lib.ToString(data.B+1111))488 os.Setenv("C", lib.ToString(data.C+1111))489 os.Setenv("D", lib.ToString(data.D+1111))490 os.Setenv("E", lib.ToString(data.E+1111))491 os.Setenv("F", data.F+lib.ToString(1111))492 os.Setenv("G", lib.ToString(!data.G))493 os.Setenv("A1", lib.ToString(data.H.A1+1111))494 os.Setenv("B1", lib.ToString(data.H.B1+1111))495 os.Setenv("C1", lib.ToString(data.H.C1+1111))496 os.Setenv("D1", lib.ToString(data.H.D1+1111))497 os.Setenv("E1", lib.ToString(data.H.E1+1111))498 os.Setenv("F1", data.H.F1+lib.ToString(1111))499 os.Setenv("G1", lib.ToString(!data.H.G1))500 os.Setenv("A3", lib.ToString(data.I.A3+1111))501 os.Setenv("B3", lib.ToString(data.I.B3+1111))502 os.Setenv("C3", lib.ToString(data.I.C3+1111))503 os.Setenv("D3", lib.ToString(data.I.D3+1111))504 os.Setenv("E3", lib.ToString(data.I.E3+1111))505 os.Setenv("F3", data.I.F3+lib.ToString(1111))506 os.Setenv("G3", lib.ToString(!data.I.G3))507 rv := reflect.ValueOf(&data)508 err := env.scanStructENV(rv.Elem())509 require.Nil(t, err)510 require.Equal(t, clone.A+1111, data.A)511 require.Equal(t, clone.B+1111, data.B)512 require.Equal(t, clone.C+1111, data.C)513 require.Equal(t, clone.D+1111, data.D)514 require.Equal(t, clone.E+1111, data.E)515 require.Equal(t, clone.F+"1111", data.F)516 require.Equal(t, !clone.G, data.G)517 require.Equal(t, clone.H.A1+1111, data.H.A1)518 require.Equal(t, clone.H.B1+1111, data.H.B1)519 require.Equal(t, clone.H.C1+1111, data.H.C1)520 require.Equal(t, clone.H.D1+1111, data.H.D1)521 require.Equal(t, clone.H.E1+1111, data.H.E1)522 require.Equal(t, clone.H.F1+"1111", data.H.F1)523 require.Equal(t, !clone.H.G1, data.H.G1)524 require.Equal(t, clone.I.A3+1111, data.I.A3)525 require.Equal(t, clone.I.B3+1111, data.I.B3)526 require.Equal(t, clone.I.C3+1111, data.I.C3)527 require.Equal(t, clone.I.D3+1111, data.I.D3)528 require.Equal(t, clone.I.E3+1111, data.I.E3)529 require.Equal(t, clone.I.F3+"1111", data.I.F3)530 require.Equal(t, !clone.I.G3, data.I.G3)531 })532}533func TestGetFieldENV(t *testing.T) {534 t.Parallel()535 prefix := "test_get_field_env"536 type test struct {537 A int538 B int64539 C uint540 D uint64541 E float64542 F string543 G bool544 }545 data := test{546 A: -1,547 B: -10,548 C: 1,549 D: 1000,550 E: 1.8,551 F: "test",552 G: true,553 }554 t.Run("empty_key_name", func(t *testing.T) {555 t.Parallel()556 clone := data557 rv := reflect.ValueOf(&clone)558 err := env.getFieldENV("A", rv.Elem().FieldByName("A"), "")559 require.Nil(t, err)560 require.Equal(t, -1, clone.A)561 })562 t.Run("notfound_key", func(t *testing.T) {563 t.Parallel()564 clone := data565 rv := reflect.ValueOf(&clone)566 err := env.getFieldENV("A", rv.Elem().FieldByName("A"), prefix+t.Name())567 require.Nil(t, err)568 require.Equal(t, -1, clone.A)569 })570 t.Run("field_cannot_set", func(t *testing.T) {571 t.Parallel()572 clone := data573 rv := reflect.ValueOf(clone)574 os.Setenv(prefix+t.Name(), "1")575 err := env.getFieldENV("A", rv.FieldByName("A"), prefix+t.Name())576 require.Error(t, err)577 require.Equal(t, -1, clone.A)578 })579 testCases := []struct {580 Name string581 FieldName string582 Value interface{}583 IsError bool584 }{585 {"int_parse_env_error", "A", "invalid", true},586 {"int_parse_env_success", "A", data.A + 100, false},587 {"int64_parse_env_error", "B", "invalid", true},588 {"int64_parse_env_success", "B", data.B + 100, false},589 {"uint_parse_env_error", "C", "invalid", true},590 {"uint_parse_env_success", "C", data.C + 100, false},591 {"uint64_parse_env_error", "D", "invalid", true},592 {"uint64_parse_env_success", "D", data.D + 100, false},593 {"float_parse_env_error", "E", "invalid", true},594 {"float_parse_env_success", "E", data.E + 100, false},595 {"string_parse_env_success", "F", data.F + "100", false},596 {"bool_parse_env_error", "G", "invalid", true},597 {"bool_parse_env_success", "G", !data.G, false},598 }599 for _, testCase := range testCases {600 tc := testCase601 t.Run(tc.Name, func(t *testing.T) {602 t.Parallel()603 clone := data604 rv := reflect.ValueOf(&clone)605 key := prefix + tc.Name606 require.Nil(t, os.Setenv(key, lib.ToString(tc.Value)))607 field, ok := rv.Elem().Type().FieldByName(tc.FieldName)608 require.True(t, ok)609 val := rv.Elem().FieldByName(tc.FieldName).Interface()610 err := env.getFieldENV(field.Name, rv.Elem().FieldByName(tc.FieldName), key)611 if tc.IsError {612 require.Error(t, err)613 require.Equal(t, val, rv.Elem().FieldByName(tc.FieldName).Interface())614 } else {615 require.Nil(t, err)616 require.Equal(t, tc.Value, rv.Elem().FieldByName(tc.FieldName).Interface())617 }618 })619 }620}621func TestCreateENV(t *testing.T) {622 t.Parallel()623 t.Run("option error", func(t *testing.T) {624 t.Parallel()625 f1 := func(_ *ENVConfig) error {626 return errors.New("error")627 }628 env, err := CreateENV(f1)629 require.Error(t, err)630 require.Nil(t, env)631 })632 t.Run("success no option", func(t *testing.T) {633 t.Parallel()634 env, err := CreateENV()635 require.Nil(t, err)636 require.NotNil(t, env)637 require.Empty(t, env.Config.Prefix)638 })639 t.Run("success with option", func(t *testing.T) {640 t.Parallel()641 prefix := "TEST_PREFIX"642 env, err := CreateENV(SetPrefixOption(prefix))643 require.Nil(t, err)644 require.NotNil(t, env)645 require.Equal(t, prefix, env.Config.Prefix)646 })647}...

Full Screen

Full Screen

main_test.go

Source:main_test.go Github

copy

Full Screen

...23 expectedCounts[lib.Point{X: 4, Y: 2}] = 524 expectedCounts[lib.Point{X: 4, Y: 3}] = 725 expectedCounts[lib.Point{X: 3, Y: 4}] = 826 expectedCounts[lib.Point{X: 4, Y: 4}] = 727 assert.Equal(t, expectedCounts, allCounts)28 assert.Equal(t, lib.Point{X: 3, Y: 4}, loc)29 assert.Equal(t, 8, count)30}31func TestMonitoringStation2(t *testing.T) {32 input := []string{33 "......#.#.",34 "#..#.#....",35 "..#######.",36 ".#.#.###..",37 ".#..#.....",38 "..#....#.#",39 "#..#....#.",40 ".##.#..###",41 "##...#..#.",42 ".#....####"}43 loc, count, _ := bestMonitoringStation(newMap(input))44 assert.Equal(t, lib.Point{X: 5, Y: 8}, loc)45 assert.Equal(t, 33, count)46}47func TestMonitoringStation3(t *testing.T) {48 input := []string{49 "#.#...#.#.",50 ".###....#.",51 ".#....#...",52 "##.#.#.#.#",53 "....#.#.#.",54 ".##..###.#",55 "..#...##..",56 "..##....##",57 "......#...",58 ".####.###."}59 loc, count, _ := bestMonitoringStation(newMap(input))60 assert.Equal(t, lib.Point{X: 1, Y: 2}, loc)61 assert.Equal(t, 35, count)62}63func TestMonitoringStation4(t *testing.T) {64 input := []string{65 ".#..#..###",66 "####.###.#",67 "....###.#.",68 "..###.##.#",69 "##.##.#.#.",70 "....###..#",71 "..#.#..#.#",72 "#..#.#.###",73 ".##...##.#",74 ".....#.#.."}75 loc, count, _ := bestMonitoringStation(newMap(input))76 assert.Equal(t, lib.Point{X: 6, Y: 3}, loc)77 assert.Equal(t, 41, count)78}79func TestMonitoringStation5(t *testing.T) {80 input := []string{81 ".#..##.###...#######",82 "##.############..##.",83 ".#.######.########.#",84 ".###.#######.####.#.",85 "#####.##.#.##.###.##",86 "..#####..#.#########",87 "####################",88 "#.####....###.#.#.##",89 "##.#################",90 "#####.##.###..####..",91 "..######..##.#######",92 "####.##.####...##..#",93 ".#####..#.######.###",94 "##...#.##########...",95 "#.##########.#######",96 ".####.#.###.###.#.##",97 "....##.##.###..#####",98 ".#.#.###########.###",99 "#.#.#.#####.####.###",100 "###.##.####.##.#..##"}101 loc, count, _ := bestMonitoringStation(newMap(input))102 assert.Equal(t, lib.Point{X: 11, Y: 13}, loc)103 assert.Equal(t, 210, count)104}105func TestDestructionOrder(t *testing.T) {106 input := []string{107 ".#....#####...#..",108 "##...##.#####..##",109 "##...#...#.#####.",110 "..#.....#...###..",111 "..#.#.....#....##"}112 from := lib.Point{X: 8, Y: 3}113 order := destructionOrder(newMap(input), &from)114 assert.Equal(t, 1, order[lib.Point{X: 8, Y: 1}])115 assert.Equal(t, 2, order[lib.Point{X: 9, Y: 0}])116 assert.Equal(t, 3, order[lib.Point{X: 9, Y: 1}])117 assert.Equal(t, 4, order[lib.Point{X: 10, Y: 0}])118 assert.Equal(t, 5, order[lib.Point{X: 9, Y: 2}])119 assert.Equal(t, 6, order[lib.Point{X: 11, Y: 1}])120 assert.Equal(t, 7, order[lib.Point{X: 12, Y: 1}])121 assert.Equal(t, 8, order[lib.Point{X: 11, Y: 2}])122 assert.Equal(t, 9, order[lib.Point{X: 15, Y: 1}])123}...

Full Screen

Full Screen

Equal

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 s1 := lib.NewString("abc")4 s2 := lib.NewString("abc")5}6type String struct {7}8func NewString(s string) *String {9 return &String{s}10}11func (s *String) Equal(s2 *String) bool {12}

Full Screen

Full Screen

Equal

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 x := []int{1, 2, 3}4 y := []int{1, 2, 3}5 fmt.Println(pretty.Equal(x, y))6}

Full Screen

Full Screen

Equal

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println("Hello, world.")4 a := lib.A{1, 2}5 b := lib.A{1, 2}6 fmt.Println(a.Equal(b))7}8type A struct {9}10func (a A) Equal(b A) bool {11}

Full Screen

Full Screen

Equal

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(lib.Equal(1, 1))4}5func Equal(a, b int) bool {6}7func Equal(a, b int) bool {8}

Full Screen

Full Screen

Equal

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(golenv.Equal("ENV", "production"))4}5import (6func main() {7 fmt.Println(golenv.Equal("ENV", "development"))8}9import (10func main() {11 fmt.Println(golenv.Equal("ENV", "staging"))12}13import (14func main() {15 fmt.Println(golenv.Equal("ENV", "testing"))16}17import (18func main() {19 fmt.Println(golenv.Equal("ENV", "production"))20}21import (22func main() {23 fmt.Println(golenv.Equal("ENV", "development"))24}25import (26func main() {27 fmt.Println(golenv.Equal("ENV", "staging"))28}29import (30func main() {31 fmt.Println(golenv.Equal("ENV", "testing"))32}33import (34func main() {35 fmt.Println(golenv.Equal("ENV", "production"))36}

Full Screen

Full Screen

Equal

Using AI Code Generation

copy

Full Screen

1import (2func main() {3 fmt.Println(lib.Equal(1, 1))4}5import (6func main() {7 fmt.Println(lib.Equal(1, 1))8}9import (10func main() {11 fmt.Println(lib.Equal(1, 1))12}

Full Screen

Full Screen

Equal

Using AI Code Generation

copy

Full Screen

1import "fmt"2import "github.com/GoLang/GoLang/GoLang/GoLang/lib"3func main() {4 fmt.Println(lib.Equal(1, 1))5}6func Equal(a, b int) bool {7}

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