summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_winreg2.py
blob: 28c998e096059d1a5c51e4f4257d38b3e534ffbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
from winreg import hives, createKey, openKey, flags, deleteKey, regtypes
import sys, traceback
import time

def testHives():
    hivenames = ["HKEY_CLASSES_ROOT","HKEY_CURRENT_USER","HKEY_LOCAL_MACHINE",
                "HKEY_USERS","HKEY_CURRENT_CONFIG"] #, # 
    for hivename in hivenames:
        hive = hives[ hivename ]
        print hive.name
        assert hive.handle
        assert hive.getSubkeys()
    for hivename in ["HKEY_DYN_DATA", "HKEY_PERFORMANCE_DATA"]:
        hive =  hives[ hivename ]
        print hive.name
        assert hive.handle
        assert not hive.getValues()
        assert not hive.getSubkeys()

def testNonZero():
    key=hives["HKLM"].openSubkey( "SOFTWARE" )
    assert key.openSubkey( "Microsoft" )
    assert key
    key.close()
    assert not key
    try:
        key.openSubkey( "Microsoft" )
        assert 0
    except EnvironmentError:
        pass

def testCmp():
    HKLM=hives["HKLM"]
    assert (openKey("HKLM\\SOFTWARE")==\
                        HKLM.openSubkey("SOFTWARE"))
    assert not HKLM.openSubkey("SYSTEM")!=HKLM.openSubkey("SYSTEM")
    assert HKLM.openSubkey("SOFTWARE")!=HKLM.openSubkey("SYSTEM")
    assert not HKLM.openSubkey("SOFTWARE")==HKLM.openSubkey("SYSTEM")
    assert not HKLM.openSubkey("SOFTWARE")=="spam"
    assert ((HKLM.openSubkey("SOFTWARE")<"spam") !=
                        (HKLM.openSubkey("SOFTWARE")>"spam"))

def testClose(): 
    key=hives["HKLM"].openSubkey( "SOFTWARE" )
    assert key
    key.close()
    assert not key

def testOpen():
    assert openKey( r"HKLM" )
    assert openKey( r"HKLM\HARDWARE" )
    assert openKey( r"HKLM\HARDWARE\DESCRIPTION\System" )

def testOpenFailure():
    try:
        print openKey( r"HKCU\Software\Python\A\B\C\D" )
        assert 0 #
    except EnvironmentError:
        pass

def testDeleteKey():
    createKey( r"HKCU\Software\Python\A\B\C\D" )
    deleteKey( r"HKCU\Software\Python\A\B\C\D" )
    deleteKey( r"HKCU\Software\Python\A\B\C" )
    deleteKey( r"HKCU\Software\Python\A\B" )
    assert "A" in \
            openKey(r"HKCU\Software\Python").getSubkeys().keys()
    openKey( r"HKCU\Software\Python" ).deleteSubkey( r"A" )
    assert "A" not in \
            openKey(r"HKCU\Software\Python").getSubkeys().keys()

def testDeleteValue():
    key=createKey( r"HKCU\Software\Python\A" )
    key.setValue( "abcde", "fghij" )
    assert key.getValueData( "abcde" )=="fghij"
    assert "abcde" in key.getValues().keys()
    assert "fghij" in map( lambda x:x[1], key.getValues().values() )
    assert "abcde" in key.getValues().keys()
    key.deleteValue( "abcde" )
    assert "abcde" not in key.getValues().keys()
    assert "fghij" not in map( lambda x:x[1], key.getValues().values() )
    deleteKey( r"HKCU\Software\Python\A" )

def testCreateKey():
    key=createKey( r"HKCU\Software\Python\A" )
    assert openKey( r"HKCU\Software\Python").getSubkeys().has_key( "A" )
    deleteKey( r"HKCU\Software\Python\A" )
    assert not openKey( r"HKCU\Software\Python").getSubkeys().\
                        has_key( "A" )
    key=openKey( r"HKCU\Software\Python" ).createSubkey( "A" )

def testOpenKeyWithFlags():
    assert openKey( r"HKCU\Software\Python", 
                                flags["KEY_READ"])
    assert openKey( r"HKCU\Software\Python", 
                                flags["KEY_ALL_ACCESS"])

def testGetSubkeys():
    keys=openKey( r"HKCU\Software" ).getSubkeys()
    assert keys
    index=0
    for i in keys:
        index=index+1
    assert index==len( keys )

def testGetValueNameDataAndType(): pass

def testGetSubkeyNames():
    subkeyNames=hives["HKLM"].getSubkeyNames()
    assert len( subkeyNames )==len(hives["HKLM"].getSubkeys())
    for name in subkeyNames:
        assert type( name )==type("")

def testGetValueNames(): 
    valNames=hives["HKLM"].getValueNames()
    assert len( valNames )==len(hives["HKLM"].getValues())
    for name in valNames:
        assert type( name )==type("")

def testRepr():
    assert repr(hives["HKCU"])==str(hives["HKCU"])

def testSetStringValue():
    hives["HKCU"].setValue( "Blah", "abc" )
    assert hives["HKCU"].getValueData( "Blah" )=="abc"
    assert hives["HKCU"].getValues().has_key( "Blah" )
    del hives["HKCU"].getValues()[ "Blah" ]
    assert not hives["HKCU"].getValues().has_key( "Blah" )

def testDeleteValue():
    hives["HKCU"].setValue( "Blah", "abc" )
    assert hives["HKCU"].getValues().has_key( "Blah" )
    del hives["HKCU"].getValues()[ "Blah" ]
    assert not hives["HKCU"].getValues().has_key( "Blah" )
    hives["HKCU"].setValue( "Blah", "abc" )
    hives["HKCU"].deleteValue( "Blah" )
    assert not hives["HKCU"].getValues().has_key( "Blah" )

def testKeyDict_ClearKeys():
    createKey( "HKLM\\Software\\a\\b\\c\\d\\e" )
    createKey( "HKLM\\Software\\a\\b\\c\\d\\f" )
    createKey( "HKLM\\Software\\a\\b\\c\\d\\g" )
    createKey( "HKLM\\Software\\a\\b\\c\\d\\h" )
    createKey( "HKLM\\Software\\a\\b\\c\\d\\i" )
    key=openKey( "HKLM\\Software\\a\\b\\c\\d" )
    assert key.getSubkeys()
    key.getSubkeys().clear()
    assert not key.getSubkeys()
    assert not openKey( "HKLM\\Software\\a\\b\\c\\d").getSubkeys()
    deleteKey( "HKLM\\Software\\a\\b\\c\\d" )
    deleteKey( "HKLM\\Software\\a\\b\\c" )
    deleteKey( "HKLM\\Software\\a\\b" )
    deleteKey( "HKLM\\Software\\a" )

def testUnicodeKeyName(): pass

def testUnicodeValueName(): pass

def testGetValueDataFromEnum(): pass

def testGetValueDataFromName(): pass

def testGetBinaryData(): pass


def testSetIntValue():
    key=createKey( "HKLM\\Software\\a\\b")
    key.setValue( "abcd", 5 )
    assert key.getValueData( "abcd" )==5
    assert key.getValues()[ "abcd" ][1]==5
    key.deleteValue( "abcd" )
    key.getValues()["abcd"]=5
    assert key.getValues()[ "abcd" ][1]==5
    key.deleteValue( "abcd" )
    key.getValues()["abcd"]=(5,regtypes["REG_DWORD"])
    assert key.getValues()[ "abcd" ][1]==5
    key.deleteValue( "abcd" )
    key.deleteKey( "HKLM\\Software\\a\\b")
    key.deleteKey( "HKLM\\Software\\a")

def testSetBinaryValue(): 
    key=createKey( "HKLM\\Software\\a\\b")
    key.setValue( "abcd", array.array( 'c', "PPPPPPPPPPPPPPP") )
    key.setValue( "abcde", array.array( 'c', "PPPPPPPPPPPPPPP"), 
                       regtypes["REG_BINARY"] )
    assert key.getValues()["abcd"]==key.getValues()["abcde"]
    key.deleteKey( "HKLM\\Software\\a\\b")
    key.deleteKey( "HKLM\\Software\\a")

def testSetNone(): pass

def testSetString(): pass

def testSetExpandString(): pass

def testSetBinaryData(): pass

def testSetDword(): pass

def testSetDwordBigEndian(): pass

def testSetLink(): pass

def testSetMultiSz(): pass

def testSetResourceList(): pass

def testSetFullResourceDescription(): pass

def testSetResourceRequirementsList(): pass



def testFlush(): pass

def testSave(): pass

def testLoad(): pass





def testNoneType(): pass

def testStringType(): pass

def testExpandStringType(): pass

def testDWordType(): pass

def testDWordBigEndianType(): pass

def testLinkType(): pass

def testMultiStringType(): pass

def testResourceLinkType(): pass

def testResourceDescriptionType(): pass

def testResourceRequirementsListType(): pass


def getSubkeysDict(): pass

def testKeyDict_Get(): pass

def testKeyDict_HasKey(): pass

def testKeyDict_Keys(): pass

def testKeyDict_Values(): pass

def testKeyDict_Items(): pass

def testKeyDict_Length(): pass

def testKeyDict_Map(): pass

def testKeyDict_GetItem(): pass

def testKeyDict_DelItem(): pass

def testRemote(): pass

def testShortcuts(): pass



def getValues(): pass

def testValueDict_ClearKeys(): pass

def testValueDict_Get(): pass

def testValueDict_HasKey(): pass

def testValueDict_Keys(): pass

def testValueDict_Values(): pass

def testValueDict_Items(): pass

def testValueDict_Length(): pass

def testValueDict_Map(): pass

def testValueDict_GetItem(): pass

def testValueDict_DelItem(): pass

for name in globals().keys():
    if name[0:4]=="test":
        func=globals()[ name ]
        try:
            func()
            print "Test Passed: %s" % name
        except Exception, e:
            print "Test Failed: %s" % name
            traceback.print_exc()

j=[0]*len( regtypes )

k=0

def test1( basekey ):
    global k
    for (name, data, type) in basekey.getValues():
        j[type.intval]=j[type.intval]+1
        if j[type.intval]==1:
            pass
            #print "[[%s]] %s [%s] = %s "% (type.msname, name, basekey, value)
    keys=basekey.getSubkeys()
    index=0
    k=k+1
    if(k%2500)==0:
        dump()
    while 1:
        try:
            test1( keys[index] )
            index=index+1
        except IndexError:
            break
            print "Done", basekey, index
        except (WindowsError, EnvironmentError):
            index=index+1
            dumpfile.write( "Skipping %s %s"% (basekey, index))

def dump(dumpfile):
    for i in range( len( j )):
        dumpfile.write( "%s %s\n" %( i,j[i] ))

def testRecursive():
    dumpfile=open( "foo.txt", "w" )
    start=time.time()
    for hive in hives.values():
        dumpfile.write( "Hive: %s\n" % hive )
        test1( hive )
        dump(dumpfile)
    print time.time()-start