go.mondoo.com/cnquery@v0.0.0-20231005093811-59568235f6ea/llx/rawdata_test.go (about)

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package llx
     5  
     6  import (
     7  	"encoding/json"
     8  	"errors"
     9  	"testing"
    10  	"time"
    11  
    12  	"github.com/stretchr/testify/assert"
    13  	"github.com/stretchr/testify/require"
    14  	"go.mondoo.com/cnquery/types"
    15  )
    16  
    17  var now = time.Now()
    18  
    19  func TestRawData_String(t *testing.T) {
    20  	strVal := "yo"
    21  	intVal := int64(1)
    22  	boolVal := true
    23  	tests := []struct {
    24  		data *RawData
    25  		res  string
    26  	}{
    27  		{NilData, "<null>"},
    28  		{BoolTrue, "true"},
    29  		{BoolFalse, "false"},
    30  		{BoolDataPtr(&boolVal), "true"},
    31  		{BoolDataPtr(nil), "<null>"},
    32  		{IntData(0), "0"},
    33  		{IntDataPtr(&intVal), "1"},
    34  		{IntDataPtr(nil), "<null>"},
    35  		{FloatData(123), "123"},
    36  		{StringData("yo"), "\"yo\""},
    37  		{StringDataPtr(nil), "<null>"},
    38  		{StringDataPtr(&strVal), "\"yo\""},
    39  		{RegexData("ex"), "/ex/"},
    40  		{TimeData(now), now.String()},
    41  		{TimeDataPtr(nil), "<null>"},
    42  		{TimeDataPtr(&now), now.String()},
    43  		{DictData(int64(1)), "1"},
    44  		{DictData(float64(1.2)), "1.2"},
    45  		{DictData(string("yo")), "\"yo\""},
    46  		{DictData([]interface{}{int64(1)}), "[1]"},
    47  		{DictData(map[string]interface{}{"a": "b"}), "{\"a\":\"b\"}"},
    48  		{ArrayData([]interface{}{"a", "b"}, types.String), "[\"a\",\"b\"]"},
    49  		{MapData(map[string]interface{}{"a": "b"}, types.String), "{\"a\":\"b\"}"},
    50  		// implicit nil:
    51  		{&RawData{types.String, nil, nil}, "<null>"},
    52  	}
    53  
    54  	for i := range tests {
    55  		assert.Equal(t, tests[i].res, tests[i].data.String())
    56  	}
    57  }
    58  
    59  func TestTruthy(t *testing.T) {
    60  	strVal := "yo"
    61  
    62  	tests := []struct {
    63  		data *RawData
    64  		res  bool
    65  	}{
    66  		{NilData, false},
    67  		{BoolTrue, true},
    68  		{BoolFalse, false},
    69  		{IntData(0), false},
    70  		{IntData(123), true},
    71  		{FloatData(0), false},
    72  		{FloatData(1.23), true},
    73  		{StringData(""), false},
    74  		{StringData("b"), true},
    75  		{StringDataPtr(nil), false},
    76  		{StringDataPtr(&strVal), true},
    77  		{RegexData(""), false},
    78  		{RegexData("r"), true},
    79  		{TimeData(time.Time{}), false},
    80  		{TimeData(now), true},
    81  		{TimeDataPtr(nil), false},
    82  		{TimeDataPtr(&now), true},
    83  		{ArrayData([]interface{}{}, types.Any), false},
    84  		{ArrayData([]interface{}{false}, types.Bool), false},
    85  		{ArrayData([]interface{}{true}, types.Bool), true},
    86  		{MapData(map[string]interface{}{}, types.Any), true},
    87  		{MapData(map[string]interface{}{"a": false}, types.Bool), false},
    88  		{MapData(map[string]interface{}{"a": true}, types.Bool), true},
    89  		{ResourceData(nil, "something"), true},
    90  		// implicit nil:
    91  		{&RawData{types.String, nil, nil}, false},
    92  	}
    93  
    94  	for i := range tests {
    95  		o := tests[i]
    96  		t.Run(o.data.String(), func(t *testing.T) {
    97  			is, _ := o.data.IsTruthy()
    98  			assert.Equal(t, o.res, is)
    99  		})
   100  	}
   101  }
   102  
   103  func TestSuccess(t *testing.T) {
   104  	tests := []struct {
   105  		data    *RawData
   106  		success bool
   107  		valid   bool
   108  	}{
   109  		{NilData, false, false},
   110  		{BoolTrue, true, true},
   111  		{BoolFalse, false, true},
   112  		{IntData(0), false, false},
   113  		{IntData(123), false, false},
   114  		{FloatData(0), false, false},
   115  		{FloatData(1.23), false, false},
   116  		{StringData(""), false, false},
   117  		{StringData("b"), false, false},
   118  		{RegexData(""), false, false},
   119  		{RegexData("r"), false, false},
   120  		{TimeData(time.Time{}), false, false},
   121  		{TimeDataPtr(nil), false, false},
   122  		{TimeData(now), false, false},
   123  		{ArrayData([]interface{}{}, types.Any), false, false},
   124  		{ArrayData([]interface{}{true, false, true}, types.Bool), false, true},
   125  		{ArrayData([]interface{}{true, true}, types.Bool), true, true},
   126  		{ResourceData(nil, "something"), false, false},
   127  		{
   128  			data: &RawData{
   129  				Type: types.Block,
   130  				Value: map[string]interface{}{
   131  					"__s": BoolData(true),
   132  				},
   133  			},
   134  			success: true,
   135  			valid:   true,
   136  		},
   137  		{
   138  			data: &RawData{
   139  				Type: types.Block,
   140  				Value: map[string]interface{}{
   141  					"__s": BoolData(false),
   142  				},
   143  			},
   144  			success: false,
   145  			valid:   true,
   146  		},
   147  		{
   148  			data: &RawData{
   149  				Type: types.Block,
   150  				Value: map[string]interface{}{
   151  					"__s": NilData,
   152  				},
   153  			},
   154  			success: false,
   155  			valid:   false,
   156  		},
   157  		{
   158  			data: &RawData{
   159  				Type:  types.Block,
   160  				Value: map[string]interface{}{},
   161  			},
   162  			success: false,
   163  			valid:   false,
   164  		},
   165  		// implicit nil:
   166  		{&RawData{types.String, nil, nil}, false, false},
   167  	}
   168  
   169  	for i := range tests {
   170  		o := tests[i]
   171  		t.Run(o.data.String(), func(t *testing.T) {
   172  			success, valid := o.data.IsSuccess()
   173  			assert.Equal(t, o.success, success)
   174  			assert.Equal(t, o.valid, valid)
   175  		})
   176  	}
   177  }
   178  
   179  func TestRawData_JSON(t *testing.T) {
   180  	tests := []*RawData{
   181  		NilData,
   182  		BoolTrue,
   183  		BoolFalse,
   184  		IntData(0),
   185  		IntData(123),
   186  		FloatData(0),
   187  		FloatData(1.23),
   188  		StringData(""),
   189  		StringData("b"),
   190  		RegexData(""),
   191  		RegexData("r"),
   192  		TimeData(time.Time{}.In(time.Local)),
   193  		TimeData(NeverFutureTime),
   194  		TimeData(NeverPastTime),
   195  		// TODO: the raw comparison here does not come out right, because of nano time
   196  		// TimeData(now),
   197  		ArrayData([]interface{}{"a", "b"}, types.String),
   198  		MapData(map[string]interface{}{"a": "b"}, types.String),
   199  		{Error: errors.New("test")},
   200  	}
   201  
   202  	for i := range tests {
   203  		o := tests[i]
   204  		t.Run(o.String(), func(t *testing.T) {
   205  			out, err := json.Marshal(o)
   206  			require.NoError(t, err)
   207  			var res RawData
   208  			err = json.Unmarshal(out, &res)
   209  			require.NoError(t, err)
   210  			assert.Equal(t, o, &res)
   211  		})
   212  	}
   213  }