func TestReadNextChunk(t *testing.T) { s := wire.NewSyncScanner(strings.NewReader( "DATA\006\000\000\000hello DATA\005\000\000\000worldDONE")) // Read 1st chunk reader, err := readNextChunk(s) assert.NoError(t, err) assert.Equal(t, int64(6), reader.(*io.LimitedReader).N) buf := make([]byte, 10) n, err := reader.Read(buf) assert.NoError(t, err) assert.Equal(t, 6, n) assert.Equal(t, "hello ", string(buf[:6])) // Read 2nd chunk reader, err = readNextChunk(s) assert.NoError(t, err) assert.Equal(t, int64(5), reader.(*io.LimitedReader).N) buf = make([]byte, 10) n, err = reader.Read(buf) assert.NoError(t, err) assert.Equal(t, 5, n) assert.Equal(t, "world", string(buf[:5])) // Read DONE _, err = readNextChunk(s) assert.Equal(t, io.EOF, err) }
func TestReadNextChunkInvalidChunkId(t *testing.T) { s := wire.NewSyncScanner(strings.NewReader( "ATAD\006\000\000\000hello ")) // Read 1st chunk _, err := readNextChunk(s) assert.EqualError(t, err, "expected chunk id 'DATA', but got 'ATAD'") }
func TestStatBadResponse(t *testing.T) { var buf bytes.Buffer conn := &wire.SyncConn{wire.NewSyncScanner(&buf), wire.NewSyncSender(&buf)} conn.SendOctetString("SPAT") entry, err := stat(conn, "/") assert.Nil(t, entry) assert.Error(t, err) }
func TestReadAll(t *testing.T) { s := wire.NewSyncScanner(strings.NewReader( "DATA\006\000\000\000hello DATA\005\000\000\000worldDONE")) reader := newSyncFileReader(s) buf := make([]byte, 20) _, err := io.ReadFull(reader, buf) assert.Equal(t, io.ErrUnexpectedEOF, err) assert.Equal(t, "hello world\000", string(buf[:12])) }
func TestStatNoExist(t *testing.T) { var buf bytes.Buffer conn := &wire.SyncConn{wire.NewSyncScanner(&buf), wire.NewSyncSender(&buf)} conn.SendOctetString("STAT") conn.SendFileMode(0) conn.SendInt32(0) conn.SendTime(time.Unix(0, 0).UTC()) entry, err := stat(conn, "/") assert.Nil(t, entry) assert.Equal(t, util.FileNoExistError, err.(*util.Err).Code) }
func TestStatValid(t *testing.T) { var buf bytes.Buffer conn := &wire.SyncConn{wire.NewSyncScanner(&buf), wire.NewSyncSender(&buf)} var mode os.FileMode = 0777 conn.SendOctetString("STAT") conn.SendFileMode(mode) conn.SendInt32(4) conn.SendTime(someTime) entry, err := stat(conn, "/thing") assert.NoError(t, err) require.NotNil(t, entry) assert.Equal(t, mode, entry.Mode, "expected os.FileMode %s, got %s", mode, entry.Mode) assert.Equal(t, int32(4), entry.Size) assert.Equal(t, someTime, entry.ModifiedAt) assert.Equal(t, "", entry.Name) }
func TestReadMultipleCalls(t *testing.T) { s := wire.NewSyncScanner(strings.NewReader( "DATA\006\000\000\000hello DATA\005\000\000\000worldDONE")) reader := newSyncFileReader(s) firstByte := make([]byte, 1) _, err := io.ReadFull(reader, firstByte) assert.NoError(t, err) assert.Equal(t, "h", string(firstByte)) restFirstChunkBytes := make([]byte, 5) _, err = io.ReadFull(reader, restFirstChunkBytes) assert.NoError(t, err) assert.Equal(t, "ello ", string(restFirstChunkBytes)) secondChunkBytes := make([]byte, 5) _, err = io.ReadFull(reader, secondChunkBytes) assert.NoError(t, err) assert.Equal(t, "world", string(secondChunkBytes)) _, err = io.ReadFull(reader, make([]byte, 5)) assert.Equal(t, io.EOF, err) }