forked from pion/dtls
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config_test.go
82 lines (73 loc) · 2.64 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package dtls
import (
"crypto/tls"
"crypto/x509"
"encoding/pem"
"testing"
"github.com/pion/dtls/v2/pkg/crypto/selfsign"
)
func TestValidateConfig(t *testing.T) {
//Empty config
if err := validateConfig(nil); err != errNoConfigProvided {
t.Fatalf("TestValidateConfig: Config validation error exp(%v) failed(%v)", errNoConfigProvided, err)
}
//PSK and Certificate
cert, err := selfsign.GenerateSelfSigned()
if err != nil {
t.Fatalf("TestValidateConfig: Config validation error(%v), self signed certificate not generated", err)
return
}
config := &Config{
CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
PSK: func(hint []byte) ([]byte, error) {
return nil, nil
},
Certificates: []tls.Certificate{cert},
}
if err = validateConfig(config); err != errPSKAndCertificate {
t.Fatalf("TestValidateConfig: Client error exp(%v) failed(%v)", errPSKAndCertificate, err)
}
//PSK identity hint with not PSK
config = &Config{
CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
PSK: nil,
PSKIdentityHint: []byte{},
}
if err = validateConfig(config); err != errIdentityNoPSK {
t.Fatalf("TestValidateConfig: Client error exp(%v) failed(%v)", errIdentityNoPSK, err)
}
//Invalid private key
block, _ := pem.Decode([]byte(rawPrivateKey))
rsaKey, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
t.Fatalf("TestValidateConfig: Config validation error(%v), parsing RSA private key", err)
}
config = &Config{
CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
Certificates: []tls.Certificate{{Certificate: cert.Certificate, PrivateKey: rsaKey}},
}
if err = validateConfig(config); err != errInvalidPrivateKey {
t.Fatalf("TestValidateConfig: Client error exp(%v) failed(%v)", errInvalidPrivateKey, err)
}
// PrivateKey without Certificate
config = &Config{
CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
Certificates: []tls.Certificate{{PrivateKey: cert.PrivateKey}},
}
if err = validateConfig(config); err != errInvalidCertificate {
t.Fatalf("TestValidateConfig: Client error exp(%v) failed(%v)", errInvalidCertificate, err)
}
//Invalid cipher suites
config = &Config{CipherSuites: []CipherSuiteID{0x0000}}
if err = validateConfig(config); err == nil {
t.Fatal("TestValidateConfig: Client error expected with invalid CipherSuiteID")
}
//Valid config
config = &Config{
CipherSuites: []CipherSuiteID{TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256},
Certificates: []tls.Certificate{cert},
}
if err = validateConfig(config); err != nil {
t.Fatalf("TestValidateConfig: Client error exp(%v) failed(%v)", nil, err)
}
}