MCPcopy Create free account
hub / github.com/WICG/webpackage / TestSignAndAddNewSignature

Function TestSignAndAddNewSignature

go/integrityblock/integrityblock_test.go:210–262  ·  view source on GitHub ↗

Test SignAndAddNewSignature when there isn't any previous signatures in the signature stack.

(t *testing.T)

Source from the content-addressed store, hash-verified

208
209// Test SignAndAddNewSignature when there isn't any previous signatures in the signature stack.
210func TestSignAndAddNewSignature(t *testing.T) {
211 pub, priv, err := ed25519.GenerateKey(rand.Reader)
212 if err != nil {
213 t.Error("Failed to generate test keys")
214 }
215
216 bundleFile, err := os.Open("./testfile.wbn")
217 if err != nil {
218 t.Error("Failed to open the test file")
219 }
220 defer bundleFile.Close()
221
222 webBundleHash, err := ComputeWebBundleSha512(bundleFile, 0)
223 if err != nil {
224 t.Fatal(err)
225 }
226
227 ibs := IntegrityBlockSigner{
228 SigningStrategy: NewParsedEd25519KeySigningStrategy(priv),
229 WebBundleHash: webBundleHash,
230 IntegrityBlock: generateEmptyIntegrityBlock(),
231 }
232 publicKey, err := ibs.SigningStrategy.GetPublicKey()
233 if err != nil {
234 t.Fatal(err)
235 }
236 err = ibs.SignAndAddNewSignature(publicKey, GenerateSignatureAttributesWithPublicKey(publicKey))
237
238 integrityBlockBytes, err := ibs.IntegrityBlock.CborBytes()
239 if err != nil {
240 t.Errorf("integrityBlock.CborBytes. err: %v", err)
241 }
242
243 publicKeyAsReadableCborString, err := bytesToCborAndToReadableStringHelper(pub)
244 if err != nil {
245 t.Errorf("integrityBlock.CborBytes. err: %v", err)
246 }
247
248 signatureAsReadableCborString, err := bytesToCborAndToReadableStringHelper(ibs.IntegrityBlock.SignatureStack[0].Signature)
249 if err != nil {
250 t.Errorf("integrityBlock.CborBytes. err: %v", err)
251 }
252
253 want := `["🖋📦" "1b\x00\x00" [[map["ed25519PublicKey":` + publicKeyAsReadableCborString + `] ` + signatureAsReadableCborString + `]]]`
254
255 got, err := testhelper.CborBinaryToReadableString(integrityBlockBytes)
256 if err != nil {
257 t.Fatal(err)
258 }
259 if got != want {
260 t.Errorf("integrityblock: got: %s\nwant: %s", got, want)
261 }
262}
263
264// Test SignAndAddNewSignature when there is an existing signature already in the stack.
265// Signature attributes should always be "freshly" made.

Callers

nothing calls this directly

Tested by

no test coverage detected