Skip to content

Commit

Permalink
util/encrypt: migrate TestReadAt from test-infra to testify (pingcap#…
Browse files Browse the repository at this point in the history
…27816)

Signed-off-by: Karuppiah Natarajan <karuppiah7890@users.noreply.github.com>
  • Loading branch information
karuppiah7890 committed Sep 13, 2021
1 parent 27ab47e commit aa9a023
Showing 1 changed file with 16 additions and 20 deletions.
36 changes: 16 additions & 20 deletions util/encrypt/aes_layer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,29 +20,25 @@ import (
"os"
"testing"

"github.com/pingcap/check"
"github.com/pingcap/tidb/util/checksum"
"github.com/stretchr/testify/require"
)

var _ = check.Suite(&testAesLayerSuite{})

type testAesLayerSuite struct{}

type readAtTestCase struct {
name string
newWriter func(f *os.File) io.WriteCloser
newReader func(f *os.File) io.ReaderAt
}

func testReadAtWithCase(c *check.C, testCase readAtTestCase) {
func testReadAtWithCase(t *testing.T, testCase readAtTestCase) {
path := "ase"
f, err := os.Create(path)
c.Assert(err, check.IsNil)
require.NoError(t, err)
defer func() {
err = f.Close()
c.Assert(err, check.IsNil)
require.NoError(t, err)
err = os.Remove(path)
c.Assert(err, check.IsNil)
require.NoError(t, err)
}()

writeString := "0123456789"
Expand All @@ -53,34 +49,34 @@ func testReadAtWithCase(c *check.C, testCase readAtTestCase) {

w := testCase.newWriter(f)
n1, err := w.Write(buf.Bytes())
c.Assert(err, check.IsNil)
require.NoError(t, err)
n2, err := w.Write(buf.Bytes())
c.Assert(err, check.IsNil)
require.NoError(t, err)
err = w.Close()
c.Assert(err, check.IsNil)
require.NoError(t, err)

f, err = os.Open(path)
c.Assert(err, check.IsNil)
require.NoError(t, err)

assertReadAt := func(off int64, assertErr interface{}, assertN int, assertString string) {
r := testCase.newReader(f)
buf := make([]byte, 10)
n, err := r.ReadAt(buf, off)
c.Assert(err, check.Equals, assertErr)
c.Assert(n, check.Equals, assertN)
c.Assert(string(buf), check.Equals, assertString)
require.Equal(t, assertErr, err)
require.Equal(t, assertN, n)
require.Equal(t, assertString, string(buf))
}

assertReadAt(0, nil, 10, "0123456789")
assertReadAt(5, nil, 10, "5678901234")
assertReadAt(int64(n1+n2)-5, io.EOF, 5, "56789\x00\x00\x00\x00\x00")
}

func (s *testAesLayerSuite) TestReadAt(c *check.C) {
func TestReadAt(t *testing.T) {
ctrCipher1, err := NewCtrCipher()
c.Assert(err, check.IsNil)
require.NoError(t, err)
ctrCipher2, err := NewCtrCipher()
c.Assert(err, check.IsNil)
require.NoError(t, err)

readAtTestCases := []readAtTestCase{
{
Expand All @@ -102,7 +98,7 @@ func (s *testAesLayerSuite) TestReadAt(c *check.C) {
}

for _, tCase := range readAtTestCases {
testReadAtWithCase(c, tCase)
testReadAtWithCase(t, tCase)
}
}

Expand Down

0 comments on commit aa9a023

Please sign in to comment.