From e0183217b69c70630331c10d772fcba64921f1b9 Mon Sep 17 00:00:00 2001 From: Nicola Murino Date: Thu, 7 May 2020 19:47:46 +0200 Subject: [PATCH] test cases: simplify TestLoginInvalidFs we can simulate an invalid filesystem config using a GCS user without a credentials file --- go.sum | 3 +++ sftpd/sftpd_test.go | 34 +++++++++++++--------------------- 2 files changed, 16 insertions(+), 21 deletions(-) diff --git a/go.sum b/go.sum index f84319ce..6ea93017 100644 --- a/go.sum +++ b/go.sum @@ -59,6 +59,7 @@ github.com/coreos/go-systemd v0.0.0-20190321100706-95778dfbb74e/go.mod h1:F5haX7 github.com/coreos/pkg v0.0.0-20180928190104-399ea9e2e55f/go.mod h1:E3G3o1h8I7cfcXa63jLwjI0eiQQMgzzUDFVpN/nH/eA= github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= github.com/dgrijalva/jwt-go v3.2.0+incompatible/go.mod h1:E3ru+11k8xSBh+hMPgOLZmtrrCbhqsmaPHjLKYnJCaQ= github.com/dgryski/go-sip13 v0.0.0-20181026042036-e10d5fee7954/go.mod h1:vAd38F8PWV+bWy6jNmig1y/TA+kYO4g3RSRF0IAv0no= @@ -194,6 +195,7 @@ github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINE github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v0.9.3/go.mod h1:/TN21ttK/J9q6uSwhBd54HahCDft0ttaMvbicHlPoso= @@ -253,6 +255,7 @@ github.com/stretchr/objx v0.1.1/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+ github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs= github.com/stretchr/testify v1.3.0/go.mod h1:M5WIy9Dh21IEIfnGCwXGc5bZfKNJtfHm1UVUgZn+9EI= github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4= github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA= github.com/subosito/gotenv v1.2.0 h1:Slr1R9HxAlEKefgq5jn9U+DnETlIUa6HfgEzj0g5d7s= github.com/subosito/gotenv v1.2.0/go.mod h1:N0PQaV/YGNqwC0u51sEeR/aUtSLEXKX9iv69rRypqCw= diff --git a/sftpd/sftpd_test.go b/sftpd/sftpd_test.go index 80df7e0c..bc095cbc 100644 --- a/sftpd/sftpd_test.go +++ b/sftpd/sftpd_test.go @@ -891,7 +891,8 @@ func TestLoginUserExpiration(t *testing.T) { assert.NoError(t, err) client, err = getSftpClient(user, usePubKey) if assert.NoError(t, err) { - client.Close() + defer client.Close() + assert.NoError(t, checkBasicSFTP(client)) } _, err = httpd.RemoveUser(user, http.StatusOK) assert.NoError(t, err) @@ -900,30 +901,21 @@ func TestLoginUserExpiration(t *testing.T) { } func TestLoginInvalidFs(t *testing.T) { - if runtime.GOOS == osWindows { - t.Skip("this test is not available on Windows") - } - err := config.LoadConfig(configDir, "") - assert.NoError(t, err) - providerConf := config.GetProviderConf() - if providerConf.Driver != dataprovider.SQLiteDataProviderName { - t.Skip("this test require sqlite provider") - } - dbPath := providerConf.Name - if !filepath.IsAbs(dbPath) { - dbPath = filepath.Join(configDir, dbPath) - } usePubKey := true u := getTestUser(usePubKey) + u.FsConfig.Provider = 2 + u.FsConfig.GCSConfig.Bucket = "test" + u.FsConfig.GCSConfig.Credentials = base64.StdEncoding.EncodeToString([]byte("invalid JSON for credentials")) user, _, err := httpd.AddUser(u, http.StatusOK) assert.NoError(t, err) - - // we update the database using sqlite3 CLI since we cannot add a user with an invalid config - updateUserQuery := fmt.Sprintf("UPDATE users SET filesystem='{\"provider\":1}' WHERE id=%v", user.ID) - cmd := exec.Command("sqlite3", "-cmd", "\".timeout 2000\"", dbPath, updateUserQuery) - out, err := cmd.CombinedOutput() - assert.NoError(t, err, "unexpected error: %v, cmd out: %v", err, string(out)) - + // now remove the credentials file so the filesystem creation will fail + providerConf := config.GetProviderConf() + credentialsFile := filepath.Join(providerConf.CredentialsPath, fmt.Sprintf("%v_gcs_credentials.json", u.Username)) + if !filepath.IsAbs(credentialsFile) { + credentialsFile = filepath.Join(configDir, credentialsFile) + } + err = os.Remove(credentialsFile) + assert.NoError(t, err) client, err := getSftpClient(user, usePubKey) if !assert.Error(t, err, "login must fail, the user has an invalid filesystem config") { client.Close()