r24683: Add two more tests.
[ab/samba.git/.git] / source4 / lib / registry / tests / generic.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    local testing of registry library
5
6    Copyright (C) Jelmer Vernooij 2005-2007
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/registry/registry.h"
24 #include "lib/cmdline/popt_common.h"
25 #include "torture/torture.h"
26 #include "librpc/gen_ndr/winreg.h"
27
28 struct torture_suite *torture_registry_hive(TALLOC_CTX *mem_ctx);
29 struct torture_suite *torture_registry_registry(TALLOC_CTX *mem_ctx);
30 struct torture_suite *torture_registry_diff(TALLOC_CTX *mem_ctx);
31
32 static bool test_str_regtype(struct torture_context *ctx)
33 {
34         torture_assert_str_equal(ctx, str_regtype(1), "REG_SZ", "REG_SZ failed");
35         torture_assert_str_equal(ctx, str_regtype(4), "REG_DWORD", "REG_DWORD failed");
36
37         return true;
38 }
39
40
41 static bool test_reg_val_data_string_dword(struct torture_context *ctx)
42 {
43         uint32_t d = 0x20;
44         DATA_BLOB db = { (uint8_t *)&d, sizeof(d) };
45         torture_assert_str_equal(ctx, "0x20", 
46                                         reg_val_data_string(ctx, REG_DWORD, db), "dword failed");
47         return true;
48 }
49
50 static bool test_reg_val_data_string_sz(struct torture_context *ctx)
51 {
52         DATA_BLOB db;
53         db.length = convert_string_talloc(ctx, CH_UNIX, CH_UTF16, "bla", 3, (void **)&db.data);
54         torture_assert_str_equal(ctx, "bla", reg_val_data_string(ctx, REG_SZ, db), "sz failed");
55         db.length = 4;
56         torture_assert_str_equal(ctx, "bl", reg_val_data_string(ctx, REG_SZ, db), "sz failed");
57         return true;
58 }
59
60 static bool test_reg_val_data_string_binary(struct torture_context *ctx)
61 {
62         uint8_t x[] = { 0x1, 0x2, 0x3, 0x4 };
63         DATA_BLOB db = { x, 4 };
64         torture_assert_str_equal(ctx, "01020304", 
65                                                          reg_val_data_string(ctx, REG_BINARY, db), 
66                                                          "binary failed");
67         return true;
68 }
69
70
71 static bool test_reg_val_data_string_empty(struct torture_context *ctx)
72 {
73         DATA_BLOB db = { NULL, 0 };
74         torture_assert_str_equal(ctx, "", 
75                                         reg_val_data_string(ctx, REG_BINARY, db), "empty failed");
76         return true;
77 }
78
79 static bool test_reg_val_description(struct torture_context *ctx)
80 {
81         DATA_BLOB data;
82         data.length = convert_string_talloc(ctx, CH_UNIX, CH_UTF16, 
83                                                                                 "stationary traveller", 
84                                                                             strlen("stationary traveller"), 
85                                                                                 (void **)&data.data);
86         torture_assert_str_equal(ctx, "camel = REG_SZ : stationary traveller", 
87                                                          reg_val_description(ctx, "camel", REG_SZ, data),
88                                                          "reg_val_description failed");
89         return true;
90 }
91
92
93 static bool test_reg_val_description_nullname(struct torture_context *ctx)
94 {
95         DATA_BLOB data;
96         data.length = convert_string_talloc(ctx, CH_UNIX, CH_UTF16, "west berlin", 
97                                                                 strlen("west berlin"), (void **)&data.data);
98         torture_assert_str_equal(ctx, "<No Name> = REG_SZ : west berlin", 
99                                                          reg_val_description(ctx, NULL, REG_SZ, data),
100                                                          "description with null name failed");
101         return true;
102 }
103
104
105
106 struct torture_suite *torture_registry(TALLOC_CTX *mem_ctx) 
107 {
108         struct torture_suite *suite = torture_suite_create(mem_ctx, 
109                                                                                                            "REGISTRY");
110         torture_suite_add_simple_test(suite, "str_regtype", test_str_regtype);
111         torture_suite_add_simple_test(suite, "reg_val_data_string dword", 
112                                                                   test_reg_val_data_string_dword);
113         torture_suite_add_simple_test(suite, "reg_val_data_string sz", 
114                                                                   test_reg_val_data_string_sz);
115         torture_suite_add_simple_test(suite, "reg_val_data_string binary", 
116                                                                   test_reg_val_data_string_binary);
117         torture_suite_add_simple_test(suite, "reg_val_data_string empty", 
118                                                                   test_reg_val_data_string_empty);
119         torture_suite_add_simple_test(suite, "reg_val_description", 
120                                                                   test_reg_val_description);
121         torture_suite_add_simple_test(suite, "reg_val_description null", 
122                                                                   test_reg_val_description_nullname);
123
124         torture_suite_add_suite(suite, torture_registry_hive(mem_ctx));
125         torture_suite_add_suite(suite, torture_registry_registry(mem_ctx));
126         torture_suite_add_suite(suite, torture_registry_diff(mem_ctx));
127
128         return suite;
129 }