github.com/adwpc/xmobile@v0.0.0-20231212131043-3f9720cf0e99/cmd/gomobile/cert_test.go (about) 1 // Copyright 2015 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 package main 6 7 import ( 8 "crypto/rand" 9 "crypto/x509" 10 "encoding/pem" 11 "io/ioutil" 12 "os" 13 "os/exec" 14 "testing" 15 ) 16 17 func TestSignPKCS7(t *testing.T) { 18 // Setup RSA key. 19 block, _ := pem.Decode([]byte(testKey)) 20 if block == nil { 21 t.Fatal("no cert") 22 } 23 privKey, err := x509.ParsePKCS1PrivateKey(block.Bytes) 24 if err != nil { 25 t.Fatal(err) 26 } 27 28 content := "Hello world,\nThis is signed." 29 cert, err := signPKCS7(rand.Reader, privKey, []byte(content)) 30 if err != nil { 31 t.Fatal(err) 32 } 33 sig, err := ioutil.TempFile("", "content.rsa") 34 if err != nil { 35 t.Fatal(err) 36 } 37 sigPath := sig.Name() 38 defer os.Remove(sigPath) 39 if _, err := sig.Write(cert); err != nil { 40 t.Fatal(err) 41 } 42 if err := sig.Close(); err != nil { 43 t.Fatal(err) 44 } 45 46 if openssl, err := exec.LookPath("openssl"); err != nil { 47 t.Log("command openssl not found, skipping") 48 } else { 49 cmd := exec.Command( 50 openssl, "asn1parse", 51 "-inform", "DER", 52 "-i", 53 "-in", sigPath, 54 ) 55 if err := cmd.Run(); err != nil { 56 t.Errorf("bad asn.1: %v", err) 57 } 58 } 59 60 if keytool, err := exec.LookPath("keytool"); err != nil { 61 t.Log("command keytool not found, skipping") 62 } else if err := exec.Command(keytool, "-v").Run(); err != nil { 63 t.Logf("command keytool not functioning: %s, skipping", err) 64 } else { 65 cmd := exec.Command(keytool, "-v", "-printcert", "-file", sigPath) 66 out, err := cmd.CombinedOutput() 67 t.Logf("%v:\n%s", cmd.Args, out) 68 if err != nil { 69 t.Errorf("keytool cannot parse signature: %v", err) 70 } 71 } 72 } 73 74 const testKey = ` 75 -----BEGIN RSA PRIVATE KE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