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

Function TestNKeyOptionFromSeed

nats_test.go:1418–1512  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

1416}
1417
1418func TestNKeyOptionFromSeed(t *testing.T) {
1419 if _, err := NkeyOptionFromSeed("file_that_does_not_exist"); err == nil {
1420 t.Fatal("Expected error got none")
1421 }
1422
1423 seedFile := createTmpFile(t, []byte(`
1424 # No seed
1425 THIS_NOT_A_NKEY_SEED
1426 `))
1427 defer os.Remove(seedFile)
1428 if _, err := NkeyOptionFromSeed(seedFile); err == nil || !strings.Contains(err.Error(), "seed found") {
1429 t.Fatalf("Expected error about seed not found, got %v", err)
1430 }
1431 os.Remove(seedFile)
1432
1433 seedFile = createTmpFile(t, []byte(`
1434 # Invalid seed
1435 SUBADSEED
1436 `))
1437 // Make sure that we detect SU (trim space) but it still fails because
1438 // this is not a valid NKey.
1439 if _, err := NkeyOptionFromSeed(seedFile); err == nil || strings.Contains(err.Error(), "seed found") {
1440 t.Fatalf("Expected error about invalid key, got %v", err)
1441 }
1442 os.Remove(seedFile)
1443
1444 kp, _ := nkeys.CreateUser()
1445 seed, _ := kp.Seed()
1446 seedFile = createTmpFile(t, seed)
1447 opt, err := NkeyOptionFromSeed(seedFile)
1448 if err != nil {
1449 t.Fatalf("Error: %v", err)
1450 }
1451
1452 l, e := net.Listen("tcp", "127.0.0.1:0")
1453 if e != nil {
1454 t.Fatal("Could not listen on an ephemeral port")
1455 }
1456 tl := l.(*net.TCPListener)
1457 defer tl.Close()
1458
1459 addr := tl.Addr().(*net.TCPAddr)
1460
1461 ch := make(chan bool, 1)
1462 errCh := make(chan error, 1)
1463 rs := func(ch chan bool) {
1464 conn, err := l.Accept()
1465 if err != nil {
1466 errCh <- fmt.Errorf("error accepting client connection: %v", err)
1467 return
1468 }
1469 defer conn.Close()
1470 info := "INFO {\"server_id\":\"foobar\",\"nonce\":\"anonce\"}\r\n"
1471 conn.Write([]byte(info))
1472
1473 // Read connect and ping commands sent from the client
1474 br := bufio.NewReaderSize(conn, 10*1024)
1475 line, _, err := br.ReadLine()

Callers

nothing calls this directly

Calls 9

NkeyOptionFromSeedFunction · 0.85
ConnectFunction · 0.85
FatalfMethod · 0.80
ErrorfMethod · 0.80
createTmpFileFunction · 0.70
checkErrChannelFunction · 0.70
ErrorMethod · 0.65
CloseMethod · 0.45
WriteMethod · 0.45

Tested by

no test coverage detected