Merge pull request #14455 from ahrtr/nil_log_assert

add verification on nil log
This commit is contained in:
Hitoshi Mitake 2022-09-13 20:14:56 +09:00 committed by GitHub
commit 5f62375d62
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 22 additions and 0 deletions

View File

@ -21,6 +21,7 @@ import (
"os"
"path/filepath"
"go.etcd.io/etcd/client/pkg/v3/verify"
"go.uber.org/zap"
)
@ -45,6 +46,7 @@ func IsDirWriteable(dir string) error {
// TouchDirAll is similar to os.MkdirAll. It creates directories with 0700 permission if any directory
// does not exists. TouchDirAll also ensures the given directory is writable.
func TouchDirAll(lg *zap.Logger, dir string) error {
verify.Assert(lg != nil, "nil log isn't allowed")
// If path is already a directory, MkdirAll does nothing and returns nil, so,
// first check if dir exists with an expected permission mode.
if Exist(dir) {

View File

@ -26,6 +26,7 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/zap/zaptest"
)
@ -218,3 +219,14 @@ func TestRemoveMatchFile(t *testing.T) {
t.Errorf("expected error, got nil")
}
}
func TestTouchDirAll(t *testing.T) {
tmpdir := t.TempDir()
assert.Panics(t, func() {
TouchDirAll(nil, tmpdir)
}, "expected panic with nil log")
if err := TouchDirAll(zaptest.NewLogger(t), tmpdir); err != nil {
t.Fatal(err)
}
}

View File

@ -34,6 +34,7 @@ import (
"go.etcd.io/etcd/client/pkg/v3/fileutil"
"go.etcd.io/etcd/client/pkg/v3/tlsutil"
"go.etcd.io/etcd/client/pkg/v3/verify"
"go.uber.org/zap"
)
@ -196,6 +197,7 @@ func (info TLSInfo) Empty() bool {
}
func SelfCert(lg *zap.Logger, dirpath string, hosts []string, selfSignedCertValidity uint, additionalUsages ...x509.ExtKeyUsage) (info TLSInfo, err error) {
verify.Assert(lg != nil, "nil log isn't allowed")
info.Logger = lg
if selfSignedCertValidity == 0 {
err = fmt.Errorf("selfSignedCertValidity is invalid,it should be greater than 0")

View File

@ -24,6 +24,7 @@ import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"go.uber.org/zap/zaptest"
)
@ -528,6 +529,7 @@ func TestNewListenerUnixSocket(t *testing.T) {
// TestNewListenerTLSInfoSelfCert tests that a new certificate accepts connections.
func TestNewListenerTLSInfoSelfCert(t *testing.T) {
tmpdir := t.TempDir()
tlsinfo, err := SelfCert(zaptest.NewLogger(t), tmpdir, []string{"127.0.0.1"}, 1)
if err != nil {
t.Fatal(err)
@ -536,6 +538,10 @@ func TestNewListenerTLSInfoSelfCert(t *testing.T) {
t.Fatalf("tlsinfo should have certs (%+v)", tlsinfo)
}
testNewListenerTLSInfoAccept(t, tlsinfo)
assert.Panics(t, func() {
SelfCert(nil, tmpdir, []string{"127.0.0.1"}, 1)
}, "expected panic with nil log")
}
func TestIsClosedConnError(t *testing.T) {