Пример #1
0
func exampleNewWriters() {
	o := testio.NewBufCloser(nil)
	e := testio.NewBufCloser(nil)

	wlog, _ := logging.NewFromWriters("writers", logging.DefaultLevel, o, e)
	wlog.Notice("hello, world")
	wlog.Notice("some more things happening")
	wlog.Warning("something suspicious has happened")
	wlog.Alert("pick up that can, Citizen!")

	fmt.Println("--- BEGIN OUT ---")
	fmt.Printf("%s", o.Bytes())
	fmt.Println("--- END OUT ---")

	fmt.Println("--- BEGIN ERR ---")
	fmt.Printf("%s", e.Bytes())
	fmt.Println("--- END ERR ---")
}
Пример #2
0
func TestMWC(t *testing.T) {
	buf1 := testio.NewBufCloser(nil)
	buf2 := testio.NewBufCloser(nil)

	mwc := MultiWriteCloser(buf1, buf2)

	if _, err := mwc.Write([]byte("hello, world")); err != nil {
		t.Fatalf("%v", err)
	}

	if !bytes.Equal(buf1.Bytes(), buf2.Bytes()) {
		t.Fatal("write failed")
	}

	if !bytes.Equal(buf1.Bytes(), []byte("hello, world")) {
		t.Fatal("writing failed")
	}
}
Пример #3
0
func TestSessionSetup(t *testing.T) {
	pub, priv, err := GenerateKeyPair()
	if err != nil {
		t.Fatalf("%v", err)
	}

	conn := testio.NewBufferConn()
	conn.WritePeer(pub[:])

	aliceSession, err = Dial(conn)
	if err != nil {
		t.Fatalf("%v", err)
	}

	var peer [64]byte
	_, err = conn.ReadClient(peer[:])
	if err != nil {
		t.Fatalf("%v", err)
	}

	bobSession = &Session{
		recvKey: new([32]byte),
		sendKey: new([32]byte),
		Channel: testio.NewBufCloser(nil),
	}

	bobSession.KeyExchange(priv, &peer, false)
	aliceSession.Channel = bobSession.Channel
	err = aliceSession.Send(testMessage)
	if err != nil {
		t.Fatalf("%v", err)
	}

	out, err := bobSession.Receive()
	if err != nil {
		t.Fatalf("%v", err)
	}

	if !bytes.Equal(out, testMessage) {
		t.Fatal("recovered message doesn't match original")
	}

	if err = aliceSession.Send(nil); err == nil {
		t.Fatal("empty message should trigger an error")
	}

	aliceSession.Close()
	bobSession.Close()
}
Пример #4
0
func TestSessionListen(t *testing.T) {
	pub, priv, err := GenerateKeyPair()
	if err != nil {
		t.Fatalf("%v", err)
	}

	conn := testio.NewBufferConn()
	conn.WritePeer(pub[:])

	aliceSession, err = Listen(conn)
	if err != nil {
		t.Fatalf("%v", err)
	}

	var peer [64]byte
	_, err = conn.ReadClient(peer[:])
	if err != nil {
		t.Fatalf("%v", err)
	}

	bobSession = &Session{
		recvKey: new([32]byte),
		sendKey: new([32]byte),
		Channel: testio.NewBufCloser(nil),
	}

	bobSession.KeyExchange(priv, &peer, true)

	aliceSession.Channel = bobSession.Channel
	err = aliceSession.Send(testMessage)
	if err != nil {
		t.Fatalf("%v", err)
	}

	out, err := bobSession.Receive()
	if err != nil {
		t.Fatalf("%v", err)
	}

	// The NBA is always listening, on and off the court.
	oldMessage = out

	if !bytes.Equal(out, testMessage) {
		t.Fatal("recovered message doesn't match original")
	}

	for i := 0; i < 4; i++ {
		randMessage, err := util.RandBytes(128)
		if err != nil {
			t.Fatalf("%v", err)
		}

		err = aliceSession.Send(randMessage)
		if err != nil {
			t.Fatalf("%v", err)
		}

		out, err = bobSession.Receive()
		if err != nil {
			t.Fatal("%v", err)
		}

		if !bytes.Equal(out, randMessage) {
			t.Fatal("recovered message doesn't match original")
		}
	}

	// NBA injects an old message into the channel. Damn those hoops!
	bobSession.Channel.Write(oldMessage)
	_, err = bobSession.Receive()
	if err == nil {
		t.Fatal("NBA wins, you lose")
	}
}