2020import sys
2121import tempfile
2222import time
23- from typing import Union
23+ from typing import NamedTuple , Union
2424import unittest
2525
2626from urumanifest import encryption
3232 "동해 물과 백두산이 마르고 닳도록, 하느님이 보우하사 우리나라 만세" ,
3333]
3434
35+ class _KnownValues (NamedTuple ):
36+ source : str
37+ BTEA : bytes
38+ XTEA : bytes
39+
40+
41+ _KnownData = [
42+ _KnownValues (
43+ "The quick brown fox jumps over the lazy dog!" ,
44+ BTEA = bytes ([
45+ 0x6E , 0x6F , 0x74 , 0x74 , 0x68 , 0x65 , 0x64 , 0x72 , 0x6F , 0x69 , 0x64 , 0x73 ,
46+ 0x2C , 0x00 , 0x00 , 0x00 , 0x52 , 0xB6 , 0xB5 , 0xDB , 0x0E , 0x76 , 0x8F , 0x44 ,
47+ 0x45 , 0x31 , 0xC6 , 0xE4 , 0xA9 , 0x64 , 0x86 , 0x6C , 0x9B , 0x1D , 0x33 , 0x7B ,
48+ 0x6B , 0xF5 , 0xE4 , 0x1D , 0x61 , 0xFE , 0x27 , 0x54 , 0x1E , 0xB2 , 0x9A , 0x6D ,
49+ 0x52 , 0x0D , 0x17 , 0x4D , 0xA7 , 0x07 , 0xDC , 0x2B , 0x93 , 0xC8 , 0x83 , 0xFE ,
50+ 0x93 , 0xA4 , 0xA7 , 0xA7
51+ ]),
52+ XTEA = bytes ([
53+ 0x77 , 0x68 , 0x61 , 0x74 , 0x64 , 0x6F , 0x79 , 0x6F , 0x75 , 0x73 , 0x65 , 0x65 ,
54+ 0x2C , 0x00 , 0x00 , 0x00 , 0x9B , 0xE3 , 0xC7 , 0xF6 , 0xA7 , 0x3C , 0xEE , 0xB2 ,
55+ 0x9D , 0x4D , 0x5F , 0x32 , 0x7E , 0x66 , 0xF7 , 0x6C , 0x0E , 0x52 , 0x24 , 0x2E ,
56+ 0x3E , 0x61 , 0x8B , 0xB8 , 0x46 , 0xE3 , 0xF4 , 0xDF , 0x33 , 0xAD , 0x28 , 0x6D ,
57+ 0x8A , 0x6C , 0xD2 , 0x2F , 0x95 , 0x24 , 0x1D , 0xEA , 0x06 , 0x8E , 0xF6 , 0x39 ,
58+ 0x27 , 0x7C , 0x4B , 0xA2
59+ ])
60+ )
61+ ]
62+
3563class _EncryptionTest (abc .ABC ):
3664 def setUp (self ):
3765 self .start_time = time .perf_counter ()
@@ -51,7 +79,7 @@ def stream_key(self):
5179
5280 @property
5381 @abc .abstractmethod
54- def stream_type (self ):
82+ def stream_type (self ) -> encryption . Encryption :
5583 ...
5684
5785 def _round_trip (self , data_out : Union [bytes , str ]):
@@ -101,6 +129,26 @@ def test_binaryUtf16(self):
101129 for i in _TestData :
102130 self ._round_trip (i .encode ("utf-16-le" ))
103131
132+ def _test_known (self , value : str , result : bytes ):
133+ fname = Path (tempfile .mktemp (suffix = ".fni" ))
134+ kwargs = dict (mode = encryption .Mode .WriteText , enc = self .stream_type , key = self .stream_key , encoding = "UTF-8" )
135+ kwargs .update (self .extra_args )
136+ try :
137+ with encryption .stream (fname , ** kwargs ) as stream :
138+ stream .write (value )
139+ with fname .open (mode = "rb" ) as stream :
140+ data_in = stream .read ()
141+ except :
142+ raise
143+ else :
144+ self .assertEqual (result , data_in )
145+ finally :
146+ fname .unlink ()
147+
148+ def test_knownValues (self ):
149+ for i in _KnownData :
150+ self ._test_known (i .source , getattr (i , self .stream_type .name ))
151+
104152 def test_randomWrites (self ):
105153 with self ._write_test () as stream :
106154 stream .write (struct .pack ("<B" , 1 ))
@@ -146,8 +194,13 @@ def stream_key(self):
146194 def stream_type (self ):
147195 return encryption .Encryption .BTEA
148196
197+ try :
198+ import _urumanifest
199+ except ImportError :
200+ print ("WARNING: C encryption module is NOT available. Only testing Python encryption!" )
201+ else :
202+ class XTEACxxEncryptionTest (_XTEAEncryptionTest , unittest .TestCase ): pass
203+ class BTEACxxEncryptionTest (_BTEAEncryptionTest , unittest .TestCase ): pass
149204
150205class XTEAPureEncryptionTest (_PureTest , _XTEAEncryptionTest , unittest .TestCase ): pass
151- class XTEACxxEncryptionTest (_XTEAEncryptionTest , unittest .TestCase ): pass
152206class BTEAPureEncryptionTest (_PureTest , _BTEAEncryptionTest , unittest .TestCase ): pass
153- class BTEACxxEncryptionTest (_BTEAEncryptionTest , unittest .TestCase ): pass
0 commit comments