@@ -13,6 +13,7 @@ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
13
See the License for the specific language governing permissions and
14
14
limitations under the License.
15
15
*/
16
+ /* eslint-disable @typescript-eslint/no-non-null-assertion */
16
17
import {
17
18
Bundle ,
18
19
HashAlgorithm ,
@@ -22,14 +23,16 @@ import {
22
23
X509CertificateChain ,
23
24
} from '@sigstore/protobuf-specs' ;
24
25
import { TUFError } from '@sigstore/tuf' ;
26
+ import { fromPartial } from '@total-typescript/shoehorn' ;
25
27
import mocktuf , { Target } from '@tufjs/repo-mock' ;
26
28
import { PolicyError , VerificationError } from '../error' ;
27
29
import { Signer } from '../sign' ;
28
- import { attest , sign , tuf , verify } from '../sigstore' ;
30
+ import { attest , createVerifier , sign , tuf , verify } from '../sigstore' ;
31
+ import { SerializedBundle } from '../types/sigstore' ;
29
32
import bundles from './__fixtures__/bundles' ;
30
33
import { trustedRoot } from './__fixtures__/trust' ;
31
34
32
- import type { VerifyOptions } from '../config' ;
35
+ import type { TUFOptions , VerifyOptions } from '../config' ;
33
36
34
37
jest . mock ( '../sign' ) ;
35
38
@@ -317,25 +320,75 @@ describe('#verify', () => {
317
320
} ) ;
318
321
} ) ;
319
322
320
- describe ( 'tuf ' , ( ) => {
323
+ describe ( '#createVerifier ' , ( ) => {
321
324
let tufRepo : ReturnType < typeof mocktuf > | undefined ;
322
- let options : VerifyOptions | undefined ;
325
+ let tufOptions : VerifyOptions | undefined ;
326
+
327
+ const trustedRootJSON = JSON . stringify ( TrustedRoot . toJSON ( trustedRoot ) ) ;
328
+ const target : Target = {
329
+ name : 'trusted_root.json' ,
330
+ content : Buffer . from ( trustedRootJSON ) ,
331
+ } ;
323
332
324
333
beforeEach ( ( ) => {
325
334
tufRepo = mocktuf ( target , { metadataPathPrefix : '' } ) ;
326
- options = {
335
+ tufOptions = {
327
336
tufMirrorURL : tufRepo . baseURL ,
328
337
tufCachePath : tufRepo . cachePath ,
329
338
} ;
330
339
} ) ;
331
340
332
341
afterEach ( ( ) => tufRepo ?. teardown ( ) ) ;
333
342
343
+ it ( 'returns a object' , async ( ) => {
344
+ const verifier = await createVerifier ( tufOptions ! ) ;
345
+ expect ( verifier ) . toBeInstanceOf ( Object ) ;
346
+ } ) ;
347
+
348
+ describe ( 'when the bundle is valid' , ( ) => {
349
+ const bundle : SerializedBundle = fromPartial (
350
+ bundles . dsse . valid . withSigningCert
351
+ ) ;
352
+
353
+ it ( 'does not throw an error when invoked' , async ( ) => {
354
+ const verifier = await createVerifier ( tufOptions ! ) ;
355
+ expect ( verifier . verify ( bundle ) ) . toBeUndefined ( ) ;
356
+ } ) ;
357
+ } ) ;
358
+
359
+ describe ( 'when the bundle is invalid' , ( ) => {
360
+ const bundle : SerializedBundle = fromPartial (
361
+ bundles . dsse . invalid . badSignature
362
+ ) ;
363
+
364
+ it ( 'throws an error when invoked' , async ( ) => {
365
+ const verifier = await createVerifier ( tufOptions ! ) ;
366
+ expect ( ( ) => {
367
+ verifier . verify ( bundle ) ;
368
+ } ) . toThrowError ( VerificationError ) ;
369
+ } ) ;
370
+ } ) ;
371
+ } ) ;
372
+
373
+ describe ( 'tuf' , ( ) => {
374
+ let tufRepo : ReturnType < typeof mocktuf > | undefined ;
375
+ let options : TUFOptions | undefined ;
376
+
334
377
const target : Target = {
335
378
name : 'foo' ,
336
379
content : 'bar' ,
337
380
} ;
338
381
382
+ beforeEach ( ( ) => {
383
+ tufRepo = mocktuf ( target , { metadataPathPrefix : '' } ) ;
384
+ options = {
385
+ tufMirrorURL : tufRepo . baseURL ,
386
+ tufCachePath : tufRepo . cachePath ,
387
+ } ;
388
+ } ) ;
389
+
390
+ afterEach ( ( ) => tufRepo ?. teardown ( ) ) ;
391
+
339
392
describe ( 'getTarget' , ( ) => {
340
393
describe ( 'when the target exists' , ( ) => {
341
394
it ( 'returns the target' , async ( ) => {
0 commit comments