Skip to content

Commit

Permalink
Add more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
n10v committed Aug 15, 2018
1 parent 3845103 commit 5da9331
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 3 deletions.
25 changes: 25 additions & 0 deletions parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -397,5 +397,30 @@ func TestParseReaderNil(t *testing.T) {
if !strings.Contains(err.Error(), "rd is nil") {
t.Fatalf("Expected err contains %q, got %q", "rd is nil", err)
}
}

func TestParseV3UnsafeSize(t *testing.T) {
buf := new(bytes.Buffer)
title := strings.Repeat("A", 254)

tag := NewEmptyTag()
tag.SetVersion(3)
tag.SetTitle(title)
if _, err := tag.WriteTo(buf); err != nil {
t.Fatalf("Error while writing tag: %v", err)
}

titleFrameHeader := buf.Bytes()[tagHeaderSize : tagHeaderSize+frameHeaderSize]
bytesSize := titleFrameHeader[4:8]
if !bytes.Equal(bytesSize, []byte{0, 0, 0, 255}) {
t.Fatalf("bytesSize should be equal to [0 0 0 255], but it's %v", bytesSize)
}

parsedTag, err := ParseReader(buf, Options{Parse: true})
if err != nil {
t.Fatalf("Error while parsing tag: %v", err)
}
if parsedTag.Title() != title {
t.Fatalf("Titles are not equal: len(parsedTag.Title()) == %v, len(title) == %v", len(parsedTag.Title()), len(title))
}
}
18 changes: 15 additions & 3 deletions size_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ var (
synchSafeSizeUint uint = 15351
synchSafeSizeBytes = []byte{0, 0, 119, 119}

synchUnsafeSizeUint uint = 255
synchUnsafeSizeBytes = []byte{0, 0, 0, 255}
synchUnsafeSizeUint uint = 65535
synchUnsafeSizeBytes = []byte{0, 0, 255, 255}
)

func testWriteSize(sizeUint uint, sizeBytes []byte, synchSafe bool, t *testing.T) {
Expand Down Expand Up @@ -52,10 +52,22 @@ func TestParseSynchSafeSize(t *testing.T) {
testParseSize(synchSafeSizeUint, synchSafeSizeBytes, true, t)
}

func TestWriteSynchUnsafeBytesSize(t *testing.T) {
func TestWriteSynchUnsafeSize(t *testing.T) {
testWriteSize(synchUnsafeSizeUint, synchUnsafeSizeBytes, false, t)
}

func TestParseSynchUnsafeSize(t *testing.T) {
testParseSize(synchUnsafeSizeUint, synchUnsafeSizeBytes, false, t)
}

func TestParseSynchUnsafeSizeUsingSynchSafeFlag(t *testing.T) {
t.Parallel()

_, err := parseSize(synchUnsafeSizeBytes, true)
if err == nil {
t.Fatal("Expected error, got nil")
}
if err != ErrInvalidSizeFormat {
t.Fatalf("Expected ErrInvalidSizeFormat, got %v", err)
}
}

0 comments on commit 5da9331

Please sign in to comment.