X-Git-Url: https://git.r.bdr.sh/rbdr/mobius/blobdiff_plain/92a7e455a347e5be7fb69b6846b9f27ca698ae12..00913df3521d967ac31ee22c23447b5aa711f2e6:/hotline/file_path_test.go?ds=inline diff --git a/hotline/file_path_test.go b/hotline/file_path_test.go index 4c2f6db..effd462 100644 --- a/hotline/file_path_test.go +++ b/hotline/file_path_test.go @@ -1,11 +1,12 @@ package hotline import ( + "fmt" "github.com/stretchr/testify/assert" "testing" ) -func TestFilePath_UnmarshalBinary(t *testing.T) { +func TestFilePath_Write(t *testing.T) { type args struct { b []byte } @@ -41,12 +42,23 @@ func TestFilePath_UnmarshalBinary(t *testing.T) { }, wantErr: false, }, + { + name: "handles empty data payload", + args: args{b: []byte{ + 0x00, 0x00, + }}, + want: FilePath{ + ItemCount: [2]byte{0x00, 0x00}, + Items: []FilePathItem(nil), + }, + wantErr: false, + }, } for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { var fp FilePath - if err := fp.UnmarshalBinary(tt.args.b); (err != nil) != tt.wantErr { - t.Errorf("UnmarshalBinary() error = %v, wantErr %v", err, tt.wantErr) + if _, err := fp.Write(tt.args.b); (err != nil) != tt.wantErr { + t.Errorf("Write() error = %v, wantErr %v", err, tt.wantErr) } if !assert.Equal(t, tt.want, fp) { t.Errorf("Read() got = %v, want %v", fp, tt.want) @@ -78,7 +90,7 @@ func Test_readPath(t *testing.T) { 0x61, 0x61, 0x61, }, }, - want: "", + want: "", wantErr: true, }, { @@ -87,7 +99,6 @@ func Test_readPath(t *testing.T) { fileRoot: "/usr/local/var/mobius/Files", filePath: nil, fileName: []byte("foo"), - }, want: "/usr/local/var/mobius/Files/foo", }, @@ -153,4 +164,78 @@ func Test_readPath(t *testing.T) { } }) } -} \ No newline at end of file +} + +func Test_fileItemScanner(t *testing.T) { + type args struct { + data []byte + in1 bool + } + tests := []struct { + name string + args args + wantAdvance int + wantToken []byte + wantErr assert.ErrorAssertionFunc + }{ + { + name: "when a full fileItem is provided", + args: args{ + data: []byte{ + 0, 0, + 0x09, + 0x73, 0x75, 0x62, 0x66, 0x6f, 0x6c, 0x64, 0x65, 0x72, + }, + in1: false, + }, + wantAdvance: 12, + wantToken: []byte{ + 0, 0, + 0x09, + 0x73, 0x75, 0x62, 0x66, 0x6f, 0x6c, 0x64, 0x65, 0x72, + }, + wantErr: assert.NoError, + }, + { + name: "when a full fileItem with extra bytes is provided", + args: args{ + data: []byte{ + 0, 0, + 0x09, + 0x73, 0x75, 0x62, 0x66, 0x6f, 0x6c, 0x64, 0x65, 0x72, + 1, 1, 1, 1, 1, 1, + }, + in1: false, + }, + wantAdvance: 12, + wantToken: []byte{ + 0, 0, + 0x09, + 0x73, 0x75, 0x62, 0x66, 0x6f, 0x6c, 0x64, 0x65, 0x72, + }, + wantErr: assert.NoError, + }, + { + name: "when insufficient bytes are provided", + args: args{ + data: []byte{ + 0, 0, + }, + in1: false, + }, + wantAdvance: 0, + wantToken: []byte(nil), + wantErr: assert.NoError, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + gotAdvance, gotToken, err := fileItemScanner(tt.args.data, tt.args.in1) + if !tt.wantErr(t, err, fmt.Sprintf("fileItemScanner(%v, %v)", tt.args.data, tt.args.in1)) { + return + } + assert.Equalf(t, tt.wantAdvance, gotAdvance, "fileItemScanner(%v, %v)", tt.args.data, tt.args.in1) + assert.Equalf(t, tt.wantToken, gotToken, "fileItemScanner(%v, %v)", tt.args.data, tt.args.in1) + }) + } +}