1 # Test the windows specific win32reg module.
2 # Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey
7 test_key_name
= "SOFTWARE\\Python Registry Test Key - Delete Me"
10 ("Int Value", 45, REG_DWORD
),
11 ("String Val", "A string value", REG_SZ
,),
12 (u
"Unicode Val", u
"A Unicode value", REG_SZ
,),
13 ("StringExpand", "The path is %path%", REG_EXPAND_SZ
),
14 ("UnicodeExpand", u
"The path is %path%", REG_EXPAND_SZ
),
15 ("Multi-string", ["Lots", "of", "string", "values"], REG_MULTI_SZ
),
16 ("Multi-unicode", [u
"Lots", u
"of", u
"unicode", u
"values"], REG_MULTI_SZ
),
17 ("Multi-mixed", [u
"Unicode", u
"and", "string", "values"],REG_MULTI_SZ
),
18 ("Raw Data", ("binary"+chr(0)+"data"), REG_BINARY
),
21 def WriteTestData(root_key
):
22 # Set the default value for this key.
23 SetValue(root_key
, test_key_name
, REG_SZ
, "Default value")
24 key
= CreateKey(root_key
, test_key_name
)
26 sub_key
= CreateKey(key
, "sub_key")
27 # Give the sub-key some named values
29 for value_name
, value_data
, value_type
in test_data
:
30 SetValueEx(sub_key
, value_name
, 0, value_type
, value_data
)
32 # Check we wrote as many items as we thought.
33 nkeys
, nvalues
, since_mod
= QueryInfoKey(key
)
34 assert nkeys
==1, "Not the correct number of sub keys"
35 assert nvalues
==1, "Not the correct number of values"
36 nkeys
, nvalues
, since_mod
= QueryInfoKey(sub_key
)
37 assert nkeys
==0, "Not the correct number of sub keys"
38 assert nvalues
==len(test_data
), "Not the correct number of values"
39 # Close this key this way...
40 # (but before we do, copy the key as an integer - this allows
41 # us to test that the key really gets closed).
42 int_sub_key
= int(sub_key
)
45 QueryInfoKey(int_sub_key
)
46 raise RuntimeError, "It appears the CloseKey() function does not close the actual key!"
47 except EnvironmentError:
49 # ... and close that key that way :-)
54 raise RuntimeError, "It appears the key.Close() function does not close the actual key!"
55 except EnvironmentError:
58 def ReadTestData(root_key
):
59 # Check we can get default value for this key.
60 val
= QueryValue(root_key
, test_key_name
)
61 assert val
=="Default value", "Registry didn't give back the correct value"
63 key
= OpenKey(root_key
, test_key_name
)
65 sub_key
= OpenKey(key
, "sub_key")
66 # Check I can enumerate over the values.
70 data
= EnumValue(sub_key
, index
)
71 except EnvironmentError:
73 assert data
in test_data
, "Didn't read back the correct test data"
75 assert index
==len(test_data
), "Didn't read the correct number of items"
76 # Check I can directly access each item
77 for value_name
, value_data
, value_type
in test_data
:
78 read_val
, read_typ
= QueryValueEx(sub_key
, value_name
)
79 assert read_val
==value_data
and read_typ
== value_type
, \
80 "Could not directly read the value"
82 # Enumerate our main key.
83 read_val
= EnumKey(key
, 0)
84 assert read_val
== "sub_key", "Read subkey value wrong"
87 assert 0, "Was able to get a second key when I only have one!"
88 except EnvironmentError:
93 def DeleteTestData(root_key
):
94 key
= OpenKey(root_key
, test_key_name
, 0, KEY_ALL_ACCESS
)
95 sub_key
= OpenKey(key
, "sub_key", 0, KEY_ALL_ACCESS
)
96 # It is not necessary to delete the values before deleting
97 # the key (although subkeys must not exist). We delete them
98 # manually just to prove we can :-)
99 for value_name
, value_data
, value_type
in test_data
:
100 DeleteValue(sub_key
, value_name
)
102 nkeys
, nvalues
, since_mod
= QueryInfoKey(sub_key
)
103 assert nkeys
==0 and nvalues
==0, "subkey not empty before delete"
105 DeleteKey(key
, "sub_key")
108 # Shouldnt be able to delete it twice!
109 DeleteKey(key
, "sub_key")
110 assert 0, "Deleting the key twice succeeded"
111 except EnvironmentError:
114 DeleteKey(root_key
, test_key_name
)
115 # Opening should now fail!
117 key
= OpenKey(root_key
, test_key_name
)
118 assert 0, "Could open the non-existent key"
119 except WindowsError: # Use this error name this time
122 def TestAll(root_key
):
123 WriteTestData(root_key
)
124 ReadTestData(root_key
)
125 DeleteTestData(root_key
)
127 # Test on my local machine.
128 TestAll(HKEY_CURRENT_USER
)
129 print "Local registry tests worked"
131 remote_name
= sys
.argv
[sys
.argv
.index("--remote")+1]
132 except (IndexError, ValueError):
135 if remote_name
is not None:
137 remote_key
= ConnectRegistry(remote_name
, HKEY_CURRENT_USER
)
138 except EnvironmentError, exc
:
139 print "Could not connect to the remote machine -", exc
.strerror
141 if remote_key
is not None:
143 print "Remote registry tests worked"
145 print "Remote registry calls can be tested using",
146 print "'test_winreg.py --remote \\\\machine_name'"