github.com/singularityware/singularity@v3.1.1+incompatible/pkg/util/capabilities/capabilities_test.go (about)

     1  // Copyright (c) 2018-2019, Sylabs Inc. All rights reserved.
     2  // This software is licensed under a 3-clause BSD license. Please consult the
     3  // LICENSE.md file distributed with the sources of this project regarding your
     4  // rights to use or distribute this software.
     5  
     6  package capabilities
     7  
     8  import (
     9  	"sort"
    10  	"testing"
    11  )
    12  
    13  func TestSplit(t *testing.T) {
    14  	testCaps := []struct {
    15  		caps   string
    16  		length int
    17  	}{
    18  		{
    19  			caps:   "chown, sys_admin",
    20  			length: 2,
    21  		},
    22  		{
    23  			caps:   "CAP_,     sys_admin        ",
    24  			length: 1,
    25  		},
    26  		{
    27  			caps:   "cap_sys_admin, cap_chown",
    28  			length: 2,
    29  		},
    30  		{
    31  			caps:   "CAP_sys_admin,CHOWN",
    32  			length: 2,
    33  		},
    34  		{
    35  			caps:   "chown, CAP_ALL",
    36  			length: len(Map),
    37  		},
    38  		{
    39  			caps:   "cap_all",
    40  			length: len(Map),
    41  		},
    42  	}
    43  	for _, tc := range testCaps {
    44  		caps, _ := Split(tc.caps)
    45  		if len(caps) != tc.length {
    46  			t.Errorf("should have returned %d as capability len instead of %d", tc.length, len(caps))
    47  		}
    48  	}
    49  }
    50  
    51  func TestRemoveDuplicated(t *testing.T) {
    52  	tt := []struct {
    53  		name   string
    54  		in     []string
    55  		expect []string
    56  	}{
    57  		{
    58  			name: "no duplicates",
    59  			in: []string{
    60  				"CAP_CHOWN",
    61  				"CAP_DAC_OVERRIDE",
    62  				"CAP_DAC_READ_SEARCH",
    63  				"CAP_FOWNER",
    64  				"CAP_FSETID",
    65  				"CAP_KILL",
    66  				"CAP_SETGID",
    67  			},
    68  			expect: []string{
    69  				"CAP_CHOWN",
    70  				"CAP_DAC_OVERRIDE",
    71  				"CAP_DAC_READ_SEARCH",
    72  				"CAP_FOWNER",
    73  				"CAP_FSETID",
    74  				"CAP_KILL",
    75  				"CAP_SETGID",
    76  			},
    77  		},
    78  		{
    79  			name: "single duplicate",
    80  			in: []string{
    81  				"CAP_CHOWN",
    82  				"CAP_DAC_OVERRIDE",
    83  				"CAP_DAC_READ_SEARCH",
    84  				"CAP_FOWNER",
    85  				"CAP_DAC_OVERRIDE",
    86  				"CAP_FSETID",
    87  				"CAP_KILL",
    88  				"CAP_SETGID",
    89  			},
    90  			expect: []string{
    91  				"CAP_CHOWN",
    92  				"CAP_DAC_OVERRIDE",
    93  				"CAP_DAC_READ_SEARCH",
    94  				"CAP_FOWNER",
    95  				"CAP_FSETID",
    96  				"CAP_KILL",
    97  				"CAP_SETGID",
    98  			},
    99  		},
   100  		{
   101  			name: "two duplicates",
   102  			in: []string{
   103  				"CAP_KILL",
   104  				"CAP_CHOWN",
   105  				"CAP_DAC_OVERRIDE",
   106  				"CAP_DAC_READ_SEARCH",
   107  				"CAP_FOWNER",
   108  				"CAP_DAC_OVERRIDE",
   109  				"CAP_FSETID",
   110  				"CAP_KILL",
   111  				"CAP_SETGID",
   112  			},
   113  			expect: []string{
   114  				"CAP_CHOWN",
   115  				"CAP_DAC_OVERRIDE",
   116  				"CAP_DAC_READ_SEARCH",
   117  				"CAP_FOWNER",
   118  				"CAP_FSETID",
   119  				"CAP_KILL",
   120  				"CAP_SETGID",
   121  			},
   122  		},
   123  		{
   124  			name: "not once duplicated",
   125  			in: []string{
   126  				"CAP_DAC_OVERRIDE",
   127  				"CAP_CHOWN",
   128  				"CAP_DAC_OVERRIDE",
   129  				"CAP_DAC_READ_SEARCH",
   130  				"CAP_FOWNER",
   131  				"CAP_DAC_OVERRIDE",
   132  				"CAP_DAC_OVERRIDE",
   133  				"CAP_FSETID",
   134  				"CAP_KILL",
   135  				"CAP_SETGID",
   136  				"CAP_DAC_OVERRIDE",
   137  			},
   138  			expect: []string{
   139  				"CAP_CHOWN",
   140  				"CAP_DAC_OVERRIDE",
   141  				"CAP_DAC_READ_SEARCH",
   142  				"CAP_FOWNER",
   143  				"CAP_FSETID",
   144  				"CAP_KILL",
   145  				"CAP_SETGID",
   146  			},
   147  		},
   148  	}
   149  
   150  	for _, tc := range tt {
   151  		t.Run(tc.name, func(t *testing.T) {
   152  			actual := RemoveDuplicated(tc.in)
   153  			sort.Strings(tc.expect)
   154  			sort.Strings(actual)
   155  			if len(tc.expect) != len(actual) {
   156  				t.Fatalf("expectected slice of len=%d, got len=%d", len(tc.expect), len(actual))
   157  			}
   158  			for i := range tc.expect {
   159  				if tc.expect[i] != actual[i] {
   160  					t.Fatalf("expected %s at position %d, but got %s", tc.expect[i], i, actual[i])
   161  				}
   162  			}
   163  		})
   164  	}
   165  }