ooni-probe-cli/internal/engine/internal/fsx/fsx_test.go
Simone Basso d57c78bc71
chore: merge probe-engine into probe-cli (#201)
This is how I did it:

1. `git clone https://github.com/ooni/probe-engine internal/engine`

2. ```
(cd internal/engine && git describe --tags)
v0.23.0
```

3. `nvim go.mod` (merging `go.mod` with `internal/engine/go.mod`

4. `rm -rf internal/.git internal/engine/go.{mod,sum}`

5. `git add internal/engine`

6. `find . -type f -name \*.go -exec sed -i 's@/ooni/probe-engine@/ooni/probe-cli/v3/internal/engine@g' {} \;`

7. `go build ./...` (passes)

8. `go test -race ./...` (temporary failure on RiseupVPN)

9. `go mod tidy`

10. this commit message

Once this piece of work is done, we can build a new version of `ooniprobe` that
is using `internal/engine` directly. We need to do more work to ensure all the
other functionality in `probe-engine` (e.g. making mobile packages) are still WAI.

Part of https://github.com/ooni/probe/issues/1335
2021-02-02 12:05:47 +01:00

75 lines
1.5 KiB
Go

package fsx_test
import (
"errors"
"os"
"sync/atomic"
"syscall"
"testing"
"github.com/ooni/probe-cli/v3/internal/engine/internal/fsx"
)
var StateBaseDir = "./testdata/"
type FailingStatFS struct {
CloseCount *int32
}
type FailingStatFile struct {
CloseCount *int32
}
var errStatFailed = errors.New("stat failed")
func (FailingStatFile) Stat() (os.FileInfo, error) {
return nil, errStatFailed
}
func (fs FailingStatFS) Open(pathname string) (fsx.File, error) {
return FailingStatFile{CloseCount: fs.CloseCount}, nil
}
func (fs FailingStatFile) Close() error {
if fs.CloseCount != nil {
atomic.AddInt32(fs.CloseCount, 1)
}
return nil
}
func (FailingStatFile) Read([]byte) (int, error) {
return 0, nil
}
func TestOpenWithFailingStat(t *testing.T) {
var count int32
_, err := fsx.OpenWithFS(FailingStatFS{CloseCount: &count}, StateBaseDir+"testfile.txt")
if !errors.Is(err, errStatFailed) {
t.Errorf("expected error with invalid FS: %+v", err)
}
if count != 1 {
t.Error("expected counter to be equal to 1")
}
}
func TestOpenNonexistentFile(t *testing.T) {
_, err := fsx.Open(StateBaseDir + "invalidtestfile.txt")
if !errors.Is(err, syscall.ENOENT) {
t.Errorf("not the error we expected")
}
}
func TestOpenDirectoryShouldFail(t *testing.T) {
_, err := fsx.Open(StateBaseDir)
if !errors.Is(err, syscall.EISDIR) {
t.Fatalf("not the error we expected: %+v", err)
}
}
func TestOpeningExistingFileShouldWork(t *testing.T) {
file, err := fsx.Open(StateBaseDir + "testfile.txt")
if err != nil {
t.Fatal(err)
}
defer file.Close()
}