aboutsummaryrefslogtreecommitdiffstats
path: root/lualdap/tests/test.lua
blob: b3261b64a11dda2a51c48a03a38c763ee34ab102 (plain) (blame)
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
#!/usr/local/bin/lua
-- LuaLDAP test file.
-- $Id: test.lua,v 1.2 2003-06-16 10:38:15 tomas Exp $

function print_attrs (attrs)
	io.write (string.format (" [dn] : %s\n", attrs.dn))
	for name, values in pairs (attrs) do
		if name ~= "dn" then
			io.write ("["..name.."] : ")
			if type (values) == "table" then
				for i = 1, (table.getn (values)-1) do
					io.write (values[i]..",")
				end
				io.write (values[table.getn(values)])
			else
				io.write (values)
			end
			io.write ("\n")
		end
	end
end

require"lualdap"

if table.getn(arg) < 1 then
	print (string.format ("Usage %s host[:port] base", arg[0]))
	os.exit()
end

local hostname = arg[1]
--local who = arg[2]
--local password = arg[3]
local base = arg[2]
local filter = arg[3] or "objectclass=*"
local attribs = {}
for n = 4, table.getn(arg) do
	attribs[n-3] = arg[n]
end

assert (lualdap, "couldn't load LDAP library")
local ld = assert (lualdap.open_simple (hostname, who, password))
assert (ld:close () == 1, "couldn't close connection")
assert (pcall (ld.close, ld) == false)

local ld = assert (lualdap.open_simple (hostname, who, password))

-- search
for msg, attrs in ld:search_attribs (base, "subtree", filter, attribs) do
	print_attrs (attrs)
end
print ("search ok")

-- compare
print("compare", ld:compare ("videoID=676DE,ou=video,dc=teste,dc=br", "videoTitulo", "Tecnologias de Video Digital"))

-- modify
print("modify", ld:modify ("videoID=676DE,ou=video,dc=teste,dc=br", {
	{ op = "a", type = "videoTitulo", values = "Tecnologias de Video Digital" },
}))


--[[
print"!!!"

for msg, attrs in ld:search_attribs (base, "subtree", filter, { "videoID" }) do
	print_attrs (attrs)
end

print"!!!"

local iter1, state1, first1 = ld:search_attribs (base, "subtree", filter, { "dn", "objectClass", "videoTitulo", })
local iter2, state2, first2 = ld:search_attribs (base, "subtree", filter, { "dn", "videoID", "videoTitulo", })

local m1,a1 = iter1 (state1, first1)
io.write ("\n 1 >")
print_attrs (a1)
local m2,a2 = iter2 (state2, first2)
io.write ("\n 2 >")
print_attrs (a2)
m1,a1 = iter1 (state1, m1)
io.write ("\n 3 >")
print_attrs (a1)
m2,a2 = iter2 (state2, m2)
io.write ("\n 4 >")
print_attrs (a2)
--]]

assert (ld:close () == 1, "couldn't close connection")
print("ok")