github.com/google/trillian-examples@v0.0.0-20240520080811-0d40d35cef0e/binary_transparency/firmware/internal/crypto/signature_test.go (about) 1 // Copyright 2020 Google LLC. All Rights Reserved. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 package crypto 15 16 import ( 17 "testing" 18 19 "github.com/google/trillian-examples/binary_transparency/firmware/api" 20 ) 21 22 func TestSignatureRoundTrip(t *testing.T) { 23 for _, test := range []struct { 24 desc string 25 signbody string 26 verifbody string 27 claimant Claimant 28 wantErr bool 29 }{ 30 { 31 desc: "Successful Signature Verification", 32 signbody: "My Test Message", 33 verifbody: "My Test Message", 34 claimant: Publisher, 35 }, { 36 desc: "Unsuccessful Signature Verification", 37 signbody: "My Test Message", 38 verifbody: "My Test1 Message", 39 claimant: Publisher, 40 wantErr: true, 41 }, 42 { 43 desc: "Successful Signature Verification malware", 44 signbody: "My Test Message", 45 verifbody: "My Test Message", 46 claimant: AnnotatorMalware, 47 }, { 48 desc: "Unsuccessful Signature Verification malware", 49 signbody: "My Test Message", 50 verifbody: "My Test1 Message", 51 claimant: AnnotatorMalware, 52 wantErr: true, 53 }, 54 } { 55 t.Run(test.desc, func(t *testing.T) { 56 msg := []byte(test.signbody) 57 sign, err := test.claimant.SignMessage(api.FirmwareMetadataType, msg) 58 if err != nil { 59 t.Fatalf("failed to marshal statement: %v", err) 60 } 61 62 // Now Verify the signature 63 msg = []byte(test.verifbody) 64 err = test.claimant.VerifySignature('f', msg, sign) 65 switch { 66 case err != nil && !test.wantErr: 67 t.Fatalf("Got unexpected error %q", err) 68 case err == nil && test.wantErr: 69 t.Fatal("Got no error, but wanted error") 70 case err != nil && test.wantErr: 71 // expected error 72 default: 73 //fall through 74 } 75 }) 76 } 77 }