TweakFile writes a xor-ed byte at a random point in a file. Used to simulate file corruption.
(tb testing.TB, dirn, fglob string)
| 124 | |
| 125 | // TweakFile writes a xor-ed byte at a random point in a file. Used to simulate file corruption. |
| 126 | func (e *CLITest) TweakFile(tb testing.TB, dirn, fglob string) { |
| 127 | tb.Helper() |
| 128 | |
| 129 | const RwUserGroupOther = 0o666 |
| 130 | |
| 131 | // find a file within the repository to corrupt |
| 132 | mch, err := fs.Glob(os.DirFS(dirn), fglob) |
| 133 | require.NoError(tb, err) |
| 134 | require.NotEmpty(tb, mch) |
| 135 | |
| 136 | // grab a random file in the directory dirn |
| 137 | fn := mch[rand.Intn(len(mch))] |
| 138 | f, err := os.OpenFile(path.Join(dirn, fn), os.O_RDWR, os.FileMode(RwUserGroupOther)) |
| 139 | require.NoError(tb, err) |
| 140 | |
| 141 | // find the length of the file, then seek to a random location |
| 142 | l, err := f.Seek(0, io.SeekEnd) |
| 143 | require.NoError(tb, err) |
| 144 | |
| 145 | i := rand.Int63n(l) |
| 146 | bs := [1]byte{} |
| 147 | |
| 148 | _, err = f.ReadAt(bs[:], i) |
| 149 | require.NoError(tb, err) |
| 150 | |
| 151 | // write the byte |
| 152 | _, err = f.WriteAt([]byte{^bs[0]}, i) |
| 153 | require.NoError(tb, err) |
| 154 | } |
| 155 | |
| 156 | func (e *CLITest) SetLogOutput(enable bool, prefix string) { |
| 157 | e.logMu.Lock() |