@@ -4,8 +4,7 @@ module PKCS7Test
4
4
class TestSMIME < TestCase
5
5
def test_read_pkcs7_should_raise_error_when_parsing_headers_fails
6
6
bio = BIO . new
7
- mime = Mime . new
8
- mime . stubs ( :parseHeaders ) . returns ( nil )
7
+ mime = Mime . impl { |name , *args | name == :parseHeaders ? nil : raise }
9
8
10
9
begin
11
10
SMIME . new ( mime ) . readPKCS7 ( bio , nil )
@@ -19,7 +18,7 @@ def test_read_pkcs7_should_raise_error_when_parsing_headers_fails
19
18
20
19
def test_read_pkcs7_should_raise_error_when_content_type_is_not_there
21
20
bio = BIO . new
22
- mime = Mime . new
21
+ mime = Mime . impl { }
23
22
24
23
headers = ArrayList . new
25
24
mime . expects ( :parseHeaders ) . with ( bio ) . returns ( headers )
@@ -34,7 +33,7 @@ def test_read_pkcs7_should_raise_error_when_content_type_is_not_there
34
33
assert_equal PKCS7 ::R_NO_CONTENT_TYPE , e . get_reason
35
34
end
36
35
37
- mime = Mime . new
36
+ mime = Mime . impl { }
38
37
mime . expects ( :parseHeaders ) . with ( bio ) . returns ( headers )
39
38
mime . expects ( :findHeader ) . with ( headers , "content-type" ) . returns ( MimeHeader . new ( "content-type" , nil ) )
40
39
@@ -49,20 +48,18 @@ def test_read_pkcs7_should_raise_error_when_content_type_is_not_there
49
48
end
50
49
51
50
def test_read_pkcs7_should_set_the_second_arguments_contents_to_null_if_its_there
52
- mime = Mime . new
53
- mime . stubs ( :parseHeaders ) . raises ( "getOutOfJailForFree" )
51
+ mime = Mime . impl { |name , *args | name == :parseHeaders ? raise ( "parseHeaders" ) : raise }
54
52
55
53
bio2 = BIO . new
56
54
arr = [ bio2 ] . to_java BIO
57
55
58
56
begin
59
57
SMIME . new ( mime ) . readPKCS7 ( nil , arr )
60
- rescue
58
+ rescue => e
59
+ assert_equal 'parseHeaders' , e . message
61
60
end
62
61
63
62
assert_nil arr [ 0 ]
64
-
65
-
66
63
arr = [ bio2 , bio2 ] . to_java BIO
67
64
begin
68
65
SMIME . new ( mime ) . readPKCS7 ( nil , arr )
@@ -75,11 +72,18 @@ def test_read_pkcs7_should_set_the_second_arguments_contents_to_null_if_its_ther
75
72
76
73
def test_read_pkcs7_should_call_methods_on_mime
77
74
bio = BIO . new
78
- mime = Mime . new
79
75
80
- headers = ArrayList . new
81
- mime . expects ( :parseHeaders ) . with ( bio ) . returns ( headers )
82
- mime . expects ( :findHeader ) . with ( headers , "content-type" ) . returns ( MimeHeader . new ( "content-type" , "application/pkcs7-mime" ) )
76
+ mime = Mime . impl do |name , *args |
77
+ case name
78
+ when :parseHeaders then ArrayList . new
79
+ when :findHeader then
80
+ if args [ 1 ] == 'content-type'
81
+ MimeHeader . new ( args [ 1 ] , "application/pkcs7-mime" )
82
+ else
83
+ raise args . inspect
84
+ end
85
+ end
86
+ end
83
87
84
88
begin
85
89
SMIME . new ( mime ) . readPKCS7 ( bio , nil )
@@ -90,11 +94,17 @@ def test_read_pkcs7_should_call_methods_on_mime
90
94
91
95
def test_read_pkcs7_throws_correct_exception_if_wrong_content_type
92
96
bio = BIO . new
93
- mime = Mime . new
94
-
95
- headers = ArrayList . new
96
- mime . expects ( :parseHeaders ) . with ( bio ) . returns ( headers )
97
- mime . expects ( :findHeader ) . with ( headers , "content-type" ) . returns ( MimeHeader . new ( "content-type" , "foo" ) )
97
+ mime = Mime . impl do |name , *args |
98
+ case name
99
+ when :parseHeaders then ArrayList . new
100
+ when :findHeader then
101
+ if args [ 1 ] == 'content-type'
102
+ MimeHeader . new ( args [ 1 ] , "foo" )
103
+ else
104
+ raise args . inspect
105
+ end
106
+ end
107
+ end
98
108
99
109
begin
100
110
SMIME . new ( mime ) . readPKCS7 ( bio , nil )
@@ -109,13 +119,19 @@ def test_read_pkcs7_throws_correct_exception_if_wrong_content_type
109
119
110
120
def test_read_pkcs7_with_multipart_should_fail_if_no_boundary_found
111
121
bio = BIO . new
112
- mime = Mime . new
113
-
114
- headers = ArrayList . new
115
122
hdr = MimeHeader . new ( "content-type" , "multipart/signed" )
116
- mime . expects ( :parseHeaders ) . with ( bio ) . returns ( headers )
117
- mime . expects ( :findHeader ) . with ( headers , "content-type" ) . returns ( hdr )
118
-
123
+ mime = Mime . impl do |name , *args |
124
+ case name
125
+ when :parseHeaders then ArrayList . new
126
+ when :findHeader then
127
+ if args [ 1 ] == 'content-type'
128
+ hdr
129
+ else
130
+ raise args . inspect
131
+ end
132
+ end
133
+ end
134
+ hdr = MimeHeader . new ( "content-type" , "multipart/signed" )
119
135
mime . expects ( :findParam ) . with ( hdr , "boundary" ) . returns ( nil )
120
136
121
137
begin
@@ -130,7 +146,7 @@ def test_read_pkcs7_with_multipart_should_fail_if_no_boundary_found
130
146
131
147
def test_read_pkcs7_with_multipart_should_fail_if_null_boundary_value
132
148
bio = BIO . new
133
- mime = Mime . new
149
+ mime = Mime . impl { }
134
150
135
151
headers = ArrayList . new
136
152
hdr = MimeHeader . new ( "content-type" , "multipart/signed" )
@@ -152,7 +168,7 @@ def test_read_pkcs7_with_multipart_should_fail_if_null_boundary_value
152
168
# TODO: redo this test to be an integration test
153
169
def _test_read_pkcs7_happy_path_without_multipart
154
170
bio = BIO . new
155
- mime = Mime . new
171
+ mime = Mime . impl { }
156
172
157
173
headers = ArrayList . new
158
174
mime . expects ( :parseHeaders ) . with ( bio ) . returns ( headers )
@@ -164,19 +180,20 @@ def _test_read_pkcs7_happy_path_without_multipart
164
180
def test_read_pkcs7_happy_path_multipart
165
181
bio = BIO ::from_string ( MultipartSignedString )
166
182
mime = Mime ::DEFAULT
167
- p7 = SMIME . new ( mime ) . readPKCS7 ( bio , nil )
183
+ SMIME . new ( mime ) . readPKCS7 ( bio , nil )
168
184
end
169
185
170
186
def test_read_pkcs7_happy_path_without_multipart_enveloped
171
187
bio = BIO ::from_string ( MimeEnvelopedString )
172
188
mime = Mime ::DEFAULT
173
- p7 = SMIME . new ( mime ) . readPKCS7 ( bio , nil )
189
+ SMIME . new ( mime ) . readPKCS7 ( bio , nil )
174
190
end
175
191
176
192
def test_read_pkcs7_happy_path_without_multipart_signed
177
193
bio = BIO ::from_string ( MimeSignedString )
178
194
mime = Mime ::DEFAULT
179
- p7 = SMIME . new ( mime ) . readPKCS7 ( bio , nil )
195
+ SMIME . new ( mime ) . readPKCS7 ( bio , nil )
180
196
end
197
+
181
198
end
182
199
end
0 commit comments