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

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package parsers
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/assert"
    10  )
    11  
    12  func TestIni(t *testing.T) {
    13  	tests := []struct {
    14  		title   string
    15  		content string
    16  		res     map[string]interface{}
    17  	}{
    18  		{
    19  			"simple assignment",
    20  			"key = value",
    21  			map[string]interface{}{
    22  				"": map[string]interface{}{
    23  					"key": "value",
    24  				},
    25  			},
    26  		},
    27  		{
    28  			"no assignment",
    29  			"key and value",
    30  			map[string]interface{}{
    31  				"": map[string]interface{}{
    32  					"key and value": "",
    33  				},
    34  			},
    35  		},
    36  		{
    37  			"newline comment",
    38  			"key\n# comment\n  # more comment\n\t# and one more\nvalue",
    39  			map[string]interface{}{
    40  				"": map[string]interface{}{
    41  					"key":   "",
    42  					"value": "",
    43  				},
    44  			},
    45  		},
    46  		{
    47  			"groups",
    48  			"key\n[some group]\nkey2=value",
    49  			map[string]interface{}{
    50  				"": map[string]interface{}{
    51  					"key": "",
    52  				},
    53  				"some group": map[string]interface{}{
    54  					"key2": "value",
    55  				},
    56  			},
    57  		},
    58  	}
    59  
    60  	for i := range tests {
    61  		cur := tests[i]
    62  		t.Run(cur.title, func(t *testing.T) {
    63  			res := ParseIni(cur.content, "=")
    64  			assert.Equal(t, cur.res, res.Fields)
    65  		})
    66  	}
    67  }
    68  
    69  func TestIni_SpaceDelim(t *testing.T) {
    70  	tests := []struct {
    71  		title   string
    72  		content string
    73  		res     map[string]interface{}
    74  	}{
    75  		{
    76  			"simple assignment",
    77  			"key value",
    78  			map[string]interface{}{
    79  				"": map[string]interface{}{
    80  					"key": "value",
    81  				},
    82  			},
    83  		},
    84  		{
    85  			"no assignment",
    86  			"keykey",
    87  			map[string]interface{}{
    88  				"": map[string]interface{}{
    89  					"keykey": "",
    90  				},
    91  			},
    92  		},
    93  		{
    94  			"newline comment",
    95  			"key\n# comment\n  # more comment\n\t# and one more\nvalue",
    96  			map[string]interface{}{
    97  				"": map[string]interface{}{
    98  					"key":   "",
    99  					"value": "",
   100  				},
   101  			},
   102  		},
   103  		{
   104  			"groups",
   105  			"key\n[some group]\nkey2 value",
   106  			map[string]interface{}{
   107  				"": map[string]interface{}{
   108  					"key": "",
   109  				},
   110  				"some group": map[string]interface{}{
   111  					"key2": "value",
   112  				},
   113  			},
   114  		},
   115  		{
   116  			"tabs",
   117  			"key\tvalue",
   118  			map[string]interface{}{
   119  				"": map[string]interface{}{
   120  					"key": "value",
   121  				},
   122  			},
   123  		},
   124  	}
   125  
   126  	for i := range tests {
   127  		cur := tests[i]
   128  		t.Run(cur.title, func(t *testing.T) {
   129  			res := ParseIni(cur.content, " ")
   130  			assert.Equal(t, cur.res, res.Fields)
   131  		})
   132  	}
   133  }
   134  
   135  func TestJournalD(t *testing.T) {
   136  	data := `
   137  [Journal]
   138  Storage=auto
   139  Compress=yes
   140  #Seal=yes
   141  `
   142  	res := ParseIni(data, "=")
   143  	assert.Equal(t, map[string]interface{}{
   144  		"Journal": map[string]interface{}{
   145  			"Storage":  "auto",
   146  			"Compress": "yes",
   147  		},
   148  	}, res.Fields)
   149  }