@@ -12,8 +12,11 @@ import (
1212 "net/http"
1313 "net/http/httptest"
1414 "os"
15+ "sort"
1516 "testing"
1617
18+ "google.golang.org/protobuf/proto"
19+
1720 "google.golang.org/protobuf/types/known/timestamppb"
1821
1922 "github.com/nginx/agent/v3/internal/model"
@@ -260,6 +263,7 @@ var (
260263`
261264)
262265
266+ // nolint: maintidx
263267func TestNginxConfigParser_Parse (t * testing.T ) {
264268 ctx := context .Background ()
265269 dir := t .TempDir ()
@@ -287,6 +291,23 @@ func TestNginxConfigParser_Parse(t *testing.T) {
287291 defer helpers .RemoveFileWithErrorCheck (t , allowedFile .Name ())
288292 fileMetaAllowedFiles , err := files .FileMeta (allowedFile .Name ())
289293 require .NoError (t , err )
294+ allowedFileWithMetas := mpi.File {FileMeta : fileMetaAllowedFiles }
295+
296+ _ , cert := helpers .GenerateSelfSignedCert (t )
297+ certContents := helpers.Cert {Name : "nginx.cert" , Type : "CERTIFICATE" , Contents : cert }
298+ certFile := helpers .WriteCertFiles (t , dir , certContents )
299+ require .NotNil (t , certFile )
300+ fileMetaCertFiles , err := files .FileMetaWithCertificate (certFile )
301+ require .NoError (t , err )
302+ certFileWithMetas := mpi.File {FileMeta : fileMetaCertFiles }
303+
304+ _ , diffCert := helpers .GenerateSelfSignedCert (t )
305+ diffCertContents := helpers.Cert {Name : "nginx1.cert" , Type : "CERTIFICATE" , Contents : diffCert }
306+ diffCertFile := helpers .WriteCertFiles (t , dir , diffCertContents )
307+ require .NotNil (t , diffCertFile )
308+ diffFileMetaCertFiles , err := files .FileMetaWithCertificate (diffCertFile )
309+ require .NoError (t , err )
310+ diffCertFileWithMetas := mpi.File {FileMeta : diffFileMetaCertFiles }
290311
291312 tests := []struct {
292313 instance * mpi.Instance
@@ -341,34 +362,13 @@ func TestNginxConfigParser_Parse(t *testing.T) {
341362 instance : protos .GetNginxPlusInstance ([]string {}),
342363 content : testconfig .GetNginxConfigWithNotAllowedDir (errorLog .Name (), allowedFile .Name (),
343364 notAllowedFile .Name (), accessLog .Name ()),
344- expectedConfigContext : & model.NginxConfigContext {
345- StubStatus : & model.APIDetails {},
346- PlusAPI : & model.APIDetails {},
347- InstanceID : protos .GetNginxPlusInstance ([]string {}).GetInstanceMeta ().GetInstanceId (),
348- Files : []* mpi.File {
349- {
350- FileMeta : fileMetaAllowedFiles ,
351- },
352- },
353- AccessLogs : []* model.AccessLog {
354- {
355- Name : accessLog .Name (),
356- Format : "$remote_addr - $remote_user [$time_local] \" $request\" $status $body_bytes_sent " +
357- "\" $http_referer\" \" $http_user_agent\" \" $http_x_forwarded_for\" \" $bytes_sent\" " +
358- "\" $request_length\" \" $request_time\" \" $gzip_ratio\" $server_protocol " ,
359- Permissions : "0600" ,
360- Readable : true ,
361- },
362- },
363- ErrorLogs : []* model.ErrorLog {
364- {
365- Name : errorLog .Name (),
366- Permissions : "0600" ,
367- Readable : true ,
368- },
369- },
370- NAPSysLogServers : nil ,
371- },
365+ expectedConfigContext : modelHelpers .GetConfigContextWithFiles (
366+ accessLog .Name (),
367+ errorLog .Name (),
368+ []* mpi.File {& allowedFileWithMetas },
369+ protos .GetNginxPlusInstance ([]string {}).GetInstanceMeta ().GetInstanceId (),
370+ nil ,
371+ ),
372372 expectedLog : "" ,
373373 allowedDirectories : []string {dir },
374374 },
@@ -427,6 +427,59 @@ func TestNginxConfigParser_Parse(t *testing.T) {
427427 "config; log errors to file to enable error monitoring" ,
428428 allowedDirectories : []string {dir },
429429 },
430+ {
431+ name : "Test 7: Check Parser for SSL Certs" ,
432+ instance : protos .GetNginxPlusInstance ([]string {}),
433+ content : testconfig .GetNginxConfigWithSSLCerts (
434+ errorLog .Name (),
435+ accessLog .Name (),
436+ certFile ,
437+ ),
438+ expectedConfigContext : modelHelpers .GetConfigContextWithFiles (
439+ accessLog .Name (),
440+ errorLog .Name (),
441+ []* mpi.File {& certFileWithMetas },
442+ protos .GetNginxPlusInstance ([]string {}).GetInstanceMeta ().GetInstanceId (),
443+ nil ,
444+ ),
445+ allowedDirectories : []string {dir },
446+ },
447+ {
448+ name : "Test 8: Check for multiple different SSL Certs" ,
449+ instance : protos .GetNginxPlusInstance ([]string {}),
450+ content : testconfig .GetNginxConfigWithMultipleSSLCerts (
451+ errorLog .Name (),
452+ accessLog .Name (),
453+ certFile ,
454+ diffCertFile ,
455+ ),
456+ expectedConfigContext : modelHelpers .GetConfigContextWithFiles (
457+ accessLog .Name (),
458+ errorLog .Name (),
459+ []* mpi.File {& diffCertFileWithMetas , & certFileWithMetas },
460+ protos .GetNginxPlusInstance ([]string {}).GetInstanceMeta ().GetInstanceId (),
461+ nil ,
462+ ),
463+ allowedDirectories : []string {dir },
464+ },
465+ {
466+ name : "Test 9: Check for multiple same SSL Certs" ,
467+ instance : protos .GetNginxPlusInstance ([]string {}),
468+ content : testconfig .GetNginxConfigWithMultipleSSLCerts (
469+ errorLog .Name (),
470+ accessLog .Name (),
471+ certFile ,
472+ certFile ,
473+ ),
474+ expectedConfigContext : modelHelpers .GetConfigContextWithFiles (
475+ accessLog .Name (),
476+ errorLog .Name (),
477+ []* mpi.File {& certFileWithMetas },
478+ protos .GetNginxPlusInstance ([]string {}).GetInstanceMeta ().GetInstanceId (),
479+ nil ,
480+ ),
481+ allowedDirectories : []string {dir },
482+ },
430483 }
431484
432485 for _ , test := range tests {
@@ -455,16 +508,28 @@ func TestNginxConfigParser_Parse(t *testing.T) {
455508 require .NoError (t , parseError )
456509
457510 helpers .ValidateLog (t , test .expectedLog , logBuf )
458-
459511 logBuf .Reset ()
460512
461- assert .ElementsMatch (t , test .expectedConfigContext .Files , result .Files )
513+ sort .Slice (test .expectedConfigContext .Files , func (i , j int ) bool {
514+ return test .expectedConfigContext .Files [i ].GetFileMeta ().GetName () >
515+ test .expectedConfigContext .Files [j ].GetFileMeta ().GetName ()
516+ })
517+
518+ sort .Slice (result .Files , func (i , j int ) bool {
519+ return result .Files [i ].GetFileMeta ().GetName () >
520+ result .Files [j ].GetFileMeta ().GetName ()
521+ })
522+
523+ assert .Truef (t ,
524+ protoListEqual (test .expectedConfigContext .Files , result .Files ),
525+ "Expect %s Got %s" , test .expectedConfigContext .Files , result .Files )
462526 assert .Equal (t , test .expectedConfigContext .NAPSysLogServers , result .NAPSysLogServers )
463527 assert .Equal (t , test .expectedConfigContext .PlusAPI , result .PlusAPI )
464528 assert .ElementsMatch (t , test .expectedConfigContext .AccessLogs , result .AccessLogs )
465529 assert .ElementsMatch (t , test .expectedConfigContext .ErrorLogs , result .ErrorLogs )
466530 assert .Equal (t , test .expectedConfigContext .StubStatus , result .StubStatus )
467531 assert .Equal (t , test .expectedConfigContext .InstanceID , result .InstanceID )
532+ assert .Equal (t , len (test .expectedConfigContext .Files ), len (result .Files ))
468533 })
469534 }
470535}
@@ -1160,3 +1225,14 @@ func TestNginxConfigParser_checkDuplicate(t *testing.T) {
11601225 })
11611226 }
11621227}
1228+
1229+ func protoListEqual (protoListA , protoListB []* mpi.File ) bool {
1230+ for i := 0 ; i < len (protoListA ); i ++ {
1231+ res := proto .Equal (protoListA [i ], protoListB [i ])
1232+ if ! res {
1233+ return false
1234+ }
1235+ }
1236+
1237+ return true
1238+ }
0 commit comments