github.com/c-darwin/mobile@v0.0.0-20160313183840-ff625c46f7c9/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 out, err := cmd.CombinedOutput() 56 t.Logf("%v:\n%s", cmd.Args, out) 57 if err != nil { 58 t.Errorf("bad asn.1: %v", err) 59 } 60 } 61 62 if keytool, err := exec.LookPath("keytool"); err != nil { 63 t.Log("command keytool not found, skipping") 64 } else if err := exec.Command(keytool, "-v").Run(); err != nil { 65 t.Log("command keytool not functioning: %s, skipping", err) 66 } else { 67 cmd := exec.Command(keytool, "-v", "-printcert", "-file", sigPath) 68 out, err := cmd.CombinedOutput() 69 t.Logf("%v:\n%s", cmd.Args, out) 70 if err != nil { 71 t.Errorf("keytool cannot parse signature: %v", err) 72 } 73 } 74 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