|  | 
|  | 1 | +package sketch | 
|  | 2 | + | 
|  | 3 | +import ( | 
|  | 4 | +	"context" | 
|  | 5 | +	"testing" | 
|  | 6 | + | 
|  | 7 | +	"github.com/arduino/arduino-cli/rpc/cc/arduino/cli/commands/v1" | 
|  | 8 | +	"github.com/stretchr/testify/require" | 
|  | 9 | +) | 
|  | 10 | + | 
|  | 11 | +func Test_SketchNameWrongPattern(t *testing.T) { | 
|  | 12 | +	invalidNames := []string{ | 
|  | 13 | +		"&", | 
|  | 14 | +		"", | 
|  | 15 | +		".hello", | 
|  | 16 | +		"_hello", | 
|  | 17 | +		"-hello", | 
|  | 18 | +		"hello*", | 
|  | 19 | +		"||||||||||||||", | 
|  | 20 | +		",`hack[}attempt{];", | 
|  | 21 | +	} | 
|  | 22 | +	for _, name := range invalidNames { | 
|  | 23 | +		_, err := NewSketch(context.Background(), &commands.NewSketchRequest{ | 
|  | 24 | +			SketchName: name, | 
|  | 25 | +			SketchDir:  t.TempDir(), | 
|  | 26 | +		}) | 
|  | 27 | +		require.NotNil(t, err) | 
|  | 28 | + | 
|  | 29 | +		require.Error(t, err, `Can't create sketch: invalid sketch name "%s". Required pattern %s`, | 
|  | 30 | +			name, | 
|  | 31 | +			sketchNameValidationRegex) | 
|  | 32 | +	} | 
|  | 33 | +} | 
|  | 34 | + | 
|  | 35 | +func Test_SketchNameTooLong(t *testing.T) { | 
|  | 36 | +	tooLongName := make([]byte, sketchNameMaxLength+1) | 
|  | 37 | +	for i := range tooLongName { | 
|  | 38 | +		tooLongName[i] = 'a' | 
|  | 39 | +	} | 
|  | 40 | +	_, err := NewSketch(context.Background(), &commands.NewSketchRequest{ | 
|  | 41 | +		SketchName: string(tooLongName), | 
|  | 42 | +		SketchDir:  t.TempDir(), | 
|  | 43 | +	}) | 
|  | 44 | +	require.NotNil(t, err) | 
|  | 45 | + | 
|  | 46 | +	require.Error(t, err, `Can't create sketch: sketch name too long (%d characters). Maximum allowed length is %d`, | 
|  | 47 | +		len(tooLongName), | 
|  | 48 | +		sketchNameMaxLength) | 
|  | 49 | +} | 
|  | 50 | + | 
|  | 51 | +func Test_SketchNameOk(t *testing.T) { | 
|  | 52 | +	lengthLimitName := make([]byte, sketchNameMaxLength) | 
|  | 53 | +	for i := range lengthLimitName { | 
|  | 54 | +		lengthLimitName[i] = 'a' | 
|  | 55 | +	} | 
|  | 56 | +	validNames := []string{ | 
|  | 57 | +		"h", | 
|  | 58 | +		"h.ello", | 
|  | 59 | +		"h..ello-world", | 
|  | 60 | +		"h..ello-world.", | 
|  | 61 | +		"hello_world__", | 
|  | 62 | +		string(lengthLimitName), | 
|  | 63 | +	} | 
|  | 64 | +	for _, name := range validNames { | 
|  | 65 | +		_, err := NewSketch(context.Background(), &commands.NewSketchRequest{ | 
|  | 66 | +			SketchName: name, | 
|  | 67 | +			SketchDir:  t.TempDir(), | 
|  | 68 | +		}) | 
|  | 69 | +		require.Nil(t, err) | 
|  | 70 | +	} | 
|  | 71 | +} | 
0 commit comments