Skip to content

Commit

Permalink
Fix test case
Browse files Browse the repository at this point in the history
  • Loading branch information
liuh-80 committed Nov 11, 2024
1 parent deab11e commit 65e9e5f
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 7 deletions.
23 changes: 16 additions & 7 deletions gnmi_server/crl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,17 @@ func TestCrlCache(t *testing.T) {
InitCrlCache()
defer ReleaseCrlCache()

rawCRL, _ := os.ReadFile("/tmp/testdata/crl/test.crl")
rawCRL, _ := os.ReadFile("../testdata/crl/test.crl")
if rawCRL == nil {
t.Errorf("TestCrlCache test failed, crl file missing.")
}
AppendCrlToCache("http://test.crl.com/test.crl", rawCRL)

// mock to make test CRL valied
mockCrlExpired := gomonkey.ApplyFunc(CrlExpired, func(crl *Crl) bool {
return true
})

// test CRL expired
exist, cacheItem := SearchCrlCache("http://test.crl.com/test.crl")
if exist {
Expand Down Expand Up @@ -67,10 +75,11 @@ func TestCrlCache(t *testing.T) {
}

// mock to make test CRL valied
mockCrlExpired := gomonkey.ApplyFunc(CrlExpired, func(crl *Crl) bool {
mockCrlExpired.Reset()
mockCrlNeedUpdate := gomonkey.ApplyFunc(CrlNeedUpdate, func(crl *Crl) bool {
return false
})
defer mockCrlExpired.Reset()
defer mockCrlNeedUpdate.Reset()

AppendCrlToCache("http://test.crl.com/test.crl", rawCRL)
exist, cacheItem = SearchCrlCache("http://test.crl.com/test.crl")
Expand Down Expand Up @@ -148,21 +157,21 @@ func TestVerifyCertCrl(t *testing.T) {
defer mockCrlExpired.Reset()

mockTryDownload := gomonkey.ApplyFunc(TryDownload, func(url string) bool {
rawCRL, _ := os.ReadFile("/tmp/testdata/crl/test.crl")
rawCRL, _ := os.ReadFile("../testdata/crl/test.crl")
AppendCrlToCache("http://test.crl.com/test.crl", rawCRL)
return true
})
defer mockTryDownload.Reset()

// test revoked cert
tlsConnState := CreateConnectionState("/tmp/testdata/crl/revokedInt.pem")
tlsConnState := CreateConnectionState("../testdata/crl/revokedInt.pem")
err := VerifyCertCrl(tlsConnState)
if err == nil {
t.Errorf("TestVerifyCertCrl verify revoked cert failed.")
}

// test valid cert
tlsConnState = CreateConnectionState("/tmp/testdata/crl/unrevoked.pem")
tlsConnState = CreateConnectionState("../testdata/crl/unrevoked.pem")
err = VerifyCertCrl(tlsConnState)
if err != nil {
t.Errorf("TestVerifyCertCrl verify unrevoked cert failed.")
Expand All @@ -185,7 +194,7 @@ func TestVerifyCertCrlWithDownloadFailed(t *testing.T) {
defer mockTryDownload.Reset()

// test valid cert,should failed because download CRL failed
tlsConnState := CreateConnectionState("/tmp/testdata/crl/unrevoked.pem")
tlsConnState := CreateConnectionState("../testdata/crl/unrevoked.pem")
err := VerifyCertCrl(tlsConnState)
if err == nil {
t.Errorf("TestVerifyCertCrl verify unrevoked cert should failed when CRL can't download.")
Expand Down
58 changes: 58 additions & 0 deletions testdata/crl/revokedInt.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
-----BEGIN CERTIFICATE-----
MIIDAzCCAqmgAwIBAgITAWjKwm2dNQvkO62Jgyr5rAvVQzAKBggqhkjOPQQDAjCB
pTELMAkGA1UEBhMCVVMxEzARBgNVBAgTCkNhbGlmb3JuaWExFjAUBgNVBAcTDU1v
dW50YWluIFZpZXcxEzARBgNVBAoTCkdvb2dsZSBMTEMxJjARBgNVBAsTClByb2R1
Y3Rpb24wEQYDVQQLEwpjYW1wdXMtc2xuMSwwKgYDVQQDEyNSb290IENBICgyMDIx
LTAyLTAyVDA3OjMxOjU0LTA4OjAwKTAgFw0yMTAyMDIxNTMxNTRaGA85OTk5MTIz
MTIzNTk1OVowgaUxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYw
FAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpHb29nbGUgTExDMSYwEQYD
VQQLEwpQcm9kdWN0aW9uMBEGA1UECxMKY2FtcHVzLXNsbjEsMCoGA1UEAxMjUm9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A4OjAwKTAgFw0yMTAyMDIxNTMxNTRaGA85OTk5MTIz
MTIzNTk1OVowADBZMBMGByqGSM49AgEGCCqGSM49AwEHA0IABN2/1le5d3hS/piw
hrNMHjd7gPEjzXwtuXQTzdV+aaeOf3ldnC6OnEF/bggym9MldQSJZLXPYSaoj430
Vu5PRNejggEkMIIBIDAOBgNVHQ8BAf8EBAMCB4AwHQYDVR0lBBYwFAYIKwYBBQUH
AwIGCCsGAQUFBwMBMB0GA1UdDgQWBBTEewP3JgrJPekWWGGjChVqaMhaqTAfBgNV
HSMEGDAWgBSFZ/LSBYGaSHYaJoGCdnOVCy86UjBrBgNVHREBAf8EYTBfghZqemFi
MTIucHJvZC5nb29nbGUuY29thkVzcGlmZmU6Ly9jc2NzLXRlYW0ubm9kZS5jYW1w
dXMtc2xuLnByb2QuZ29vZ2xlLmNvbS9yb2xlL2JvcmctYWRtaW4tY28wQgYDVR0f
BDswOTA3oDWgM4YxaHR0cDovL3N0YXRpYy5jb3JwLmdvb2dsZS5jb20vY3JsL2Nh
bXB1cy1zbG4vbm9kZTAKBggqhkjOPQQDAgNJADBGAiEA9w4qp3nHpXo+6d7mZc69
QoALfP5ynfBCArt8bAlToo8CIQCgc/lTfl2BtBko+7h/w6pKxLeuoQkvCL5gHFyK
LXE6vA==
-----END CERTIFICATE-----
11 changes: 11 additions & 0 deletions testdata/crl/test.crl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-----BEGIN X509 CRL-----
MIIBiDCCAS8CAQEwCgYIKoZIzj0EAwIwgaUxCzAJBgNVBAYTAlVTMRMwEQYDVQQI
EwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRMwEQYDVQQKEwpH
b29nbGUgTExDMSYwEQYDVQQLEwpQcm9kdWN0aW9uMBEGA1UECxMKY2FtcHVzLXNs
bjEsMCoGA1UEAxMjUm9vdCBDQSAoMjAyMS0wMi0wMlQwNzozMTo1NC0wODowMCkX
DTIxMDIwMjE1MzE1NFoXDTIxMDIwOTE1MzE1NFowJzAlAhQAroEYW855BRqTrlov
5cBCGvkutxcNMjEwMjAyMTUzMTU0WqAvMC0wHwYDVR0jBBgwFoAUeq/TQ959KbWk
/um08jSTXogXpWUwCgYDVR0UBAMCAQEwCgYIKoZIzj0EAwIDRwAwRAIgaSOIhJDg
wOLYlbXkmxW0cqy/AfOUNYbz5D/8/FfvhosCICftg7Vzlu0Nh83jikyjy+wtkiJt
ZYNvGFQ3Sp2L3A9e
-----END X509 CRL-----
58 changes: 58 additions & 0 deletions testdata/crl/unrevoked.pem
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
-----BEGIN CERTIFICATE-----
MIIDBDCCAqqgAwIBAgIUALy864QhnkTdceLH52k2XVOe8IQwCgYIKoZIzj0EAwIw
gaUxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQHEw1N
b3VudGFpbiBWaWV3MRMwEQYDVQQKEwpHb29nbGUgTExDMSYwEQYDVQQLEwpQcm9k
dWN0aW9uMBEGA1UECxMKY2FtcHVzLXNsbjEsMCoGA1UEAxMjUm9vdCBDQSAoMjAy
MS0wMi0wMlQwNzozMDozNi0wODowMCkwIBcNMjEwMjAyMTUzMDM2WhgPOTk5OTEy
MzEyMzU5NTlaMIGlMQswCQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEW
MBQGA1UEBxMNTW91bnRhaW4gVmlldzETMBEGA1UEChMKR29vZ2xlIExMQzEmMBEG
A1UECxMKUHJvZHVjdGlvbjARBgNVBAsTCmNhbXB1cy1zbG4xLDAqBgNVBAMTI1Jv
b3QgQ0EgKDIwMjEtMDItMDJUMDc6MzA6MzYtMDg6MDApMFkwEwYHKoZIzj0CAQYI
KoZIzj0DAQcDQgAEYv/JS5hQ5kIgdKqYZWTKCO/6gloHAmIb1G8lmY0oXLXYNHQ4
qHN7/pPtlcHQp0WK/hM8IGvgOUDoynA8mj0H9KOBszCBsDA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WjggEkMIIBIDAOBgNVHQ8BAf8EBAMCB4AwHQYDVR0lBBYwFAYIKwYBBQUH
AwIGCCsGAQUFBwMBMB0GA1UdDgQWBBTTCjXX1Txjc00tBg/5cFzpeCSKuDAfBgNV
HSMEGDAWgBQY6FeSZ5P4KdZO35lsJL05P3BfyDBrBgNVHREBAf8EYTBfghZqemFi
MTIucHJvZC5nb29nbGUuY29thkVzcGlmZmU6Ly9jc2NzLXRlYW0ubm9kZS5jYW1w
dXMtc2xuLnByb2QuZ29vZ2xlLmNvbS9yb2xlL2JvcmctYWRtaW4tY28wQgYDVR0f
BDswOTA3oDWgM4YxaHR0cDovL3N0YXRpYy5jb3JwLmdvb2dsZS5jb20vY3JsL2Nh
bXB1cy1zbG4vbm9kZTAKBggqhkjOPQQDAgNIADBFAiBq3URViNyMLpvzZHC1Y+4L
+35guyIJfjHu08P3S8/xswIhAJtWSQ1ZtozdOzGxg7GfUo4hR+5SP6rBTgIqXEfq
48fW
-----END CERTIFICATE-----

0 comments on commit 65e9e5f

Please sign in to comment.