samba-tool tests: add test for 'samba-tool user edit', using LDB_FLAG_FORCE_NO_BASE64...
[samba.git] / python / samba / tests / samba_tool / user_edit.sh
1 #!/bin/sh
2 #
3 # Test for 'samba-tool user edit'
4
5 if [ $# -lt 3 ]; then
6 cat <<EOF
7 Usage: user_edit.sh SERVER USERNAME PASSWORD
8 EOF
9 exit 1;
10 fi
11
12 SERVER="$1"
13 USERNAME="$2"
14 PASSWORD="$3"
15
16 STpath=$(pwd)
17 . $STpath/testprogs/blackbox/subunit.sh
18
19 display_name="Björn"
20 display_name_b64="QmrDtnJu"
21 display_name_new="Renamed Bjoern"
22 # attribute value including control character
23 # echo -e "test \a string" | base64
24 display_name_con_b64="dGVzdCAHIHN0cmluZwo="
25
26 tmpeditor=$(mktemp --suffix .sh -p $STpath/bin samba-tool-editor-XXXXXXXX)
27 chmod +x $tmpeditor
28
29 create_test_user() {
30         $PYTHON ${STpath}/source4/scripting/bin/samba-tool \
31                 user create sambatool1 --random-password \
32                 -H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
33 }
34
35 edit_user() {
36         # create editor.sh
37         cat >$tmpeditor <<-'EOF'
38 #!/usr/bin/env bash
39 user_ldif="$1"
40 SED=$(which sed)
41 $SED -i -e 's/userAccountControl: 512/userAccountControl: 514/' $user_ldif
42 EOF
43
44         $PYTHON ${STpath}/source4/scripting/bin/samba-tool \
45         user edit sambatool1 --editor=$tmpeditor \
46         -H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
47 }
48
49 # Test edit user - add base64 attributes
50 add_attribute_base64() {
51         # create editor.sh
52         cat >$tmpeditor <<EOF
53 #!/usr/bin/env bash
54 user_ldif="\$1"
55
56 grep -v '^$' \$user_ldif > \${user_ldif}.tmp
57 echo "displayName:: $display_name_b64" >> \${user_ldif}.tmp
58
59 mv \${user_ldif}.tmp \$user_ldif
60 EOF
61
62         $PYTHON ${STpath}/source4/scripting/bin/samba-tool user edit \
63                 sambatool1 --editor=$tmpeditor \
64                 -H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
65 }
66
67 get_attribute_base64() {
68         $PYTHON ${STpath}/source4/scripting/bin/samba-tool user show \
69                 sambatool1 --attributes=displayName \
70                 -H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
71 }
72
73 delete_attribute() {
74         # create editor.sh
75         cat >$tmpeditor <<EOF
76 #!/usr/bin/env bash
77 user_ldif="\$1"
78
79 grep -v '^displayName' \$user_ldif >> \${user_ldif}.tmp
80 mv \${user_ldif}.tmp \$user_ldif
81 EOF
82         $PYTHON ${STpath}/source4/scripting/bin/samba-tool user edit \
83                 sambatool1 --editor=$tmpeditor \
84                 -H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
85 }
86
87 # Test edit user - add base64 attribute value including control character
88 add_attribute_base64_control() {
89         # create editor.sh
90         cat >$tmpeditor <<EOF
91 #!/usr/bin/env bash
92 user_ldif="\$1"
93
94 grep -v '^$' \$user_ldif > \${user_ldif}.tmp
95 echo "displayName:: $display_name_con_b64" >> \${user_ldif}.tmp
96
97 mv \${user_ldif}.tmp \$user_ldif
98 EOF
99         $PYTHON ${STpath}/source4/scripting/bin/samba-tool user edit \
100                 sambatool1 --editor=$tmpeditor \
101                 -H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
102 }
103
104 get_attribute_base64_control() {
105         $PYTHON ${STpath}/source4/scripting/bin/samba-tool user show \
106                 sambatool1 --attributes=displayName \
107                 -H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
108 }
109
110
111 # Test edit user - change base64 attribute value including control character
112 change_attribute_base64_control() {
113         # create editor.sh
114         cat >$tmpeditor <<EOF
115 #!/usr/bin/env bash
116 user_ldif="\$1"
117
118 sed -i -e 's/displayName:: $display_name_con_b64/displayName: $display_name/' \
119         \$user_ldif
120 EOF
121         $PYTHON ${STpath}/source4/scripting/bin/samba-tool user edit \
122                 sambatool1 --editor=$tmpeditor \
123                 -H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
124 }
125
126 get_attribute_base64_control() {
127         $PYTHON ${STpath}/source4/scripting/bin/samba-tool user show \
128                 sambatool1 --attributes=displayName \
129                 -H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
130 }
131
132 # Test edit user - change attributes with LDB_FLAG_FORCE_NO_BASE64_LDIF
133 change_attribute_force_no_base64() {
134         # create editor.sh
135         # Expects that the original attribute is available as clear text,
136         # because the LDB_FLAG_FORCE_NO_BASE64_LDIF should be used here.
137         cat >$tmpeditor <<EOF
138 #!/usr/bin/env bash
139 user_ldif="\$1"
140
141 sed -i -e 's/displayName: $display_name/displayName: $display_name_new/' \
142         \$user_ldif
143 EOF
144
145         $PYTHON ${STpath}/source4/scripting/bin/samba-tool user edit \
146                 sambatool1 --editor=$tmpeditor \
147                 -H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
148 }
149
150 get_changed_attribute_force_no_base64() {
151         $PYTHON ${STpath}/source4/scripting/bin/samba-tool user show \
152                  sambatool1 --attributes=displayName \
153                  -H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
154 }
155
156 delete_user() {
157         $PYTHON ${STpath}/source4/scripting/bin/samba-tool \
158                 user delete sambatool1 \
159                 -H "ldap://$SERVER" "-U$USERNAME" "--password=$PASSWORD"
160 }
161
162 failed=0
163
164 testit "create_test_user" create_test_user || failed=`expr $failed + 1`
165 testit "edit_user" edit_user || failed=`expr $failed + 1`
166 testit "add_attribute_base64" add_attribute_base64 || failed=`expr $failed + 1`
167 testit_grep "get_attribute_base64" "^displayName:: $display_name_b64" get_attribute_base64 || failed=`expr $failed + 1`
168 testit "delete_attribute" delete_attribute || failed=`expr $failed + 1`
169 testit "add_attribute_base64_control" add_attribute_base64_control || failed=`expr $failed + 1`
170 testit_grep "get_attribute_base64_control" "^displayName:: $display_name_con_b64" get_attribute_base64_control || failed=`expr $failed + 1`
171 testit "change_attribute_base64_control" change_attribute_base64_control || failed=`expr $failed + 1`
172 testit_grep "get_attribute_base64_control" "^displayName:: $display_name_b64" get_attribute_base64_control || failed=`expr $failed + 1`
173 testit "change_attribute_force_no_base64" change_attribute_force_no_base64 || failed=`expr $failed + 1`
174 testit_grep "get_changed_attribute_force_no_base64" "^displayName: $display_name_new" get_changed_attribute_force_no_base64 || failed=`expr $failed + 1`
175 testit "delete_user" delete_user || failed=`expr $failed + 1`
176
177 rm -f $tmpeditor
178
179 exit $failed