~exarkun/pyopenssl/session-methods

« back to all changes in this revision

Viewing changes to OpenSSL/test/test_ssl.py

  • Committer: Jean-Paul Calderone
  • Date: 2012-02-16 19:25:44 UTC
  • Revision ID: exarkun@twistedmatrix.com-20120216192544-5b81qbddgyxl271f
Add ASN1 serialization and deserialization.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1194
1194
        self.assertRaises(TypeError, session.set_timeout, 123, 456)
1195
1195
 
1196
1196
 
 
1197
    def test_asn1(self):
 
1198
        """
 
1199
        :py:method:`Session.to_asn1` returns a :py:obj:`str` giving the ASN.1
 
1200
        representation of the session which can be turned back into a
 
1201
        :py:class:`Session` instance by passing it to
 
1202
        :py:method:`Session.from_asn1`.
 
1203
        """
 
1204
        session = self._create_session(Context(TLSv1_METHOD))
 
1205
        serialized = session.to_asn1()
 
1206
        reloaded = Session.from_asn1(serialized)
 
1207
        self.assertEqual(session.get_time(), reloaded.get_time())
 
1208
        self.assertEqual(session.get_timeout(), reloaded.get_timeout())
 
1209
 
 
1210
 
 
1211
    def test_to_asn1_wrong_args(self):
 
1212
        """
 
1213
        If any arguments are passed to :py:method:`Session.to_asn1`,
 
1214
        :py:obj:`TypeError` is raised.
 
1215
        """
 
1216
        session = self._create_session(Context(TLSv1_METHOD))
 
1217
        self.assertRaises(TypeError, session.to_asn1, 123)
 
1218
        self.assertRaises(TypeError, session.to_asn1, "hello, world")
 
1219
        self.assertRaises(TypeError, session.to_asn1, object())
 
1220
 
 
1221
 
 
1222
    def test_to_asn1_uninitialized(self):
 
1223
        """
 
1224
        If :py:method:`Session.to_asn1` is called on an uninitialized
 
1225
        :py:class:`Session`, :py:obj:`ValueError` is raised.
 
1226
        """
 
1227
        session = Session()
 
1228
        self.assertRaises(ValueError, session.to_asn1)
 
1229
 
 
1230
 
 
1231
    def test_from_asn1_wrong_args(self):
 
1232
        """
 
1233
        If a non-bytes argument is passed to :py:method:`Session.from_asn1`, or
 
1234
        if other than one argument is passed, :py:obj:`TypeError` is raised.
 
1235
        """
 
1236
        self.assertRaises(TypeError, Session.from_asn1)
 
1237
        self.assertRaises(TypeError, Session.from_asn1, object())
 
1238
        self.assertRaises(TypeError, Session.from_asn1, b("hello"), b("world"))
 
1239
 
 
1240
 
 
1241
    def test_from_asn1_bad_serialized_data(self):
 
1242
        """
 
1243
        If bytes not representing a serialized session are passed to
 
1244
        :py:method:`Session.from_asn1`, :py:obj:`OpenSSL.SSL.Error` is raised.
 
1245
        """
 
1246
        self.assertRaises(Error, Session.from_asn1, b("hello"))
 
1247
 
 
1248
 
1197
1249
 
1198
1250
class ConnectionTests(TestCase, _LoopbackMixin):
1199
1251
    """