Exemple #1
0
func (this *Config) GetAuthenticator() (internet.Authenticator, error) {
	auth := NewSimpleAuthenticator()
	if this.HeaderConfig != nil {
		rawConfig, err := this.HeaderConfig.GetInstance()
		if err != nil {
			return nil, err
		}

		header, err := internet.CreateAuthenticator(this.HeaderConfig.Type, rawConfig)
		if err != nil {
			return nil, err
		}
		auth = internet.NewAuthenticatorChain(header, auth)
	}
	return auth, nil
}
func TestConnectionReadWrite(t *testing.T) {
	assert := assert.On(t)

	upReader, upWriter := io.Pipe()
	downReader, downWriter := io.Pipe()

	auth := internet.NewAuthenticatorChain(srtp.SRTPFactory{}.Create(nil), NewSimpleAuthenticator())

	connClient := NewConnection(1, upWriter, &net.UDPAddr{IP: v2net.LocalHostIP.IP(), Port: 1}, &net.UDPAddr{IP: v2net.LocalHostIP.IP(), Port: 2}, auth)
	connClient.FetchInputFrom(downReader)

	connServer := NewConnection(1, downWriter, &net.UDPAddr{IP: v2net.LocalHostIP.IP(), Port: 2}, &net.UDPAddr{IP: v2net.LocalHostIP.IP(), Port: 1}, auth)
	connServer.FetchInputFrom(upReader)

	totalWritten := 1024 * 1024
	clientSend := make([]byte, totalWritten)
	rand.Read(clientSend)
	go func() {
		nBytes, err := connClient.Write(clientSend)
		assert.Int(nBytes).Equals(totalWritten)
		assert.Error(err).IsNil()
	}()

	serverReceived := make([]byte, totalWritten)
	totalRead := 0
	for totalRead < totalWritten {
		nBytes, err := connServer.Read(serverReceived[totalRead:])
		assert.Error(err).IsNil()
		totalRead += nBytes
	}
	assert.Bytes(serverReceived).Equals(clientSend)

	connClient.Close()
	connServer.Close()

	for connClient.State() != StateTerminated || connServer.State() != StateTerminated {
		time.Sleep(time.Second)
	}
}