639805c9336c8c1c01acb3a2208344da6f960699
[gd/samba/.git] / source4 / dsdb / schema / tests / schema_syntax.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    Test DSDB syntax functions
5
6    Copyright (C) Andrew Bartlet <abartlet@samba.org> 2008
7    
8    This program is free software; you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3 of the License, or
11    (at your option) any later version.
12    
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17    
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "includes.h"
23 #include "lib/events/events.h"
24 #include "lib/ldb/include/ldb.h"
25 #include "lib/ldb/include/ldb_errors.h"
26 #include "lib/ldb-samba/ldif_handlers.h"
27 #include "ldb_wrap.h"
28 #include "dsdb/samdb/samdb.h"
29 #include "param/param.h"
30 #include "torture/smbtorture.h"
31 #include "torture/local/proto.h"
32 #include "param/provision.h"
33
34
35 DATA_BLOB hexstr_to_data_blob(TALLOC_CTX *mem_ctx, const char *string) 
36 {
37         DATA_BLOB binary = data_blob_talloc(mem_ctx, NULL, strlen(string)/2);
38         binary.length = strhex_to_str((char *)binary.data, binary.length, string, strlen(string));
39         return binary;
40 }
41
42 static bool torture_test_syntax(struct torture_context *torture, 
43                                 const char *oid,
44                                 const char *attr_string, 
45                                 const char *ldb_str,
46                                 const char *drs_str)
47 {
48         TALLOC_CTX *tmp_ctx = talloc_new(torture);
49         DATA_BLOB drs_binary = hexstr_to_data_blob(tmp_ctx, drs_str);
50         DATA_BLOB ldb_blob = data_blob_string_const(ldb_str);
51         struct drsuapi_DsReplicaAttribute drs, drs2;
52         struct drsuapi_DsAttributeValue val;
53         const struct dsdb_syntax *syntax;
54         const struct dsdb_schema *schema;
55         const struct dsdb_attribute *attr;
56         struct ldb_context *ldb;
57         struct ldb_message_element el;
58
59         drs.value_ctr.num_values = 1;
60         drs.value_ctr.values = &val;
61         val.blob = &drs_binary;
62
63         torture_assert(torture, ldb = provision_get_schema(tmp_ctx, torture->lp_ctx), "Failed to load schema from disk");
64         torture_assert(torture, schema = dsdb_get_schema(ldb), "Failed to fetch schema");
65         torture_assert(torture, syntax = find_syntax_map_by_standard_oid(oid), "Failed to find syntax handler");
66         torture_assert(torture, attr = dsdb_attribute_by_lDAPDisplayName(schema, attr_string), "Failed to find attribute handler");
67         torture_assert_str_equal(torture, attr->syntax->name, syntax->name, "Syntax from schema not as expected");
68         
69
70         torture_assert_werr_ok(torture, syntax->drsuapi_to_ldb(ldb, schema, attr, &drs, tmp_ctx, &el), "Failed to convert from DRS to ldb format");
71         
72         torture_assert_data_blob_equal(torture, el.values[0], ldb_blob, "Incorrect conversion from DRS to ldb format");
73
74         torture_assert_werr_ok(torture, syntax->ldb_to_drsuapi(ldb, schema, attr, &el, tmp_ctx, &drs2), "Failed to convert from ldb to DRS format");
75         
76         torture_assert(torture, drs2.value_ctr.values[0].blob, "No blob returned from conversion");
77         torture_assert_data_blob_equal(torture, *drs2.value_ctr.values[0].blob, drs_binary, "Incorrect conversion from ldb to DRS format");
78         return true;
79 }
80
81 static bool torture_dsdb_drs_DN_BINARY(struct torture_context *torture) 
82 {
83         bool ret;
84         const char *ldb_str = "B:32:A9D1CA15768811D1ADED00C04FD8D5CD:<GUID=a8378c29-6319-45b3-b216-6a3108452d6c>;CN=Users,DC=ad,DC=ruth,DC=abartlet,DC=net";
85         const char *drs_str = "8C00000000000000298C37A81963B345B2166A3108452D6C000000000000000000000000000000000000000000000000000000002900000043004E003D00550073006500720073002C00440043003D00610064002C00440043003D0072007500740068002C00440043003D00610062006100720074006C00650074002C00440043003D006E0065007400000014000000A9D1CA15768811D1ADED00C04FD8D5CD";
86         const char *ldb_str2 = "B:8:00000002:<GUID=2b475208-3180-4ad4-b6bb-a26cfb44ac50>;<SID=S-1-5-21-3686369990-3108025515-1819299124>;DC=ad,DC=ruth,DC=abartlet,DC=net";
87         const char *drs_str2 = "7A000000180000000852472B8031D44AB6BBA26CFB44AC50010400000000000515000000C68AB9DBABB440B9344D706C0000000020000000440043003D00610064002C00440043003D0072007500740068002C00440043003D00610062006100720074006C00650074002C00440043003D006E0065007400000000000800000000000002";
88         ret = torture_test_syntax(torture, DSDB_SYNTAX_BINARY_DN, "wellKnownObjects", ldb_str, drs_str);
89         if (!ret) return false;
90         return torture_test_syntax(torture, DSDB_SYNTAX_BINARY_DN, "msDS-HasInstantiatedNCs", ldb_str2, drs_str2);
91 }
92
93 static bool torture_dsdb_drs_DN(struct torture_context *torture) 
94 {
95         const char *ldb_str = "<GUID=fbee08fd-6f75-4bd4-af3f-e4f063a6379e>;OU=Domain Controllers,DC=ad,DC=naomi,DC=abartlet,DC=net";
96         const char *drs_str = "A800000000000000FD08EEFB756FD44BAF3FE4F063A6379E00000000000000000000000000000000000000000000000000000000370000004F0055003D0044006F006D00610069006E00200043006F006E00740072006F006C006C006500720073002C00440043003D00610064002C00440043003D006E0061006F006D0069002C00440043003D00610062006100720074006C00650074002C00440043003D006E00650074000000";
97         return torture_test_syntax(torture, LDB_SYNTAX_DN, "lastKnownParent", ldb_str, drs_str);
98 }
99
100 static bool torture_dsdb_drs_INT32(struct torture_context *torture) 
101 {
102         const char *ldb_str = "532480";
103         const char *drs_str = "00200800";
104         return torture_test_syntax(torture, LDB_SYNTAX_INTEGER, "userAccountControl", ldb_str, drs_str);
105 }
106
107 static bool torture_dsdb_drs_INT64(struct torture_context *torture) 
108 {
109         const char *ldb_str = "129022979538281250";
110         const char *drs_str = "22E33D5FB761CA01";
111         return torture_test_syntax(torture, "1.2.840.113556.1.4.906", "pwdLastSet", ldb_str, drs_str);
112 }
113
114 static bool torture_dsdb_drs_NTTIME(struct torture_context *torture) 
115 {
116         const char *ldb_str = "20091109003446.0Z";
117         const char *drs_str = "A6F4070103000000";
118         return torture_test_syntax(torture, "1.3.6.1.4.1.1466.115.121.1.24", "whenCreated", ldb_str, drs_str);
119 }
120
121 static bool torture_dsdb_drs_BOOL(struct torture_context *torture) 
122 {
123         const char *ldb_str = "TRUE";
124         const char *drs_str = "01000000";
125         return torture_test_syntax(torture, LDB_SYNTAX_BOOLEAN, "isDeleted", ldb_str, drs_str);
126 }
127
128 static bool torture_dsdb_drs_UNICODE(struct torture_context *torture) 
129 {
130         const char *ldb_str = "primaryTelexNumber,Numéro de télex";
131         const char *drs_str = "7000720069006D00610072007900540065006C00650078004E0075006D006200650072002C004E0075006D00E90072006F0020006400650020007400E9006C0065007800";
132         return torture_test_syntax(torture, LDB_SYNTAX_DIRECTORY_STRING, "attributeDisplayNames", ldb_str, drs_str);
133 }
134
135 struct torture_suite *torture_dsdb_syntax(TALLOC_CTX *mem_ctx)
136 {
137         struct torture_suite *suite = torture_suite_create(mem_ctx, "DSDB-SYNTAX");
138
139         if (suite == NULL) {
140                 return NULL;
141         }
142
143         torture_suite_add_simple_test(suite, "DN-BINARY", torture_dsdb_drs_DN_BINARY);
144         torture_suite_add_simple_test(suite, "DN", torture_dsdb_drs_DN);
145         torture_suite_add_simple_test(suite, "INT32", torture_dsdb_drs_INT32);
146         torture_suite_add_simple_test(suite, "INT64", torture_dsdb_drs_INT64);
147         torture_suite_add_simple_test(suite, "NTTIME", torture_dsdb_drs_NTTIME);
148         torture_suite_add_simple_test(suite, "BOOL", torture_dsdb_drs_BOOL);
149         torture_suite_add_simple_test(suite, "UNICODE", torture_dsdb_drs_UNICODE);
150
151         suite->description = talloc_strdup(suite, "DSDB syntax tests");
152
153         return suite;
154 }