| 1 | package x509tools |
| 2 | |
| 3 | import ( |
| 4 | "crypto/x509/pkix" |
| 5 | "encoding/asn1" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/stretchr/testify/assert" |
| 9 | "github.com/stretchr/testify/require" |
| 10 | ) |
| 11 | |
| 12 | func TestNames(t *testing.T) { |
| 13 | n := pkix.Name{ |
| 14 | CommonName: "foo/bar", |
| 15 | Organization: []string{"ham ", "\"spam\"", " eggs"}, |
| 16 | Locality: []string{"north+southville"}, |
| 17 | Country: []string{""}, |
| 18 | ExtraNames: []pkix.AttributeTypeAndValue{ |
| 19 | {Type: asn1.ObjectIdentifier{1, 2, 3, 4}, Value: "5678"}, |
| 20 | {Type: asn1.ObjectIdentifier{2, 5, 4, 17}, Value: "888"}, |
| 21 | }, |
| 22 | } |
| 23 | name, err := asn1.Marshal(n.ToRDNSequence()) |
| 24 | require.NoError(t, err) |
| 25 | assert.Equal(t, `/C=/L=north+southville/O=ham /O= eggs/O="spam"/CN=foo\/bar/1.2.3.4=5678/postalCode=888`, FormatPkixName(name, NameStyleOpenSsl)) |
| 26 | assert.Equal(t, `postalCode=888, 1.2.3.4=5678, CN=foo/bar, O="ham " + O=" eggs" + O="""spam""", L="north+southville", C=""`, FormatPkixName(name, NameStyleLdap)) |
| 27 | assert.Equal(t, `PostalCode=888, OID.1.2.3.4=5678, CN=foo/bar, O="ham " + O=" eggs" + O="""spam""", L="north+southville", C=""`, FormatPkixName(name, NameStyleMsOsco)) |
| 28 | } |
| 29 | |
| 30 | func TestBMP(t *testing.T) { |
| 31 | str := "✔" |
| 32 | bmp := ToBMPString(str) |
| 33 | assert.Equal(t, []byte{0x27, 0x14}, bmp.Bytes) |
| 34 | } |
| 35 | |
| 36 | func TestBMPName(t *testing.T) { |
| 37 | n := pkix.RDNSequence{ |
| 38 | pkix.RelativeDistinguishedNameSET{pkix.AttributeTypeAndValue{ |
| 39 | Type: asn1.ObjectIdentifier{2, 5, 4, 10}, |
| 40 | Value: asn1.RawValue{ |
| 41 | Tag: asn1.TagBMPString, |
| 42 | Bytes: []byte{0x27, 0x14}, |
| 43 | }, |
| 44 | }}, |
| 45 | } |
| 46 | blob, err := asn1.Marshal(n) |
| 47 | require.NoError(t, err) |
| 48 | assert.Equal(t, `O=✔`, FormatPkixName(blob, NameStyleLdap)) |
| 49 | } |