python-2.5.2/win32/Lib/test/test_winreg.py
changeset 0 ae805ac0140d
equal deleted inserted replaced
-1:000000000000 0:ae805ac0140d
       
     1 # Test the windows specific win32reg module.
       
     2 # Only win32reg functions not hit here: FlushKey, LoadKey and SaveKey
       
     3 
       
     4 from _winreg import *
       
     5 import os, sys
       
     6 
       
     7 from test.test_support import verify, have_unicode
       
     8 
       
     9 test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
       
    10 
       
    11 test_data = [
       
    12     ("Int Value",     45,                                      REG_DWORD),
       
    13     ("String Val",    "A string value",                        REG_SZ),
       
    14     ("StringExpand",  "The path is %path%",                    REG_EXPAND_SZ),
       
    15     ("Multi-string",  ["Lots", "of", "string", "values"],      REG_MULTI_SZ),
       
    16     ("Raw Data",      ("binary"+chr(0)+"data"),                REG_BINARY),
       
    17     ("Big String",    "x"*(2**14-1),                           REG_SZ),
       
    18     ("Big Binary",    "x"*(2**14),                             REG_BINARY),
       
    19 ]
       
    20 if have_unicode:
       
    21     test_data+=[
       
    22     (unicode("Unicode Val"),  unicode("A Unicode value"),                      REG_SZ,),
       
    23     ("UnicodeExpand", unicode("The path is %path%"),                   REG_EXPAND_SZ),
       
    24     ("Multi-unicode", [unicode("Lots"), unicode("of"), unicode("unicode"), unicode("values")], REG_MULTI_SZ),
       
    25     ("Multi-mixed",   [unicode("Unicode"), unicode("and"), "string", "values"],REG_MULTI_SZ),
       
    26     ]
       
    27 
       
    28 def WriteTestData(root_key):
       
    29     # Set the default value for this key.
       
    30     SetValue(root_key, test_key_name, REG_SZ, "Default value")
       
    31     key = CreateKey(root_key, test_key_name)
       
    32     # Create a sub-key
       
    33     sub_key = CreateKey(key, "sub_key")
       
    34     # Give the sub-key some named values
       
    35 
       
    36     for value_name, value_data, value_type in test_data:
       
    37         SetValueEx(sub_key, value_name, 0, value_type, value_data)
       
    38 
       
    39     # Check we wrote as many items as we thought.
       
    40     nkeys, nvalues, since_mod = QueryInfoKey(key)
       
    41     verify(nkeys==1, "Not the correct number of sub keys")
       
    42     verify(nvalues==1, "Not the correct number of values")
       
    43     nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
       
    44     verify(nkeys==0, "Not the correct number of sub keys")
       
    45     verify(nvalues==len(test_data), "Not the correct number of values")
       
    46     # Close this key this way...
       
    47     # (but before we do, copy the key as an integer - this allows
       
    48     # us to test that the key really gets closed).
       
    49     int_sub_key = int(sub_key)
       
    50     CloseKey(sub_key)
       
    51     try:
       
    52         QueryInfoKey(int_sub_key)
       
    53         raise RuntimeError, "It appears the CloseKey() function does not close the actual key!"
       
    54     except EnvironmentError:
       
    55         pass
       
    56     # ... and close that key that way :-)
       
    57     int_key = int(key)
       
    58     key.Close()
       
    59     try:
       
    60         QueryInfoKey(int_key)
       
    61         raise RuntimeError, "It appears the key.Close() function does not close the actual key!"
       
    62     except EnvironmentError:
       
    63         pass
       
    64 
       
    65 def ReadTestData(root_key):
       
    66     # Check we can get default value for this key.
       
    67     val = QueryValue(root_key, test_key_name)
       
    68     verify(val=="Default value", "Registry didn't give back the correct value")
       
    69 
       
    70     key = OpenKey(root_key, test_key_name)
       
    71     # Read the sub-keys
       
    72     sub_key = OpenKey(key, "sub_key")
       
    73     # Check I can enumerate over the values.
       
    74     index = 0
       
    75     while 1:
       
    76         try:
       
    77             data = EnumValue(sub_key, index)
       
    78         except EnvironmentError:
       
    79             break
       
    80         verify(data in test_data, "Didn't read back the correct test data")
       
    81         index = index + 1
       
    82     verify(index==len(test_data), "Didn't read the correct number of items")
       
    83     # Check I can directly access each item
       
    84     for value_name, value_data, value_type in test_data:
       
    85         read_val, read_typ = QueryValueEx(sub_key, value_name)
       
    86         verify(read_val==value_data and read_typ == value_type, \
       
    87                "Could not directly read the value" )
       
    88     sub_key.Close()
       
    89     # Enumerate our main key.
       
    90     read_val = EnumKey(key, 0)
       
    91     verify(read_val == "sub_key", "Read subkey value wrong")
       
    92     try:
       
    93         EnumKey(key, 1)
       
    94         verify(0, "Was able to get a second key when I only have one!")
       
    95     except EnvironmentError:
       
    96         pass
       
    97 
       
    98     key.Close()
       
    99 
       
   100 def DeleteTestData(root_key):
       
   101     key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
       
   102     sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS)
       
   103     # It is not necessary to delete the values before deleting
       
   104     # the key (although subkeys must not exist).  We delete them
       
   105     # manually just to prove we can :-)
       
   106     for value_name, value_data, value_type in test_data:
       
   107         DeleteValue(sub_key, value_name)
       
   108 
       
   109     nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
       
   110     verify(nkeys==0 and nvalues==0, "subkey not empty before delete")
       
   111     sub_key.Close()
       
   112     DeleteKey(key, "sub_key")
       
   113 
       
   114     try:
       
   115         # Shouldnt be able to delete it twice!
       
   116         DeleteKey(key, "sub_key")
       
   117         verify(0, "Deleting the key twice succeeded")
       
   118     except EnvironmentError:
       
   119         pass
       
   120     key.Close()
       
   121     DeleteKey(root_key, test_key_name)
       
   122     # Opening should now fail!
       
   123     try:
       
   124         key = OpenKey(root_key, test_key_name)
       
   125         verify(0, "Could open the non-existent key")
       
   126     except WindowsError: # Use this error name this time
       
   127         pass
       
   128 
       
   129 def TestAll(root_key):
       
   130     WriteTestData(root_key)
       
   131     ReadTestData(root_key)
       
   132     DeleteTestData(root_key)
       
   133 
       
   134 # Test on my local machine.
       
   135 TestAll(HKEY_CURRENT_USER)
       
   136 print "Local registry tests worked"
       
   137 try:
       
   138     remote_name = sys.argv[sys.argv.index("--remote")+1]
       
   139 except (IndexError, ValueError):
       
   140     remote_name = None
       
   141 
       
   142 if remote_name is not None:
       
   143     try:
       
   144         remote_key = ConnectRegistry(remote_name, HKEY_CURRENT_USER)
       
   145     except EnvironmentError, exc:
       
   146         print "Could not connect to the remote machine -", exc.strerror
       
   147         remote_key = None
       
   148     if remote_key is not None:
       
   149         TestAll(remote_key)
       
   150         print "Remote registry tests worked"
       
   151 else:
       
   152     print "Remote registry calls can be tested using",
       
   153     print "'test_winreg.py --remote \\\\machine_name'"
       
   154     # perform minimal ConnectRegistry test which just invokes it
       
   155     h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
       
   156     h.Close()