github.com/khulnasoft-lab/defsec@v1.0.5-0.20230827010352-5e9f46893d95/pkg/scan/code_test.go (about)

     1  package scan
     2  
     3  import (
     4  	"os"
     5  	"strings"
     6  	"testing"
     7  
     8  	defsecTypes "github.com/khulnasoft-lab/defsec/pkg/types"
     9  
    10  	"github.com/stretchr/testify/assert"
    11  
    12  	"github.com/stretchr/testify/require"
    13  
    14  	"github.com/liamg/memoryfs"
    15  )
    16  
    17  func TestResult_GetCode(t *testing.T) {
    18  
    19  	tests := []struct {
    20  		name       string
    21  		source     string
    22  		filename   string
    23  		start      int
    24  		end        int
    25  		outerStart int
    26  		outerEnd   int
    27  		expected   []Line
    28  		options    []CodeOption
    29  		wantErr    bool
    30  		annotation string
    31  	}{
    32  		{
    33  			name: "basic w/ defaults",
    34  			source: `1
    35  2
    36  3
    37  4`,
    38  			filename: "test.txt",
    39  			start:    2,
    40  			end:      3,
    41  			expected: []Line{
    42  				{
    43  					Number:      2,
    44  					Content:     "2",
    45  					IsCause:     true,
    46  					Highlighted: "2",
    47  					FirstCause:  true,
    48  					LastCause:   false,
    49  				},
    50  				{
    51  					Number:      3,
    52  					Content:     "3",
    53  					IsCause:     true,
    54  					Highlighted: "3",
    55  					FirstCause:  false,
    56  					LastCause:   true,
    57  				},
    58  			},
    59  		},
    60  		{
    61  			name: "nested ranges",
    62  			source: `resource "aws_s3_bucket" "something" {
    63  	bucket = "something"
    64  }`,
    65  			filename:   "main.tf",
    66  			start:      2,
    67  			end:        2,
    68  			outerStart: 1,
    69  			outerEnd:   3,
    70  			options:    []CodeOption{OptionCodeWithHighlighted(false)},
    71  			expected: []Line{
    72  				{
    73  					Number:  1,
    74  					Content: `resource "aws_s3_bucket" "something" {`,
    75  				},
    76  				{
    77  					Number:     2,
    78  					Content:    `	bucket = "something"`,
    79  					IsCause:    true,
    80  					FirstCause: true,
    81  					LastCause:  true,
    82  				},
    83  				{
    84  					Number:  3,
    85  					Content: "}",
    86  				},
    87  			},
    88  		},
    89  		{
    90  			name: "bad filename",
    91  			source: `1
    92  2
    93  3
    94  4`,
    95  			filename: "",
    96  			start:    2,
    97  			end:      3,
    98  			wantErr:  true,
    99  		},
   100  		{
   101  			name: "no line numbers",
   102  			source: `1
   103  2
   104  3
   105  4`,
   106  			filename: "test.txt",
   107  			start:    0,
   108  			end:      0,
   109  			wantErr:  true,
   110  		},
   111  		{
   112  			name: "negative line numbers",
   113  			source: `1
   114  2
   115  3
   116  4`,
   117  			filename: "test.txt",
   118  			start:    -2,
   119  			end:      -1,
   120  			wantErr:  true,
   121  		},
   122  		{
   123  			name: "invalid line numbers",
   124  			source: `1
   125  2
   126  3
   127  4`,
   128  			filename: "test.txt",
   129  			start:    5,
   130  			end:      6,
   131  			wantErr:  true,
   132  		},
   133  		{
   134  			name:     "syntax highlighting",
   135  			source:   `FROM ubuntu`,
   136  			filename: "Dockerfile",
   137  			start:    1,
   138  			end:      1,
   139  			expected: []Line{
   140  				{
   141  					Number:      1,
   142  					Content:     "FROM ubuntu",
   143  					IsCause:     true,
   144  					Highlighted: "\x1b[38;5;64mFROM\x1b[0m\x1b[38;5;37m ubuntu\x1b[0m",
   145  					FirstCause:  true,
   146  					LastCause:   true,
   147  				},
   148  			},
   149  		},
   150  		{
   151  			name:     "truncation",
   152  			source:   strings.Repeat("If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.\n", 100),
   153  			filename: "longfile.txt",
   154  			start:    1,
   155  			end:      100,
   156  			expected: []Line{
   157  				{
   158  					Number:      1,
   159  					Content:     "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   160  					IsCause:     true,
   161  					Highlighted: "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   162  					FirstCause:  true,
   163  					LastCause:   false,
   164  				},
   165  				{
   166  					Number:      2,
   167  					Content:     "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   168  					IsCause:     true,
   169  					Highlighted: "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   170  					FirstCause:  false,
   171  					LastCause:   false,
   172  				},
   173  				{
   174  					Number:      3,
   175  					Content:     "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   176  					IsCause:     true,
   177  					Highlighted: "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   178  					FirstCause:  false,
   179  					LastCause:   false,
   180  				},
   181  				{
   182  					Number:      4,
   183  					Content:     "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   184  					IsCause:     true,
   185  					Highlighted: "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   186  					FirstCause:  false,
   187  					LastCause:   false,
   188  				},
   189  				{
   190  					Number:      5,
   191  					Content:     "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   192  					IsCause:     true,
   193  					Highlighted: "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   194  					FirstCause:  false,
   195  					LastCause:   false,
   196  				},
   197  				{
   198  					Number:      6,
   199  					Content:     "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   200  					IsCause:     true,
   201  					Highlighted: "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   202  					FirstCause:  false,
   203  					LastCause:   false,
   204  				},
   205  				{
   206  					Number:      7,
   207  					Content:     "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   208  					IsCause:     true,
   209  					Highlighted: "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   210  					FirstCause:  false,
   211  					LastCause:   false,
   212  				},
   213  				{
   214  					Number:      8,
   215  					Content:     "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   216  					IsCause:     true,
   217  					Highlighted: "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   218  					FirstCause:  false,
   219  					LastCause:   false,
   220  				},
   221  				{
   222  					Number:      9,
   223  					Content:     "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   224  					IsCause:     true,
   225  					Highlighted: "If you can do a half-assed job of anything, you're a one-eyed man in a kingdom of the blind.",
   226  					FirstCause:  false,
   227  					LastCause:   true,
   228  				},
   229  				{
   230  					Number:    10,
   231  					Truncated: true,
   232  				},
   233  			},
   234  		},
   235  	}
   236  
   237  	for _, test := range tests {
   238  		t.Run(test.name, func(t *testing.T) {
   239  			system := memoryfs.New()
   240  			require.NoError(t, system.WriteFile(test.filename, []byte(test.source), os.ModePerm))
   241  			meta := defsecTypes.NewMetadata(
   242  				defsecTypes.NewRange(test.filename, test.start, test.end, "", system),
   243  				"",
   244  			)
   245  			if test.outerStart > 0 {
   246  				meta = meta.WithParent(defsecTypes.NewMetadata(
   247  					defsecTypes.NewRange(test.filename, test.outerStart, test.outerEnd, "", system),
   248  					"",
   249  				))
   250  			}
   251  			result := &Result{
   252  				annotation: test.annotation,
   253  				metadata:   meta,
   254  				fsPath:     test.filename,
   255  			}
   256  			code, err := result.GetCode(test.options...)
   257  			if test.wantErr {
   258  				require.Error(t, err)
   259  				return
   260  			}
   261  			require.NoError(t, err)
   262  			assert.Equal(t, test.expected, code.Lines)
   263  		})
   264  	}
   265  
   266  }