zotregistry.io/zot@v1.4.4-0.20231124084042-02a8ed785457/pkg/common/http_client_test.go (about) 1 package common_test 2 3 import ( 4 "context" 5 "crypto/x509" 6 "os" 7 "path" 8 "testing" 9 10 ispec "github.com/opencontainers/image-spec/specs-go/v1" 11 . "github.com/smartystreets/goconvey/convey" 12 13 "zotregistry.io/zot/pkg/api" 14 "zotregistry.io/zot/pkg/api/config" 15 "zotregistry.io/zot/pkg/common" 16 "zotregistry.io/zot/pkg/log" 17 test "zotregistry.io/zot/pkg/test/common" 18 ) 19 20 func TestHTTPClient(t *testing.T) { 21 Convey("test getTLSConfig()", t, func() { 22 caCertPool, _ := x509.SystemCertPool() 23 tlsConfig, err := common.GetTLSConfig("wrongPath", caCertPool) 24 So(tlsConfig, ShouldBeNil) 25 So(err, ShouldNotBeNil) 26 27 tempDir := t.TempDir() 28 err = test.CopyTestKeysAndCerts(tempDir) 29 So(err, ShouldBeNil) 30 err = os.Chmod(path.Join(tempDir, "ca.crt"), 0o000) 31 So(err, ShouldBeNil) 32 _, err = common.GetTLSConfig(tempDir, caCertPool) 33 So(err, ShouldNotBeNil) 34 }) 35 36 Convey("test CreateHTTPClient() no permissions on certificate", t, func() { 37 tempDir := t.TempDir() 38 err := test.CopyTestKeysAndCerts(tempDir) 39 So(err, ShouldBeNil) 40 err = os.Chmod(path.Join(tempDir, "ca.crt"), 0o000) 41 So(err, ShouldBeNil) 42 43 _, err = common.CreateHTTPClient(true, "localhost", tempDir) 44 So(err, ShouldNotBeNil) 45 }) 46 47 Convey("test CreateHTTPClient() no permissions on key", t, func() { 48 tempDir := t.TempDir() 49 err := test.CopyTestKeysAndCerts(tempDir) 50 So(err, ShouldBeNil) 51 err = os.Chmod(path.Join(tempDir, "client.key"), 0o000) 52 So(err, ShouldBeNil) 53 54 _, err = common.CreateHTTPClient(true, "localhost", tempDir) 55 So(err, ShouldNotBeNil) 56 }) 57 58 Convey("test MakeHTTPGetRequest() no permissions on key", t, func() { 59 port := test.GetFreePort() 60 baseURL := test.GetBaseURL(port) 61 62 conf := config.New() 63 conf.HTTP.Port = port 64 65 ctlr := api.NewController(conf) 66 tempDir := t.TempDir() 67 err := test.CopyTestKeysAndCerts(tempDir) 68 So(err, ShouldBeNil) 69 ctlr.Config.Storage.RootDirectory = tempDir 70 71 cm := test.NewControllerManager(ctlr) 72 cm.StartServer() 73 defer cm.StopServer() 74 test.WaitTillServerReady(baseURL) 75 76 var resultPtr interface{} 77 httpClient, err := common.CreateHTTPClient(true, "localhost", tempDir) 78 So(err, ShouldBeNil) 79 _, _, _, err = common.MakeHTTPGetRequest(context.Background(), httpClient, "", "", 80 resultPtr, baseURL+"/v2/", ispec.MediaTypeImageManifest, log.NewLogger("", "")) 81 So(err, ShouldBeNil) 82 }) 83 }