refactor(errorsx): autogenerate all failure names (#474)

Part of https://github.com/ooni/probe/issues/1591
This commit is contained in:
Simone Basso 2021-09-07 14:50:38 +02:00 committed by GitHub
parent 8b38ea7e98
commit 5c217594d7
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 69 additions and 56 deletions

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-07 13:05:58.447943 +0200 CEST m=+0.137110292 // Generated: 2021-09-07 14:38:45.497503 +0200 CEST m=+0.144277376
package errorsx package errorsx
@ -10,12 +10,12 @@ import (
"syscall" "syscall"
) )
// This enumeration lists the syscall-derived failures defined at // This enumeration lists the failures defined at
// https://github.com/ooni/spec/blob/master/data-formats/df-007-errors.md // https://github.com/ooni/spec/blob/master/data-formats/df-007-errors.md
//
// See also the enumeration at failures.go for the failures that
// DO NOT derive from system call errors.
const ( const (
//
// System errors
//
FailureOperationCanceled = "operation_canceled" FailureOperationCanceled = "operation_canceled"
FailureConnectionRefused = "connection_refused" FailureConnectionRefused = "connection_refused"
FailureConnectionReset = "connection_reset" FailureConnectionReset = "connection_reset"
@ -44,6 +44,20 @@ const (
FailurePermissionDenied = "permission_denied" FailurePermissionDenied = "permission_denied"
FailureProtocolNotSupported = "protocol_not_supported" FailureProtocolNotSupported = "protocol_not_supported"
FailureWrongProtocolType = "wrong_protocol_type" FailureWrongProtocolType = "wrong_protocol_type"
//
// Library errors
//
FailureDNSBogonError = "dns_bogon_error"
FailureDNSNXDOMAINError = "dns_nxdomain_error"
FailureEOFError = "eof_error"
FailureGenericTimeoutError = "generic_timeout_error"
FailureQUICIncompatibleVersion = "quic_incompatible_version"
FailureSSLFailedHandshake = "ssl_failed_handshake"
FailureSSLInvalidHostname = "ssl_invalid_hostname"
FailureSSLUnknownAuthority = "ssl_unknown_authority"
FailureSSLInvalidCertificate = "ssl_invalid_certificate"
FailureJSONParseError = "json_parse_error"
) )
// toSyscallErr converts a syscall error to the // toSyscallErr converts a syscall error to the

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-07 13:05:58.496075 +0200 CEST m=+0.185240417 // Generated: 2021-09-07 14:38:45.5818 +0200 CEST m=+0.228575418
package errorsx package errorsx

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-07 13:05:58.31181 +0200 CEST m=+0.000981501 // Generated: 2021-09-07 14:38:45.354514 +0200 CEST m=+0.001286668
package errorsx package errorsx

View File

@ -1,5 +1,5 @@
// Code generated by go generate; DO NOT EDIT. // Code generated by go generate; DO NOT EDIT.
// Generated: 2021-09-07 13:05:58.421578 +0200 CEST m=+0.110745959 // Generated: 2021-09-07 14:38:45.459702 +0200 CEST m=+0.106475584
package errorsx package errorsx

View File

@ -162,7 +162,7 @@ func TestClassifyQUICFailure(t *testing.T) {
} }
}) })
t.Run("for incompatible quic version", func(t *testing.T) { t.Run("for incompatible quic version", func(t *testing.T) {
if classifyQUICFailure(&quic.VersionNegotiationError{}) != FailureNoCompatibleQUICVersion { if classifyQUICFailure(&quic.VersionNegotiationError{}) != FailureQUICIncompatibleVersion {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })
@ -183,7 +183,7 @@ func TestClassifyQUICFailure(t *testing.T) {
}) })
t.Run("for QUIC CRYPTO Handshake", func(t *testing.T) { t.Run("for QUIC CRYPTO Handshake", func(t *testing.T) {
var err quic.TransportErrorCode = quicTLSAlertHandshakeFailure var err quic.TransportErrorCode = quicTLSAlertHandshakeFailure
if classifyQUICFailure(&quic.TransportError{ErrorCode: err}) != FailureSSLHandshake { if classifyQUICFailure(&quic.TransportError{ErrorCode: err}) != FailureSSLFailedHandshake {
t.Fatal("unexpected results") t.Fatal("unexpected results")
} }
}) })

View File

@ -1,39 +0,0 @@
package errorsx
// This enumeration lists the non-syscall-derived failures defined at
// https://github.com/ooni/spec/blob/master/data-formats/df-007-errors.md
//
// See also the enumeration at errno.go for the failures that
// derive directly from system call errors.
const (
// FailureDNSBogonError means we detected bogon in DNS reply.
FailureDNSBogonError = "dns_bogon_error"
// FailureDNSNXDOMAINError means we got NXDOMAIN in DNS reply.
FailureDNSNXDOMAINError = "dns_nxdomain_error"
// FailureEOFError means we got unexpected EOF on connection.
FailureEOFError = "eof_error"
// FailureGenericTimeoutError means we got some timer has expired.
FailureGenericTimeoutError = "generic_timeout_error"
// FailureNoCompatibleQUICVersion means that the server does not support the proposed QUIC version
FailureNoCompatibleQUICVersion = "quic_incompatible_version"
// FailureSSLHandshake means that the negotiation of cryptographic parameters failed
FailureSSLHandshake = "ssl_failed_handshake"
// FailureSSLInvalidHostname means we got certificate is not valid for SNI.
FailureSSLInvalidHostname = "ssl_invalid_hostname"
// FailureSSLUnknownAuthority means we cannot find CA validating certificate.
FailureSSLUnknownAuthority = "ssl_unknown_authority"
// FailureSSLInvalidCertificate means certificate experired or other
// sort of errors causing it to be invalid.
FailureSSLInvalidCertificate = "ssl_invalid_certificate"
// FailureJSONParseError indicates that we couldn't parse a JSON
FailureJSONParseError = "json_parse_error"
)

View File

@ -35,7 +35,7 @@ func (es *ErrorSpec) AsFailureVar() string {
// AsFailureString returns the OONI failure string. // AsFailureString returns the OONI failure string.
func (es *ErrorSpec) AsFailureString() string { func (es *ErrorSpec) AsFailureString() string {
return es.failure return strcase.ToSnake(es.failure)
} }
// NewSystemError constructs a new ErrorSpec representing a system // NewSystemError constructs a new ErrorSpec representing a system
@ -87,6 +87,20 @@ var Specs = []*ErrorSpec{
NewSystemError("EACCES", "permission_denied"), NewSystemError("EACCES", "permission_denied"),
NewSystemError("EPROTONOSUPPORT", "protocol_not_supported"), NewSystemError("EPROTONOSUPPORT", "protocol_not_supported"),
NewSystemError("EPROTOTYPE", "wrong_protocol_type"), NewSystemError("EPROTOTYPE", "wrong_protocol_type"),
// Implementation note: we need to specify acronyms we
// want to be upper case in uppercase here. For example,
// we must write "DNS" rather than writing "dns".
NewLibraryError("DNS_bogon_error"),
NewLibraryError("DNS_NXDOMAIN_error"),
NewLibraryError("EOF_error"),
NewLibraryError("generic_timeout_error"),
NewLibraryError("QUIC_incompatible_version"),
NewLibraryError("SSL_failed_handshake"),
NewLibraryError("SSL_invalid_hostname"),
NewLibraryError("SSL_unknown_authority"),
NewLibraryError("SSL_invalid_certificate"),
NewLibraryError("JSON_parse_error"),
} }
func fileCreate(filename string) *os.File { func fileCreate(filename string) *os.File {
@ -129,6 +143,9 @@ func writeSystemSpecificFile(kind string) {
filePrintf(filep, "import \"golang.org/x/sys/%s\"\n\n", kind) filePrintf(filep, "import \"golang.org/x/sys/%s\"\n\n", kind)
fileWrite(filep, "const (\n") fileWrite(filep, "const (\n")
for _, spec := range Specs { for _, spec := range Specs {
if !spec.IsSystemError() {
continue
}
filePrintf(filep, "\t%s = %s.%s\n", filePrintf(filep, "\t%s = %s.%s\n",
spec.AsErrnoName(), kind, spec.AsErrnoName()) spec.AsErrnoName(), kind, spec.AsErrnoName())
} }
@ -149,13 +166,28 @@ func writeGenericFile() {
fileWrite(filep, "\t\"syscall\"\n") fileWrite(filep, "\t\"syscall\"\n")
fileWrite(filep, ")\n\n") fileWrite(filep, ")\n\n")
fileWrite(filep, "// This enumeration lists the syscall-derived failures defined at\n") fileWrite(filep, "// This enumeration lists the failures defined at\n")
fileWrite(filep, "// https://github.com/ooni/spec/blob/master/data-formats/df-007-errors.md\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") fileWrite(filep, "const (\n")
fileWrite(filep, "//\n")
fileWrite(filep, "// System errors\n")
fileWrite(filep, "//\n")
for _, spec := range Specs { for _, spec := range Specs {
if !spec.IsSystemError() {
continue
}
filePrintf(filep, "\t%s = \"%s\"\n",
spec.AsFailureVar(),
spec.AsFailureString())
}
fileWrite(filep, "\n")
fileWrite(filep, "//\n")
fileWrite(filep, "// Library errors\n")
fileWrite(filep, "//\n")
for _, spec := range Specs {
if spec.IsSystemError() {
continue
}
filePrintf(filep, "\t%s = \"%s\"\n", filePrintf(filep, "\t%s = \"%s\"\n",
spec.AsFailureVar(), spec.AsFailureVar(),
spec.AsFailureString()) spec.AsFailureString())
@ -175,6 +207,9 @@ func writeGenericFile() {
fileWrite(filep, "\t}\n") fileWrite(filep, "\t}\n")
fileWrite(filep, "\tswitch errno {\n") fileWrite(filep, "\tswitch errno {\n")
for _, spec := range Specs { for _, spec := range Specs {
if !spec.IsSystemError() {
continue
}
filePrintf(filep, "\tcase %s:\n", spec.AsErrnoName()) filePrintf(filep, "\tcase %s:\n", spec.AsErrnoName())
filePrintf(filep, "\t\treturn %s\n", spec.AsFailureVar()) filePrintf(filep, "\t\treturn %s\n", spec.AsFailureVar())
} }
@ -205,6 +240,9 @@ func writeGenericTestFile() {
fileWrite(filep, "\t}\n") fileWrite(filep, "\t}\n")
for _, spec := range Specs { for _, spec := range Specs {
if !spec.IsSystemError() {
continue
}
filePrintf(filep, "\tif v := toSyscallErr(%s); v != %s {\n", filePrintf(filep, "\tif v := toSyscallErr(%s); v != %s {\n",
spec.AsErrnoName(), spec.AsFailureVar()) spec.AsErrnoName(), spec.AsFailureVar())
filePrintf(filep, "\t\tt.Fatalf(\"expected '%%s', got '%%s'\", %s, v)\n", filePrintf(filep, "\t\tt.Fatalf(\"expected '%%s', got '%%s'\", %s, v)\n",

View File

@ -107,7 +107,7 @@ func classifyQUICFailure(err error) string {
var transportError *quic.TransportError var transportError *quic.TransportError
if errors.As(err, &versionNegotiation) { if errors.As(err, &versionNegotiation) {
return FailureNoCompatibleQUICVersion return FailureQUICIncompatibleVersion
} }
if errors.As(err, &statelessReset) { if errors.As(err, &statelessReset) {
return FailureConnectionReset return FailureConnectionReset
@ -130,7 +130,7 @@ func classifyQUICFailure(err error) string {
// TLSAlertDecryptError and TLSAlertHandshakeFailure are summarized to a FailureSSLHandshake error because both // TLSAlertDecryptError and TLSAlertHandshakeFailure are summarized to a FailureSSLHandshake error because both
// alerts are caused by a failed or corrupted parameter negotiation during the TLS handshake. // alerts are caused by a failed or corrupted parameter negotiation during the TLS handshake.
if errCode == quicTLSAlertDecryptError || errCode == quicTLSAlertHandshakeFailure { if errCode == quicTLSAlertDecryptError || errCode == quicTLSAlertHandshakeFailure {
return FailureSSLHandshake return FailureSSLFailedHandshake
} }
if errCode == quicTLSAlertUnknownCA { if errCode == quicTLSAlertUnknownCA {
return FailureSSLUnknownAuthority return FailureSSLUnknownAuthority