1
1
# -*- coding: utf-8 -*-
2
2
3
+ import codecs
3
4
import importlib
4
5
import os
5
6
import sys
7
+ from past .builtins import basestring
6
8
7
9
sys .path .append (os .path .realpath (os .path .join (os .path .dirname (__file__ ), "." )))
8
10
9
11
from support import MigTestCase , testmain
12
+ from mig .shared .safeinput import \
13
+ filter_commonname , valid_commonname
14
+
15
+ PY2 = sys .version_info [0 ] == 2
16
+
17
+
18
+ def as_string_of_unicode (value ):
19
+ assert isinstance (value , basestring )
20
+ if not is_string_of_unicode (value ):
21
+ assert PY2 , "unreachable unless Python 2"
22
+ return unicode (codecs .decode (value , 'utf8' ))
23
+ return value
24
+
25
+
26
+ def is_string_of_unicode (value ):
27
+ return type (value ) == type (u'' )
10
28
11
29
12
30
class MigSharedSafeinput (MigTestCase ):
@@ -18,6 +36,47 @@ def test_existing_main(self):
18
36
safeimport = importlib .import_module ("mig.shared.safeinput" )
19
37
safeimport .main (_print = lambda _ : None )
20
38
39
+ COMMONNAME_PERMITTED = (
40
+ 'Firstname Lastname' ,
41
+ 'Test Æøå' ,
42
+ 'Test Überh4x0r' ,
43
+ 'Harry S. Truman' ,
44
+ u'Unicode æøå' )
45
+
46
+ COMMONNAME_PROHIBITED = (
47
+ "Invalid D'Angelo" ,
48
+ 'Test Maybe Invalid Źacãŕ' ,
49
+ 'Test Invalid ?' ,
50
+ 'Test HTML Invalid <code/>' )
51
+
52
+ def test_commonname_valid (self ):
53
+ for test_cn in self .COMMONNAME_PERMITTED :
54
+ saw_raise = False
55
+ try :
56
+ valid_commonname (test_cn )
57
+ except Exception :
58
+ saw_raise = True
59
+ self .assertFalse (saw_raise )
60
+
61
+ for test_cn in self .COMMONNAME_PROHIBITED :
62
+ saw_raise = False
63
+ try :
64
+ valid_commonname (test_cn )
65
+ except Exception :
66
+ saw_raise = True
67
+ self .assertTrue (saw_raise )
68
+
69
+ def test_commonname_filter (self ):
70
+ for test_cn in self .COMMONNAME_PERMITTED :
71
+ test_cn_unicode = as_string_of_unicode (test_cn )
72
+ filtered_cn = filter_commonname (test_cn )
73
+ self .assertEqual (filtered_cn , test_cn_unicode )
74
+
75
+ for test_cn in self .COMMONNAME_PROHIBITED :
76
+ test_cn_unicode = as_string_of_unicode (test_cn )
77
+ filtered_cn = filter_commonname (test_cn )
78
+ self .assertNotEqual (filtered_cn , test_cn_unicode )
79
+
21
80
22
81
if __name__ == '__main__' :
23
82
testmain ()
0 commit comments