diff --git a/crl/crlloader/filecrlloader.go b/crl/crlloader/filecrlloader.go index 2e9bf62..91ab348 100644 --- a/crl/crlloader/filecrlloader.go +++ b/crl/crlloader/filecrlloader.go @@ -28,7 +28,7 @@ func (f FileLoader) copyToTargetFile(filePath string) error { if stat.IsDir() { return fmt.Errorf("CRL File %s is a directory", f.FileName) } - crlFile, err := os.OpenFile(filePath, os.O_RDWR|os.O_EXCL|os.O_CREATE, 0600) + crlFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { return err } diff --git a/crl/crlloader/urlcrlloader.go b/crl/crlloader/urlcrlloader.go index 01fc61e..be53dfa 100644 --- a/crl/crlloader/urlcrlloader.go +++ b/crl/crlloader/urlcrlloader.go @@ -46,7 +46,7 @@ func (L URLLoader) GetDescription() string { } func (L URLLoader) downloadCRL(url string, filePath string) error { - crlFile, err := os.OpenFile(filePath, os.O_RDWR|os.O_EXCL|os.O_CREATE, 0600) + crlFile, err := os.OpenFile(filePath, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0600) if err != nil { return err } diff --git a/crl/crlloader/urlcrlloader_test.go b/crl/crlloader/urlcrlloader_test.go index fc8a4a4..bf4a906 100644 --- a/crl/crlloader/urlcrlloader_test.go +++ b/crl/crlloader/urlcrlloader_test.go @@ -56,6 +56,34 @@ func (suite *URLLoaderSuite) TestLoadCRL() { assert.Equal(suite.T(), mockCRLContent, string(downloadedCRLData), "Downloaded CRL content doesn't match") } +func (suite *URLLoaderSuite) TestLoadCRLWithInvalidUrl() { + // Create a temporary directory to store the downloaded CRL + tmpDir, err := ioutil.TempDir("", "test-crl-dir-") + assert.NoError(suite.T(), err, "Error creating temporary directory") + defer os.RemoveAll(tmpDir) + + // Create a mock HTTP server that serves the CRL content + mockCRLContent := "Mock CRL Data" + mockServer := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + w.Write([]byte(mockCRLContent)) + })) + defer mockServer.Close() + + // Create a URLLoader instance + urlLoader := URLLoader{ + UrlString: "httppppp:dsdsd", + Logger: suite.logger, + } + + // Define the path to save the downloaded CRL + downloadFilePath := filepath.Join(tmpDir, "downloaded-crl.crl") + + // Call the LoadCRL method + err = urlLoader.LoadCRL(downloadFilePath) + assert.Error(suite.T(), err) + assert.Contains(suite.T(), err.Error(), "unsupported protocol") +} + func (suite *URLLoaderSuite) TestGetCRLLocationIdentifier() { // Create a URLLoader instance urlLoader := URLLoader{