-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathadd_test.go
115 lines (111 loc) · 3.37 KB
/
add_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Copyright (c) Jim Lambert
// SPDX-License-Identifier: MIT
package gldap
import (
"testing"
ber "github.com/go-asn1-ber/asn1-ber"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAttribute_decode(t *testing.T) {
tests := []struct {
name string
packet *ber.Packet
want *Attribute
wantErr bool
wantErrIs error
wantErrContains string
}{
{
name: "missing packet",
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing ber packet",
},
{
name: "not-attr-packet",
packet: func() *ber.Packet {
return ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.Tag(ber.TypePrimitive), nil, "invalid-primitive")
}(),
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing/invalid attributes ber packet",
},
{
name: "missing-type-child",
packet: func() *ber.Packet {
return ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.Tag(ber.TagSequence), nil, "Attribute")
}(),
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing/invalid attributes type",
},
{
name: "invalid-type-child",
packet: func() *ber.Packet {
seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.Tag(ber.TagSequence), nil, "Attribute")
seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypeConstructed, ber.TagBoolean, "false", "invalid-type-bool"))
return seq
}(),
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing/invalid attributes type",
},
{
name: "missing-values",
packet: func() *ber.Packet {
seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.Tag(ber.TagSequence), nil, "Attribute")
seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "email", "Type"))
return seq
}(),
wantErr: true,
wantErrIs: ErrInvalidParameter,
wantErrContains: "missing/invalid attributes values",
},
{
name: "bad-values",
packet: func() *ber.Packet {
seq := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.Tag(ber.TagSequence), nil, "Attribute")
seq.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagOctetString, "email", "Type"))
set := ber.Encode(ber.ClassUniversal, ber.TypeConstructed, ber.TagSet, nil, "AttributeValue")
set.AppendChild(ber.NewString(ber.ClassUniversal, ber.TypePrimitive, ber.TagBoolean, "false", "invalid-type-bool"))
seq.AppendChild(set)
return seq
}(),
wantErr: true,
wantErrContains: "invalid attribute values packet",
},
{
name: "success",
packet: func() *ber.Packet {
attr := Attribute{
Type: "email",
Vals: []string{"[email protected]"},
}
return attr.encode()
}(),
want: &Attribute{
Type: "email",
Vals: []string{"[email protected]"},
},
},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert, require := assert.New(t), require.New(t)
got, err := decodeAttribute(tc.packet)
if tc.wantErr {
require.Error(err)
if tc.wantErrIs != nil {
assert.ErrorIs(err, tc.wantErrIs)
}
if tc.wantErrContains != "" {
assert.Contains(err.Error(), tc.wantErrContains)
}
return
}
require.NoError(err)
assert.NotNil(got)
})
}
}