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))
Beispiel #2
0
    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))
    def readDataAndCreateManifests(self, filename, groupSize, keepData):
        if groupSize < 1:
            raise RuntimeError("Group size cannot be less than 1")

        self.allChunks = [
        ]  # for holding the generated data packets, including unsigned manifests
        self.allManifests = [
        ]  # for storing first unsigned manifest packets, which are then signed in-place
        self.rawDataCount = 0
        self.ndnChunkCount = 0

        seqNo = 0  # sequence number of data packets
        chunkNo = 0  # number of the chunk in the group

        with open(filename, 'rb') as f:

            # prepare space to store all manifests of the group (last manifest will not use all the space)
            def allocateBufferForDigests():
                return bytearray(groupSize * SHA256_DIGEST_SIZE)

            digests = allocateBufferForDigests()

            while f.readable():
                chunkPayload = f.read(self._maxSegmentPayloadLength)
                if len(chunkPayload) == 0:
                    break
                self.rawDataCount = self.rawDataCount + len(chunkPayload)

                chunk = Data(
                    Name("/icn2019/test/data").appendSequenceNumber(seqNo))
                seqNo = seqNo + 1
                chunk.setContent(chunkPayload)

                digestSignature = DigestSha256Signature()
                digestSignature.setSignature(
                    Blob(bytearray(SHA256_DIGEST_SIZE))
                )  # not real a valid signature, but ok for the experiment
                chunk.setSignature(digestSignature)

                if keepData:
                    self.allChunks.append(chunk)

                # only data chunks; manifest sizes counted separatedly, as they are signed
                self.ndnChunkCount = self.ndnChunkCount + chunk.wireEncode(
                ).size()

                # For storing data packet to a file
                # with open(writepath + "-1.txt", "wb") as dataf
                #     dataf.write(dpacket_bytes)

                implicitDigest = chunk.getFullName()[-1].getValue()

                offset = chunkNo * SHA256_DIGEST_SIZE
                digests[offset:offset +
                        SHA256_DIGEST_SIZE] = implicitDigest.toBytes()[:]

                chunkNo = chunkNo + 1

                if chunkNo == groupSize:
                    manifest = self._createManifest(
                        Name("/icn2019/test/data").appendSequenceNumber(seqNo),
                        digests, groupSize)  # full group
                    seqNo = seqNo + 1
                    self.allChunks.append(manifest)
                    self.allManifests.append(manifest)
                    chunkNo = 0
                    digests = allocateBufferForDigests()

            if chunkNo != 0:
                manifest = self._createManifest(
                    Name("/icn2019/test/data").appendSequenceNumber(seqNo),
                    digests, groupSize)  # partial group
                self.allChunks.append(manifest)
                self.allManifests.append(manifest)

            self.nDataChunks = seqNo - len(
                self.allManifests
            )  # number of data packets, excluding the manifests
Beispiel #4
0
    def setObject(self, namespace, obj, useSignatureManifest=False):
        """
        Segment the object and create child segment packets of the given Namespace.

        :param Namespace namespace: The Namespace to append segment packets to.
          This ignores the Namespace from setNamespace().
        :param Blob obj: The object to segment.
        :param bool useSignatureManifest: (optional) If True, only use a
          DigestSha256Signature on the segment packets and create a signed
          _manifest packet as a child of the given Namespace. If omitted or
          False, sign each segment packet individually.
        """
        keyChain = namespace._getKeyChain()
        if keyChain == None:
            raise RuntimeError(
                "SegmentStreamHandler.setObject: There is no KeyChain")

        # Get the final block ID.
        finalSegment = 0
        # Instead of a brute calculation, imitate the loop we will use below.
        segment = 0
        offset = 0
        while offset < obj.size():
            finalSegment = segment
            segment += 1
            offset += self._maxSegmentPayloadLength
        finalBlockId = Name().appendSegment(finalSegment)[0]

        SHA256_DIGEST_SIZE = 32
        if useSignatureManifest:
            # Get ready to save the segment implicit digests.
            manifestContent = bytearray(
                (finalSegment + 1) * SHA256_DIGEST_SIZE)

            # Use a DigestSha256Signature with all zeros.
            digestSignature = DigestSha256Signature()
            digestSignature.setSignature(Blob(bytearray(SHA256_DIGEST_SIZE)))

        segment = 0
        offset = 0
        while offset < obj.size():
            payloadLength = self._maxSegmentPayloadLength
            if offset + payloadLength > obj.size():
                payloadLength = obj.size() - offset

            # Make the Data packet.
            segmentNamespace = namespace[Name.Component.fromSegment(segment)]
            data = Data(segmentNamespace.getName())

            metaInfo = namespace._getNewDataMetaInfo()
            if metaInfo != None:
                # Start with a copy of the provided MetaInfo.
                data.setMetaInfo(metaInfo)
            data.getMetaInfo().setFinalBlockId(finalBlockId)

            data.setContent(obj.toBytes()[offset:offset + payloadLength])

            if useSignatureManifest:
                data.setSignature(digestSignature)

                # Append the implicit digest to the manifestContent.
                implicitDigest = data.getFullName()[-1].getValue()
                digestOffset = segment * SHA256_DIGEST_SIZE
                manifestContent[digestOffset:digestOffset + SHA256_DIGEST_SIZE] = \
                  implicitDigest.toBytes()[:]
            else:
                keyChain.sign(data)

            segmentNamespace.setData(data)

            segment += 1
            offset += self._maxSegmentPayloadLength

        if useSignatureManifest:
            # Create the _manifest data packet.
            namespace[self.NAME_COMPONENT_MANIFEST].serializeObject(
                Blob(manifestContent))

        # TODO: Do this in a canSerialize callback from Namespace.serializeObject?
        namespace._setObject(obj)