@@ -884,8 +884,14 @@ def valid_email_address(addr, allow_real_name=True):
884
884
raise InputException ("Only plain addresses without name allowed" )
885
885
if not silent_email_validator (plain_addr ):
886
886
raise InputException ("No actual email address included" )
887
- merged = formataddr (name_address_pair )
888
- if merged != addr :
887
+ raise_error = False
888
+ try :
889
+ merged = formataddr (name_address_pair )
890
+ if merged != addr :
891
+ raise InputException ("" )
892
+ except (InputException , UnicodeEncodeError ) as exc :
893
+ raise_error = True
894
+ if raise_error :
889
895
raise InputException ("Invalid email format" )
890
896
891
897
@@ -2281,24 +2287,49 @@ def __str__(self):
2281
2287
return force_utf8 (force_unicode (self .value ))
2282
2288
2283
2289
2284
- def main (_print = print ):
2290
+ def main (_exit = sys . exit , _print = print ):
2285
2291
print = _print # workaround print as reserved word on PY2
2286
2292
2287
2293
for test_org in ('UCPH' , 'Some University, Some Dept.' , 'Green Shoes Ltd.' ,
2288
- u'Unicode Org' , "Invalid R+D" , "Invalid R/D" ,
2294
+ u'Unicode Org' ):
2295
+ try :
2296
+ print ('Testing valid_organization: %r' % test_org )
2297
+ print ('Filtered organization: %s' % filter_organization (test_org ))
2298
+ valid_organization (test_org )
2299
+ print ('Accepted raw organization!' )
2300
+ except InputException as exc :
2301
+ print ('Rejected raw organization %r: %s' % (test_org , exc ))
2302
+ _exit (1 )
2303
+
2304
+ for test_org in (
2305
+ "Invalid R+D" , "Invalid R/D" ,
2289
2306
"Invalid R@D" , 'Test HTML Invalid <code/>' ):
2290
2307
try :
2291
2308
print ('Testing valid_organization: %r' % test_org )
2292
2309
print ('Filtered organization: %s' % filter_organization (test_org ))
2293
2310
valid_organization (test_org )
2294
2311
print ('Accepted raw organization!' )
2295
- except Exception as exc :
2312
+ _exit (1 )
2313
+ except InputException as exc :
2296
2314
print ('Rejected raw organization %r: %s' % (test_org , exc ))
2297
2315
2298
2316
for test_path in ('test.txt' , 'Test Æøå' , 'Test Überh4x0r' ,
2299
2317
'Test valid Jean-Luc Géraud' , 'Test valid Źacãŕ' ,
2300
2318
'Test valid special%&()!$¶â€' , 'Test look-alike-å å' ,
2301
- 'Test north Atlantic Barður Ðýþ' , 'Test exotic لرحيم' ,
2319
+ 'Test north Atlantic Barður Ðýþ' ):
2320
+ try :
2321
+ print ('Testing valid_path: %s' % test_path )
2322
+ print ('Filtered path: %s' % filter_path (test_path ))
2323
+ # print 'DEBUG %s only in %s' % ([test_path],
2324
+ # [VALID_PATH_CHARACTERS])
2325
+ valid_path (test_path )
2326
+ print ('Accepted raw path!' )
2327
+ except InputException as exc :
2328
+ print ('Rejected raw path %r: %s' % (test_path , exc ))
2329
+ _exit (1 )
2330
+
2331
+ for test_path in (
2332
+ 'Test exotic لرحيم' ,
2302
2333
'Test Invalid ?' , 'Test Invalid `' ,
2303
2334
'Test invalid <' , 'Test Invalid >' ,
2304
2335
'Test Invalid *' , 'Test Invalid "' ):
@@ -2309,14 +2340,26 @@ def main(_print=print):
2309
2340
# [VALID_PATH_CHARACTERS])
2310
2341
valid_path (test_path )
2311
2342
print ('Accepted raw path!' )
2312
- except Exception as exc :
2343
+ _exit (1 )
2344
+ except InputException as exc :
2313
2345
print ('Rejected raw path %r: %s' % (test_path , exc ))
2314
2346
2315
- for test_addr in ('' , 'invalid' , 'abc@dk' , 'abc@def.org' , 'abc@def.gh.org' ,
2316
- 'aBc@Def.org' , '<invalid@def.org>' ,
2317
- 'Test <abc@def.org>' , 'Test Æøå <æøå@abc.org>' ,
2347
+ for test_addr in ('abc@def.org' , 'abc@def.gh.org' ,
2348
+ 'Test <abc@def.org>' ,
2318
2349
'Test <abc.def@ghi.org>' , 'Test <abc+def@ghi.org>' ,
2319
2350
'A valid-name <abc@ghi.org>' ,
2351
+ ):
2352
+ try :
2353
+ print ('Testing valid_email_address: %s' % test_addr )
2354
+ valid_email_address (test_addr )
2355
+ print ('Accepted raw address! %s' % [parseaddr (test_addr )])
2356
+ except InputException as exc :
2357
+ print ('Rejected raw address %r: %s' % (test_addr , exc ))
2358
+ _exit (1 )
2359
+
2360
+ for test_addr in (
2361
+ '' , 'invalid' , 'abc@dk' , 'Test Æøå <æøå@abc.org>' ,
2362
+ 'aBc@Def.org' , '<invalid@def.org>' ,
2320
2363
' false-positive@ghi.org' ,
2321
2364
'False Positive <abc@ghi.org>' ,
2322
2365
' Another False Positive <abc@ghi.org>' ,
@@ -2331,7 +2374,8 @@ def main(_print=print):
2331
2374
print ('Testing valid_email_address: %s' % test_addr )
2332
2375
valid_email_address (test_addr )
2333
2376
print ('Accepted raw address! %s' % [parseaddr (test_addr )])
2334
- except Exception as exc :
2377
+ _exit (1 )
2378
+ except InputException as exc :
2335
2379
print ('Rejected raw address %r: %s' % (test_addr , exc ))
2336
2380
2337
2381
# OpenID 2.0 version
@@ -2378,19 +2422,24 @@ def main(_print=print):
2378
2422
print ("Accepted:" )
2379
2423
for (key , val ) in accepted .items ():
2380
2424
print ("\t %s: %s" % (key , val ))
2381
- print ("Rejected:" )
2382
- for (key , val ) in rejected .items ():
2383
- print ("\t %s: %s" % (key , val ))
2425
+ if rejected :
2426
+ print ("Rejected:" )
2427
+ for (key , val ) in rejected .items ():
2428
+ print ("\t %s: %s" % (key , val ))
2429
+ _exit (1 )
2430
+
2384
2431
user_arguments_dict ['openid.sreg.fullname' ] = [
2385
2432
force_unicode ('Jonas Æøå Bardino' )]
2386
2433
(accepted , rejected ) = validated_input (
2387
2434
user_arguments_dict , autocreate_defaults )
2388
2435
print ("Accepted:" )
2389
2436
for (key , val ) in accepted .items ():
2390
2437
print ("\t %s: %s" % (key , val ))
2391
- print ("Rejected:" )
2392
- for (key , val ) in rejected .items ():
2393
- print ("\t %s: %s" % (key , val ))
2438
+ if rejected :
2439
+ print ("Rejected:" )
2440
+ for (key , val ) in rejected .items ():
2441
+ print ("\t %s: %s" % (key , val ))
2442
+ _exit (1 )
2394
2443
2395
2444
# OpenID Connect version
2396
2445
autocreate_defaults = {
@@ -2437,9 +2486,12 @@ def main(_print=print):
2437
2486
print ("Accepted:" )
2438
2487
for (key , val ) in accepted .items ():
2439
2488
print ("\t %s: %s" % (key , val ))
2440
- print ("Rejected:" )
2441
- for (key , val ) in rejected .items ():
2442
- print ("\t %s: %s" % (key , val ))
2489
+ if rejected :
2490
+ print ("Rejected:" )
2491
+ for (key , val ) in rejected .items ():
2492
+ print ("\t %s: %s" % (key , val ))
2493
+ _exit (1 )
2494
+
2443
2495
user_arguments_dict = {'oidc.claim.aud' : ['http://somedomain.org' ],
2444
2496
'oidc.claim.country' : ['DK' ],
2445
2497
'oidc.claim.email' : ['bardino@nbi.ku.dk,bardino@science.ku.dk' ],
@@ -2464,9 +2516,13 @@ def main(_print=print):
2464
2516
print ("Accepted:" )
2465
2517
for (key , val ) in accepted .items ():
2466
2518
print ("\t %s: %s" % (key , val ))
2467
- print ("Rejected:" )
2468
- for (key , val ) in rejected .items ():
2469
- print ("\t %s: %s" % (key , val ))
2519
+ if rejected :
2520
+ print ("Rejected:" )
2521
+ for (key , val ) in rejected .items ():
2522
+ print ("\t %s: %s" % (key , val ))
2523
+ _exit (1 )
2524
+
2525
+ _exit (0 )
2470
2526
2471
2527
if __name__ == '__main__' :
2472
2528
main ()
0 commit comments