MCPcopy
hub / github.com/nats-io/nats.go / TestNkeyAuth

Function TestNkeyAuth

test/nats_test.go:434–486  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

432}
433
434func TestNkeyAuth(t *testing.T) {
435 if server.VERSION[0] == '1' {
436 t.Skip()
437 }
438
439 seed := []byte("SUAKYRHVIOREXV7EUZTBHUHL7NUMHPMAS7QMDU3GTIUWEI5LDNOXD43IZY")
440 kp, _ := nkeys.FromSeed(seed)
441 pub, _ := kp.PublicKey()
442
443 sopts := natsserver.DefaultTestOptions
444 sopts.Port = TEST_PORT
445 sopts.Nkeys = []*server.NkeyUser{{Nkey: string(pub)}}
446 ts := RunServerWithOptions(&sopts)
447 defer ts.Shutdown()
448
449 opts := natsReconnectOpts
450 if _, err := opts.Connect(); err == nil {
451 t.Fatalf("Expected to fail with no nkey auth defined")
452 }
453 opts.Nkey = string(pub)
454 if _, err := opts.Connect(); err != nats.ErrNkeyButNoSigCB {
455 t.Fatalf("Expected to fail with nkey defined but no signature callback, got %v", err)
456 }
457 badSign := func(nonce []byte) ([]byte, error) {
458 return []byte("VALID?"), nil
459 }
460 opts.SignatureCB = badSign
461 if _, err := opts.Connect(); err == nil {
462 t.Fatalf("Expected to fail with nkey and bad signature callback")
463 }
464 goodSign := func(nonce []byte) ([]byte, error) {
465 sig, err := kp.Sign(nonce)
466 if err != nil {
467 t.Fatalf("Failed signing nonce: %v", err)
468 }
469 return sig, nil
470 }
471 opts.SignatureCB = goodSign
472 nc, err := opts.Connect()
473 if err != nil {
474 t.Fatalf("Expected to succeed but got %v", err)
475 }
476 defer nc.Close()
477
478 // Now disconnect by killing the server and restarting.
479 ts.Shutdown()
480 ts = RunServerWithOptions(&sopts)
481 defer ts.Shutdown()
482
483 if err := nc.FlushTimeout(5 * time.Second); err != nil {
484 t.Fatalf("Error on Flush: %v", err)
485 }
486}
487
488func TestLookupHostResultIsRandomized(t *testing.T) {
489 orgAddrs, err := net.LookupHost("localhost")

Callers

nothing calls this directly

Calls 5

ConnectMethod · 0.80
FatalfMethod · 0.80
RunServerWithOptionsFunction · 0.70
CloseMethod · 0.45
FlushTimeoutMethod · 0.45

Tested by

no test coverage detected