|
1 | 1 | package ngrok |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "crypto/rand" |
5 | | - "crypto/rsa" |
6 | 4 | "crypto/tls" |
7 | | - "crypto/x509" |
8 | | - "crypto/x509/pkix" |
9 | | - "math/big" |
10 | 5 | "net" |
11 | 6 | "testing" |
12 | | - "time" |
13 | | -) |
14 | | - |
15 | | -// createTestCertificate creates a self-signed certificate for testing |
16 | | -func createTestCertificate(t *testing.T) *tls.Certificate { |
17 | | - // Generate a private key |
18 | | - privateKey, err := rsa.GenerateKey(rand.Reader, 2048) |
19 | | - if err != nil { |
20 | | - t.Fatalf("Failed to generate private key: %v", err) |
21 | | - } |
22 | 7 |
|
23 | | - // Create a certificate template |
24 | | - template := x509.Certificate{ |
25 | | - SerialNumber: big.NewInt(1), |
26 | | - Subject: pkix.Name{CommonName: "localhost"}, |
27 | | - NotBefore: time.Now(), |
28 | | - NotAfter: time.Now().Add(time.Hour * 24), // Valid for 24 hours |
29 | | - KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, |
30 | | - ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, |
31 | | - BasicConstraintsValid: true, |
32 | | - } |
33 | | - |
34 | | - // Create a self-signed certificate |
35 | | - certBytes, err := x509.CreateCertificate(rand.Reader, &template, &template, &privateKey.PublicKey, privateKey) |
36 | | - if err != nil { |
37 | | - t.Fatalf("Failed to create certificate: %v", err) |
38 | | - } |
39 | | - |
40 | | - // Create a TLS certificate |
41 | | - cert := &tls.Certificate{ |
42 | | - Certificate: [][]byte{certBytes}, |
43 | | - PrivateKey: privateKey, |
44 | | - Leaf: &template, |
45 | | - } |
46 | | - |
47 | | - return cert |
48 | | -} |
| 8 | + "golang.ngrok.com/ngrok/v2/internal/tlstest" |
| 9 | +) |
49 | 10 |
|
50 | 11 | // TestWrapConnWithTLS tests the TLS connection wrapper |
51 | 12 | func TestWrapConnWithTLS(t *testing.T) { |
52 | 13 | // Create a test certificate |
53 | | - cert := createTestCertificate(t) |
| 14 | + cert, err := tlstest.CreateCertificate() |
| 15 | + if err != nil { |
| 16 | + t.Fatal(err) |
| 17 | + } |
54 | 18 |
|
55 | 19 | // Create a pipe for testing |
56 | 20 | serverConn, clientConn := net.Pipe() |
|
0 commit comments