diff --git a/supernode/daemon/mgr/cdn/super_reader_test.go b/supernode/daemon/mgr/cdn/super_reader_test.go index d5fa95be7..59bab350e 100644 --- a/supernode/daemon/mgr/cdn/super_reader_test.go +++ b/supernode/daemon/mgr/cdn/super_reader_test.go @@ -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" "github.com/go-check/check" ) @@ -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")