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

     1  // Copyright (c) Mondoo, Inc.
     2  // SPDX-License-Identifier: BUSL-1.1
     3  
     4  package mqlc_test
     5  
     6  import (
     7  	"testing"
     8  
     9  	"github.com/stretchr/testify/require"
    10  	"go.mondoo.com/cnquery/mqlc"
    11  )
    12  
    13  func TestIfChecksumming(t *testing.T) {
    14  	queries := []string{
    15  		`
    16  			a = "a"
    17  			if(a == "a") {
    18  				return [1,2,3]
    19  			}
    20  			return [4,5,6]
    21  	`,
    22  		`
    23  			a = "b"
    24  			if(a == "a") {
    25  				return [1,2,3]
    26  			}
    27  			return [4,5,6]
    28  	`,
    29  		`
    30  			a = "a"
    31  			if(a == "a") {
    32  				return [1,2,3,4]
    33  			}
    34  			return [4,5,6]
    35  	`,
    36  		`
    37  			a = "a"
    38  			if(a == "a") {
    39  				return [1,2,3]
    40  			}
    41  			return [4,5,6,7]
    42  	`,
    43  		`
    44  			a = "a"
    45  			if(a == "a") {
    46  				[1,2,3] == [1,2,3]
    47  			} else if (a == "b") {
    48  				[1,2,3] == [1,2,3]
    49  			} else {
    50  				[1,2,3] != [1,2,3]
    51  			}
    52  	`,
    53  		`
    54  			a = "a"
    55  			if(a == "a") {
    56  				[1,2,3] == [1,2,3]
    57  			} else if (a == "c") {
    58  				[1,2,3] == [1,2,3]
    59  			} else {
    60  				[1,2,3] != [1,2,3]
    61  			}
    62  	`,
    63  		`
    64  			a = "a"
    65  			if(a == "a") {
    66  				[1,2,3] == [1,2,3]
    67  			} else if (a == "c") {
    68  				[1,2,3] == [1,2,3,4]
    69  			} else {
    70  				[1,2,3] != [1,2,3]
    71  			}
    72  	`,
    73  		`
    74  			a = "a"
    75  			if(a == "a") {
    76  				[1,2,3] == [1,2,3]
    77  			} else if (a == "c") {
    78  				[1,2,3] == [1,2,3]
    79  			} else {
    80  				[1,2,3] != [1,2,3,4]
    81  			}
    82  	`,
    83  	}
    84  
    85  	checksums := map[string]struct{}{}
    86  
    87  	for _, q := range queries {
    88  		res, err := mqlc.Compile(q, nil, conf)
    89  		require.Nil(t, err)
    90  		require.NotNil(t, res)
    91  		if res == nil {
    92  			return
    93  		}
    94  
    95  		checksum := res.CodeV2.Checksums[res.CodeV2.TailRef(1<<32)]
    96  		require.Equal(t, res.Labels.Labels[checksum], "if")
    97  		checksums[checksum] = struct{}{}
    98  
    99  		rechecksum := res.CodeV2.Blocks[0].LastChunk().ChecksumV2(1<<32, res.CodeV2)
   100  		require.Equal(t, checksum, rechecksum)
   101  	}
   102  
   103  	require.Equal(t, len(checksums), len(queries))
   104  }
   105  
   106  func TestSwitchChecksumming(t *testing.T) {
   107  	queries := []string{
   108  		`
   109  		switch {
   110  		case 1 == 2:
   111  			return [1,2,3];
   112  		default:
   113  			return [4,5,6];
   114  		}
   115  	`,
   116  		`
   117  		switch {
   118  		case 1 == 2:
   119  			return [1,2,3];
   120  		case 1 == 1:
   121  			return [1,2,3];
   122  		default:
   123  			return [4,5,6];
   124  		}
   125  	`,
   126  		`
   127  		switch {
   128  		case 1 == 2:
   129  			return [1,2,3,4];
   130  		default:
   131  			return [4,5,6];
   132  		}
   133  	`,
   134  		`
   135  		switch {
   136  		case 1 == 2:
   137  			return [1,2,3];
   138  		default:
   139  			return [4,5,6,7];
   140  		}
   141  	`,
   142  	}
   143  
   144  	checksums := map[string]struct{}{}
   145  
   146  	for _, q := range queries {
   147  		res, err := mqlc.Compile(q, nil, conf)
   148  		require.Nil(t, err)
   149  		require.NotNil(t, res)
   150  		if res == nil {
   151  			return
   152  		}
   153  
   154  		checksum := res.CodeV2.Checksums[res.CodeV2.TailRef(1<<32)]
   155  		require.Equal(t, res.Labels.Labels[checksum], "switch")
   156  		checksums[checksum] = struct{}{}
   157  
   158  		rechecksum := res.CodeV2.Blocks[0].LastChunk().ChecksumV2(1<<32, res.CodeV2)
   159  		require.Equal(t, checksum, rechecksum)
   160  	}
   161  
   162  	require.Equal(t, len(checksums), len(queries))
   163  }