29
29
import os
30
30
import logging
31
31
# Dependency Imports
32
+ import asn1crypto as asn1
33
+ import asn1crypto .core as asn1_core
34
+ '''
32
35
import pyasn1
33
36
import pyasn1.type.univ as pyasn1_univ
34
37
import pyasn1.type.char as pyasn1_char
35
38
import pyasn1.codec.der.encoder as pyasn1_der_encoder
39
+ '''
36
40
# TUF Imports
37
41
import tuf
38
42
import tuf .log
@@ -63,6 +67,43 @@ def tearDown(self):
63
67
64
68
65
69
70
+ def test_baseline (self ):
71
+ """
72
+ Fail if basic asn1crypto functionality is broken.
73
+ Use Integer and VisibleString.
74
+ """
75
+
76
+ i = asn1_core .Integer (5 )
77
+ self .assertEqual (5 , i .native )
78
+
79
+ i_der = i .dump ()
80
+ self .assertEqual (b'\x02 \x01 \x05 ' , i_der )
81
+
82
+ # Convert back and test.
83
+ self .assertEqual (5 , asn1_core .load (i_der ).native )
84
+ self .assertEqual (5 , asn1_core .Integer .load (i_der ).native )
85
+
86
+
87
+ s = 'testword'
88
+ expected_der_of_string = b'\x1a \x08 testword'
89
+
90
+ s_asn1 = asn1_core .VisibleString (s )
91
+ self .assertEqual (s , s_asn1 .native )
92
+
93
+ s_der = s_asn1 .dump ()
94
+ self .assertEqual (expected_der_of_string , s_der )
95
+
96
+ self .assertEqual (s_asn1 , asn1_core .load (s_der ))
97
+ self .assertEqual (s_asn1 , asn1_core .VisibleString .load (s_der ))
98
+
99
+ self .assertEqual (s , asn1_core .load (s_der ).native )
100
+ self .assertEqual (s , asn1_core .VisibleString .load (s_der ).native )
101
+
102
+
103
+
104
+
105
+ '''
106
+
66
107
def baseline_convert_and_encode(self):
67
108
"""
68
109
Fail if basic pyasn1 functionality is broken.
@@ -80,7 +121,7 @@ def baseline_convert_and_encode(self):
80
121
81
122
82
123
83
- def test_hex_string_octets_conversions (self ):
124
+ def test_hex_string_octets_conversions_pyasn1 (self):
84
125
hex_string = '01234567890abcdef0'
85
126
expected_der_of_octet_string = b'\x04 \t \x01 #Eg\x89 \n \xbc \xde \xf0 '
86
127
@@ -134,8 +175,8 @@ def test_to_pyasn1_sig(self):
134
175
expected_der = \
135
176
b'0\x18 \x04 \x03 \x12 4V\x1a \x07 magical\x04 \x08 \xab \xcd \xef \x12 4Vx\x90 '
136
177
137
- """sig_asn1, sig_der = self.conversion_check ("""
138
- self .conversion_check (
178
+ """sig_asn1, sig_der = self.conversion_check_pyasn1 ("""
179
+ self.conversion_check_pyasn1 (
139
180
sig,
140
181
asn1_convert.to_pyasn1,
141
182
from_asn1_func=asn1_convert.from_pyasn1,
@@ -183,7 +224,7 @@ def test_to_pyasn1_hashes(self):
183
224
h_expected_der = \
184
225
b'0*\x1a \x06 sha256\x04 i\x90 \xb6 Xn\xd5 E8|jQ\xdb b\x17 ;\x90 :]\xff F\xb1 {\x1b \xc3 \xfe \x1e l\xa0 \xd0 \x84 O/'
185
226
186
- self .conversion_check (
227
+ self.conversion_check_pyasn1 (
187
228
h,
188
229
asn1_convert.to_pyasn1,
189
230
#from_asn1_func=asn1_convert.from_pyasn1, # TODO: DO NOT SKIP CONVERTING BACK
@@ -215,14 +256,14 @@ def test_to_pyasn1_hashes(self):
215
256
216
257
217
258
# Test using the custom converter for hashes, hashes_to_pyasn1.
218
- hashes_asn1_alt , junk = self .conversion_check (
259
+ hashes_asn1_alt, junk = self.conversion_check_pyasn1 (
219
260
hashes_dict,
220
261
asn1_convert.hashes_to_pyasn1,
221
262
#from_asn1_func=asn1_convert.hashes_from_pyasn1, # TODO: DO NOT SKIP CONVERTING BACK; func not yet written?
222
263
expected_der=expected_der)
223
264
224
265
# Test using the generic converter, to_pyasn1.
225
- hashes_asn1 , junk = self .conversion_check (
266
+ hashes_asn1, junk = self.conversion_check_pyasn1 (
226
267
hashes_dict,
227
268
asn1_convert.to_pyasn1,
228
269
#from_asn1_func=asn1_convert.from_pyasn1, # TODO: DO NOT SKIP CONVERTING BACK
@@ -281,14 +322,14 @@ def test_to_pyasn1_keys(self):
281
322
282
323
283
324
# Convert them and test along the way.
284
- self .conversion_check (
325
+ self.conversion_check_pyasn1 (
285
326
ed_pub,
286
327
asn1_convert.to_pyasn1,
287
328
# from_asn1_func=asn1_convert.from_pyasn1, # TODO: DO NOT SKIP CONVERTING BACK
288
329
expected_der=ed_key_expected_der,
289
330
second_arg=asn1_defs.PublicKey)
290
331
291
- self .conversion_check (
332
+ self.conversion_check_pyasn1 (
292
333
rsa_pub,
293
334
asn1_convert.to_pyasn1,
294
335
# from_asn1_func=asn1_convert.from_pyasn1, # TODO: DO NOT SKIP CONVERTING BACK
@@ -340,7 +381,7 @@ def test_to_pyasn1_timestamp_hash_of_snapshot(self):
340
381
expected_der = asn1_convert.pyasn1_to_der(expected_pyasn1)
341
382
342
383
343
- hashes_of_snapshot_pyasn1 , hashes_of_snapshot_der = self .conversion_check (
384
+ hashes_of_snapshot_pyasn1, hashes_of_snapshot_der = self.conversion_check_pyasn1 (
344
385
hashes_of_snapshot,
345
386
asn1_convert.to_pyasn1,
346
387
# from_asn1_func=asn1_convert.from_pyasn1, # TODO: DO NOT SKIP CONVERTING BACK
@@ -413,7 +454,7 @@ def test_hashes_to_pyasn1(self):
413
454
hashes_dict = {hash_type1: hash_value1, hash_type2: hash_value2}
414
455
expected_der = b'1x0*\x1a \x06 sha256\x04 i\x90 \xb6 Xn\xd5 E8|jQ\xdb b\x17 ;\x90 :]\xff F\xb1 {\x1b \xc3 \xfe \x1e l\xa0 \xd0 \x84 O/0J\x1a \x06 sha512\x04 @\x12 4Vx\x90 \xab \xcd \xef \x00 \x00 \x00 \x00 \x02 \x17 ;\x90 :]\xff F\xb1 {\x1b \xc3 \xfe \x1e l\xa0 \xd0 \x84 O/i\x90 \xb6 Xn\xd5 E8|jQ\xdb b\x17 ;\x90 :]\xff F\xb1 {\x1b \xc3 \xfe \x1e l\xa0 \xd0 \x84 O/'
415
456
416
- hashes_pyasn1 , hashes_der = self .conversion_check (
457
+ hashes_pyasn1, hashes_der = self.conversion_check_pyasn1 (
417
458
hashes_dict, asn1_convert.hashes_to_pyasn1, expected_der=expected_der)
418
459
419
460
self.assertEqual(len(hashes_dict), len(hashes_pyasn1))
@@ -451,7 +492,7 @@ def test_public_key_to_pyasn1(self):
451
492
452
493
453
494
454
- def conversion_check (self , data , to_asn1_func ,
495
+ def conversion_check_pyasn1 (self, data, to_asn1_func,
455
496
from_asn1_func=None, expected_der=None, second_arg=None):
456
497
"""
457
498
By default:
@@ -512,6 +553,11 @@ def conversion_check(self, data, to_asn1_func,
512
553
return data_asn1, data_der
513
554
514
555
556
+ '''
557
+
558
+ def conversion_check (self , data ):
559
+ raise NotImplementedError ()
560
+
515
561
516
562
# Run unit test.
517
563
if __name__ == '__main__' :
0 commit comments