77#
88import unittest
99
10+ from win32ctypes .core import _backend
1011from win32ctypes .core ._authentication import (
1112 CREDENTIAL , CREDENTIAL_ATTRIBUTE , FILETIME ,
1213 credential2dict , credential_attribute2dict )
1314from win32ctypes .constants import (
1415 CRED_TYPE_GENERIC , CRED_PERSIST_ENTERPRISE )
1516
16-
1717class TestCREDENTIAL (unittest .TestCase ):
1818
1919 maxDiff = None
2020
21- def test_from_dict (self ):
21+ def test_fromdict (self ):
2222 # given
2323 username = 'john'
2424 password = 'doefsajfsakfj'
@@ -37,15 +37,20 @@ def test_from_dict(self):
3737
3838 # then
3939 self .assertEqual (result .Type , CRED_TYPE_GENERIC )
40- self .assertEqual (result .TargetName , target )
41- self .assertIsNone (result .TargetAlias )
4240 self .assertEqual (result .Flags , 0 )
4341 self .assertEqual (result .AttributeCount , 0 )
44- self .assertIsNotNone (result .CredentialBlob )
45- self .assertEqual (result .UserName , username )
4642 self .assertEqual (result .CredentialBlobSize , 26 )
47- self .assertEqual (result .Comment , comment )
4843 self .assertEqual (result .Persist , CRED_PERSIST_ENTERPRISE )
44+ if _backend == 'cffi' :
45+ from win32ctypes .core .cffi ._util import ffi
46+ self .assertEqual (ffi .string (result .UserName ), username )
47+ self .assertEqual (result .TargetAlias , ffi .NULL )
48+ self .assertEqual (ffi .string (result .Comment ), comment )
49+ self .assertNotEqual (result .CredentialBlob , ffi .NULL )
50+ else :
51+ self .assertEqual (result .Comment , comment )
52+ self .assertEqual (result .UserName , username )
53+ self .assertIsNone (result .TargetAlias )
4954
5055 def test_roundtrip (self ):
5156 # given
@@ -54,9 +59,11 @@ def test_roundtrip(self):
5459 comment = 'Created by MiniPyWin32Cred test suite'
5560 target = '{0}@{1}' .format (username , password )
5661 keyword = 'mysecret-attribute'
57- value = b 'Created by MiniPyWin32Cred test suite'
62+ value = 'Created by MiniPyWin32Cred test suite'
5863 attribute1 = {'Keyword' : keyword , 'Value' : value , 'Flags' : 2 }
5964 data = {
65+ 'Flags' : 2 ,
66+ 'TargetAlias' : 'test' ,
6067 'Type' : CRED_TYPE_GENERIC ,
6168 'TargetName' : target ,
6269 'UserName' : username ,
@@ -70,23 +77,21 @@ def test_roundtrip(self):
7077 result = credential2dict (credential )
7178
7279 # then
73- data ['TargetAlias' ] = None
74- data ['Flags' ] = 0
75- self .assertIsInstance (result ['LastWritten' ], FILETIME )
76- # Remove unused keys
80+ if _backend == 'ctypes' :
81+ self .assertIsInstance (result ['LastWritten' ], FILETIME )
7782 del result ['LastWritten' ]
78- del result ['CredentialBlobSize' ]
79- del result ['AttributeCount' ]
8083 result ['CredentialBlob' ] = result ['CredentialBlob' ].decode ('utf-16' )
84+ attribute = result ['Attributes' ][0 ]
85+ attribute ['Value' ] = attribute ['Value' ].decode ('utf-16' )
8186 self .assertEqual (result , data )
8287
8388
8489class TestCREDENTIAL_ATTRIBUTE (unittest .TestCase ):
8590
86- def test_from_dict (self ):
91+ def test_fromdict (self ):
8792 # given
8893 keyword = 'mysecret-attribute'
89- value = b 'Created by MiniPyWin32Cred test suite'
94+ value = 'Created by MiniPyWin32Cred test suite'
9095 data = {
9196 'Keyword' : keyword ,
9297 'Flags' : 2 ,
@@ -96,12 +101,34 @@ def test_from_dict(self):
96101 result = CREDENTIAL_ATTRIBUTE .fromdict (data )
97102
98103 # then
99- self .assertEqual (result .Keyword , 'mysecret-attribute' )
100104 self .assertEqual (result .Flags , 2 )
101105 self .assertIsNotNone (result .Value )
102- self .assertEqual (result .ValueSize , 37 )
106+ self .assertEqual (result .ValueSize , 74 )
107+ if _backend == 'cffi' :
108+ from win32ctypes .core .cffi ._util import ffi
109+ self .assertEqual (
110+ ffi .string (result .Keyword ), 'mysecret-attribute' )
111+ else :
112+ self .assertEqual (result .Keyword , 'mysecret-attribute' )
113+
114+ def test_roundtrip_with_string (self ):
115+ # given
116+ keyword = 'mysecret-attribute'
117+ value = 'Created by MiniPyWin32Cred test suite'
118+ data = {
119+ 'Keyword' : keyword ,
120+ 'Flags' : 7 ,
121+ 'Value' : value }
103122
104- def test_roundtrip (self ):
123+ # when
124+ attribute = CREDENTIAL_ATTRIBUTE .fromdict (data )
125+ result = credential_attribute2dict (attribute )
126+
127+ # then
128+ result ['Value' ] = result ['Value' ].decode ('utf-16' )
129+ self .assertEqual (result , data )
130+
131+ def test_roundtrip_with_bytes (self ):
105132 # given
106133 keyword = 'mysecret-attribute'
107134 value = b'Created by MiniPyWin32Cred test suite'
@@ -115,6 +142,4 @@ def test_roundtrip(self):
115142 result = credential_attribute2dict (attribute )
116143
117144 # then
118- # Add missing keys to original data
119- result ['Value' ] = result ['Value' ]
120145 self .assertEqual (result , data )
0 commit comments