Skip to content
This repository was archived by the owner on Dec 20, 2024. It is now read-only.
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 30 additions & 2 deletions supernode/daemon/mgr/cdn/super_reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@ package cdn

import (
"bytes"
"context"
"crypto/md5"
"encoding/binary"
"fmt"

"github.com/dragonflyoss/Dragonfly/pkg/fileutils"

"github.com/dragonflyoss/Dragonfly/supernode/config"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a blank line below

"github.com/go-check/check"
)

Expand All @@ -36,7 +38,33 @@ func init() {
check.Suite(&SuperReaderTestSuite{})
}

// TODO: add more unit tests
func (s *SuperReaderTestSuite) TestReadFile(c *check.C) {
testStr1 := []byte("hello ")
testStr2 := []byte("dragonfly")
testPiece1 := append(append([]byte{0, 0, 0, 6}, testStr1...), 0x7f)
testPiece2 := append(append([]byte{0, 0, 0, 9}, testStr2...), 0x7f)
testStr := append(testPiece1, testPiece2...)

contentBuf := &bytes.Buffer{}
binary.Write(contentBuf, binary.BigEndian, testStr)

cacheReader := newSuperReader()
result, err := cacheReader.readFile(context.Background(), contentBuf, true, true)

c.Check(err, check.IsNil)
c.Check(int64(len(testStr)), check.Equals, result.fileLength)
c.Check(2, check.Equals, result.pieceCount)
md5Init := md5.New()
md5Init.Write(testPiece1)
c.Check(fmt.Sprintf("%s:%d", fileutils.GetMd5Sum(md5Init, nil), len(testStr1)+config.PieceWrapSize), check.Equals, result.pieceMd5s[0])
md5Init.Reset()
md5Init.Write(testPiece2)
c.Check(fmt.Sprintf("%s:%d", fileutils.GetMd5Sum(md5Init, nil), len(testStr2)+config.PieceWrapSize), check.Equals, result.pieceMd5s[1])
md5Init.Reset()
md5Init.Write(testStr1)
md5Init.Write(testStr2)
c.Check(fileutils.GetMd5Sum(md5Init, nil), check.Equals, fileutils.GetMd5Sum(result.fileMd5, nil))
}

func (s *SuperReaderTestSuite) TestGetMD5ByReadFile(c *check.C) {
testStr := []byte("hello dragonfly")
Expand Down