-
Notifications
You must be signed in to change notification settings - Fork 62
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'dev' into alibabacloud-rrsa
- Loading branch information
Showing
7 changed files
with
338 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
// Copyright The Ratify Authors. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package notation | ||
|
||
import ( | ||
"net/http" | ||
|
||
"github.com/notaryproject/notation-core-go/revocation" | ||
corecrl "github.com/notaryproject/notation-core-go/revocation/crl" | ||
"github.com/notaryproject/notation-go/dir" | ||
"github.com/notaryproject/notation-go/verifier/crl" | ||
) | ||
|
||
type RevocationFactoryImpl struct { | ||
cacheRoot string | ||
httpClient *http.Client | ||
} | ||
|
||
// NewRevocationFactoryImpl returns a new NewRevocationFactoryImpl instance | ||
func NewRevocationFactoryImpl() RevocationFactory { | ||
return &RevocationFactoryImpl{ | ||
cacheRoot: dir.PathCRLCache, | ||
httpClient: &http.Client{}, | ||
} | ||
} | ||
|
||
// NewFetcher returns a new fetcher instance | ||
func (f *RevocationFactoryImpl) NewFetcher() (corecrl.Fetcher, error) { | ||
crlFetcher, err := corecrl.NewHTTPFetcher(f.httpClient) | ||
if err != nil { | ||
return nil, err | ||
} | ||
crlFetcher.Cache, err = newFileCache(f.cacheRoot) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return crlFetcher, nil | ||
} | ||
|
||
// NewValidator returns a new validator instance | ||
func (f *RevocationFactoryImpl) NewValidator(opts revocation.Options) (revocation.Validator, error) { | ||
return revocation.NewWithOptions(opts) | ||
} | ||
|
||
// newFileCache returns a new file cache instance | ||
func newFileCache(root string) (*crl.FileCache, error) { | ||
cacheRoot, err := dir.CacheFS().SysPath(root) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return crl.NewFileCache(cacheRoot) | ||
} |
103 changes: 103 additions & 0 deletions
103
pkg/verifier/notation/notationrevocationfactory_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
// Copyright The Ratify Authors. | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
|
||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
|
||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package notation | ||
|
||
import ( | ||
"net/http" | ||
"runtime" | ||
"testing" | ||
|
||
"github.com/notaryproject/notation-core-go/revocation" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestNewRevocationFactoryImpl(t *testing.T) { | ||
factory := NewRevocationFactoryImpl() | ||
assert.NotNil(t, factory) | ||
} | ||
|
||
func TestNewFetcher(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cacheRoot string | ||
httpClient *http.Client | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid fetcher", | ||
cacheRoot: "/valid/path", | ||
httpClient: &http.Client{}, | ||
wantErr: false, | ||
}, | ||
{ | ||
name: "invalid fetcher with nil httpClient", | ||
cacheRoot: "/valid/path", | ||
httpClient: nil, | ||
wantErr: true, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
factory := &RevocationFactoryImpl{ | ||
cacheRoot: tt.cacheRoot, | ||
httpClient: tt.httpClient, | ||
} | ||
|
||
fetcher, err := factory.NewFetcher() | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
assert.Nil(t, fetcher) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestNewValidator(t *testing.T) { | ||
factory := &RevocationFactoryImpl{} | ||
opts := revocation.Options{} | ||
|
||
validator, err := factory.NewValidator(opts) | ||
assert.NoError(t, err) | ||
assert.NotNil(t, validator) | ||
} | ||
func TestNewFileCache(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
cacheRoot string | ||
wantErr bool | ||
}{ | ||
{ | ||
name: "valid cache root", | ||
cacheRoot: "/valid/path", | ||
wantErr: false, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if runtime.GOOS == "windows" { | ||
t.Skip("skipping test on Windows") | ||
} | ||
cache, err := newFileCache(tt.cacheRoot) | ||
if tt.wantErr { | ||
assert.Error(t, err) | ||
assert.Nil(t, cache) | ||
} else { | ||
assert.NoError(t, err) | ||
assert.NotNil(t, cache) | ||
} | ||
}) | ||
} | ||
} |
Oops, something went wrong.