go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/providers/os/resources/file_test.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package resources_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  	"go.mondoo.com/cnquery/llx"
    12  	"go.mondoo.com/cnquery/providers-sdk/v1/plugin"
    13  	"go.mondoo.com/cnquery/providers-sdk/v1/testutils"
    14  	"go.mondoo.com/cnquery/providers/os/resources"
    15  )
    16  
    17  const passwdContent = `root:x:0:0::/root:/bin/bash
    18  bin:x:1:1::/:/usr/bin/nologin
    19  daemon:x:2:2::/:/usr/bin/nologin
    20  mail:x:8:12::/var/spool/mail:/usr/bin/nologin
    21  `
    22  
    23  func TestResource_File(t *testing.T) {
    24  	x.TestSimple(t, []testutils.SimpleTest{
    25  		{
    26  			Code:        "file('/etc/passwd').exists",
    27  			ResultIndex: 0, Expectation: true,
    28  		},
    29  		{
    30  			Code:        "file('/etc/passwd').basename",
    31  			ResultIndex: 0, Expectation: "passwd",
    32  		},
    33  		{
    34  			Code:        "file('/etc/passwd').dirname",
    35  			ResultIndex: 0, Expectation: "/etc",
    36  		},
    37  		{
    38  			Code:        "file('/etc/passwd').size",
    39  			ResultIndex: 0, Expectation: int64(len(passwdContent)),
    40  		},
    41  		{
    42  			Code:        "file('/etc/passwd').permissions.mode",
    43  			ResultIndex: 0, Expectation: int64(420),
    44  		},
    45  		{
    46  			Code:        "file('/etc/passwd').content",
    47  			ResultIndex: 0, Expectation: passwdContent,
    48  		},
    49  	})
    50  }
    51  
    52  func TestResource_File_NotExist(t *testing.T) {
    53  	res := x.TestQuery(t, "file('Nope').content")
    54  	assert.EqualError(t, res[0].Data.Error, "file 'Nope' not found")
    55  }
    56  
    57  func TestResource_File_Permissions(t *testing.T) {
    58  	testCases := []struct {
    59  		mode            int64
    60  		userReadable    bool
    61  		userWriteable   bool
    62  		userExecutable  bool
    63  		groupReadable   bool
    64  		groupWriteable  bool
    65  		groupExecutable bool
    66  		otherReadable   bool
    67  		otherWriteable  bool
    68  		otherExecutable bool
    69  		suid            bool
    70  		sgid            bool
    71  		sticky          bool
    72  		isDir           bool
    73  		isFile          bool
    74  		isSymlink       bool
    75  
    76  		focus      bool
    77  		expectedID string
    78  	}{
    79  		{
    80  			mode:            0o755,
    81  			userReadable:    true,
    82  			userWriteable:   true,
    83  			userExecutable:  true,
    84  			groupReadable:   true,
    85  			groupExecutable: true,
    86  			otherReadable:   true,
    87  			otherExecutable: true,
    88  			isFile:          true,
    89  
    90  			expectedID: "-rwxr-xr-x",
    91  		},
    92  		{
    93  			mode:            0o755,
    94  			userReadable:    true,
    95  			userWriteable:   true,
    96  			userExecutable:  true,
    97  			groupReadable:   true,
    98  			groupExecutable: true,
    99  			otherReadable:   true,
   100  			otherExecutable: true,
   101  			isFile:          true,
   102  			suid:            true,
   103  
   104  			expectedID: "-rwsr-xr-x",
   105  		},
   106  		{
   107  			mode:            0o655,
   108  			userReadable:    true,
   109  			userWriteable:   true,
   110  			userExecutable:  false,
   111  			groupReadable:   true,
   112  			groupExecutable: true,
   113  			otherReadable:   true,
   114  			otherExecutable: true,
   115  			isFile:          true,
   116  			suid:            true,
   117  
   118  			expectedID: "-rwSr-xr-x",
   119  		},
   120  		{
   121  			mode:            0o755,
   122  			userReadable:    true,
   123  			userWriteable:   true,
   124  			userExecutable:  true,
   125  			groupReadable:   true,
   126  			groupExecutable: true,
   127  			otherReadable:   true,
   128  			otherExecutable: true,
   129  			isDir:           true,
   130  
   131  			expectedID: "drwxr-xr-x",
   132  		},
   133  		{
   134  			mode:            0o755,
   135  			userReadable:    true,
   136  			userWriteable:   true,
   137  			userExecutable:  true,
   138  			groupReadable:   true,
   139  			groupExecutable: true,
   140  			otherReadable:   true,
   141  			otherExecutable: true,
   142  			isDir:           true,
   143  			sticky:          true,
   144  
   145  			expectedID: "drwxr-xr-t",
   146  		},
   147  		{
   148  			mode:            0o754,
   149  			userReadable:    true,
   150  			userWriteable:   true,
   151  			userExecutable:  true,
   152  			groupReadable:   true,
   153  			groupExecutable: true,
   154  			otherReadable:   true,
   155  			otherExecutable: false,
   156  			isDir:           true,
   157  			sticky:          true,
   158  
   159  			expectedID: "drwxr-xr-T",
   160  		},
   161  		{
   162  			mode:            0o755,
   163  			userReadable:    true,
   164  			userWriteable:   true,
   165  			userExecutable:  true,
   166  			groupReadable:   true,
   167  			groupExecutable: true,
   168  			otherReadable:   true,
   169  			otherExecutable: true,
   170  			isFile:          true,
   171  			sgid:            true,
   172  			focus:           true,
   173  			expectedID:      "-rwxr-sr-x",
   174  		},
   175  		{
   176  			mode:            0o754,
   177  			userReadable:    true,
   178  			userWriteable:   true,
   179  			userExecutable:  true,
   180  			groupReadable:   true,
   181  			groupExecutable: false,
   182  			otherReadable:   true,
   183  			otherExecutable: true,
   184  			isFile:          true,
   185  			sgid:            true,
   186  
   187  			expectedID: "-rwxr-Sr-x",
   188  		},
   189  		{
   190  			mode:            0o755,
   191  			userReadable:    true,
   192  			userWriteable:   true,
   193  			userExecutable:  true,
   194  			groupReadable:   true,
   195  			groupExecutable: true,
   196  			otherReadable:   true,
   197  			otherExecutable: true,
   198  			isSymlink:       true,
   199  
   200  			expectedID: "lrwxr-xr-x",
   201  		},
   202  	}
   203  
   204  	runtime := &plugin.Runtime{}
   205  
   206  	for _, tc := range testCases {
   207  		if !tc.focus {
   208  			continue
   209  		}
   210  
   211  		permRaw, err := resources.CreateResource(
   212  			runtime,
   213  			"file.permissions",
   214  			map[string]*llx.RawData{
   215  				"mode":             llx.IntData(int64(tc.mode)),
   216  				"user_readable":    llx.BoolData(tc.userReadable),
   217  				"user_writeable":   llx.BoolData(tc.userWriteable),
   218  				"user_executable":  llx.BoolData(tc.userExecutable),
   219  				"group_readable":   llx.BoolData(tc.groupReadable),
   220  				"group_writeable":  llx.BoolData(tc.groupWriteable),
   221  				"group_executable": llx.BoolData(tc.groupExecutable),
   222  				"other_readable":   llx.BoolData(tc.otherReadable),
   223  				"other_writeable":  llx.BoolData(tc.otherWriteable),
   224  				"other_executable": llx.BoolData(tc.otherExecutable),
   225  				"suid":             llx.BoolData(tc.suid),
   226  				"sgid":             llx.BoolData(tc.sgid),
   227  				"sticky":           llx.BoolData(tc.sticky),
   228  				"isDirectory":      llx.BoolData(tc.isDir),
   229  				"isFile":           llx.BoolData(tc.isFile),
   230  				"isSymlink":        llx.BoolData(tc.isSymlink),
   231  			},
   232  		)
   233  		require.NoError(t, err)
   234  		require.Equal(t, tc.expectedID, permRaw.MqlID())
   235  	}
   236  }