mirror of
https://github.com/python/cpython.git
synced 2025-12-04 16:43:27 +00:00
Patch #1006: port test_winreg to unittest.
This commit is contained in:
parent
94bda3a586
commit
3376a9a711
2 changed files with 152 additions and 136 deletions
|
|
@ -1,3 +0,0 @@
|
||||||
test_winreg
|
|
||||||
Local registry tests worked
|
|
||||||
Remote registry calls can be tested using 'test_winreg.py --remote \\machine_name'
|
|
||||||
|
|
@ -3,8 +3,9 @@
|
||||||
|
|
||||||
from _winreg import *
|
from _winreg import *
|
||||||
import os, sys
|
import os, sys
|
||||||
|
import unittest
|
||||||
|
|
||||||
from test.test_support import verify, have_unicode
|
from test import test_support
|
||||||
|
|
||||||
test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
|
test_key_name = "SOFTWARE\\Python Registry Test Key - Delete Me"
|
||||||
|
|
||||||
|
|
@ -17,15 +18,21 @@ test_data = [
|
||||||
("Big String", "x"*(2**14-1), REG_SZ),
|
("Big String", "x"*(2**14-1), REG_SZ),
|
||||||
("Big Binary", "x"*(2**14), REG_BINARY),
|
("Big Binary", "x"*(2**14), REG_BINARY),
|
||||||
]
|
]
|
||||||
if have_unicode:
|
|
||||||
test_data+=[
|
if test_support.have_unicode:
|
||||||
|
test_data += [
|
||||||
(unicode("Unicode Val"), unicode("A Unicode value"), REG_SZ,),
|
(unicode("Unicode Val"), unicode("A Unicode value"), REG_SZ,),
|
||||||
("UnicodeExpand", unicode("The path is %path%"), REG_EXPAND_SZ),
|
("UnicodeExpand", unicode("The path is %path%"), REG_EXPAND_SZ),
|
||||||
("Multi-unicode", [unicode("Lots"), unicode("of"), unicode("unicode"), unicode("values")], REG_MULTI_SZ),
|
("Multi-unicode", [unicode("Lots"), unicode("of"), unicode("unicode"),
|
||||||
("Multi-mixed", [unicode("Unicode"), unicode("and"), "string", "values"],REG_MULTI_SZ),
|
unicode("values")], REG_MULTI_SZ),
|
||||||
|
("Multi-mixed", [unicode("Unicode"), unicode("and"), "string",
|
||||||
|
"values"], REG_MULTI_SZ),
|
||||||
]
|
]
|
||||||
|
|
||||||
def WriteTestData(root_key):
|
class WinregTests(unittest.TestCase):
|
||||||
|
remote_name = None
|
||||||
|
|
||||||
|
def WriteTestData(self, root_key):
|
||||||
# Set the default value for this key.
|
# Set the default value for this key.
|
||||||
SetValue(root_key, test_key_name, REG_SZ, "Default value")
|
SetValue(root_key, test_key_name, REG_SZ, "Default value")
|
||||||
key = CreateKey(root_key, test_key_name)
|
key = CreateKey(root_key, test_key_name)
|
||||||
|
|
@ -38,11 +45,12 @@ def WriteTestData(root_key):
|
||||||
|
|
||||||
# Check we wrote as many items as we thought.
|
# Check we wrote as many items as we thought.
|
||||||
nkeys, nvalues, since_mod = QueryInfoKey(key)
|
nkeys, nvalues, since_mod = QueryInfoKey(key)
|
||||||
verify(nkeys==1, "Not the correct number of sub keys")
|
self.assertEquals(nkeys, 1, "Not the correct number of sub keys")
|
||||||
verify(nvalues==1, "Not the correct number of values")
|
self.assertEquals(nvalues, 1, "Not the correct number of values")
|
||||||
nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
|
nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
|
||||||
verify(nkeys==0, "Not the correct number of sub keys")
|
self.assertEquals(nkeys, 0, "Not the correct number of sub keys")
|
||||||
verify(nvalues==len(test_data), "Not the correct number of values")
|
self.assertEquals(nvalues, len(test_data),
|
||||||
|
"Not the correct number of values")
|
||||||
# Close this key this way...
|
# Close this key this way...
|
||||||
# (but before we do, copy the key as an integer - this allows
|
# (but before we do, copy the key as an integer - this allows
|
||||||
# us to test that the key really gets closed).
|
# us to test that the key really gets closed).
|
||||||
|
|
@ -50,7 +58,8 @@ def WriteTestData(root_key):
|
||||||
CloseKey(sub_key)
|
CloseKey(sub_key)
|
||||||
try:
|
try:
|
||||||
QueryInfoKey(int_sub_key)
|
QueryInfoKey(int_sub_key)
|
||||||
raise RuntimeError, "It appears the CloseKey() function does not close the actual key!"
|
self.fail("It appears the CloseKey() function does "
|
||||||
|
"not close the actual key!")
|
||||||
except EnvironmentError:
|
except EnvironmentError:
|
||||||
pass
|
pass
|
||||||
# ... and close that key that way :-)
|
# ... and close that key that way :-)
|
||||||
|
|
@ -58,14 +67,16 @@ def WriteTestData(root_key):
|
||||||
key.Close()
|
key.Close()
|
||||||
try:
|
try:
|
||||||
QueryInfoKey(int_key)
|
QueryInfoKey(int_key)
|
||||||
raise RuntimeError, "It appears the key.Close() function does not close the actual key!"
|
self.fail("It appears the key.Close() function "
|
||||||
|
"does not close the actual key!")
|
||||||
except EnvironmentError:
|
except EnvironmentError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def ReadTestData(root_key):
|
def ReadTestData(self, root_key):
|
||||||
# Check we can get default value for this key.
|
# Check we can get default value for this key.
|
||||||
val = QueryValue(root_key, test_key_name)
|
val = QueryValue(root_key, test_key_name)
|
||||||
verify(val=="Default value", "Registry didn't give back the correct value")
|
self.assertEquals(val, "Default value",
|
||||||
|
"Registry didn't give back the correct value")
|
||||||
|
|
||||||
key = OpenKey(root_key, test_key_name)
|
key = OpenKey(root_key, test_key_name)
|
||||||
# Read the sub-keys
|
# Read the sub-keys
|
||||||
|
|
@ -77,27 +88,31 @@ def ReadTestData(root_key):
|
||||||
data = EnumValue(sub_key, index)
|
data = EnumValue(sub_key, index)
|
||||||
except EnvironmentError:
|
except EnvironmentError:
|
||||||
break
|
break
|
||||||
verify(data in test_data, "Didn't read back the correct test data")
|
self.assertEquals(data in test_data, True,
|
||||||
|
"Didn't read back the correct test data")
|
||||||
index = index + 1
|
index = index + 1
|
||||||
verify(index==len(test_data), "Didn't read the correct number of items")
|
self.assertEquals(index, len(test_data),
|
||||||
|
"Didn't read the correct number of items")
|
||||||
# Check I can directly access each item
|
# Check I can directly access each item
|
||||||
for value_name, value_data, value_type in test_data:
|
for value_name, value_data, value_type in test_data:
|
||||||
read_val, read_typ = QueryValueEx(sub_key, value_name)
|
read_val, read_typ = QueryValueEx(sub_key, value_name)
|
||||||
verify(read_val==value_data and read_typ == value_type, \
|
self.assertEquals(read_val, value_data,
|
||||||
"Could not directly read the value" )
|
"Could not directly read the value")
|
||||||
|
self.assertEquals(read_typ, value_type,
|
||||||
|
"Could not directly read the value")
|
||||||
sub_key.Close()
|
sub_key.Close()
|
||||||
# Enumerate our main key.
|
# Enumerate our main key.
|
||||||
read_val = EnumKey(key, 0)
|
read_val = EnumKey(key, 0)
|
||||||
verify(read_val == "sub_key", "Read subkey value wrong")
|
self.assertEquals(read_val, "sub_key", "Read subkey value wrong")
|
||||||
try:
|
try:
|
||||||
EnumKey(key, 1)
|
EnumKey(key, 1)
|
||||||
verify(0, "Was able to get a second key when I only have one!")
|
self.fail("Was able to get a second key when I only have one!")
|
||||||
except EnvironmentError:
|
except EnvironmentError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
key.Close()
|
key.Close()
|
||||||
|
|
||||||
def DeleteTestData(root_key):
|
def DeleteTestData(self, root_key):
|
||||||
key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
|
key = OpenKey(root_key, test_key_name, 0, KEY_ALL_ACCESS)
|
||||||
sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS)
|
sub_key = OpenKey(key, "sub_key", 0, KEY_ALL_ACCESS)
|
||||||
# It is not necessary to delete the values before deleting
|
# It is not necessary to delete the values before deleting
|
||||||
|
|
@ -107,14 +122,15 @@ def DeleteTestData(root_key):
|
||||||
DeleteValue(sub_key, value_name)
|
DeleteValue(sub_key, value_name)
|
||||||
|
|
||||||
nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
|
nkeys, nvalues, since_mod = QueryInfoKey(sub_key)
|
||||||
verify(nkeys==0 and nvalues==0, "subkey not empty before delete")
|
self.assertEquals(nkeys, 0, "subkey not empty before delete")
|
||||||
|
self.assertEquals(nvalues, 0, "subkey not empty before delete")
|
||||||
sub_key.Close()
|
sub_key.Close()
|
||||||
DeleteKey(key, "sub_key")
|
DeleteKey(key, "sub_key")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
# Shouldnt be able to delete it twice!
|
# Shouldnt be able to delete it twice!
|
||||||
DeleteKey(key, "sub_key")
|
DeleteKey(key, "sub_key")
|
||||||
verify(0, "Deleting the key twice succeeded")
|
self.fail("Deleting the key twice succeeded")
|
||||||
except EnvironmentError:
|
except EnvironmentError:
|
||||||
pass
|
pass
|
||||||
key.Close()
|
key.Close()
|
||||||
|
|
@ -122,35 +138,38 @@ def DeleteTestData(root_key):
|
||||||
# Opening should now fail!
|
# Opening should now fail!
|
||||||
try:
|
try:
|
||||||
key = OpenKey(root_key, test_key_name)
|
key = OpenKey(root_key, test_key_name)
|
||||||
verify(0, "Could open the non-existent key")
|
self.fail("Could open the non-existent key")
|
||||||
except WindowsError: # Use this error name this time
|
except WindowsError: # Use this error name this time
|
||||||
pass
|
pass
|
||||||
|
|
||||||
def TestAll(root_key):
|
def TestAll(self, root_key):
|
||||||
WriteTestData(root_key)
|
self.WriteTestData(root_key)
|
||||||
ReadTestData(root_key)
|
self.ReadTestData(root_key)
|
||||||
DeleteTestData(root_key)
|
self.DeleteTestData(root_key)
|
||||||
|
|
||||||
# Test on my local machine.
|
def testLocalMachineRegistryWorks(self):
|
||||||
TestAll(HKEY_CURRENT_USER)
|
self.TestAll(HKEY_CURRENT_USER)
|
||||||
print "Local registry tests worked"
|
|
||||||
try:
|
|
||||||
remote_name = sys.argv[sys.argv.index("--remote")+1]
|
|
||||||
except (IndexError, ValueError):
|
|
||||||
remote_name = None
|
|
||||||
|
|
||||||
if remote_name is not None:
|
def testConnectRegistryToLocalMachineWorks(self):
|
||||||
try:
|
|
||||||
remote_key = ConnectRegistry(remote_name, HKEY_CURRENT_USER)
|
|
||||||
except EnvironmentError, exc:
|
|
||||||
print "Could not connect to the remote machine -", exc.strerror
|
|
||||||
remote_key = None
|
|
||||||
if remote_key is not None:
|
|
||||||
TestAll(remote_key)
|
|
||||||
print "Remote registry tests worked"
|
|
||||||
else:
|
|
||||||
print "Remote registry calls can be tested using",
|
|
||||||
print "'test_winreg.py --remote \\\\machine_name'"
|
|
||||||
# perform minimal ConnectRegistry test which just invokes it
|
# perform minimal ConnectRegistry test which just invokes it
|
||||||
h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
|
h = ConnectRegistry(None, HKEY_LOCAL_MACHINE)
|
||||||
h.Close()
|
h.Close()
|
||||||
|
|
||||||
|
def testRemoteMachineRegistryWorks(self):
|
||||||
|
if not self.remote_name:
|
||||||
|
raise test_support.TestSkipped("Remote machine name "
|
||||||
|
"not specified.")
|
||||||
|
remote_key = ConnectRegistry(self.remote_name, HKEY_CURRENT_USER)
|
||||||
|
self.TestAll(remote_key)
|
||||||
|
|
||||||
|
def test_main():
|
||||||
|
test_support.run_unittest(WinregTests)
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
try:
|
||||||
|
WinregTests.remote_name = sys.argv[sys.argv.index("--remote")+1]
|
||||||
|
except (IndexError, ValueError):
|
||||||
|
print "Remote registry calls can be tested using",
|
||||||
|
print "'test_winreg.py --remote \\\\machine_name'"
|
||||||
|
WinregTests.remote_name = None
|
||||||
|
test_main()
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue