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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411
|
#!/usr/bin/env lua
---------------------------------------------------------------------
-- LuaLDAP test file.
-- This test will create a copy of an existing entry on the
-- directory to work on. This new entry will be modified,
-- renamed and deleted at the end.
--
-- See Copyright Notice in license.md
---------------------------------------------------------------------
--[=[
---------------------------------------------------------------------
-- Print attributes.
---------------------------------------------------------------------
local function print_attrs (dn, attrs)
if not dn then
io.write ("nil\n")
return
end
io.write (string.format ("\t[%s]\n", dn))
for name, values in pairs (attrs) do
io.write ("["..name.."] : ")
local tv = type (values)
if tv == "string" then
io.write (values)
elseif tv == "table" then
local n = #values
for i = 1, n-1 do
io.write (values[i]..",")
end
io.write (values[n])
end
io.write ("\n")
end
end
--]=]
---------------------------------------------------------------------
-- clone a table.
---------------------------------------------------------------------
local function clone (tab)
local new = {}
for i, v in pairs (tab) do
new[i] = v
end
return new
end
---------------------------------------------------------------------
-- checks for a value and throw an error if it is not the expected.
---------------------------------------------------------------------
local function assert2 (expected, value, msg)
if not msg then
msg = ''
else
msg = tostring(msg)..'\n'
end
local ret = assert (value == expected,
msg.."wrong value (["..tostring(value).."] instead of "..
tostring(expected)..")")
io.write('.')
return ret
end
---------------------------------------------------------------------
-- object test.
---------------------------------------------------------------------
local function test_object (obj, objmethods)
-- checking object type.
assert2 ("userdata", type(obj), "incorrect object type")
-- trying to get metatable.
assert2 ("LuaLDAP: you're not allowed to get this metatable",
getmetatable(obj), "error permitting access to object's metatable")
-- trying to set metatable.
assert2 (false, pcall (setmetatable, obj, {}))
-- checking existence of object's methods.
for i = 1, #objmethods do
local method = obj[objmethods[i]]
assert2 ("function", type(method))
assert2 (false, pcall (method), "no 'self' parameter accepted")
end
return obj
end
local function CONN_OK (obj, err)
if obj == nil then
error (err, 2)
end
return test_object (obj, { "close", "add", "compare", "delete", "modify", "rename", "search", })
end
local DN_PAT = "^([^,=]+)%=([^,]+)%,?(.*)$"
local HOSTNAME
local BASE
local WHO
local BIND_DN
local PASSWORD
local LD
local CLOSED_LD
local DN
local ENTRY
local NEW
local NEW_DN
---------------------------------------------------------------------
-- basic checking test.
---------------------------------------------------------------------
local function basic_test ()
local ld = CONN_OK (lualdap.open_simple (HOSTNAME, BIND_DN, PASSWORD))
assert2 (1, ld:close(), "couldn't close connection")
-- trying to close without a connection.
assert2 (false, pcall (ld.close))
-- trying to close an invalid connection.
assert2 (false, pcall (ld.close, io.output()))
-- trying to use a closed connection.
local _,_,rdn_name,rdn_value = string.find (BASE, DN_PAT)
assert2 (false, pcall (ld.compare, ld, BASE, rdn_name, rdn_value),
"permitting the use of a closed connection")
-- it is ok to close a closed object, but nil is returned instead of 1.
assert2 (nil, ld:close())
-- trying to connect to an invalid host.
assert2 (nil, lualdap.open_simple ("unknown-server"), "this should be an error")
-- reopen the connection.
-- first, try using TLS
local ok = lualdap.open_simple (HOSTNAME, BIND_DN, PASSWORD, true)
if not ok then
-- second, try without TLS
io.write ("\nWarning! Couldn't connect with TLS. Trying again without it.")
ok = lualdap.open_simple (HOSTNAME, BIND_DN, PASSWORD, false)
end
LD = CONN_OK (ok)
CLOSED_LD = ld
collectgarbage()
end
---------------------------------------------------------------------
-- checks return value which should be a function AND also its return value.
---------------------------------------------------------------------
local function check_future (ret, method, ...)
local ok, f = pcall (method, ...)
assert (ok, f)
assert2 ("function", type(f))
assert2 (ret, f())
io.write('.')
end
---------------------------------------------------------------------
-- checking compare operation.
---------------------------------------------------------------------
local function compare_test ()
local _,_,rdn_name,rdn_value = string.find (BASE, DN_PAT)
assert (type(rdn_name) == "string", "could not extract RDN name")
assert (type(rdn_value) == "string", "could not extract RDN value")
-- comparing against the correct value.
check_future (true, LD.compare, LD, BASE, rdn_name, rdn_value)
-- comparing against a wrong value.
check_future (false, LD.compare, LD, BASE, rdn_name, rdn_value..'_')
-- comparing against an incorrect attribute name.
check_future (nil, LD.compare, LD, BASE, rdn_name..'x', rdn_value)
-- comparing on a wrong base.
check_future (nil, LD.compare, LD, 'qwerty', rdn_name, rdn_value)
-- comparing with a closed connection.
assert2 (false, pcall (LD.compare, CLOSED_LD, BASE, rdn_name, rdn_value))
-- comparing with an invalid userdata.
assert2 (false, pcall (LD.compare, io.output(), BASE, rdn_name, rdn_value))
end
---------------------------------------------------------------------
-- checking basic search operation.
---------------------------------------------------------------------
local function search_test_1 ()
local _,_,rdn = string.find (WHO, "^([^,]+)%,.*$")
local iter = LD:search {
base = BASE,
scope = "onelevel",
sizelimit = 1,
filter = "("..rdn..")",
}
assert2 ("function", type(iter))
collectgarbage()
CONN_OK (LD)
local dn, entry = iter ()
assert2 ("string", type(dn))
assert2 ("table", type(entry))
collectgarbage()
assert2 ("function", type(iter))
CONN_OK (LD)
DN, ENTRY = LD:search {
base = BASE,
scope = "onelevel",
sizelimit = 1,
filter = "("..rdn..")",
}()
collectgarbage()
assert2 ("string", type(DN))
assert2 ("table", type(ENTRY))
end
---------------------------------------------------------------------
-- checking add operation.
---------------------------------------------------------------------
local function add_test ()
-- clone an entry.
NEW = clone (ENTRY)
local _,_,rdn_name, rdn_value, parent_dn = string.find (DN, DN_PAT)
NEW[rdn_name] = rdn_value.."_copy"
NEW_DN = string.format ("%s=%s,%s", rdn_name, NEW[rdn_name], parent_dn)
-- trying to insert an entry with a wrong connection.
assert2 (false, pcall (LD.add, CLOSED_LD, NEW_DN, NEW))
-- trying to insert an entry with an invalid connection.
assert2 (false, pcall (LD.add, io.output(), NEW_DN, NEW))
-- trying to insert an entry with a wrong DN.
local wrong_dn = string.format ("%s_x=%s,%s", rdn_name, NEW_DN, parent_dn)
--assert2 (nil, LD:add (wrong_dn, NEW))
check_future (nil, LD.add, LD, wrong_dn, NEW)
-- trying to insert the clone on the LDAP data base.
check_future (true, LD.add, LD, NEW_DN, NEW)
-- trying to reinsert the clone entry on the directory.
check_future (nil, LD.add, LD, NEW_DN, NEW)
end
---------------------------------------------------------------------
-- checking modify operation.
---------------------------------------------------------------------
local function modify_test ()
-- modifying without connection.
assert2 (false, pcall (LD.modify, nil, NEW_DN, {}))
-- modifying with a closed connection.
assert2 (false, pcall (LD.modify, CLOSED_LD, NEW_DN, {}))
-- modifying with an invalid userdata.
assert2 (false, pcall (LD.modify, io.output(), NEW_DN, {}))
-- checking invalid DN.
assert2 (false, pcall (LD.modify, LD, {}))
-- no modification to apply.
check_future (true, LD.modify, LD, NEW_DN)
-- forgotten operation on modifications table.
local a_attr, a_value = next (ENTRY)
assert2 (false, pcall (LD.modify, LD, NEW_DN, { [a_attr] = "abc"}))
-- modifying an unknown entry.
local _,_, rdn_name, rdn_value, parent_dn = string.find (NEW_DN, DN_PAT)
local new_rdn = rdn_name..'='..rdn_value..'_'
local new_dn = string.format ("%s,%s", new_rdn, parent_dn)
check_future (nil, LD.modify, LD, new_dn)
-- trying to create an undefined attribute.
check_future (nil, LD.modify, LD, NEW_DN, {'+', unknown_attribute = 'a'})
end
---------------------------------------------------------------------
local function count (tab)
local counter = 0
for dn, entry in LD:search (tab) do
counter = counter + 1
end
return counter
end
---------------------------------------------------------------------
-- checking advanced search operation.
---------------------------------------------------------------------
local function search_test_2 ()
local _,_,rdn = string.find (WHO, "^([^,]+)%,.*$")
local iter = LD:search {
base = BASE,
scope = "onelevel",
sizelimit = 1,
filter = "("..rdn..")",
}
assert2 ("function", type(iter))
collectgarbage ()
assert2 ("function", type(iter))
local dn, entry = iter ()
assert2 ("string", type(dn))
assert2 ("table", type(entry))
collectgarbage ()
assert2 ("function", type(iter))
iter = nil
collectgarbage ()
-- checking no search specification.
assert2 (false, pcall (LD.search, LD))
-- checking invalid scope.
assert2 (false, pcall (LD.search, LD, { scope = 'BASE', base = BASE, }))
-- checking invalid base.
check_future (nil, LD.search, LD, { base = "invalid", scope = "base", })
-- checking filter.
local _,_, rdn_name, rdn_value, parent_dn = string.find (NEW_DN, DN_PAT)
local filter = string.format ("(%s=%s)", rdn_name, rdn_value)
assert (count { base = BASE, scope = "subtree", filter = filter, } == 1)
-- checking sizelimit.
assert (count { base = BASE, scope = "subtree", sizelimit = 1, } == 1)
-- checking attrsonly parameter.
for dn, entry in LD:search { base = BASE, scope = "subtree", attrsonly = true, } do
for attr, value in pairs (entry) do
assert (value == true, "attrsonly failed")
end
end
-- checking reuse of search object.
local iter = assert (LD:search { base = BASE, scope = "base", })
assert (type(iter) == "function")
local dn, e1 = iter()
assert (type(dn) == "string")
assert (type(e1) == "table")
dn, e1 = iter()
assert (type(dn) == "nil")
assert (type(e1) == "nil")
assert2 (false, pcall (iter))
iter = nil
-- checking collecting search objects.
local dn, entry = LD:search { base = BASE, scope = "base" }()
collectgarbage()
end
---------------------------------------------------------------------
-- checking rename operation.
---------------------------------------------------------------------
local function rename_test ()
local _,_, rdn_name, rdn_value, parent_dn = string.find (NEW_DN, DN_PAT)
local new_rdn = rdn_name..'='..rdn_value..'_'
local new_dn = string.format ("%s,%s", new_rdn, parent_dn)
-- trying to rename with no parent.
check_future (true, LD.rename, LD, NEW_DN, new_rdn, nil)
-- trying to rename an invalid dn.
check_future (nil, LD.rename, LD, NEW_DN, new_rdn, nil)
-- trying to rename with the same parent.
check_future (true, LD.rename, LD, new_dn, rdn_name..'='..rdn_value, parent_dn)
-- trying to rename to an inexistent parent.
check_future (nil, LD.rename, LD, NEW_DN, new_rdn, new_dn)
-- mal-formed DN.
assert2 (false, pcall (LD.rename, LD, ""))
-- trying to rename with a closed connection.
assert2 (false, pcall (LD.rename, CLOSED_LD, NEW_DN, new_rdn, nil))
-- trying to rename with an invalid connection.
assert2 (false, pcall (LD.rename, io.output(), NEW_DN, new_rdn, nil))
end
---------------------------------------------------------------------
-- checking delete operation.
---------------------------------------------------------------------
local function delete_test ()
-- trying to delete with a closed connection.
assert2 (false, pcall (LD.delete, CLOSED_LD, NEW_DN))
-- trying to delete with an invalid connection.
assert2 (false, pcall (LD.delete, io.output(), NEW_DN))
-- trying to delete new entry.
check_future (true, LD.delete, LD, NEW_DN)
-- trying to delete an already deleted entry.
check_future (nil, LD.delete, LD, NEW_DN)
-- mal-formed DN.
check_future (nil, LD.delete, LD, "")
-- no DN.
assert2 (false, pcall (LD.delete, LD))
end
---------------------------------------------------------------------
-- checking close operation.
---------------------------------------------------------------------
local function close_test ()
assert (LD:close () == 1, "couldn't close connection")
end
---------------------------------------------------------------------
local tests = {
{ "basic checking", basic_test },
{ "checking compare operation", compare_test },
{ "checking basic search operation", search_test_1 },
{ "checking add operation", add_test },
{ "checking modify operation", modify_test },
{ "checking advanced search operation", search_test_2 },
{ "checking rename operation", rename_test },
{ "checking delete operation", delete_test },
{ "closing everything", close_test },
}
---------------------------------------------------------------------
-- Main
---------------------------------------------------------------------
if #arg < 3 then
print (string.format ("Usage %s host[:port] base who [bind_dn [password]]", arg[0]))
os.exit()
end
HOSTNAME = arg[1]
BASE = arg[2]
WHO = arg[3]
BIND_DN = arg[4]
PASSWORD = arg[5]
lualdap = require"lualdap"
assert (type(lualdap)=="table", "couldn't load LDAP library")
for i = 1, #tests do
local t = tests[i]
io.write (t[1].." ...")
t[2] ()
io.write (" OK !\n")
end
|