...
  
    Source file
    src/crypto/tls/cache_test.go
  
  
     1  
     2  
     3  
     4  package tls
     5  
     6  import (
     7  	"encoding/pem"
     8  	"runtime"
     9  	"testing"
    10  	"time"
    11  )
    12  
    13  func TestWeakCertCache(t *testing.T) {
    14  	wcc := weakCertCache{}
    15  	p, _ := pem.Decode([]byte(rsaCertPEM))
    16  	if p == nil {
    17  		t.Fatal("Failed to decode certificate")
    18  	}
    19  
    20  	certA, err := wcc.newCert(p.Bytes)
    21  	if err != nil {
    22  		t.Fatalf("newCert failed: %s", err)
    23  	}
    24  	certB, err := wcc.newCert(p.Bytes)
    25  	if err != nil {
    26  		t.Fatalf("newCert failed: %s", err)
    27  	}
    28  	if certA != certB {
    29  		t.Fatal("newCert returned a unique reference for a duplicate certificate")
    30  	}
    31  
    32  	if _, ok := wcc.Load(string(p.Bytes)); !ok {
    33  		t.Fatal("cache does not contain expected entry")
    34  	}
    35  
    36  	timeoutRefCheck := func(t *testing.T, key string, present bool) {
    37  		t.Helper()
    38  		timeout := time.After(4 * time.Second)
    39  		for {
    40  			select {
    41  			case <-timeout:
    42  				t.Fatal("timed out waiting for expected ref count")
    43  			default:
    44  				_, ok := wcc.Load(key)
    45  				if ok == present {
    46  					return
    47  				}
    48  			}
    49  			
    50  			
    51  			
    52  			
    53  			
    54  			
    55  			
    56  			
    57  			runtime.Gosched()
    58  		}
    59  	}
    60  
    61  	
    62  	
    63  	
    64  	runtime.KeepAlive(certA)
    65  	certA = nil
    66  	runtime.GC()
    67  
    68  	timeoutRefCheck(t, string(p.Bytes), true)
    69  
    70  	
    71  	
    72  	
    73  	runtime.KeepAlive(certB)
    74  	certB = nil
    75  	runtime.GC()
    76  
    77  	timeoutRefCheck(t, string(p.Bytes), false)
    78  }
    79  
View as plain text