feat(errorsx): cover more syscall errors and add tests (#430)
* feat(errorsx): cover more syscall errors and add tests Part of https://github.com/ooni/probe/issues/1505 * fix tests
This commit is contained in:
parent
3747598b4a
commit
14c1640f7f
7 changed files with 293 additions and 48 deletions
|
|
@ -6,6 +6,7 @@ import (
|
|||
"os"
|
||||
"time"
|
||||
|
||||
"github.com/iancoleman/strcase"
|
||||
"golang.org/x/sys/execabs"
|
||||
)
|
||||
|
||||
|
|
@ -18,13 +19,10 @@ type ErrorSpec struct {
|
|||
Failure string
|
||||
}
|
||||
|
||||
// TODO(kelmenhorst): find out if we need more system errors here. Currently
|
||||
// the code does not generate any mapping if Failure is empty.
|
||||
|
||||
// Specs contains all the error specs.
|
||||
var Specs = []ErrorSpec{{
|
||||
Errno: "ECANCELED",
|
||||
Failure: "Interrupted",
|
||||
Failure: "OperationCanceled",
|
||||
}, {
|
||||
Errno: "ECONNREFUSED",
|
||||
Failure: "ConnectionRefused",
|
||||
|
|
@ -36,53 +34,76 @@ var Specs = []ErrorSpec{{
|
|||
Failure: "HostUnreachable",
|
||||
}, {
|
||||
Errno: "ETIMEDOUT",
|
||||
Failure: "GenericTimeoutError",
|
||||
Failure: "TimedOut",
|
||||
}, {
|
||||
Errno: "EAFNOSUPPORT",
|
||||
Errno: "EAFNOSUPPORT",
|
||||
Failure: "AddressFamilyNotSupported",
|
||||
}, {
|
||||
Errno: "EADDRINUSE",
|
||||
Errno: "EADDRINUSE",
|
||||
Failure: "AddressInUse",
|
||||
}, {
|
||||
Errno: "EADDRNOTAVAIL",
|
||||
Errno: "EADDRNOTAVAIL",
|
||||
Failure: "AddressNotAvailable",
|
||||
}, {
|
||||
Errno: "EISCONN",
|
||||
Errno: "EISCONN",
|
||||
Failure: "AlreadyConnected",
|
||||
}, {
|
||||
Errno: "EFAULT",
|
||||
Errno: "EFAULT",
|
||||
Failure: "BadAddress",
|
||||
}, {
|
||||
Errno: "EBADF",
|
||||
Errno: "EBADF",
|
||||
Failure: "BadFileDescriptor",
|
||||
}, {
|
||||
Errno: "ECONNABORTED",
|
||||
Errno: "ECONNABORTED",
|
||||
Failure: "ConnectionAborted",
|
||||
}, {
|
||||
Errno: "EALREADY",
|
||||
Errno: "EALREADY",
|
||||
Failure: "ConnectionAlreadyInProgress",
|
||||
}, {
|
||||
Errno: "EDESTADDRREQ",
|
||||
Errno: "EDESTADDRREQ",
|
||||
Failure: "DestinationAddressRequired",
|
||||
}, {
|
||||
Errno: "EINTR",
|
||||
Errno: "EINTR",
|
||||
Failure: "Interrupted",
|
||||
}, {
|
||||
Errno: "EINVAL",
|
||||
Errno: "EINVAL",
|
||||
Failure: "InvalidArgument",
|
||||
}, {
|
||||
Errno: "EMSGSIZE",
|
||||
Errno: "EMSGSIZE",
|
||||
Failure: "MessageSize",
|
||||
}, {
|
||||
Errno: "ENETDOWN",
|
||||
Errno: "ENETDOWN",
|
||||
Failure: "NetworkDown",
|
||||
}, {
|
||||
Errno: "ENETRESET",
|
||||
Errno: "ENETRESET",
|
||||
Failure: "NetworkReset",
|
||||
}, {
|
||||
Errno: "ENETUNREACH",
|
||||
Errno: "ENETUNREACH",
|
||||
Failure: "NetworkUnreachable",
|
||||
}, {
|
||||
Errno: "ENOBUFS",
|
||||
Errno: "ENOBUFS",
|
||||
Failure: "NoBufferSpace",
|
||||
}, {
|
||||
Errno: "ENOPROTOOPT",
|
||||
Errno: "ENOPROTOOPT",
|
||||
Failure: "NoProtocolOption",
|
||||
}, {
|
||||
Errno: "ENOTSOCK",
|
||||
Errno: "ENOTSOCK",
|
||||
Failure: "NotASocket",
|
||||
}, {
|
||||
Errno: "ENOTCONN",
|
||||
Errno: "ENOTCONN",
|
||||
Failure: "NotConnected",
|
||||
}, {
|
||||
Errno: "EWOULDBLOCK",
|
||||
Errno: "EWOULDBLOCK",
|
||||
Failure: "OperationWouldBlock",
|
||||
}, {
|
||||
Errno: "EACCES",
|
||||
Errno: "EACCES",
|
||||
Failure: "PermissionDenied",
|
||||
}, {
|
||||
Errno: "EPROTONOSUPPORT",
|
||||
Errno: "EPROTONOSUPPORT",
|
||||
Failure: "ProtocolNotSupported",
|
||||
}, {
|
||||
Errno: "EPROTOTYPE",
|
||||
Errno: "EPROTOTYPE",
|
||||
Failure: "WrongProtocolType",
|
||||
}}
|
||||
|
||||
func fileCreate(filename string) *os.File {
|
||||
|
|
@ -143,6 +164,19 @@ func writeGenericFile() {
|
|||
fileWrite(filep, "\t\"errors\"\n")
|
||||
fileWrite(filep, "\t\"syscall\"\n")
|
||||
fileWrite(filep, ")\n\n")
|
||||
|
||||
fileWrite(filep, "// This enumeration lists the syscall-derived failures defined at\n")
|
||||
fileWrite(filep, "// https://github.com/ooni/spec/blob/master/data-formats/df-007-errors.md\n")
|
||||
fileWrite(filep, "//\n")
|
||||
fileWrite(filep, "// See also the enumeration at failures.go for the failures that\n")
|
||||
fileWrite(filep, "// DO NOT derive from system call errors.\n")
|
||||
fileWrite(filep, "const (\n")
|
||||
for _, spec := range Specs {
|
||||
filePrintf(filep, "\tFailure%s = \"%s\"\n", spec.Failure,
|
||||
strcase.ToSnake(spec.Failure))
|
||||
}
|
||||
fileWrite(filep, ")\n\n")
|
||||
|
||||
fileWrite(filep, "// toSyscallErr converts a syscall error to the\n")
|
||||
fileWrite(filep, "// proper OONI error. Returns the OONI error string\n")
|
||||
fileWrite(filep, "// on success, an empty string otherwise.\n")
|
||||
|
|
@ -164,6 +198,40 @@ func writeGenericFile() {
|
|||
fileWrite(filep, "\t}\n")
|
||||
fileWrite(filep, "\treturn \"\"\n")
|
||||
fileWrite(filep, "}\n\n")
|
||||
|
||||
fileClose(filep)
|
||||
gofmt(filename)
|
||||
}
|
||||
|
||||
func writeGenericTestFile() {
|
||||
filename := "errno_test.go"
|
||||
filep := fileCreate(filename)
|
||||
|
||||
fileWrite(filep, "// Code generated by go generate; DO NOT EDIT.\n")
|
||||
filePrintf(filep, "// Generated: %+v\n\n", time.Now())
|
||||
fileWrite(filep, "package errorsx\n\n")
|
||||
fileWrite(filep, "import (\n")
|
||||
fileWrite(filep, "\t\"io\"\n")
|
||||
fileWrite(filep, "\t\"syscall\"\n")
|
||||
fileWrite(filep, "\t\"testing\"\n")
|
||||
fileWrite(filep, ")\n\n")
|
||||
|
||||
fileWrite(filep, "func TestToSyscallErr(t *testing.T) {\n")
|
||||
fileWrite(filep, "\tif v := toSyscallErr(io.EOF); v != \"\" {\n")
|
||||
fileWrite(filep, "\t\tt.Fatalf(\"expected empty string, got '%s'\", v)\n")
|
||||
fileWrite(filep, "\t}\n")
|
||||
|
||||
for _, spec := range Specs {
|
||||
filePrintf(filep, "\tif v := toSyscallErr(%s); v != Failure%s {\n", spec.Errno, spec.Failure)
|
||||
filePrintf(filep, "\t\tt.Fatalf(\"expected '%%s', got '%%s'\", Failure%s, v)\n", spec.Failure)
|
||||
fileWrite(filep, "\t}\n")
|
||||
}
|
||||
|
||||
fileWrite(filep, "\tif v := toSyscallErr(syscall.Errno(0)); v != \"\" {\n")
|
||||
fileWrite(filep, "\t\tt.Fatalf(\"expected empty string, got '%s'\", v)\n")
|
||||
fileWrite(filep, "\t}\n")
|
||||
fileWrite(filep, "}\n")
|
||||
|
||||
fileClose(filep)
|
||||
gofmt(filename)
|
||||
}
|
||||
|
|
@ -172,4 +240,5 @@ func main() {
|
|||
writeSystemSpecificFile("unix")
|
||||
writeSystemSpecificFile("windows")
|
||||
writeGenericFile()
|
||||
writeGenericTestFile()
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue