Issues (3)

decrypt_pem_test.go (1 issue)

Severity
1
package decryptpem
2
3
import (
4
	"crypto/x509"
5
	"testing"
6
)
7
8
func TestDecryptPEM(t *testing.T) {
9
	pem, err := DecryptFileWithPassword("./test/encrypted_rsa.pem", "foobar")
10
	if err != nil {
11
		t.Error(err)
12
		return
13
	}
14
15
	_, err = x509.ParsePKCS1PrivateKey(pem.Bytes)
16
	if err != nil {
17
		t.Error(err)
18
		return
19
	}
20
21
	_, err = DecryptFileWithPassword("./test/encrypted_rsa.pem", "badpass")
22
	if err == nil {
23
		t.Error("Error should return with bad password")
24
		return
25
	}
26
27
	// Decrypting with prompt should error on non tty
28
	pem, err = DecryptFileWithPrompt("./test/encrypted_rsa.pem")
0 ignored issues
show
this value of pem is never used (SA4006)
Loading history...
29
	if err == nil {
30
		t.Error("Error should return with non tty")
31
		return
32
	}
33
34
	// Test decrypt on non-encrypted file
35
	pem, err = DecryptFileWithPassword("./test/plaintext_rsa.pem", "foobar")
36
	if err != nil {
37
		t.Error(err)
38
		return
39
	}
40
	_, err = x509.ParsePKCS1PrivateKey(pem.Bytes)
41
	if err != nil {
42
		t.Error(err)
43
		return
44
	}
45
46
	// Decrypting with prompt should be OK on non-encypted file even without tty
47
	pem, err = DecryptFileWithPrompt("./test/plaintext_rsa.pem")
48
	if err != nil {
49
		t.Error(err)
50
		return
51
	}
52
	_, err = x509.ParsePKCS1PrivateKey(pem.Bytes)
53
	if err != nil {
54
		t.Error(err)
55
		return
56
	}
57
58
}
59