PyXR

c:\python24\lib \ test \ test_winreg.py



0001 # Test the windows specific win32reg module.
0002 # Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey
0003 
0004 from _winreg import *
0005 import os, sys
0006 
0007 from test.test_support import verify, have_unicode
0008 
0009 test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
0010 
0011 test_data = [
0012     ("Int Value",     45,                                      REG_DWORD),
0013     ("String Val",    "A string value",                        REG_SZ),
0014     ("StringExpand",  "The path is %path%",                    REG_EXPAND_SZ),
0015     ("Multi-string",  ["Lots", "of", "string", "values"],      REG_MULTI_SZ),
0016     ("Raw Data",      ("binary"+chr(0)+"data"),                REG_BINARY),
0017     ("Big String",    "x"*(2**14-1),                           REG_SZ),
0018     ("Big Binary",    "x"*(2**14),                             REG_BINARY),
0019 ]
0020 if have_unicode:
0021     test_data+=[
0022     (unicode("Unicode Val"),  unicode("A Unicode value"),                      REG_SZ,),
0023     ("UnicodeExpand", unicode("The path is %path%"),                   REG_EXPAND_SZ),
0024     ("Multi-unicode", [unicode("Lots"), unicode("of"), unicode("unicode"), unicode("values")], REG_MULTI_SZ),
0025     ("Multi-mixed",   [unicode("Unicode"), unicode("and"), "string", "values"],REG_MULTI_SZ),
0026     ]
0027 
0028 def WriteTestData(root_key):
0029     # Set the default value for this key.
0030     SetValue(root_key, test_key_name, REG_SZ, "Default value")
0031     key = CreateKey(root_key, test_key_name)
0032     # Create a sub-key
0033     sub_key = CreateKey(key, "sub_key")
0034     # Give the sub-key some named values
0035 
0036     for value_name, value_data, value_type in test_data:
0037         SetValueEx(sub_key, value_name, 0, value_type, value_data)
0038 
0039     # Check we wrote as many items as we thought.
0040     nkeys, nvalues, since_mod = QueryInfoKey(key)
0041     verify(nkeys==1, "Not the correct number of sub keys")
0042     verify(nvalues==1, "Not the correct number of values")
0043     nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
0044     verify(nkeys==0, "Not the correct number of sub keys")
0045     verify(nvalues==len(test_data), "Not the correct number of values")
0046     # Close this key this way...
0047     # (but before we do, copy the key as an integer - this allows
0048     # us to test that the key really gets closed).
0049     int_sub_key = int(sub_key)
0050     CloseKey(sub_key)
0051     try:
0052         QueryInfoKey(int_sub_key)
0053         raise RuntimeError, "It appears the CloseKey() function does not close the actual key!"
0054     except EnvironmentError:
0055         pass
0056     # ... and close that key that way :-)
0057     int_key = int(key)
0058     key.Close()
0059     try:
0060         QueryInfoKey(int_key)
0061         raise RuntimeError, "It appears the key.Close() function does not close the actual key!"
0062     except EnvironmentError:
0063         pass
0064 
0065 def ReadTestData(root_key):
0066     # Check we can get default value for this key.
0067     val = QueryValue(root_key, test_key_name)
0068     verify(val=="Default value", "Registry didn't give back the correct value")
0069 
0070     key = OpenKey(root_key, test_key_name)
0071     # Read the sub-keys
0072     sub_key = OpenKey(key, "sub_key")
0073     # Check I can enumerate over the values.
0074     index = 0
0075     while 1:
0076         try:
0077             data = EnumValue(sub_key, index)
0078         except EnvironmentError:
0079             break
0080         verify(data in test_data, "Didn't read back the correct test data")
0081         index = index + 1
0082     verify(index==len(test_data), "Didn't read the correct number of items")
0083     # Check I can directly access each item
0084     for value_name, value_data, value_type in test_data:
0085         read_val, read_typ = QueryValueEx(sub_key, value_name)
0086         verify(read_val==value_data and read_typ == value_type, \
0087                "Could not directly read the value" )
0088     sub_key.Close()
0089     # Enumerate our main key.
0090     read_val = EnumKey(key, 0)
0091     verify(read_val == "sub_key", "Read subkey value wrong")
0092     try:
0093         EnumKey(key, 1)
0094         verify(0, "Was able to get a second key when I only have one!")
0095     except EnvironmentError:
0096         pass
0097 
0098     key.Close()
0099 
0100 def DeleteTestData(root_key):
0101     key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
0102     sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS)
0103     # It is not necessary to delete the values before deleting
0104     # the key (although subkeys must not exist).  We delete them
0105     # manually just to prove we can :-)
0106     for value_name, value_data, value_type in test_data:
0107         DeleteValue(sub_key, value_name)
0108 
0109     nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
0110     verify(nkeys==0 and nvalues==0, "subkey not empty before delete")
0111     sub_key.Close()
0112     DeleteKey(key, "sub_key")
0113 
0114     try:
0115         # Shouldnt be able to delete it twice!
0116         DeleteKey(key, "sub_key")
0117         verify(0, "Deleting the key twice succeeded")
0118     except EnvironmentError:
0119         pass
0120     key.Close()
0121     DeleteKey(root_key, test_key_name)
0122     # Opening should now fail!
0123     try:
0124         key = OpenKey(root_key, test_key_name)
0125         verify(0, "Could open the non-existent key")
0126     except WindowsError: # Use this error name this time
0127         pass
0128 
0129 def TestAll(root_key):
0130     WriteTestData(root_key)
0131     ReadTestData(root_key)
0132     DeleteTestData(root_key)
0133 
0134 # Test on my local machine.
0135 TestAll(HKEY_CURRENT_USER)
0136 print "Local registry tests worked"
0137 try:
0138     remote_name = sys.argv[sys.argv.index("--remote")+1]
0139 except (IndexError, ValueError):
0140     remote_name = None
0141 
0142 if remote_name is not None:
0143     try:
0144         remote_key = ConnectRegistry(remote_name, HKEY_CURRENT_USER)
0145     except EnvironmentError, exc:
0146         print "Could not connect to the remote machine -", exc.strerror
0147         remote_key = None
0148     if remote_key is not None:
0149         TestAll(remote_key)
0150         print "Remote registry tests worked"
0151 else:
0152     print "Remote registry calls can be tested using",
0153     print "'test_winreg.py --remote \\\\machine_name'"
0154 

Generated by PyXR 0.9.4
SourceForge.net Logo