go_pgp/Creation_test.go
2024-05-10 22:24:14 +02:00

113 lines
4.3 KiB
Go

package go_pgp
import (
"testing"
)
const (
testPassword = "p4ssw0rd"
)
func TestCreatePGPEntity(t *testing.T) {
entity, err := CreatePGPEntity("")
if err == nil || entity != nil {
t.Error("TestCreatePGPEntity: test case 1: unexpectedly succeeded in creating an unnamed entity")
}
entity, err = CreatePGPEntity("Me", RSABitsOption(0))
if err != nil {
t.Fatal("TestCreatePGPEntity: test case 2.1: received an error, trying to create a named entity")
} else if entity == nil {
t.Fatal("TestCreatePGPEntity: test case 2.1: failed to create a named entity")
}
if entity.PrimaryIdentity().Name != "Me" {
t.Errorf("TestCreatePGPEntity: test case 2.2: expected identity name 'Me' but got '%s'", entity.PrimaryIdentity().Name)
}
}
func TestSetPassword(t *testing.T) {
entity, _ := CreatePGPEntity("Me", RSABitsOption(0))
if entity.PrivateKey.Encrypted {
t.Fatal("TestSetPassword: test case 1: private key should not be encrypted yet")
}
err := entity.SetPassword([]byte(testPassword))
if err != nil || !entity.PrivateKey.Encrypted {
t.Fatal("TestSetPassword: test case 2: encryption of private key failed")
}
err = entity.SetPassword([]byte(testPassword + "2"))
if err != nil || !entity.PrivateKey.Encrypted {
t.Fatal("TestSetPassword: test case 3: second encryption of private key failed")
}
}
func TestCreatePGPEntityEmail(t *testing.T) {
entity, err := CreatePGPEntityEmail("", "")
if err == nil || entity != nil {
t.Error("TestCreatePGPEntityEmail: test case 1: unexpectedly succeeded in creating an unnamed entity")
}
expectedName := "<Jenkins@heye-international.com>"
entity, err = CreatePGPEntityEmail("", "Jenkins@heye-international.com")
if err != nil {
t.Fatal("TestCreatePGPEntityEmail: test case 2.1: received an error, trying to create a named entity")
} else if entity == nil {
t.Fatal("TestCreatePGPEntityEmail: test case 2.1: failed to create a named entity")
}
if name := entity.PrimaryIdentity().Name; name != expectedName {
t.Errorf("TestCreatePGPEntityEmail: test case 2.2: expected identity name '%s' but got '%s'", expectedName, name)
}
expectedName = "Me <Jenkins@heye-international.com>"
entity, err = CreatePGPEntityEmail("Me", "Jenkins@heye-international.com")
if err != nil {
t.Fatal("TestCreatePGPEntityEmail: test case 3.1: received an error, trying to create a named entity")
} else if entity == nil {
t.Fatal("TestCreatePGPEntityEmail: test case 3.1: failed to create a named entity")
}
if name := entity.PrimaryIdentity().Name; name != expectedName {
t.Errorf("TestCreatePGPEntityEmail: test case 3.2: expected identity name '%s' but got '%s'", expectedName, name)
}
expectedName = "Me"
entity, err = CreatePGPEntityEmail("Me", "")
if err != nil {
t.Fatal("TestCreatePGPEntityEmail: test case 4.1: received an error, trying to create a named entity")
} else if entity == nil {
t.Fatal("TestCreatePGPEntityEmail: test case 4.1: failed to create a named entity")
}
if name := entity.PrimaryIdentity().Name; name != expectedName {
t.Errorf("TestCreatePGPEntityEmail: test case 4.2: expected identity name '%s' but got '%s'", expectedName, name)
}
}
func TestCreateCommentedPGPEntity(t *testing.T) {
entity, err := CreateCommentedPGPEntity("", "myComment")
if err == nil || entity != nil {
t.Error("TestCreateCommentedPGPEntity: test case 1: unexpectedly succeeded in creating an unnamed entity")
}
expectedName := "Me (myComment)"
entity, err = CreateCommentedPGPEntity("Me", "myComment")
if err != nil {
t.Fatal("TestCreateCommentedPGPEntity: test case 2.1: received an error, trying to create a named entity")
} else if entity == nil {
t.Fatal("TestCreateCommentedPGPEntity: test case 2.1: failed to create a named entity")
}
if name := entity.PrimaryIdentity().Name; name != expectedName {
t.Errorf("TestCreateCommentedPGPEntity: test case 2.2: expected identity name '%s' but got '%s'", expectedName, name)
}
expectedName = "Me"
entity, err = CreateCommentedPGPEntity("Me", "")
if err != nil {
t.Fatal("TestCreateCommentedPGPEntity: test case 3.1: received an error, trying to create a named entity")
} else if entity == nil {
t.Fatal("TestCreateCommentedPGPEntity: test case 3.1: failed to create a named entity")
}
if name := entity.PrimaryIdentity().Name; name != expectedName {
t.Errorf("TestCreateCommentedPGPEntity: test case 3.2: expected identity name '%s' but got '%s'", expectedName, name)
}
}