2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2018-08-06 18:14:08 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-09-04 12:45:06 -04:00
|
|
|
"context"
|
2019-01-17 07:58:18 -05:00
|
|
|
"io"
|
2018-08-06 18:14:08 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2019-01-19 21:28:40 -05:00
|
|
|
func testBitrotReaderWriterAlgo(t *testing.T, bitrotAlgo BitrotAlgorithm) {
|
2022-07-25 15:37:26 -04:00
|
|
|
tmpDir := t.TempDir()
|
2018-08-06 18:14:08 -04:00
|
|
|
|
|
|
|
volume := "testvol"
|
|
|
|
filePath := "testfile"
|
|
|
|
|
2020-08-25 13:55:15 -04:00
|
|
|
disk, err := newLocalXLStorage(tmpDir)
|
2018-08-06 18:14:08 -04:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-09-04 12:45:06 -04:00
|
|
|
disk.MakeVol(context.Background(), volume)
|
2018-08-06 18:14:08 -04:00
|
|
|
|
2021-05-17 11:32:28 -04:00
|
|
|
writer := newBitrotWriter(disk, volume, filePath, 35, bitrotAlgo, 10)
|
2018-08-06 18:14:08 -04:00
|
|
|
|
2019-01-17 07:58:18 -05:00
|
|
|
_, err = writer.Write([]byte("aaaaaaaaaa"))
|
2018-08-06 18:14:08 -04:00
|
|
|
if err != nil {
|
2021-02-24 03:14:16 -05:00
|
|
|
t.Fatal(err)
|
2018-08-06 18:14:08 -04:00
|
|
|
}
|
2019-01-17 07:58:18 -05:00
|
|
|
_, err = writer.Write([]byte("aaaaaaaaaa"))
|
2018-08-06 18:14:08 -04:00
|
|
|
if err != nil {
|
2021-02-24 03:14:16 -05:00
|
|
|
t.Fatal(err)
|
2018-08-06 18:14:08 -04:00
|
|
|
}
|
2019-01-17 07:58:18 -05:00
|
|
|
_, err = writer.Write([]byte("aaaaaaaaaa"))
|
2018-08-06 18:14:08 -04:00
|
|
|
if err != nil {
|
2021-02-24 03:14:16 -05:00
|
|
|
t.Fatal(err)
|
2018-08-06 18:14:08 -04:00
|
|
|
}
|
2019-01-17 07:58:18 -05:00
|
|
|
_, err = writer.Write([]byte("aaaaa"))
|
2018-08-06 18:14:08 -04:00
|
|
|
if err != nil {
|
2021-02-24 03:14:16 -05:00
|
|
|
t.Fatal(err)
|
2018-08-06 18:14:08 -04:00
|
|
|
}
|
2022-07-25 15:37:26 -04:00
|
|
|
if bw, ok := writer.(io.Closer); ok {
|
|
|
|
bw.Close()
|
|
|
|
}
|
2019-01-17 07:58:18 -05:00
|
|
|
|
2021-01-07 22:27:31 -05:00
|
|
|
reader := newBitrotReader(disk, nil, volume, filePath, 35, bitrotAlgo, bitrotWriterSum(writer), 10)
|
2019-01-17 07:58:18 -05:00
|
|
|
b := make([]byte, 10)
|
|
|
|
if _, err = reader.ReadAt(b, 0); err != nil {
|
2021-02-24 03:14:16 -05:00
|
|
|
t.Fatal(err)
|
2018-08-06 18:14:08 -04:00
|
|
|
}
|
2019-01-17 07:58:18 -05:00
|
|
|
if _, err = reader.ReadAt(b, 10); err != nil {
|
2021-02-24 03:14:16 -05:00
|
|
|
t.Fatal(err)
|
2019-01-17 07:58:18 -05:00
|
|
|
}
|
|
|
|
if _, err = reader.ReadAt(b, 20); err != nil {
|
2021-02-24 03:14:16 -05:00
|
|
|
t.Fatal(err)
|
2019-01-17 07:58:18 -05:00
|
|
|
}
|
|
|
|
if _, err = reader.ReadAt(b[:5], 30); err != nil {
|
2021-02-24 03:14:16 -05:00
|
|
|
t.Fatal(err)
|
2018-08-06 18:14:08 -04:00
|
|
|
}
|
2022-07-25 15:37:26 -04:00
|
|
|
if br, ok := reader.(io.Closer); ok {
|
|
|
|
br.Close()
|
|
|
|
}
|
2018-08-06 18:14:08 -04:00
|
|
|
}
|
2019-01-19 21:28:40 -05:00
|
|
|
|
|
|
|
func TestAllBitrotAlgorithms(t *testing.T) {
|
|
|
|
for bitrotAlgo := range bitrotAlgorithms {
|
|
|
|
testBitrotReaderWriterAlgo(t, bitrotAlgo)
|
|
|
|
}
|
|
|
|
}
|