github.com/pion/webrtc/v4@v4.0.1/vnet_test.go (about)

     1  // SPDX-FileCopyrightText: 2023 The Pion community <https://pion.ly>
     2  // SPDX-License-Identifier: MIT
     3  
     4  //go:build !js
     5  // +build !js
     6  
     7  package webrtc
     8  
     9  import (
    10  	"testing"
    11  	"time"
    12  
    13  	"github.com/pion/interceptor"
    14  	"github.com/pion/logging"
    15  	"github.com/pion/transport/v3/vnet"
    16  	"github.com/stretchr/testify/assert"
    17  )
    18  
    19  func createVNetPair(t *testing.T, interceptorRegistry *interceptor.Registry) (*PeerConnection, *PeerConnection, *vnet.Router) {
    20  	// Create a root router
    21  	wan, err := vnet.NewRouter(&vnet.RouterConfig{
    22  		CIDR:          "1.2.3.0/24",
    23  		LoggerFactory: logging.NewDefaultLoggerFactory(),
    24  	})
    25  	assert.NoError(t, err)
    26  
    27  	// Create a network interface for offerer
    28  	offerVNet, err := vnet.NewNet(&vnet.NetConfig{
    29  		StaticIPs: []string{"1.2.3.4"},
    30  	})
    31  	assert.NoError(t, err)
    32  
    33  	// Add the network interface to the router
    34  	assert.NoError(t, wan.AddNet(offerVNet))
    35  
    36  	offerSettingEngine := SettingEngine{}
    37  	offerSettingEngine.SetNet(offerVNet)
    38  	offerSettingEngine.SetICETimeouts(time.Second, time.Second, time.Millisecond*200)
    39  
    40  	// Create a network interface for answerer
    41  	answerVNet, err := vnet.NewNet(&vnet.NetConfig{
    42  		StaticIPs: []string{"1.2.3.5"},
    43  	})
    44  	assert.NoError(t, err)
    45  
    46  	// Add the network interface to the router
    47  	assert.NoError(t, wan.AddNet(answerVNet))
    48  
    49  	answerSettingEngine := SettingEngine{}
    50  	answerSettingEngine.SetNet(answerVNet)
    51  	answerSettingEngine.SetICETimeouts(time.Second, time.Second, time.Millisecond*200)
    52  
    53  	// Start the virtual network by calling Start() on the root router
    54  	assert.NoError(t, wan.Start())
    55  
    56  	offerOptions := []func(*API){WithSettingEngine(offerSettingEngine)}
    57  	if interceptorRegistry != nil {
    58  		offerOptions = append(offerOptions, WithInterceptorRegistry(interceptorRegistry))
    59  	}
    60  	offerPeerConnection, err := NewAPI(offerOptions...).NewPeerConnection(Configuration{})
    61  	assert.NoError(t, err)
    62  
    63  	answerOptions := []func(*API){WithSettingEngine(answerSettingEngine)}
    64  	if interceptorRegistry != nil {
    65  		answerOptions = append(answerOptions, WithInterceptorRegistry(interceptorRegistry))
    66  	}
    67  	answerPeerConnection, err := NewAPI(answerOptions...).NewPeerConnection(Configuration{})
    68  	assert.NoError(t, err)
    69  
    70  	return offerPeerConnection, answerPeerConnection, wan
    71  }