2023-06-22 06:29:45 -05:00
|
|
|
package common_test
|
|
|
|
|
|
|
|
import (
|
2023-09-05 11:48:56 -05:00
|
|
|
"context"
|
2023-06-22 06:29:45 -05:00
|
|
|
"crypto/x509"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
ispec "github.com/opencontainers/image-spec/specs-go/v1"
|
|
|
|
. "github.com/smartystreets/goconvey/convey"
|
|
|
|
|
|
|
|
"zotregistry.io/zot/pkg/api"
|
|
|
|
"zotregistry.io/zot/pkg/api/config"
|
|
|
|
"zotregistry.io/zot/pkg/common"
|
|
|
|
"zotregistry.io/zot/pkg/log"
|
2023-09-27 13:34:48 -05:00
|
|
|
test "zotregistry.io/zot/pkg/test/common"
|
2023-06-22 06:29:45 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestHTTPClient(t *testing.T) {
|
|
|
|
Convey("test getTLSConfig()", t, func() {
|
|
|
|
caCertPool, _ := x509.SystemCertPool()
|
|
|
|
tlsConfig, err := common.GetTLSConfig("wrongPath", caCertPool)
|
|
|
|
So(tlsConfig, ShouldBeNil)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
|
|
|
|
tempDir := t.TempDir()
|
2023-08-18 03:46:11 -05:00
|
|
|
err = test.CopyTestKeysAndCerts(tempDir)
|
2023-06-22 06:29:45 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = os.Chmod(path.Join(tempDir, "ca.crt"), 0o000)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
_, err = common.GetTLSConfig(tempDir, caCertPool)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("test CreateHTTPClient() no permissions on certificate", t, func() {
|
|
|
|
tempDir := t.TempDir()
|
2023-08-18 03:46:11 -05:00
|
|
|
err := test.CopyTestKeysAndCerts(tempDir)
|
2023-06-22 06:29:45 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = os.Chmod(path.Join(tempDir, "ca.crt"), 0o000)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
_, err = common.CreateHTTPClient(true, "localhost", tempDir)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("test CreateHTTPClient() no permissions on key", t, func() {
|
|
|
|
tempDir := t.TempDir()
|
2023-08-18 03:46:11 -05:00
|
|
|
err := test.CopyTestKeysAndCerts(tempDir)
|
2023-06-22 06:29:45 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
err = os.Chmod(path.Join(tempDir, "client.key"), 0o000)
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
|
|
|
|
_, err = common.CreateHTTPClient(true, "localhost", tempDir)
|
|
|
|
So(err, ShouldNotBeNil)
|
|
|
|
})
|
|
|
|
|
|
|
|
Convey("test MakeHTTPGetRequest() no permissions on key", t, func() {
|
|
|
|
port := test.GetFreePort()
|
|
|
|
baseURL := test.GetBaseURL(port)
|
|
|
|
|
|
|
|
conf := config.New()
|
|
|
|
conf.HTTP.Port = port
|
|
|
|
|
|
|
|
ctlr := api.NewController(conf)
|
|
|
|
tempDir := t.TempDir()
|
2023-08-18 03:46:11 -05:00
|
|
|
err := test.CopyTestKeysAndCerts(tempDir)
|
2023-06-22 06:29:45 -05:00
|
|
|
So(err, ShouldBeNil)
|
|
|
|
ctlr.Config.Storage.RootDirectory = tempDir
|
|
|
|
|
|
|
|
cm := test.NewControllerManager(ctlr)
|
|
|
|
cm.StartServer()
|
|
|
|
defer cm.StopServer()
|
|
|
|
test.WaitTillServerReady(baseURL)
|
|
|
|
|
|
|
|
var resultPtr interface{}
|
|
|
|
httpClient, err := common.CreateHTTPClient(true, "localhost", tempDir)
|
|
|
|
So(err, ShouldBeNil)
|
2023-09-05 11:48:56 -05:00
|
|
|
_, _, _, err = common.MakeHTTPGetRequest(context.Background(), httpClient, "", "",
|
2023-06-22 06:29:45 -05:00
|
|
|
resultPtr, baseURL+"/v2/", ispec.MediaTypeImageManifest, log.NewLogger("", ""))
|
|
|
|
So(err, ShouldBeNil)
|
|
|
|
})
|
|
|
|
}
|