github.com/jasoet/gopki@v1.15.0/encryption/cms_test.go (about) 1 package encryption 2 3 import ( 4 "crypto/rand" 5 "crypto/x509" 6 "crypto/x509/pkix" 7 "math/big" 8 "testing" 9 "time" 10 11 "github.com/jasoet/gopki/keypair/algo" 12 ) 13 14 func TestNewCMSImplementation(t *testing.T) { 15 // Generate RSA key pair 16 rsaKeys, err := algo.GenerateRSAKeyPair(algo.KeySize2048) 17 if err != nil { 18 t.Fatalf("Failed to generate RSA key pair: %v", err) 19 } 20 21 // Create test certificate 22 template := x509.Certificate{ 23 SerialNumber: big.NewInt(1), 24 Subject: pkix.Name{ 25 CommonName: "Test Certificate", 26 Organization: []string{"Test Org"}, 27 }, 28 NotBefore: time.Now(), 29 NotAfter: time.Now().Add(365 * 24 * time.Hour), 30 KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, 31 ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageEmailProtection}, 32 BasicConstraintsValid: true, 33 } 34 35 certDER, err := x509.CreateCertificate(rand.Reader, &template, &template, rsaKeys.PublicKey, rsaKeys.PrivateKey) 36 if err != nil { 37 t.Fatalf("Failed to create certificate: %v", err) 38 } 39 40 cert, err := x509.ParseCertificate(certDER) 41 if err != nil { 42 t.Fatalf("Failed to parse certificate: %v", err) 43 } 44 45 testData := []byte("test data for new CMS implementation") 46 47 // Create EncryptedData with recipient 48 encData := &EncryptedData{ 49 Algorithm: AlgorithmEnvelope, 50 Format: FormatCMS, 51 Data: testData, 52 Recipients: []*RecipientInfo{ 53 { 54 Certificate: cert, 55 KeyEncryptionAlgorithm: AlgorithmRSAOAEP, 56 }, 57 }, 58 Timestamp: time.Now(), 59 Metadata: make(map[string]any), 60 } 61 62 t.Run("EncodeToCMS", func(t *testing.T) { 63 encoded, err := EncodeToCMS(encData) 64 if err != nil { 65 t.Fatalf("Failed to encode to CMS: %v", err) 66 } 67 68 if len(encoded) == 0 { 69 t.Error("Encoded CMS data is empty") 70 } 71 }) 72 73 t.Run("ValidateCMS", func(t *testing.T) { 74 encoded, err := EncodeToCMS(encData) 75 if err != nil { 76 t.Fatalf("Failed to encode to CMS: %v", err) 77 } 78 79 err = ValidateCMS(encoded) 80 if err != nil { 81 t.Errorf("CMS validation failed: %v", err) 82 } 83 }) 84 85 t.Run("DecodeFromCMS", func(t *testing.T) { 86 encoded, err := EncodeToCMS(encData) 87 if err != nil { 88 t.Fatalf("Failed to encode to CMS: %v", err) 89 } 90 91 decoded, err := DecodeFromCMS(encoded, cert, rsaKeys.PrivateKey) 92 if err != nil { 93 t.Fatalf("Failed to decode from CMS: %v", err) 94 } 95 96 if string(decoded.Data) != string(testData) { 97 t.Errorf("Decoded data doesn't match. Expected: %s, Got: %s", string(testData), string(decoded.Data)) 98 } 99 }) 100 101 t.Run("DecodeDataWithKey", func(t *testing.T) { 102 encoded, err := EncodeData(encData) 103 if err != nil { 104 t.Fatalf("Failed to encode data: %v", err) 105 } 106 107 decoded, err := DecodeDataWithKey(encoded, cert, rsaKeys.PrivateKey) 108 if err != nil { 109 t.Fatalf("Failed to decode with key: %v", err) 110 } 111 112 if string(decoded.Data) != string(testData) { 113 t.Errorf("Decoded data doesn't match. Expected: %s, Got: %s", string(testData), string(decoded.Data)) 114 } 115 }) 116 }