@@ -34,32 +34,133 @@ func TestRSAGenKey(t *testing.T) {
3434 assert .NotNil (t , pubkey )
3535}
3636
37- func TestOAPEEncryptAndDecrypt (t * testing.T ) {
38- prikey , pubkey , err := GenRSAKey (2048 )
39- assert .Nil (t , err )
40-
41- plaintext := []byte ("hello, this is plain, we will test the encryption and decryption of OAEP" )
42-
43- ciphertext , err := EncryptOAEP (plaintext , pubkey )
37+ func TestEncryptOAEP (t * testing.T ) {
38+ privKey , pubKey , err := GenRSAKey (1024 )
4439 assert .Nil (t , err )
40+ tests := []struct {
41+ name string
42+ plaintext []byte
43+ wantErr bool
44+ pubKey []byte
45+ }{
46+ {"should pass with correct arguments" , []byte ("hello, world" ), false , pubKey },
47+ {"should respond err with empty plain text" , []byte ("" ), true , pubKey },
48+ {"should respond err with incorrect public key" , []byte ("hello, world" ), true , []byte ("error_public_key" )},
49+ {"should respond err with empty public key" , []byte ("hello, world" ), true , []byte ("" )},
50+ }
51+ for _ , tt := range tests {
52+ t .Run (tt .name , func (t * testing.T ) {
53+ ciphertext , err := EncryptOAEP (tt .plaintext , tt .pubKey )
54+ if err != nil {
55+ if tt .wantErr {
56+ return
57+ }
58+ assert .FailNow (t , "want nil error but get error %s" , err )
59+ }
60+ actual , _ := DecryptOAEP (ciphertext , privKey )
61+ assert .Equal (t , tt .plaintext , actual )
62+ })
63+ }
64+ }
4565
46- actual , err := DecryptOAEP (ciphertext , prikey )
66+ func TestDecryptOAEP (t * testing.T ) {
67+ privKey , pubKey , err := GenRSAKey (1024 )
4768 assert .Nil (t , err )
48-
49- assert .Equal (t , plaintext , actual )
69+ ciphertext := func (plaintext []byte ) []byte {
70+ val , _ := EncryptOAEP (plaintext , pubKey )
71+ return val
72+ }
73+ tests := []struct {
74+ name string
75+ ciphertext []byte
76+ wantErr bool
77+ expect string
78+ privKey []byte
79+ }{
80+ {"should pass with correct arguments" , ciphertext ([]byte ("hello, world" )), false , "hello, world" , privKey },
81+ {"should respond err with empty cipher text" , []byte ("" ), true , "hello, world" , privKey },
82+ {"should respond err with incorrect private key" , []byte ("hello, world" ), true , "hello, world" , []byte ("error_private_key" )},
83+ {"should respond err with empty private key" , ciphertext ([]byte ("hello, world" )), true , "hello, world" , []byte ("" )},
84+ }
85+ for _ , tt := range tests {
86+ t .Run (tt .name , func (t * testing.T ) {
87+ actual , err := DecryptOAEP (tt .ciphertext , tt .privKey )
88+ if err != nil {
89+ if tt .wantErr {
90+ return
91+ }
92+ assert .FailNow (t , "want nil error but get error %s" , err )
93+ }
94+ assert .Equal (t , tt .expect , string (actual ))
95+ })
96+ }
5097}
5198
52- func TestEncryptAndDecryptOfPKCS1V15 (t * testing.T ) {
53- prikey , pubkey , err := GenRSAKey (1024 )
99+ func TestEncryptPKCS1v15 (t * testing.T ) {
100+ privKey , pubKey , err := GenRSAKey (1024 )
54101 assert .Nil (t , err )
102+ tests := []struct {
103+ name string
104+ plaintext []byte
105+ wantErr bool
106+ pubKey []byte
107+ }{
108+ {"should pass with correct arguments" , []byte ("hello, world" ), false , pubKey },
109+ {"should respond err with empty plain text" , []byte ("" ), true , pubKey },
110+ {"should respond err with incorrect public key" , []byte ("hello, world" ), true , []byte ("error_public_key" )},
111+ {"should respond err with empty public key" , []byte ("hello, world" ), true , []byte ("" )},
112+ }
113+ for _ , tt := range tests {
114+ t .Run (tt .name , func (t * testing.T ) {
115+ ciphertext , err := EncryptPKCS1v15 (tt .plaintext , tt .pubKey )
116+ if err != nil {
117+ if tt .wantErr {
118+ return
119+ }
120+ assert .FailNow (t , "want nil error but get error %s" , err )
121+ }
122+ actual , _ := DecryptPKCS1v15 (ciphertext , privKey )
123+ assert .Equal (t , tt .plaintext , actual )
124+ })
125+ }
126+ }
55127
56- plaintext := []byte ("hello, this is plain, we will test the encryption and decryption of PKCS1V15" )
57-
58- ciphertext , err := EncryptPKCS1v15 (plaintext , pubkey )
128+ func TestDecryptPKCS1v15 (t * testing.T ) {
129+ privKey , pubKey , err := GenRSAKey (1024 )
59130 assert .Nil (t , err )
131+ ciphertext := func (plaintext []byte ) []byte {
132+ val , _ := EncryptPKCS1v15 (plaintext , pubKey )
133+ return val
134+ }
135+ tests := []struct {
136+ name string
137+ ciphertext []byte
138+ wantErr bool
139+ expect string
140+ privKey []byte
141+ }{
142+ {"should pass with correct arguments" , ciphertext ([]byte ("hello, world" )), false , "hello, world" , privKey },
143+ {"should respond err with empty cipher text" , []byte ("" ), true , "hello, world" , privKey },
144+ {"should respond err with incorrect private key" , []byte ("hello, world" ), true , "hello, world" , []byte ("error_private_key" )},
145+ {"should respond err with empty private key" , ciphertext ([]byte ("hello, world" )), true , "hello, world" , []byte ("" )},
146+ }
147+ for _ , tt := range tests {
148+ t .Run (tt .name , func (t * testing.T ) {
149+ actual , err := DecryptPKCS1v15 (tt .ciphertext , tt .privKey )
150+ if err != nil {
151+ if tt .wantErr {
152+ return
153+ }
154+ assert .FailNow (t , "want nil error but get error %s" , err )
155+ }
156+ assert .Equal (t , tt .expect , string (actual ))
157+ })
158+ }
159+ }
60160
61- actual , err := DecryptPKCS1v15 (ciphertext , prikey )
161+ func TestGenerateKey (t * testing.T ) {
162+ privateKey , publicKey , err := GenRSAKey (1024 )
62163 assert .Nil (t , err )
63-
64- assert .Equal (t , plaintext , actual )
164+ assert . NotEmpty ( t , privateKey )
165+ assert .NotEmpty (t , publicKey )
65166}
0 commit comments