...

Source file src/edge-infra.dev/pkg/sds/emergencyaccess/emulator/session_test.go

Documentation: edge-infra.dev/pkg/sds/emergencyaccess/emulator

     1  package emulator
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  //nolint:goconst
    10  func TestProcessSessionInput(t *testing.T) {
    11  	t.Parallel()
    12  
    13  	tests := map[string]struct {
    14  		input string
    15  
    16  		command    string
    17  		opts       CommandOpts
    18  		errChecker assert.ErrorAssertionFunc
    19  	}{
    20  		"No Options": {
    21  			input:      "echo hello",
    22  			command:    "echo hello",
    23  			opts:       CommandOpts{},
    24  			errChecker: assert.NoError,
    25  		},
    26  		"With options": {
    27  			input:      "echo hello " + sendOptionsSplitter + ` > file.txt`,
    28  			command:    "echo hello ", // Notice trailing space
    29  			opts:       CommandOpts{FileRedirection: "file.txt"},
    30  			errChecker: assert.NoError,
    31  		},
    32  		"Empty Options": {
    33  			input:   `echo hello ` + sendOptionsSplitter + ` `,
    34  			command: "echo hello ",
    35  			opts:    CommandOpts{},
    36  			// TODO confirm error type/value
    37  			errChecker: assert.Error,
    38  		},
    39  		"Unknown option type": {
    40  			input:      `echo hello ` + sendOptionsSplitter + ` | file.txt`,
    41  			command:    "echo hello ",
    42  			opts:       CommandOpts{},
    43  			errChecker: assert.Error,
    44  		},
    45  		"Too many options": {
    46  			input:      `echo hello ` + sendOptionsSplitter + ` > file.txt help`,
    47  			command:    "echo hello ",
    48  			opts:       CommandOpts{},
    49  			errChecker: assert.Error,
    50  		},
    51  		"Too few options": {
    52  			input:      `echo hello ` + sendOptionsSplitter + ` > `,
    53  			command:    "echo hello ",
    54  			opts:       CommandOpts{},
    55  			errChecker: assert.Error,
    56  		},
    57  		"Invalid command": {
    58  			// Command includes an incomplete quote
    59  			// Currently the split on the separator is done first, and then options
    60  			// validation is carried out, without validating command, so this passes
    61  			// Command validation is expected later on, e.g. in cliservices
    62  			input:      `echo "hello ` + sendOptionsSplitter + ` > file.txt`,
    63  			command:    `echo "hello `,
    64  			opts:       CommandOpts{FileRedirection: "file.txt"},
    65  			errChecker: assert.NoError,
    66  		},
    67  		"Invalid options": {
    68  			// Options include an incomplete quote
    69  			// Options are validated so we would expect an error returned here.
    70  			input:      `echo hello ` + sendOptionsSplitter + ` > "file.txt`,
    71  			command:    `echo hello `,
    72  			opts:       CommandOpts{},
    73  			errChecker: assert.Error,
    74  		},
    75  		"homonym": {
    76  			// It looks the same but isn't
    77  			input:      `echo hello ` + `||||` + ` > file.txt`,
    78  			command:    `echo hello |||| > file.txt`,
    79  			opts:       CommandOpts{},
    80  			errChecker: assert.NoError,
    81  		},
    82  	}
    83  
    84  	for name, tc := range tests {
    85  		tc := tc
    86  		t.Run(name, func(t *testing.T) {
    87  			command, opts, err := processSessionInput(tc.input)
    88  			tc.errChecker(t, err)
    89  
    90  			assert.Equal(t, tc.command, command)
    91  			assert.Equal(t, tc.opts, opts)
    92  		})
    93  	}
    94  }
    95  

View as plain text