Fred Drake | 2658d15 | 2000-06-30 19:36:23 +0000 | [diff] [blame] | 1 | from winreg import hives, createKey, openKey, flags, deleteKey, regtypes |
| 2 | import sys, traceback |
| 3 | import time |
Mark Hammond | 152e3f0 | 2000-07-28 03:45:32 +0000 | [diff] [blame] | 4 | import array |
Fred Drake | 2658d15 | 2000-06-30 19:36:23 +0000 | [diff] [blame] | 5 | |
| 6 | def testHives(): |
| 7 | hivenames = ["HKEY_CLASSES_ROOT","HKEY_CURRENT_USER","HKEY_LOCAL_MACHINE", |
| 8 | "HKEY_USERS","HKEY_CURRENT_CONFIG"] #, # |
| 9 | for hivename in hivenames: |
| 10 | hive = hives[ hivename ] |
| 11 | print hive.name |
| 12 | assert hive.handle |
| 13 | assert hive.getSubkeys() |
| 14 | for hivename in ["HKEY_DYN_DATA", "HKEY_PERFORMANCE_DATA"]: |
| 15 | hive = hives[ hivename ] |
| 16 | print hive.name |
| 17 | assert hive.handle |
| 18 | assert not hive.getValues() |
| 19 | assert not hive.getSubkeys() |
| 20 | |
| 21 | def testNonZero(): |
| 22 | key=hives["HKLM"].openSubkey( "SOFTWARE" ) |
| 23 | assert key.openSubkey( "Microsoft" ) |
| 24 | assert key |
| 25 | key.close() |
| 26 | assert not key |
| 27 | try: |
| 28 | key.openSubkey( "Microsoft" ) |
| 29 | assert 0 |
| 30 | except EnvironmentError: |
| 31 | pass |
| 32 | |
| 33 | def testCmp(): |
| 34 | HKLM=hives["HKLM"] |
| 35 | assert (openKey("HKLM\\SOFTWARE")==\ |
| 36 | HKLM.openSubkey("SOFTWARE")) |
| 37 | assert not HKLM.openSubkey("SYSTEM")!=HKLM.openSubkey("SYSTEM") |
| 38 | assert HKLM.openSubkey("SOFTWARE")!=HKLM.openSubkey("SYSTEM") |
| 39 | assert not HKLM.openSubkey("SOFTWARE")==HKLM.openSubkey("SYSTEM") |
| 40 | assert not HKLM.openSubkey("SOFTWARE")=="spam" |
| 41 | assert ((HKLM.openSubkey("SOFTWARE")<"spam") != |
| 42 | (HKLM.openSubkey("SOFTWARE")>"spam")) |
| 43 | |
| 44 | def testClose(): |
| 45 | key=hives["HKLM"].openSubkey( "SOFTWARE" ) |
| 46 | assert key |
| 47 | key.close() |
| 48 | assert not key |
| 49 | |
| 50 | def testOpen(): |
| 51 | assert openKey( r"HKLM" ) |
| 52 | assert openKey( r"HKLM\HARDWARE" ) |
| 53 | assert openKey( r"HKLM\HARDWARE\DESCRIPTION\System" ) |
| 54 | |
| 55 | def testOpenFailure(): |
| 56 | try: |
| 57 | print openKey( r"HKCU\Software\Python\A\B\C\D" ) |
| 58 | assert 0 # |
| 59 | except EnvironmentError: |
| 60 | pass |
| 61 | |
| 62 | def testDeleteKey(): |
| 63 | createKey( r"HKCU\Software\Python\A\B\C\D" ) |
| 64 | deleteKey( r"HKCU\Software\Python\A\B\C\D" ) |
| 65 | deleteKey( r"HKCU\Software\Python\A\B\C" ) |
| 66 | deleteKey( r"HKCU\Software\Python\A\B" ) |
| 67 | assert "A" in \ |
| 68 | openKey(r"HKCU\Software\Python").getSubkeys().keys() |
| 69 | openKey( r"HKCU\Software\Python" ).deleteSubkey( r"A" ) |
| 70 | assert "A" not in \ |
| 71 | openKey(r"HKCU\Software\Python").getSubkeys().keys() |
| 72 | |
| 73 | def testDeleteValue(): |
| 74 | key=createKey( r"HKCU\Software\Python\A" ) |
| 75 | key.setValue( "abcde", "fghij" ) |
| 76 | assert key.getValueData( "abcde" )=="fghij" |
| 77 | assert "abcde" in key.getValues().keys() |
| 78 | assert "fghij" in map( lambda x:x[1], key.getValues().values() ) |
| 79 | assert "abcde" in key.getValues().keys() |
| 80 | key.deleteValue( "abcde" ) |
| 81 | assert "abcde" not in key.getValues().keys() |
| 82 | assert "fghij" not in map( lambda x:x[1], key.getValues().values() ) |
| 83 | deleteKey( r"HKCU\Software\Python\A" ) |
| 84 | |
| 85 | def testCreateKey(): |
| 86 | key=createKey( r"HKCU\Software\Python\A" ) |
| 87 | assert openKey( r"HKCU\Software\Python").getSubkeys().has_key( "A" ) |
| 88 | deleteKey( r"HKCU\Software\Python\A" ) |
| 89 | assert not openKey( r"HKCU\Software\Python").getSubkeys().\ |
| 90 | has_key( "A" ) |
| 91 | key=openKey( r"HKCU\Software\Python" ).createSubkey( "A" ) |
| 92 | |
| 93 | def testOpenKeyWithFlags(): |
| 94 | assert openKey( r"HKCU\Software\Python", |
| 95 | flags["KEY_READ"]) |
| 96 | assert openKey( r"HKCU\Software\Python", |
| 97 | flags["KEY_ALL_ACCESS"]) |
| 98 | |
| 99 | def testGetSubkeys(): |
| 100 | keys=openKey( r"HKCU\Software" ).getSubkeys() |
| 101 | assert keys |
| 102 | index=0 |
| 103 | for i in keys: |
| 104 | index=index+1 |
| 105 | assert index==len( keys ) |
| 106 | |
| 107 | def testGetValueNameDataAndType(): pass |
| 108 | |
| 109 | def testGetSubkeyNames(): |
| 110 | subkeyNames=hives["HKLM"].getSubkeyNames() |
| 111 | assert len( subkeyNames )==len(hives["HKLM"].getSubkeys()) |
| 112 | for name in subkeyNames: |
| 113 | assert type( name )==type("") |
| 114 | |
| 115 | def testGetValueNames(): |
| 116 | valNames=hives["HKLM"].getValueNames() |
| 117 | assert len( valNames )==len(hives["HKLM"].getValues()) |
| 118 | for name in valNames: |
| 119 | assert type( name )==type("") |
| 120 | |
| 121 | def testRepr(): |
| 122 | assert repr(hives["HKCU"])==str(hives["HKCU"]) |
| 123 | |
| 124 | def testSetStringValue(): |
| 125 | hives["HKCU"].setValue( "Blah", "abc" ) |
| 126 | assert hives["HKCU"].getValueData( "Blah" )=="abc" |
| 127 | assert hives["HKCU"].getValues().has_key( "Blah" ) |
| 128 | del hives["HKCU"].getValues()[ "Blah" ] |
| 129 | assert not hives["HKCU"].getValues().has_key( "Blah" ) |
| 130 | |
Mark Hammond | 152e3f0 | 2000-07-28 03:45:32 +0000 | [diff] [blame] | 131 | # Ensure Unicode works as we expect |
| 132 | hives["HKCU"].setValue( u"Blah", u"abc" ) |
| 133 | assert hives["HKCU"].getValueData( "Blah" )=="abc" |
| 134 | assert hives["HKCU"].getValues().has_key( u"Blah" ) |
| 135 | del hives["HKCU"].getValues()[ u"Blah" ] |
| 136 | assert not hives["HKCU"].getValues().has_key( u"Blah" ) |
| 137 | |
Fred Drake | 2658d15 | 2000-06-30 19:36:23 +0000 | [diff] [blame] | 138 | def testDeleteValue(): |
| 139 | hives["HKCU"].setValue( "Blah", "abc" ) |
| 140 | assert hives["HKCU"].getValues().has_key( "Blah" ) |
| 141 | del hives["HKCU"].getValues()[ "Blah" ] |
| 142 | assert not hives["HKCU"].getValues().has_key( "Blah" ) |
| 143 | hives["HKCU"].setValue( "Blah", "abc" ) |
| 144 | hives["HKCU"].deleteValue( "Blah" ) |
| 145 | assert not hives["HKCU"].getValues().has_key( "Blah" ) |
| 146 | |
| 147 | def testKeyDict_ClearKeys(): |
| 148 | createKey( "HKLM\\Software\\a\\b\\c\\d\\e" ) |
| 149 | createKey( "HKLM\\Software\\a\\b\\c\\d\\f" ) |
| 150 | createKey( "HKLM\\Software\\a\\b\\c\\d\\g" ) |
| 151 | createKey( "HKLM\\Software\\a\\b\\c\\d\\h" ) |
| 152 | createKey( "HKLM\\Software\\a\\b\\c\\d\\i" ) |
| 153 | key=openKey( "HKLM\\Software\\a\\b\\c\\d" ) |
| 154 | assert key.getSubkeys() |
| 155 | key.getSubkeys().clear() |
| 156 | assert not key.getSubkeys() |
| 157 | assert not openKey( "HKLM\\Software\\a\\b\\c\\d").getSubkeys() |
| 158 | deleteKey( "HKLM\\Software\\a\\b\\c\\d" ) |
| 159 | deleteKey( "HKLM\\Software\\a\\b\\c" ) |
| 160 | deleteKey( "HKLM\\Software\\a\\b" ) |
| 161 | deleteKey( "HKLM\\Software\\a" ) |
| 162 | |
| 163 | def testUnicodeKeyName(): pass |
| 164 | |
| 165 | def testUnicodeValueName(): pass |
| 166 | |
| 167 | def testGetValueDataFromEnum(): pass |
| 168 | |
| 169 | def testGetValueDataFromName(): pass |
| 170 | |
| 171 | def testGetBinaryData(): pass |
| 172 | |
| 173 | |
| 174 | def testSetIntValue(): |
| 175 | key=createKey( "HKLM\\Software\\a\\b") |
| 176 | key.setValue( "abcd", 5 ) |
| 177 | assert key.getValueData( "abcd" )==5 |
| 178 | assert key.getValues()[ "abcd" ][1]==5 |
| 179 | key.deleteValue( "abcd" ) |
| 180 | key.getValues()["abcd"]=5 |
| 181 | assert key.getValues()[ "abcd" ][1]==5 |
| 182 | key.deleteValue( "abcd" ) |
| 183 | key.getValues()["abcd"]=(5,regtypes["REG_DWORD"]) |
| 184 | assert key.getValues()[ "abcd" ][1]==5 |
| 185 | key.deleteValue( "abcd" ) |
Mark Hammond | 152e3f0 | 2000-07-28 03:45:32 +0000 | [diff] [blame] | 186 | deleteKey( "HKLM\\Software\\a\\b") |
| 187 | deleteKey( "HKLM\\Software\\a") |
Fred Drake | 2658d15 | 2000-06-30 19:36:23 +0000 | [diff] [blame] | 188 | |
| 189 | def testSetBinaryValue(): |
| 190 | key=createKey( "HKLM\\Software\\a\\b") |
| 191 | key.setValue( "abcd", array.array( 'c', "PPPPPPPPPPPPPPP") ) |
| 192 | key.setValue( "abcde", array.array( 'c', "PPPPPPPPPPPPPPP"), |
| 193 | regtypes["REG_BINARY"] ) |
Mark Hammond | 152e3f0 | 2000-07-28 03:45:32 +0000 | [diff] [blame] | 194 | assert key.getValues()["abcd"][1]==key.getValues()["abcde"][1] |
| 195 | key = None # Remove our reference. |
| 196 | deleteKey( "HKLM\\Software\\a\\b") |
| 197 | deleteKey( "HKLM\\Software\\a") |
Fred Drake | 2658d15 | 2000-06-30 19:36:23 +0000 | [diff] [blame] | 198 | |
| 199 | def testSetNone(): pass |
| 200 | |
| 201 | def testSetString(): pass |
| 202 | |
| 203 | def testSetExpandString(): pass |
| 204 | |
| 205 | def testSetBinaryData(): pass |
| 206 | |
| 207 | def testSetDword(): pass |
| 208 | |
| 209 | def testSetDwordBigEndian(): pass |
| 210 | |
| 211 | def testSetLink(): pass |
| 212 | |
| 213 | def testSetMultiSz(): pass |
| 214 | |
| 215 | def testSetResourceList(): pass |
| 216 | |
| 217 | def testSetFullResourceDescription(): pass |
| 218 | |
| 219 | def testSetResourceRequirementsList(): pass |
| 220 | |
| 221 | |
| 222 | |
| 223 | def testFlush(): pass |
| 224 | |
| 225 | def testSave(): pass |
| 226 | |
| 227 | def testLoad(): pass |
| 228 | |
| 229 | |
| 230 | |
| 231 | |
| 232 | |
| 233 | def testNoneType(): pass |
| 234 | |
| 235 | def testStringType(): pass |
| 236 | |
| 237 | def testExpandStringType(): pass |
| 238 | |
| 239 | def testDWordType(): pass |
| 240 | |
| 241 | def testDWordBigEndianType(): pass |
| 242 | |
| 243 | def testLinkType(): pass |
| 244 | |
| 245 | def testMultiStringType(): pass |
| 246 | |
| 247 | def testResourceLinkType(): pass |
| 248 | |
| 249 | def testResourceDescriptionType(): pass |
| 250 | |
| 251 | def testResourceRequirementsListType(): pass |
| 252 | |
| 253 | |
| 254 | def getSubkeysDict(): pass |
| 255 | |
| 256 | def testKeyDict_Get(): pass |
| 257 | |
| 258 | def testKeyDict_HasKey(): pass |
| 259 | |
| 260 | def testKeyDict_Keys(): pass |
| 261 | |
| 262 | def testKeyDict_Values(): pass |
| 263 | |
| 264 | def testKeyDict_Items(): pass |
| 265 | |
| 266 | def testKeyDict_Length(): pass |
| 267 | |
| 268 | def testKeyDict_Map(): pass |
| 269 | |
| 270 | def testKeyDict_GetItem(): pass |
| 271 | |
| 272 | def testKeyDict_DelItem(): pass |
| 273 | |
| 274 | def testRemote(): pass |
| 275 | |
| 276 | def testShortcuts(): pass |
| 277 | |
| 278 | |
| 279 | |
| 280 | def getValues(): pass |
| 281 | |
| 282 | def testValueDict_ClearKeys(): pass |
| 283 | |
| 284 | def testValueDict_Get(): pass |
| 285 | |
| 286 | def testValueDict_HasKey(): pass |
| 287 | |
| 288 | def testValueDict_Keys(): pass |
| 289 | |
| 290 | def testValueDict_Values(): pass |
| 291 | |
| 292 | def testValueDict_Items(): pass |
| 293 | |
| 294 | def testValueDict_Length(): pass |
| 295 | |
| 296 | def testValueDict_Map(): pass |
| 297 | |
| 298 | def testValueDict_GetItem(): pass |
| 299 | |
| 300 | def testValueDict_DelItem(): pass |
| 301 | |
| 302 | for name in globals().keys(): |
| 303 | if name[0:4]=="test": |
| 304 | func=globals()[ name ] |
| 305 | try: |
| 306 | func() |
| 307 | print "Test Passed: %s" % name |
| 308 | except Exception, e: |
| 309 | print "Test Failed: %s" % name |
| 310 | traceback.print_exc() |
| 311 | |
| 312 | j=[0]*len( regtypes ) |
| 313 | |
| 314 | k=0 |
| 315 | |
| 316 | def test1( basekey ): |
| 317 | global k |
| 318 | for (name, data, type) in basekey.getValues(): |
| 319 | j[type.intval]=j[type.intval]+1 |
| 320 | if j[type.intval]==1: |
| 321 | pass |
| 322 | #print "[[%s]] %s [%s] = %s "% (type.msname, name, basekey, value) |
| 323 | keys=basekey.getSubkeys() |
| 324 | index=0 |
| 325 | k=k+1 |
| 326 | if(k%2500)==0: |
| 327 | dump() |
| 328 | while 1: |
| 329 | try: |
| 330 | test1( keys[index] ) |
| 331 | index=index+1 |
| 332 | except IndexError: |
| 333 | break |
| 334 | print "Done", basekey, index |
| 335 | except (WindowsError, EnvironmentError): |
| 336 | index=index+1 |
| 337 | dumpfile.write( "Skipping %s %s"% (basekey, index)) |
| 338 | |
| 339 | def dump(dumpfile): |
| 340 | for i in range( len( j )): |
| 341 | dumpfile.write( "%s %s\n" %( i,j[i] )) |
| 342 | |
| 343 | def testRecursive(): |
| 344 | dumpfile=open( "foo.txt", "w" ) |
| 345 | start=time.time() |
| 346 | for hive in hives.values(): |
| 347 | dumpfile.write( "Hive: %s\n" % hive ) |
| 348 | test1( hive ) |
| 349 | dump(dumpfile) |
| 350 | print time.time()-start |