コード例 #1
0
ファイル: test_data_methods.py プロジェクト: bboalimoe/PyNDN2
class TestDataDump(ut.TestCase):
    def setUp(self):
        self.credentials = CredentialStorage()
        self.freshData = self.createFreshData()
    
    def createFreshData(self):
        freshData = Data(Name("/ndn/abc"))
        freshData.setContent("SUCCESS!")
        freshData.getMetaInfo().setFreshnessPeriod(5000.0)
        freshData.getMetaInfo().setFinalBlockID(Name("/%00%09")[0])
    
        # Initialize the storage.
        return freshData



    def test_dump(self):
        data = Data()
        data.wireDecode(codedData)
        self.assertEqual(dumpData(data), initialDump, 'Initial dump does not have expected format')

    def test_encode_decode(self):
        data = Data()
        data.wireDecode(codedData)
        data.setContent(data.getContent())
        encoding = data.wireEncode()

        reDecodedData = Data()
        reDecodedData.wireDecode(encoding)
        self.assertEqual(dumpData(reDecodedData), initialDump, 'Re-decoded data does not match original dump')

    def test_empty_signature(self):
        # make sure nothing is set in the signature of newly created data
        data = Data()
        signature = data.getSignature()
        self.assertIsNone(signature.getKeyLocator().getType(), 'Key locator type on unsigned data should not be set')
        self.assertTrue(signature.getSignature().isNull(), 'Non-empty signature on unsigned data')

    def test_copy_fields(self):
        data = Data(self.freshData.getName())
        data.setContent(self.freshData.getContent())
        data.setMetaInfo(self.freshData.getMetaInfo())
        self.credentials.signData(data)
        freshDump = dumpData(data)
        self.assertTrue(dataDumpsEqual(freshDump, initialDump), 'Freshly created data does not match original dump')

    def test_verify(self):
        # we create 'mock' objects to replace callbacks
        # since we're not interested in the effect of the callbacks themselves
        failedCallback = Mock()
        verifiedCallback = Mock()

        self.credentials.signData(self.freshData)

        self.credentials.verifyData(self.freshData, verifiedCallback, failedCallback)
        self.assertEqual(failedCallback.call_count, 0, 'Signature verification failed')
        self.assertEqual(verifiedCallback.call_count, 1, 'Verification callback was not used.')
コード例 #2
0
ファイル: test_data_methods.py プロジェクト: bboalimoe/PyNDN2
 def setUp(self):
     self.credentials = CredentialStorage()
     self.freshData = self.createFreshData()
コード例 #3
0
 def setUp(self):
     self.credentials = CredentialStorage()
     self.freshData = self.createFreshData()
コード例 #4
0
class TestDataDump(ut.TestCase):
    def setUp(self):
        self.credentials = CredentialStorage()
        self.freshData = self.createFreshData()

    def createFreshData(self):
        freshData = Data(Name("/ndn/abc"))
        freshData.setContent("SUCCESS!")
        freshData.getMetaInfo().setFreshnessPeriod(5000.0)
        freshData.getMetaInfo().setFinalBlockId(Name("/%00%09")[0])

        # Initialize the storage.
        return freshData

    def test_dump(self):
        data = Data()
        data.wireDecode(codedData)
        self.assertEqual(dumpData(data), initialDump,
                         'Initial dump does not have expected format')

    def test_encode_decode(self):
        data = Data()
        data.wireDecode(codedData)
        data.setContent(data.getContent())
        encoding = data.wireEncode()

        reDecodedData = Data()
        reDecodedData.wireDecode(encoding)
        self.assertEqual(dumpData(reDecodedData), initialDump,
                         'Re-decoded data does not match original dump')

    def test_empty_signature(self):
        # make sure nothing is set in the signature of newly created data
        data = Data()
        signature = data.getSignature()
        self.assertIsNone(
            signature.getKeyLocator().getType(),
            'Key locator type on unsigned data should not be set')
        self.assertTrue(signature.getSignature().isNull(),
                        'Non-empty signature on unsigned data')

    def test_copy_fields(self):
        data = Data(self.freshData.getName())
        data.setContent(self.freshData.getContent())
        data.setMetaInfo(self.freshData.getMetaInfo())
        self.credentials.signData(data)
        freshDump = dumpData(data)
        self.assertTrue(dataDumpsEqual(freshDump, initialDump),
                        'Freshly created data does not match original dump')

    def test_verify(self):
        # we create 'mock' objects to replace callbacks
        # since we're not interested in the effect of the callbacks themselves
        failedCallback = Mock()
        verifiedCallback = Mock()

        self.credentials.signData(self.freshData)

        self.credentials.verifyData(self.freshData, verifiedCallback,
                                    failedCallback)
        self.assertEqual(failedCallback.call_count, 0,
                         'Signature verification failed')
        self.assertEqual(verifiedCallback.call_count, 1,
                         'Verification callback was not used.')

    def test_verify_digest_sha256(self):
        # We create 'mock' objects to replace callbacks since we're not
        # interested in the effect of the callbacks themselves.
        failedCallback = Mock()
        verifiedCallback = Mock()

        self.credentials.signDataWithSha256(self.freshData)

        self.credentials.verifyData(self.freshData, verifiedCallback,
                                    failedCallback)
        self.assertEqual(failedCallback.call_count, 0,
                         'Signature verification failed')
        self.assertEqual(verifiedCallback.call_count, 1,
                         'Verification callback was not used.')
コード例 #5
0
ファイル: test_data_methods.py プロジェクト: MAHIS/PyNDN2
class TestDataDump(ut.TestCase):
    def setUp(self):
        self.credentials = CredentialStorage()
        self.freshData = self.createFreshData()

    def createFreshData(self):
        freshData = Data(Name("/ndn/abc"))
        freshData.setContent("SUCCESS!")
        freshData.getMetaInfo().setFreshnessPeriod(5000.0)
        freshData.getMetaInfo().setFinalBlockId(Name("/%00%09")[0])

        # Initialize the storage.
        return freshData

    def test_dump(self):
        data = Data()
        data.wireDecode(codedData)
        self.assertEqual(dumpData(data), initialDump, 'Initial dump does not have expected format')

    def test_encode_decode(self):
        data = Data()
        data.wireDecode(codedData)
        data.setContent(data.getContent())
        encoding = data.wireEncode()

        reDecodedData = Data()
        reDecodedData.wireDecode(encoding)
        self.assertEqual(dumpData(reDecodedData), initialDump, 'Re-decoded data does not match original dump')

    def test_empty_signature(self):
        # make sure nothing is set in the signature of newly created data
        data = Data()
        signature = data.getSignature()
        self.assertIsNone(signature.getKeyLocator().getType(), 'Key locator type on unsigned data should not be set')
        self.assertTrue(signature.getSignature().isNull(), 'Non-empty signature on unsigned data')

    def test_copy_fields(self):
        data = Data(self.freshData.getName())
        data.setContent(self.freshData.getContent())
        data.setMetaInfo(self.freshData.getMetaInfo())
        self.credentials.signData(data)
        freshDump = dumpData(data)
        self.assertTrue(dataDumpsEqual(freshDump, initialDump), 'Freshly created data does not match original dump')

    def test_verify(self):
        # we create 'mock' objects to replace callbacks
        # since we're not interested in the effect of the callbacks themselves
        failedCallback = Mock()
        verifiedCallback = Mock()

        self.credentials.signData(self.freshData)

        self.credentials.verifyData(self.freshData, verifiedCallback, failedCallback)
        self.assertEqual(failedCallback.call_count, 0, 'Signature verification failed')
        self.assertEqual(verifiedCallback.call_count, 1, 'Verification callback was not used.')

    def test_verify_ecdsa(self):
        # we create 'mock' objects to replace callbacks
        # since we're not interested in the effect of the callbacks themselves
        failedCallback = Mock()
        verifiedCallback = Mock()

        self.credentials.signData(self.freshData, self.credentials.ecdsaCertName)

        self.credentials.verifyData(self.freshData, verifiedCallback, failedCallback)
        self.assertEqual(failedCallback.call_count, 0, 'Signature verification failed')
        self.assertEqual(verifiedCallback.call_count, 1, 'Verification callback was not used.')

    def test_verify_digest_sha256(self):
        # We create 'mock' objects to replace callbacks since we're not
        # interested in the effect of the callbacks themselves.
        failedCallback = Mock()
        verifiedCallback = Mock()

        self.credentials.signDataWithSha256(self.freshData)

        self.credentials.verifyData(self.freshData, verifiedCallback, failedCallback)
        self.assertEqual(failedCallback.call_count, 0, 'Signature verification failed')
        self.assertEqual(verifiedCallback.call_count, 1, 'Verification callback was not used.')

    def test_generic_signature(self):
        # Test correct encoding.
        signature = GenericSignature()
        signature.setSignatureInfoEncoding(
          Blob(experimentalSignatureInfo, False), None)
        signatureValue = Blob([1, 2, 3, 4], False)
        signature.setSignature(signatureValue)

        self.freshData.setSignature(signature)
        encoding = self.freshData.wireEncode()

        decodedData = Data()
        decodedData.wireDecode(encoding)

        decodedSignature = decodedData.getSignature()
        self.assertEqual(decodedSignature.getTypeCode(), experimentalSignatureType)
        self.assertTrue(Blob(experimentalSignatureInfo, False).equals
                        (decodedSignature.getSignatureInfoEncoding()))
        self.assertTrue(signatureValue.equals(decodedSignature.getSignature()))

        # Test bad encoding.
        signature = GenericSignature()
        signature.setSignatureInfoEncoding(
          Blob(experimentalSignatureInfoNoSignatureType, False), None)
        signature.setSignature(signatureValue)
        self.freshData.setSignature(signature)
        gotError = True
        try:
            self.freshData.wireEncode()
            gotError = False
        except:
            pass
        if not gotError:
          self.fail("Expected encoding error for experimentalSignatureInfoNoSignatureType")

        signature = GenericSignature()
        signature.setSignatureInfoEncoding(
          Blob(experimentalSignatureInfoBadTlv, False), None)
        signature.setSignature(signatureValue)
        self.freshData.setSignature(signature)
        gotError = True
        try:
            self.freshData.wireEncode()
            gotError = False
        except:
            pass
        if not gotError:
          self.fail("Expected encoding error for experimentalSignatureInfoBadTlv")
コード例 #6
0
class TestDataDump(ut.TestCase):
    def setUp(self):
        self.credentials = CredentialStorage()
        self.freshData = self.createFreshData()

    def createFreshData(self):
        freshData = Data(Name("/ndn/abc"))
        freshData.setContent("SUCCESS!")
        freshData.getMetaInfo().setFreshnessPeriod(5000.0)
        freshData.getMetaInfo().setFinalBlockId(Name("/%00%09")[0])

        # Initialize the storage.
        return freshData

    def test_dump(self):
        data = Data()
        data.wireDecode(codedData)
        self.assertEqual(dumpData(data), initialDump,
                         'Initial dump does not have expected format')

    def test_encode_decode(self):
        data = Data()
        data.wireDecode(codedData)
        data.setContent(data.getContent())
        encoding = data.wireEncode()

        reDecodedData = Data()
        reDecodedData.wireDecode(encoding)
        self.assertEqual(dumpData(reDecodedData), initialDump,
                         'Re-decoded data does not match original dump')

    def test_empty_signature(self):
        # make sure nothing is set in the signature of newly created data
        data = Data()
        signature = data.getSignature()
        self.assertIsNone(
            signature.getKeyLocator().getType(),
            'Key locator type on unsigned data should not be set')
        self.assertTrue(signature.getSignature().isNull(),
                        'Non-empty signature on unsigned data')

    def test_copy_fields(self):
        data = Data(self.freshData.getName())
        data.setContent(self.freshData.getContent())
        data.setMetaInfo(self.freshData.getMetaInfo())
        self.credentials.signData(data)
        freshDump = dumpData(data)
        self.assertTrue(dataDumpsEqual(freshDump, initialDump),
                        'Freshly created data does not match original dump')

    def test_verify(self):
        # we create 'mock' objects to replace callbacks
        # since we're not interested in the effect of the callbacks themselves
        failedCallback = Mock()
        verifiedCallback = Mock()

        self.credentials.signData(self.freshData)

        self.credentials.verifyData(self.freshData, verifiedCallback,
                                    failedCallback)
        self.assertEqual(failedCallback.call_count, 0,
                         'Signature verification failed')
        self.assertEqual(verifiedCallback.call_count, 1,
                         'Verification callback was not used.')

    def test_verify_ecdsa(self):
        # we create 'mock' objects to replace callbacks
        # since we're not interested in the effect of the callbacks themselves
        failedCallback = Mock()
        verifiedCallback = Mock()

        self.credentials.signData(self.freshData,
                                  self.credentials.ecdsaCertName)

        self.credentials.verifyData(self.freshData, verifiedCallback,
                                    failedCallback)
        self.assertEqual(failedCallback.call_count, 0,
                         'Signature verification failed')
        self.assertEqual(verifiedCallback.call_count, 1,
                         'Verification callback was not used.')

    def test_verify_digest_sha256(self):
        # We create 'mock' objects to replace callbacks since we're not
        # interested in the effect of the callbacks themselves.
        failedCallback = Mock()
        verifiedCallback = Mock()

        self.credentials.signDataWithSha256(self.freshData)

        self.credentials.verifyData(self.freshData, verifiedCallback,
                                    failedCallback)
        self.assertEqual(failedCallback.call_count, 0,
                         'Signature verification failed')
        self.assertEqual(verifiedCallback.call_count, 1,
                         'Verification callback was not used.')

    def test_generic_signature(self):
        # Test correct encoding.
        signature = GenericSignature()
        signature.setSignatureInfoEncoding(
            Blob(experimentalSignatureInfo, False), None)
        signatureValue = Blob([1, 2, 3, 4], False)
        signature.setSignature(signatureValue)

        self.freshData.setSignature(signature)
        encoding = self.freshData.wireEncode()

        decodedData = Data()
        decodedData.wireDecode(encoding)

        decodedSignature = decodedData.getSignature()
        self.assertEqual(decodedSignature.getTypeCode(),
                         experimentalSignatureType)
        self.assertTrue(
            Blob(experimentalSignatureInfo,
                 False).equals(decodedSignature.getSignatureInfoEncoding()))
        self.assertTrue(signatureValue.equals(decodedSignature.getSignature()))

        # Test bad encoding.
        signature = GenericSignature()
        signature.setSignatureInfoEncoding(
            Blob(experimentalSignatureInfoNoSignatureType, False), None)
        signature.setSignature(signatureValue)
        self.freshData.setSignature(signature)
        gotError = True
        try:
            self.freshData.wireEncode()
            gotError = False
        except:
            pass
        if not gotError:
            self.fail(
                "Expected encoding error for experimentalSignatureInfoNoSignatureType"
            )

        signature = GenericSignature()
        signature.setSignatureInfoEncoding(
            Blob(experimentalSignatureInfoBadTlv, False), None)
        signature.setSignature(signatureValue)
        self.freshData.setSignature(signature)
        gotError = True
        try:
            self.freshData.wireEncode()
            gotError = False
        except:
            pass
        if not gotError:
            self.fail(
                "Expected encoding error for experimentalSignatureInfoBadTlv")

    def test_full_name(self):
        data = Data()
        data.wireDecode(codedData)

        # Check the full name format.
        self.assertEqual(data.getFullName().size(), data.getName().size() + 1)
        self.assertEqual(data.getName(), data.getFullName().getPrefix(-1))
        self.assertEqual(data.getFullName().get(-1).getValue().size(), 32)

        # Check the independent digest calculation.
        sha256 = hashes.Hash(hashes.SHA256(), backend=default_backend())
        sha256.update(Blob(codedData).toBytes())
        newDigest = Blob(bytearray(sha256.finalize()), False)
        self.assertTrue(newDigest.equals(
            data.getFullName().get(-1).getValue()))

        # Check the expected URI.
        self.assertEqual(
            data.getFullName().toUri(), "/ndn/abc/sha256digest=" +
            "96556d685dcb1af04be4ae57f0e7223457d4055ea9b3d07c0d337bef4a8b3ee9")

        # Changing the Data packet should change the full name.
        saveFullName = Name(data.getFullName())
        data.setContent(Blob())
        self.assertNotEqual(data.getFullName().get(-1), saveFullName.get(-1))
コード例 #7
0
class TestDataDump(ut.TestCase):
    def setUp(self):
        self.credentials = CredentialStorage()
        self.freshData = self.createFreshData()

    def createFreshData(self):
        freshData = Data(Name("/ndn/abc"))
        freshData.setContent("SUCCESS!")
        freshData.getMetaInfo().setFreshnessPeriod(5000.0)
        freshData.getMetaInfo().setFinalBlockId(Name("/%00%09")[0])

        # Initialize the storage.
        return freshData

    def test_dump(self):
        data = Data()
        data.wireDecode(codedData)
        self.assertEqual(dumpData(data), initialDump, 'Initial dump does not have expected format')

    def test_encode_decode(self):
        data = Data()
        data.wireDecode(codedData)
        data.setContent(data.getContent())
        encoding = data.wireEncode()

        reDecodedData = Data()
        reDecodedData.wireDecode(encoding)
        self.assertEqual(dumpData(reDecodedData), initialDump, 'Re-decoded data does not match original dump')

    def test_empty_signature(self):
        # make sure nothing is set in the signature of newly created data
        data = Data()
        signature = data.getSignature()
        self.assertIsNone(signature.getKeyLocator().getType(), 'Key locator type on unsigned data should not be set')
        self.assertTrue(signature.getSignature().isNull(), 'Non-empty signature on unsigned data')

    def test_copy_fields(self):
        data = Data(self.freshData.getName())
        data.setContent(self.freshData.getContent())
        data.setMetaInfo(self.freshData.getMetaInfo())
        self.credentials.signData(data)
        freshDump = dumpData(data)
        self.assertTrue(dataDumpsEqual(freshDump, initialDump), 'Freshly created data does not match original dump')

    def test_verify(self):
        # we create 'mock' objects to replace callbacks
        # since we're not interested in the effect of the callbacks themselves
        failedCallback = Mock()
        verifiedCallback = Mock()

        self.credentials.signData(self.freshData)

        self.credentials.verifyData(self.freshData, verifiedCallback, failedCallback)
        self.assertEqual(failedCallback.call_count, 0, 'Signature verification failed')
        self.assertEqual(verifiedCallback.call_count, 1, 'Verification callback was not used.')

    def test_verify_ecdsa(self):
        # we create 'mock' objects to replace callbacks
        # since we're not interested in the effect of the callbacks themselves
        failedCallback = Mock()
        verifiedCallback = Mock()

        self.credentials.signData(self.freshData, self.credentials.ecdsaCertName)

        self.credentials.verifyData(self.freshData, verifiedCallback, failedCallback)
        self.assertEqual(failedCallback.call_count, 0, 'Signature verification failed')
        self.assertEqual(verifiedCallback.call_count, 1, 'Verification callback was not used.')

    def test_verify_digest_sha256(self):
        # We create 'mock' objects to replace callbacks since we're not
        # interested in the effect of the callbacks themselves.
        failedCallback = Mock()
        verifiedCallback = Mock()

        self.credentials.signDataWithSha256(self.freshData)

        self.credentials.verifyData(self.freshData, verifiedCallback, failedCallback)
        self.assertEqual(failedCallback.call_count, 0, 'Signature verification failed')
        self.assertEqual(verifiedCallback.call_count, 1, 'Verification callback was not used.')

    def test_generic_signature(self):
        # Test correct encoding.
        signature = GenericSignature()
        signature.setSignatureInfoEncoding(
          Blob(experimentalSignatureInfo, False), None)
        signatureValue = Blob([1, 2, 3, 4], False)
        signature.setSignature(signatureValue)

        self.freshData.setSignature(signature)
        encoding = self.freshData.wireEncode()

        decodedData = Data()
        decodedData.wireDecode(encoding)

        decodedSignature = decodedData.getSignature()
        self.assertEqual(decodedSignature.getTypeCode(), experimentalSignatureType)
        self.assertTrue(Blob(experimentalSignatureInfo, False).equals
                        (decodedSignature.getSignatureInfoEncoding()))
        self.assertTrue(signatureValue.equals(decodedSignature.getSignature()))

        # Test bad encoding.
        signature = GenericSignature()
        signature.setSignatureInfoEncoding(
          Blob(experimentalSignatureInfoNoSignatureType, False), None)
        signature.setSignature(signatureValue)
        self.freshData.setSignature(signature)
        gotError = True
        try:
            self.freshData.wireEncode()
            gotError = False
        except:
            pass
        if not gotError:
          self.fail("Expected encoding error for experimentalSignatureInfoNoSignatureType")

        signature = GenericSignature()
        signature.setSignatureInfoEncoding(
          Blob(experimentalSignatureInfoBadTlv, False), None)
        signature.setSignature(signatureValue)
        self.freshData.setSignature(signature)
        gotError = True
        try:
            self.freshData.wireEncode()
            gotError = False
        except:
            pass
        if not gotError:
          self.fail("Expected encoding error for experimentalSignatureInfoBadTlv")

    def test_full_name(self):
        data = Data()
        data.wireDecode(codedData)

        # Check the full name format.
        self.assertEqual(data.getFullName().size(), data.getName().size() + 1)
        self.assertEqual(data.getName(), data.getFullName().getPrefix(-1))
        self.assertEqual(data.getFullName().get(-1).getValue().size(), 32)

        # Check the independent digest calculation.
        sha256 = hashes.Hash(hashes.SHA256(), backend=default_backend())
        sha256.update(Blob(codedData).toBytes())
        newDigest = Blob(bytearray(sha256.finalize()), False)
        self.assertTrue(newDigest.equals(data.getFullName().get(-1).getValue()))

        # Check the expected URI.
        self.assertEqual(
          data.getFullName().toUri(), "/ndn/abc/sha256digest=" +
            "96556d685dcb1af04be4ae57f0e7223457d4055ea9b3d07c0d337bef4a8b3ee9")

        # Changing the Data packet should change the full name.
        saveFullName = Name(data.getFullName())
        data.setContent(Blob())
        self.assertNotEqual(data.getFullName().get(-1), saveFullName.get(-1))