mirror of
				https://github.com/pion/webrtc.git
				synced 2025-10-31 10:46:39 +08:00 
			
		
		
		
	 34e5a89f71
			
		
	
	34e5a89f71
	
	
	
		
			
			The tests are run in a Node.js environment, and this does not include any browser tests. This requires the wrtc package from npm as well as a shim which adds portions of the WebRTC API to the global scope. Some tests introduced here can be combined when differences between the Go API and the WASM bindings are addressed and as missing features are added to the WASM bindings. We can and should add more tests in the future to improve test coverage. This should be considered the minimum number of tests reuqired to ensure basic functionality is working.
		
			
				
	
	
		
			91 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			91 lines
		
	
	
		
			1.8 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
| // +build !js
 | |
| 
 | |
| package webrtc
 | |
| 
 | |
| import (
 | |
| 	"crypto/ecdsa"
 | |
| 	"crypto/elliptic"
 | |
| 	"crypto/rand"
 | |
| 	"crypto/rsa"
 | |
| 	"crypto/tls"
 | |
| 	"crypto/x509"
 | |
| 	"encoding/pem"
 | |
| 	"testing"
 | |
| 	"time"
 | |
| 
 | |
| 	"github.com/stretchr/testify/assert"
 | |
| )
 | |
| 
 | |
| func TestGenerateCertificateRSA(t *testing.T) {
 | |
| 	sk, err := rsa.GenerateKey(rand.Reader, 2048)
 | |
| 	assert.Nil(t, err)
 | |
| 
 | |
| 	skPEM := pem.EncodeToMemory(&pem.Block{
 | |
| 		Type:  "RSA PRIVATE KEY",
 | |
| 		Bytes: x509.MarshalPKCS1PrivateKey(sk),
 | |
| 	})
 | |
| 
 | |
| 	cert, err := GenerateCertificate(sk)
 | |
| 	assert.Nil(t, err)
 | |
| 
 | |
| 	certPEM := pem.EncodeToMemory(&pem.Block{
 | |
| 		Type:  "CERTIFICATE",
 | |
| 		Bytes: cert.x509Cert.Raw,
 | |
| 	})
 | |
| 
 | |
| 	_, err = tls.X509KeyPair(certPEM, skPEM)
 | |
| 	assert.Nil(t, err)
 | |
| }
 | |
| 
 | |
| func TestGenerateCertificateECDSA(t *testing.T) {
 | |
| 	sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
 | |
| 	assert.Nil(t, err)
 | |
| 
 | |
| 	skDER, err := x509.MarshalECPrivateKey(sk)
 | |
| 	assert.Nil(t, err)
 | |
| 
 | |
| 	skPEM := pem.EncodeToMemory(&pem.Block{
 | |
| 		Type:  "EC PRIVATE KEY",
 | |
| 		Bytes: skDER,
 | |
| 	})
 | |
| 
 | |
| 	cert, err := GenerateCertificate(sk)
 | |
| 	assert.Nil(t, err)
 | |
| 
 | |
| 	certPEM := pem.EncodeToMemory(&pem.Block{
 | |
| 		Type:  "CERTIFICATE",
 | |
| 		Bytes: cert.x509Cert.Raw,
 | |
| 	})
 | |
| 
 | |
| 	_, err = tls.X509KeyPair(certPEM, skPEM)
 | |
| 	assert.Nil(t, err)
 | |
| }
 | |
| 
 | |
| func TestGenerateCertificateEqual(t *testing.T) {
 | |
| 	sk1, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
 | |
| 	assert.Nil(t, err)
 | |
| 
 | |
| 	cert1, err := GenerateCertificate(sk1)
 | |
| 	assert.Nil(t, err)
 | |
| 
 | |
| 	sk2, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
 | |
| 	assert.Nil(t, err)
 | |
| 
 | |
| 	cert2, err := GenerateCertificate(sk2)
 | |
| 	assert.Nil(t, err)
 | |
| 
 | |
| 	assert.True(t, cert1.Equals(*cert1))
 | |
| 	assert.False(t, cert1.Equals(*cert2))
 | |
| }
 | |
| 
 | |
| func TestGenerateCertificateExpires(t *testing.T) {
 | |
| 	sk, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
 | |
| 	assert.Nil(t, err)
 | |
| 
 | |
| 	cert, err := GenerateCertificate(sk)
 | |
| 	assert.Nil(t, err)
 | |
| 
 | |
| 	now := time.Now()
 | |
| 	assert.False(t, cert.Expires().IsZero() || now.After(cert.Expires()))
 | |
| }
 |