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