...

Source file src/github.com/go-ldap/ldap/v3/search_test.go

Documentation: github.com/go-ldap/ldap/v3

     1  package ldap
     2  
     3  import (
     4  	"reflect"
     5  	"testing"
     6  	"time"
     7  
     8  	"github.com/stretchr/testify/assert"
     9  )
    10  
    11  // TestNewEntry tests that repeated calls to NewEntry return the same value with the same input
    12  func TestNewEntry(t *testing.T) {
    13  	dn := "testDN"
    14  	attributes := map[string][]string{
    15  		"alpha":   {"value"},
    16  		"beta":    {"value"},
    17  		"gamma":   {"value"},
    18  		"delta":   {"value"},
    19  		"epsilon": {"value"},
    20  	}
    21  	executedEntry := NewEntry(dn, attributes)
    22  
    23  	iteration := 0
    24  	for {
    25  		if iteration == 100 {
    26  			break
    27  		}
    28  		testEntry := NewEntry(dn, attributes)
    29  		if !reflect.DeepEqual(executedEntry, testEntry) {
    30  			t.Fatalf("subsequent calls to NewEntry did not yield the same result:\n\texpected:\n\t%v\n\tgot:\n\t%v\n", executedEntry, testEntry)
    31  		}
    32  		iteration = iteration + 1
    33  	}
    34  }
    35  
    36  func TestGetAttributeValue(t *testing.T) {
    37  	dn := "testDN"
    38  	attributes := map[string][]string{
    39  		"Alpha":   {"value"},
    40  		"bEta":    {"value"},
    41  		"gaMma":   {"value"},
    42  		"delTa":   {"value"},
    43  		"epsiLon": {"value"},
    44  	}
    45  	entry := NewEntry(dn, attributes)
    46  	if entry.GetAttributeValue("Alpha") != "value" {
    47  		t.Errorf("failed to get attribute in original case")
    48  	}
    49  
    50  	if entry.GetEqualFoldAttributeValue("alpha") != "value" {
    51  		t.Errorf("failed to get attribute in changed case")
    52  	}
    53  }
    54  
    55  func TestEntry_Unmarshal(t *testing.T) {
    56  	t.Run("passing a struct should fail", func(t *testing.T) {
    57  		entry := &Entry{}
    58  
    59  		type toStruct struct{}
    60  
    61  		result := toStruct{}
    62  		err := entry.Unmarshal(result)
    63  
    64  		assert.NotNil(t, err)
    65  	})
    66  
    67  	t.Run("passing a ptr to string should fail", func(t *testing.T) {
    68  		entry := &Entry{}
    69  
    70  		str := "foo"
    71  		err := entry.Unmarshal(&str)
    72  
    73  		assert.NotNil(t, err)
    74  	})
    75  
    76  	t.Run("user struct be decoded", func(t *testing.T) {
    77  		entry := &Entry{
    78  			DN: "cn=mario,ou=Users,dc=go-ldap,dc=github,dc=com",
    79  			Attributes: []*EntryAttribute{
    80  				{
    81  					Name:       "cn",
    82  					Values:     []string{"mario"},
    83  					ByteValues: nil,
    84  				},
    85  				{
    86  					Name:       "mail",
    87  					Values:     []string{"mario@go-ldap.com"},
    88  					ByteValues: nil,
    89  				},
    90  				// Tests int value.
    91  				{
    92  					Name:       "id",
    93  					Values:     []string{"2147483647"},
    94  					ByteValues: nil,
    95  				},
    96  				// Tests int64 value.
    97  				{
    98  					Name:       "longId",
    99  					Values:     []string{"9223372036854775807"},
   100  					ByteValues: nil,
   101  				},
   102  				// Tests []byte value.
   103  				{
   104  					Name:   "data",
   105  					Values: []string{"data"},
   106  					ByteValues: [][]byte{
   107  						[]byte("data"),
   108  					},
   109  				},
   110  				// Tests time.Time value.
   111  				{
   112  					Name:       "createdTimestamp",
   113  					Values:     []string{"202305041930Z"},
   114  					ByteValues: nil,
   115  				},
   116  				// Tests *DN value
   117  				{
   118  					Name:       "owner",
   119  					Values:     []string{"uid=foo,dc=example,dc=org"},
   120  					ByteValues: nil,
   121  				},
   122  				// Tests []*DN value
   123  				{
   124  					Name:       "children",
   125  					Values:     []string{"uid=bar,dc=example,dc=org", "uid=baz,dc=example,dc=org"},
   126  					ByteValues: nil,
   127  				},
   128  			},
   129  		}
   130  
   131  		type User struct {
   132  			Dn       string    `ldap:"dn"`
   133  			Cn       string    `ldap:"cn"`
   134  			Mail     string    `ldap:"mail"`
   135  			ID       int       `ldap:"id"`
   136  			LongID   int64     `ldap:"longId"`
   137  			Data     []byte    `ldap:"data"`
   138  			Created  time.Time `ldap:"createdTimestamp"`
   139  			Owner    *DN       `ldap:"owner"`
   140  			Children []*DN     `ldap:"children"`
   141  		}
   142  
   143  		created, err := time.Parse("200601021504Z", "202305041930Z")
   144  		if err != nil {
   145  			t.Errorf("failed to parse ref time: %s", err)
   146  		}
   147  		owner, err := ParseDN("uid=foo,dc=example,dc=org")
   148  		if err != nil {
   149  			t.Errorf("failed to parse ref DN: %s", err)
   150  		}
   151  		var children []*DN
   152  		for _, child := range []string{"uid=bar,dc=example,dc=org", "uid=baz,dc=example,dc=org"} {
   153  			dn, err := ParseDN(child)
   154  			if err != nil {
   155  				t.Errorf("failed to parse child ref DN: %s", err)
   156  			}
   157  			children = append(children, dn)
   158  		}
   159  
   160  		expect := &User{
   161  			Dn:       "cn=mario,ou=Users,dc=go-ldap,dc=github,dc=com",
   162  			Cn:       "mario",
   163  			Mail:     "mario@go-ldap.com",
   164  			ID:       2147483647,
   165  			LongID:   9223372036854775807,
   166  			Data:     []byte("data"),
   167  			Created:  created,
   168  			Owner:    owner,
   169  			Children: children,
   170  		}
   171  		result := &User{}
   172  		err = entry.Unmarshal(result)
   173  
   174  		assert.Nil(t, err)
   175  		assert.Equal(t, expect, result)
   176  	})
   177  
   178  	t.Run("group struct be decoded", func(t *testing.T) {
   179  		entry := &Entry{
   180  			DN: "cn=DREAM_TEAM,ou=Groups,dc=go-ldap,dc=github,dc=com",
   181  			Attributes: []*EntryAttribute{
   182  				{
   183  					Name:       "cn",
   184  					Values:     []string{"DREAM_TEAM"},
   185  					ByteValues: nil,
   186  				},
   187  				{
   188  					Name:       "member",
   189  					Values:     []string{"mario", "luigi", "browser"},
   190  					ByteValues: nil,
   191  				},
   192  			},
   193  		}
   194  
   195  		type Group struct {
   196  			DN      string   `ldap:"dn" yaml:"dn" json:"dn"`
   197  			CN      string   `ldap:"cn" yaml:"cn" json:"cn"`
   198  			Members []string `ldap:"member"`
   199  		}
   200  
   201  		expect := &Group{
   202  			DN:      "cn=DREAM_TEAM,ou=Groups,dc=go-ldap,dc=github,dc=com",
   203  			CN:      "DREAM_TEAM",
   204  			Members: []string{"mario", "luigi", "browser"},
   205  		}
   206  
   207  		result := &Group{}
   208  		err := entry.Unmarshal(result)
   209  
   210  		assert.Nil(t, err)
   211  		assert.Equal(t, expect, result)
   212  	})
   213  }
   214  

View as plain text