@@ -335,6 +335,132 @@ func TestFileManagerService_checkAllowedDirectory(t *testing.T) {
335
335
require .Error (t , err )
336
336
}
337
337
338
+ func TestFileManagerService_validateAndUpdateFilePermissions (t * testing.T ) {
339
+ ctx := context .Background ()
340
+ fileManagerService := NewFileManagerService (nil , types .AgentConfig (), & sync.RWMutex {})
341
+
342
+ testFiles := []* mpi.File {
343
+ {
344
+ FileMeta : & mpi.FileMeta {
345
+ Name : "exec.conf" ,
346
+ Permissions : "0700" ,
347
+ },
348
+ },
349
+ {
350
+ FileMeta : & mpi.FileMeta {
351
+ Name : "normal.conf" ,
352
+ Permissions : "0620" ,
353
+ },
354
+ },
355
+ }
356
+
357
+ err := fileManagerService .validateAndUpdateFilePermissions (ctx , testFiles )
358
+ require .NoError (t , err )
359
+ assert .Equal (t , "0600" , testFiles [0 ].GetFileMeta ().GetPermissions ())
360
+ assert .Equal (t , "0620" , testFiles [1 ].GetFileMeta ().GetPermissions ())
361
+ }
362
+
363
+ func TestFileManagerService_areExecuteFilePermissionsSet (t * testing.T ) {
364
+ fileManagerService := NewFileManagerService (nil , types .AgentConfig (), & sync.RWMutex {})
365
+
366
+ tests := []struct {
367
+ name string
368
+ permissions string
369
+ expectBool bool
370
+ }{
371
+ {
372
+ name : "Test 1: File with read and write permissions for owner" ,
373
+ permissions : "0600" ,
374
+ expectBool : false ,
375
+ },
376
+ {
377
+ name : "Test 2: File with read/write and execute permissions for owner" ,
378
+ permissions : "0700" ,
379
+ expectBool : true ,
380
+ },
381
+ {
382
+ name : "Test 3: File with read/write and execute permissions for owner and group" ,
383
+ permissions : "0770" ,
384
+ expectBool : true ,
385
+ },
386
+ {
387
+ name : "Test 4: File with read and execute permissions for everyone" ,
388
+ permissions : "0555" ,
389
+ expectBool : true ,
390
+ },
391
+ {
392
+ name : "Test 5: File with malformed permissions" ,
393
+ permissions : "abcde" ,
394
+ expectBool : false ,
395
+ },
396
+ {
397
+ name : "Test 6: File with invalid permissions" ,
398
+ permissions : "000070" ,
399
+ expectBool : false ,
400
+ },
401
+ }
402
+
403
+ for _ , test := range tests {
404
+ t .Run (test .name , func (t * testing.T ) {
405
+ file := & mpi.File {
406
+ FileMeta : & mpi.FileMeta {
407
+ Name : "test.conf" ,
408
+ Permissions : test .permissions ,
409
+ },
410
+ }
411
+
412
+ got := fileManagerService .areExecuteFilePermissionsSet (file )
413
+ assert .Equal (t , test .expectBool , got )
414
+ })
415
+ }
416
+ }
417
+
418
+ func TestFileManagerService_removeExecuteFilePermissions (t * testing.T ) {
419
+ fileManagerService := NewFileManagerService (nil , types .AgentConfig (), & sync.RWMutex {})
420
+
421
+ tests := []struct {
422
+ name string
423
+ permissions string
424
+ errorMsg string
425
+ expectPermissions string
426
+ expectError bool
427
+ }{
428
+ {
429
+ name : "Test 1: File with execute permissions for owner and others" ,
430
+ permissions : "0703" ,
431
+ expectError : false ,
432
+ expectPermissions : "0602" ,
433
+ },
434
+ {
435
+ name : "Test 2: File with malformed permissions" ,
436
+ permissions : "abcde" ,
437
+ expectError : true ,
438
+ errorMsg : "falied to parse file permissions" ,
439
+ },
440
+ }
441
+
442
+ for _ , test := range tests {
443
+ t .Run (test .name , func (t * testing.T ) {
444
+ file := & mpi.File {
445
+ FileMeta : & mpi.FileMeta {
446
+ Name : "test.conf" ,
447
+ Permissions : test .permissions ,
448
+ },
449
+ }
450
+
451
+ parseErr := fileManagerService .removeExecuteFilePermissions (t .Context (), file )
452
+
453
+ if test .expectError {
454
+ require .Error (t , parseErr )
455
+ assert .Contains (t , parseErr .Error (), test .errorMsg )
456
+ } else {
457
+ require .NoError (t , parseErr )
458
+ assert .Equal (t , test .expectPermissions , file .GetFileMeta ().GetPermissions ())
459
+ }
460
+ })
461
+ }
462
+ }
463
+
338
464
//nolint:usetesting // need to use MkDirTemp instead of t.tempDir for rollback as t.tempDir does not accept a pattern
339
465
func TestFileManagerService_ClearCache (t * testing.T ) {
340
466
tempDir := t .TempDir ()
0 commit comments