github.com/prebid/prebid-server/v2@v2.18.0/server/ssl/ssl_test.go (about)

     1  package ssl
     2  
     3  import (
     4  	"crypto/x509"
     5  	"testing"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestCertsFromFilePoolExists(t *testing.T) {
    11  	// Load hardcoded certificates found in ssl.go
    12  	certPool := GetRootCAPool()
    13  
    14  	// Assert loaded certificates by looking at the length of the subjects array of strings
    15  	subjects := certPool.Subjects()
    16  	hardCodedSubNum := len(subjects)
    17  	assert.True(t, hardCodedSubNum > 0)
    18  
    19  	// Load certificates from file
    20  	certificatesFile := "mockcertificates/mock-certs.pem"
    21  	certPool, err := AppendPEMFileToRootCAPool(certPool, certificatesFile)
    22  
    23  	// Assert loaded certificates by looking at the length of the subjects array of strings
    24  	assert.NoError(t, err, "Error thrown by AppendPEMFileToRootCAPool while loading file %s: %v", certificatesFile, err)
    25  	subjects = certPool.Subjects()
    26  	subNumIncludingFile := len(subjects)
    27  	assert.True(t, subNumIncludingFile > hardCodedSubNum, "subNumIncludingFile should be greater than hardCodedSubNum")
    28  }
    29  
    30  func TestCertsFromFilePoolDontExist(t *testing.T) {
    31  	// Empty certpool
    32  	var certPool *x509.CertPool = nil
    33  
    34  	// Load certificates from file
    35  	certificatesFile := "mockcertificates/mock-certs.pem"
    36  	certPool, err := AppendPEMFileToRootCAPool(certPool, certificatesFile)
    37  
    38  	// Assert loaded certificates by looking at the length of the subjects array of strings
    39  	assert.NoError(t, err, "Error thrown by AppendPEMFileToRootCAPool while loading file %s: %v", certificatesFile, err)
    40  	subjects := certPool.Subjects()
    41  	assert.Equal(t, len(subjects), 1, "We only loaded one certificate from the file, len(subjects) should equal 1")
    42  }
    43  
    44  func TestAppendPEMFileToRootCAPoolFail(t *testing.T) {
    45  	// Empty certpool
    46  	var certPool *x509.CertPool
    47  
    48  	// In this test we are going to pass a file that does not exist as value of second argument
    49  	fakeCertificatesFile := "mockcertificates/NO-FILE.pem"
    50  	_, err := AppendPEMFileToRootCAPool(certPool, fakeCertificatesFile)
    51  
    52  	// Assert AppendPEMFileToRootCAPool correctly throws an error when trying to load an nonexisting file
    53  	assert.Errorf(t, err, "AppendPEMFileToRootCAPool should throw an error by while loading fake file %s \n", fakeCertificatesFile)
    54  }