github.com/whtcorpsinc/MilevaDB-Prod@v0.0.0-20211104133533-f57f4be3b597/soliton/encrypt/aes_layer_test.go (about) 1 // Copyright 2020 WHTCORPS INC, Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // See the License for the specific language governing permissions and 12 // limitations under the License. 13 14 package encrypt 15 16 import ( 17 "bytes" 18 "io" 19 "os" 20 "testing" 21 22 "github.com/whtcorpsinc/check" 23 "github.com/whtcorpsinc/milevadb/soliton/checksum" 24 ) 25 26 var _ = check.Suite(&testAesLayerSuite{}) 27 28 type testAesLayerSuite struct{} 29 30 type readAtTestCase struct { 31 name string 32 newWriter func(f *os.File) io.WriteCloser 33 newReader func(f *os.File) io.ReaderAt 34 } 35 36 func testReadAtWithCase(c *check.C, testCase readAtTestCase) { 37 path := "ase" 38 f, err := os.Create(path) 39 c.Assert(err, check.IsNil) 40 defer func() { 41 err = f.Close() 42 c.Assert(err, check.IsNil) 43 err = os.Remove(path) 44 c.Assert(err, check.IsNil) 45 }() 46 47 writeString := "0123456789" 48 buf := bytes.NewBuffer(nil) 49 for i := 0; i < 510; i++ { 50 buf.WriteString(writeString) 51 } 52 53 w := testCase.newWriter(f) 54 n1, err := w.Write(buf.Bytes()) 55 c.Assert(err, check.IsNil) 56 n2, err := w.Write(buf.Bytes()) 57 c.Assert(err, check.IsNil) 58 err = w.Close() 59 c.Assert(err, check.IsNil) 60 61 f, err = os.Open(path) 62 c.Assert(err, check.IsNil) 63 64 assertReadAt := func(off int64, assertErr interface{}, assertN int, assertString string) { 65 r := testCase.newReader(f) 66 buf := make([]byte, 10) 67 n, err := r.ReadAt(buf, off) 68 c.Assert(err, check.Equals, assertErr) 69 c.Assert(n, check.Equals, assertN) 70 c.Assert(string(buf), check.Equals, assertString) 71 } 72 73 assertReadAt(0, nil, 10, "0123456789") 74 assertReadAt(5, nil, 10, "5678901234") 75 assertReadAt(int64(n1+n2)-5, io.EOF, 5, "56789\x00\x00\x00\x00\x00") 76 } 77 78 func (s *testAesLayerSuite) TestReadAt(c *check.C) { 79 ctrCipher1, err := NewCtrCipher() 80 c.Assert(err, check.IsNil) 81 ctrCipher2, err := NewCtrCipher() 82 c.Assert(err, check.IsNil) 83 84 readAtTestCases := []readAtTestCase{ 85 { 86 newWriter: func(f *os.File) io.WriteCloser { return NewWriter(f, ctrCipher1) }, 87 newReader: func(f *os.File) io.ReaderAt { return NewReader(f, ctrCipher1) }, 88 }, 89 { 90 newWriter: func(f *os.File) io.WriteCloser { return checksum.NewWriter(NewWriter(f, ctrCipher1)) }, 91 newReader: func(f *os.File) io.ReaderAt { return checksum.NewReader(NewReader(f, ctrCipher1)) }, 92 }, 93 { 94 newWriter: func(f *os.File) io.WriteCloser { return NewWriter(checksum.NewWriter(f), ctrCipher1) }, 95 newReader: func(f *os.File) io.ReaderAt { return NewReader(checksum.NewReader(f), ctrCipher1) }, 96 }, 97 { 98 newWriter: func(f *os.File) io.WriteCloser { return NewWriter(NewWriter(f, ctrCipher1), ctrCipher2) }, 99 newReader: func(f *os.File) io.ReaderAt { return NewReader(NewReader(f, ctrCipher1), ctrCipher2) }, 100 }, 101 } 102 103 for _, tCase := range readAtTestCases { 104 testReadAtWithCase(c, tCase) 105 } 106 } 107 108 func benchmarkReadAtWithCase(b *testing.B, testCase readAtTestCase) { 109 path := "ase" 110 f, err := os.Create(path) 111 if err != nil { 112 b.Fatal(err) 113 } 114 defer func() { 115 err = f.Close() 116 if err != nil { 117 b.Fatal(err) 118 } 119 err = os.Remove(path) 120 if err != nil { 121 b.Fatal(err) 122 } 123 }() 124 125 writeString := "0123456789" 126 buf := bytes.NewBuffer(nil) 127 for i := 0; i < 510; i++ { 128 buf.WriteString(writeString) 129 } 130 131 w := testCase.newWriter(f) 132 n1, err := w.Write(buf.Bytes()) 133 if err != nil { 134 b.Fatal(err) 135 } 136 n2, err := w.Write(buf.Bytes()) 137 if err != nil { 138 b.Fatal(err) 139 } 140 err = w.Close() 141 if err != nil { 142 b.Fatal(err) 143 } 144 f, err = os.Open(path) 145 if err != nil { 146 b.Fatal(err) 147 } 148 149 r := testCase.newReader(f) 150 rBuf := make([]byte, 10) 151 b.ResetTimer() 152 for i := 0; i < b.N; i++ { 153 r.ReadAt(rBuf, int64(i%(n1+n2))) 154 } 155 } 156 157 func BenchmarkReadAt(b *testing.B) { 158 ctrCipher, err := NewCtrCipher() 159 if err != nil { 160 b.Fatal(err) 161 } 162 163 readAtTestCases := []readAtTestCase{ 164 { 165 name: "data->file", 166 newWriter: func(f *os.File) io.WriteCloser { return f }, 167 newReader: func(f *os.File) io.ReaderAt { return f }, 168 }, 169 { 170 name: "data->checksum->file", 171 newWriter: func(f *os.File) io.WriteCloser { return checksum.NewWriter(f) }, 172 newReader: func(f *os.File) io.ReaderAt { return checksum.NewReader(f) }, 173 }, 174 { 175 name: "data->checksum->encrypt->file", 176 newWriter: func(f *os.File) io.WriteCloser { 177 return checksum.NewWriter(NewWriter(f, ctrCipher)) 178 }, 179 newReader: func(f *os.File) io.ReaderAt { 180 return checksum.NewReader(NewReader(f, ctrCipher)) 181 }, 182 }, 183 } 184 185 for _, tCase := range readAtTestCases { 186 b.Run(tCase.name, func(b *testing.B) { 187 benchmarkReadAtWithCase(b, tCase) 188 }) 189 } 190 }