func testDelete(t *testing.T) { lc := ldap.NewConnection("localhost:9999") err := lc.Connect() if err != nil { t.Error(err) } err = lc.Bind(slapd.DefaultConfig.Rootdn.Dn, slapd.DefaultConfig.Rootdn.Password) if err != nil { t.Error(err) } c := New(lc, "dc=example,dc=com") p := foobarPerson err = c.Delete(&p) if err != nil { t.Error(err) } err = c.Read(&p) if err == nil { t.Error("object wasn't deleted from ldap") } }
func testCreate(t *testing.T) { lc := ldap.NewConnection(slapd.DefaultConfig.Address()) err := lc.Connect() if err != nil { t.Error(err) } err = lc.Bind(slapd.DefaultConfig.Rootdn.Dn, slapd.DefaultConfig.Rootdn.Password) if err != nil { t.Error(err) } c := New(lc, "dc=example,dc=com") err = c.Create(&fritzFoobarPerson) t.Logf("Created: %+v", fritzFoobarPerson) if err != nil { t.Error(err) } p := Person{sn: []string{"Foobar"}, cn: []string{}} err = c.Read(&p) if err != nil { t.Error("object wasn't deleted from ldap") } t.Logf("Read: %+v", p) }
func TestDeleteSubtree(t *testing.T) { var s = new(slapd.Slapd) s.Config = &slapd.DefaultConfig err := s.StartAndInitialize() defer s.Stop() if err != nil { t.Error(err) } lc := ldap.NewConnection("localhost:9999") err = lc.Connect() if err != nil { t.Error(err) } err = lc.Bind(slapd.DefaultConfig.Rootdn.Dn, slapd.DefaultConfig.Rootdn.Password) if err != nil { t.Error(err) } c := New(lc, "dc=example,dc=com") err = c.Create(&fritzFoobarPerson) if err != nil { t.Error(err) } var fritzSubPerson = fritzFoobarPerson fritzSubPerson.dn = fmt.Sprintf("sn=%v,%v", fritzSubPerson.sn, fritzFoobarPerson.Dn()) err = c.Create(&fritzSubPerson) if err != nil { t.Error(err) } err = c.DeleteSubtree(&fritzFoobarPerson) if err != nil { t.Error(err) } err = c.Read(&fritzSubPerson) if err == nil { t.Error("object wasn't deleted from ldap") } err = c.Read(&fritzFoobarPerson) if err == nil { t.Error("object wasn't deleted from ldap") } }
func testReadAllSubtree(t *testing.T) { lc := ldap.NewConnection("localhost:9999") err := lc.Connect() if err != nil { t.Error(err) } err = lc.Bind(slapd.DefaultConfig.Rootdn.Dn, slapd.DefaultConfig.Rootdn.Password) if err != nil { t.Error(err) } c := New(lc, "dc=example,dc=com") err = c.Create(&fritzFoobarPerson) if err != nil { t.Error(err) } err = c.Create(&fritzQuxPerson) if err != nil { t.Error(err) } entries, err := c.ReadAllSubtree(&foobarPerson) if err != nil { t.Error(err) } if len(entries) != 2 { t.Error("Expected exactly two results, got", len(entries)) } var count int for _, v := range entries { t.Log(v.Dn(), v) for _, w := range []*Person{&fritzFoobarPerson, &fritzQuxPerson} { if equalStringSlice(v.(*Person).sn, w.sn) && equalStringSlice(v.(*Person).cn, w.cn) { count++ continue } } } if count != 2 { t.Fail() } }
func testUpdate(t *testing.T) { lc := ldap.NewConnection("localhost:9999") err := lc.Connect() if err != nil { t.Error(err) } err = lc.Bind(slapd.DefaultConfig.Rootdn.Dn, slapd.DefaultConfig.Rootdn.Password) if err != nil { t.Error(err) } c := New(lc, "dc=example,dc=com") // get the person created in testRead oldPerson := foobarPerson err = c.Read(&oldPerson) if err != nil { t.Error(err) } t.Logf("values before update: %+v", oldPerson) // create a new person with new cn values "Gonzo", "von" // and perform an update newPerson := gonzoPerson err = c.Update(&newPerson) if err != nil { t.Error(err) } // get the person again from ldap. it should now have the cn values // we previously set err = c.Read(&oldPerson) if err != nil { t.Error(err) } t.Logf("values after update: %+v", oldPerson) if !equalStringSlice(oldPerson.sn, gonzoPerson.sn) || !equalStringSlice(oldPerson.cn, gonzoPerson.cn) { t.Error("Read entry has unexpected attribute values. Expected:", gonzoPerson, "Got:", oldPerson) } }
func testRead(t *testing.T) { lc := ldap.NewConnection("localhost:9999") err := lc.Connect() if err != nil { t.Error(err) } err = lc.Bind(slapd.DefaultConfig.Rootdn.Dn, slapd.DefaultConfig.Rootdn.Password) if err != nil { t.Error(err) } c := New(lc, "dc=example,dc=com") p := foobarPerson err = c.Read(&p) t.Logf("Read: %+v", p) if err != nil { t.Error(err) } }
func TestPasswd(t *testing.T) { var s = new(slapd.Slapd) s.Config = &slapd.DefaultConfig err := s.StartAndInitialize() defer s.Stop() if err != nil { t.Error(err) } lc := ldap.NewConnection("localhost:9999") err = lc.Connect() if err != nil { t.Error(err) } err = lc.Bind(slapd.DefaultConfig.Rootdn.Dn, slapd.DefaultConfig.Rootdn.Password) if err != nil { t.Error(err) } c := New(lc, "dc=example,dc=com") // create test person err = c.Create(&fritzFoobarPerson) if err != nil { t.Error(err) } // set password of test person to "foobaz" err = c.Passwd(&fritzFoobarPerson, "foobaz") if err != nil { t.Error(err) } c.Close() lc = ldap.NewConnection("localhost:9999") err = lc.Connect() if err != nil { t.Error(err) } // try to login as the test person with password "foobaz" err = lc.Bind(fritzFoobarPerson.Dn()+","+slapd.DefaultConfig.Suffix.Dn, "foobaz") if err != nil { t.Error(err) } c = New(lc, "dc=example,dc=com") // let the test person change its own password to "foobar" // this needs these acls set in slapd.conf: // access to attrs=userPassword // by self write // by anonymous auth // by users none // access to * by * read err = c.Passwd(nil, "foobar") if err != nil { t.Error(err) } c.Close() lc = ldap.NewConnection("localhost:9999") err = lc.Connect() if err != nil { t.Error(err) } // try to login as the test person with password "foobar" err = lc.Bind(fritzFoobarPerson.Dn()+","+slapd.DefaultConfig.Suffix.Dn, "foobar") if err != nil { t.Error(err) } }