Exemplo n.º 1
0
 def test_decode(self):
     test_cases = [{
         'about': 'empty string',
         'input': '',
         'expect': '',
     }, {
         'about': 'standard encoding, padded',
         'input': 'Z29+IQ==',
         'expect': 'go~!',
     }, {
         'about': 'URL encoding, padded',
         'input': 'Z29-IQ==',
         'expect': 'go~!',
     }, {
         'about': 'standard encoding, not padded',
         'input': 'Z29+IQ',
         'expect': 'go~!',
     }, {
         'about': 'URL encoding, not padded',
         'input': 'Z29-IQ',
         'expect': 'go~!',
     }, {
         'about': 'standard encoding, not enough much padding',
         'input': 'Z29+IQ=',
         'expect_error': 'illegal base64 data at input byte 8',
     }]
     for test in test_cases:
         if test.get('expect_error'):
             with self.assertRaises(ValueError, msg=test['about']) as e:
                 bakery.b64decode(test['input'])
             self.assertEqual(str(e.exception), 'Incorrect padding')
         else:
             self.assertEqual(bakery.b64decode(test['input']), test['expect'].encode('utf-8'), msg=test['about'])
 def test_decode(self):
     test_cases = [{
         'about': 'empty string',
         'input': '',
         'expect': '',
     }, {
         'about': 'standard encoding, padded',
         'input': 'Z29+IQ==',
         'expect': 'go~!',
     }, {
         'about': 'URL encoding, padded',
         'input': 'Z29-IQ==',
         'expect': 'go~!',
     }, {
         'about': 'standard encoding, not padded',
         'input': 'Z29+IQ',
         'expect': 'go~!',
     }, {
         'about': 'URL encoding, not padded',
         'input': 'Z29-IQ',
         'expect': 'go~!',
     }, {
         'about': 'standard encoding, not enough much padding',
         'input': 'Z29+IQ=',
         'expect_error': 'illegal base64 data at input byte 8',
     }]
     for test in test_cases:
         if test.get('expect_error'):
             with self.assertRaises(ValueError, msg=test['about']) as e:
                 bakery.b64decode(test['input'])
             self.assertEqual(str(e.exception), 'Incorrect padding')
         else:
             self.assertEqual(bakery.b64decode(test['input']), test['expect'].encode('utf-8'), msg=test['about'])
Exemplo n.º 3
0
 def test_decode_caveat_v3_from_go(self):
     tp_key = bakery.PrivateKey(
         nacl.public.PrivateKey(
             base64.b64decode(
                 'TSpvLpQkRj+T3JXnsW2n43n5zP/0X4zn0RvDiWC3IJ0=')))
     fp_key = bakery.PrivateKey(
         nacl.public.PrivateKey(
             base64.b64decode(
                 'KXpsoJ9ujZYi/O2Cca6kaWh65MSawzy79LWkrjOfzcs=')))
     root_key = base64.b64decode(b'oqOXI3/Mz/pKjCuFOt2eYxb7ndLq66GY')
     # This caveat has been generated from the go code
     # to check the compatibilty
     encrypted_cav = bakery.b64decode(
         'A_D-xlUf2MdGMgtu7OKRQnCP1OQJk6PKeFWRK26WIBA6DNwKGNLeFSkD2M-8A'
         'EYvmgVH95GWu7T7caKxKhhOQFcEKgnXKJvYXxz1zin4cZc4Q6C7gVqA-J4_j3'
         '1LX4VKxymqG62UGPo78wOv0_fKjr3OI6PPJOYOQgBMclemlRF2', )
     cav = bakery.decode_caveat(tp_key, encrypted_cav)
     self.assertEquals(
         cav,
         bakery.ThirdPartyCaveatInfo(
             condition='third party condition',
             first_party_public_key=fp_key.public_key,
             third_party_key_pair=tp_key,
             root_key=root_key,
             caveat=encrypted_cav,
             version=bakery.VERSION_3,
             id=None,
             namespace=bakery.legacy_namespace()))
Exemplo n.º 4
0
 def test_decode_caveat_v2_from_go(self):
     tp_key = bakery.PrivateKey(
         nacl.public.PrivateKey(
             base64.b64decode(
                 'TSpvLpQkRj+T3JXnsW2n43n5zP/0X4zn0RvDiWC3IJ0=')))
     fp_key = bakery.PrivateKey(
         nacl.public.PrivateKey(
             base64.b64decode(
                 'KXpsoJ9ujZYi/O2Cca6kaWh65MSawzy79LWkrjOfzcs=')))
     root_key = base64.b64decode('wh0HSM65wWHOIxoGjgJJOFvQKn2jJFhC')
     # This caveat has been generated from the go code
     # to check the compatibilty
     encrypted_cav = bakery.b64decode(
         'AvD-xlUf2MdGMgtu7OKRQnCP1OQJk6PKeFWRK26WIBA6DNwKGIHq9xGcHS9IZ'
         'Lh0cL6D9qpeKI0mXmCPfnwRQDuVYC8y5gVWd-oCGZaj5TGtk3byp2Vnw6ojmt'
         'sULDhY59YA_J_Y0ATkERO5T9ajoRWBxU2OXBoX6bImXA', )
     cav = bakery.decode_caveat(tp_key, encrypted_cav)
     self.assertEqual(
         cav,
         bakery.ThirdPartyCaveatInfo(
             condition='third party condition',
             first_party_public_key=fp_key.public_key,
             third_party_key_pair=tp_key,
             root_key=root_key,
             caveat=encrypted_cav,
             version=bakery.VERSION_2,
             id=None,
             namespace=bakery.legacy_namespace()))
 def test_decode_caveat_v3_from_go(self):
     tp_key = bakery.PrivateKey(
         nacl.public.PrivateKey(base64.b64decode(
             'TSpvLpQkRj+T3JXnsW2n43n5zP/0X4zn0RvDiWC3IJ0=')))
     fp_key = bakery.PrivateKey(nacl.public.PrivateKey(
         base64.b64decode(
             'KXpsoJ9ujZYi/O2Cca6kaWh65MSawzy79LWkrjOfzcs=')))
     root_key = base64.b64decode(b'oqOXI3/Mz/pKjCuFOt2eYxb7ndLq66GY')
     # This caveat has been generated from the go code
     # to check the compatibilty
     encrypted_cav = bakery.b64decode(
         'A_D-xlUf2MdGMgtu7OKRQnCP1OQJk6PKeFWRK26WIBA6DNwKGNLeFSkD2M-8A'
         'EYvmgVH95GWu7T7caKxKhhOQFcEKgnXKJvYXxz1zin4cZc4Q6C7gVqA-J4_j3'
         '1LX4VKxymqG62UGPo78wOv0_fKjr3OI6PPJOYOQgBMclemlRF2',
     )
     cav = bakery.decode_caveat(tp_key, encrypted_cav)
     self.assertEquals(cav, bakery.ThirdPartyCaveatInfo(
         condition='third party condition',
         first_party_public_key=fp_key.public_key,
         third_party_key_pair=tp_key,
         root_key=root_key,
         caveat=encrypted_cav,
         version=bakery.VERSION_3,
         id=None,
         namespace=bakery.legacy_namespace()
     ))
 def test_decode_caveat_v2_from_go(self):
     tp_key = bakery.PrivateKey(nacl.public.PrivateKey(
         base64.b64decode(
             'TSpvLpQkRj+T3JXnsW2n43n5zP/0X4zn0RvDiWC3IJ0=')))
     fp_key = bakery.PrivateKey(
         nacl.public.PrivateKey(base64.b64decode(
             'KXpsoJ9ujZYi/O2Cca6kaWh65MSawzy79LWkrjOfzcs=')))
     root_key = base64.b64decode('wh0HSM65wWHOIxoGjgJJOFvQKn2jJFhC')
     # This caveat has been generated from the go code
     # to check the compatibilty
     encrypted_cav = bakery.b64decode(
         'AvD-xlUf2MdGMgtu7OKRQnCP1OQJk6PKeFWRK26WIBA6DNwKGIHq9xGcHS9IZ'
         'Lh0cL6D9qpeKI0mXmCPfnwRQDuVYC8y5gVWd-oCGZaj5TGtk3byp2Vnw6ojmt'
         'sULDhY59YA_J_Y0ATkERO5T9ajoRWBxU2OXBoX6bImXA',
     )
     cav = bakery.decode_caveat(tp_key, encrypted_cav)
     self.assertEqual(cav, bakery.ThirdPartyCaveatInfo(
         condition='third party condition',
         first_party_public_key=fp_key.public_key,
         third_party_key_pair=tp_key,
         root_key=root_key,
         caveat=encrypted_cav,
         version=bakery.VERSION_2,
         id=None,
         namespace=bakery.legacy_namespace()
     ))