package sigcred import ( "math/rand" "testing" "time" "dehub.dev/src/dehub.git/fs" ) // There are not currently tests for testing pgp signature creation, as they // require calls out to the gpg executable. Wrapping tests in docker containers // would make this doable. func TestPGPVerification(t *testing.T) { tests := []struct { descr string init func(pubKeyBody []byte) (Signifier, fs.FS) }{ { descr: "SignifierPGP Body", init: func(pubKeyBody []byte) (Signifier, fs.FS) { return SignifierPGP{Body: string(pubKeyBody)}, nil }, }, { descr: "SignifierPGP Path", init: func(pubKeyBody []byte) (Signifier, fs.FS) { pubKeyPath := "some/dir/pubkey.asc" fs := fs.Stub{pubKeyPath: pubKeyBody} return SignifierPGP{Path: pubKeyPath}, fs }, }, } for _, test := range tests { t.Run(test.descr, func(t *testing.T) { seed := time.Now().UnixNano() t.Logf("seed: %d", seed) rand := rand.New(rand.NewSource(seed)) privKey, pubKeyBody := TestSignifierPGP("", false, rand) sig, fs := test.init(pubKeyBody) data := make([]byte, rand.Intn(1024)) if _, err := rand.Read(data); err != nil { t.Fatal(err) } cred, err := privKey.Sign(nil, data) if err != nil { t.Fatal(err) } signed, err := sig.Signed(fs, cred) if err != nil { t.Fatal(err) } else if !signed { t.Fatal("expected signed to be true") } if err := sig.Verify(fs, data, cred); err != nil { t.Fatal(err) } }) } }