예제 #1
0
        /// <summary>
        /// Check if the given Data packet can satisfy this Interest. This method
        /// considers the Name, MinSuffixComponents, MaxSuffixComponents,
        /// PublisherPublicKeyLocator, and Exclude. It does not consider the
        /// ChildSelector or MustBeFresh. This uses the given wireFormat to get the
        /// Data packet encoding for the full Name.
        /// </summary>
        ///
        /// <param name="data">The Data packet to check.</param>
        /// <param name="wireFormat"></param>
        /// <returns>True if the given Data packet can satisfy this Interest.</returns>
        public bool matchesData(Data data, WireFormat wireFormat)
        {
            // Imitate ndn-cxx Interest::matchesData.
            int interestNameLength = getName().size();
            Name dataName = data.getName();
            int fullNameLength = dataName.size() + 1;

            // Check MinSuffixComponents.
            bool hasMinSuffixComponents = getMinSuffixComponents() >= 0;
            int minSuffixComponents = (hasMinSuffixComponents) ? getMinSuffixComponents()
                    : 0;
            if (!(interestNameLength + minSuffixComponents <= fullNameLength))
                return false;

            // Check MaxSuffixComponents.
            bool hasMaxSuffixComponents = getMaxSuffixComponents() >= 0;
            if (hasMaxSuffixComponents
                    && !(interestNameLength + getMaxSuffixComponents() >= fullNameLength))
                return false;

            // Check the prefix.
            if (interestNameLength == fullNameLength) {
                if (getName().get(-1).isImplicitSha256Digest()) {
                    if (!getName().equals(data.getFullName(wireFormat)))
                        return false;
                } else
                    // The Interest Name is the same length as the Data full Name, but the
                    //   last component isn't a digest so there's no possibility of matching.
                    return false;
            } else {
                // The Interest Name should be a strict prefix of the Data full Name,
                if (!getName().isPrefixOf(dataName))
                    return false;
            }

            // Check the Exclude.
            // The Exclude won't be violated if the Interest Name is the same as the
            //   Data full Name.
            if (getExclude().size() > 0 && fullNameLength > interestNameLength) {
                if (interestNameLength == fullNameLength - 1) {
                    // The component to exclude is the digest.
                    if (getExclude().matches(
                            data.getFullName(wireFormat).get(interestNameLength)))
                        return false;
                } else {
                    // The component to exclude is not the digest.
                    if (getExclude().matches(dataName.get(interestNameLength)))
                        return false;
                }
            }

            // Check the KeyLocator.
            KeyLocator publisherPublicKeyLocator = getKeyLocator();
            if (publisherPublicKeyLocator.getType() != net.named_data.jndn.KeyLocatorType.NONE) {
                Signature signature = data.getSignature();
                if (!net.named_data.jndn.KeyLocator.canGetFromSignature(signature))
                    // No KeyLocator in the Data packet.
                    return false;
                if (!publisherPublicKeyLocator.equals(net.named_data.jndn.KeyLocator
                        .getFromSignature(signature)))
                    return false;
            }

            return true;
        }
예제 #2
0
        public void testFullName()
        {
            Data data = new Data();
            try {
                data.wireDecode(codedData);
            } catch (EncodingException ex) {
                Assert.Fail("Can't decode codedData");
            }

            // Check the full name format.
            Assert.AssertEquals(data.getName().size() + 1, data.getFullName().size());
            Assert.AssertEquals(data.getName(), data.getFullName().getPrefix(-1));
            Assert.AssertEquals(32, data.getFullName().get(-1).getValue().size());

            // Check the independent digest calculation.
            Blob newDigest = new Blob(net.named_data.jndn.util.Common.digestSha256(codedData));
            Assert.AssertTrue(newDigest.equals(data.getFullName().get(-1).getValue()));

            // Check the expected URI.
            Assert.AssertEquals(
                    "/ndn/abc/sha256digest="
                            + "96556d685dcb1af04be4ae57f0e7223457d4055ea9b3d07c0d337bef4a8b3ee9",
                    data.getFullName().toUri());

            // Changing the Data packet should change the full name.
            Name saveFullName = new Name(data.getFullName());
            data.setContent(new Blob());
            Assert.AssertFalse(data.getFullName().get(-1).equals(saveFullName.get(-1)));
        }
예제 #3
0
        /// <summary>
        /// Check if the given Data packet can satisfy this Interest. This method
        /// considers the Name, MinSuffixComponents, MaxSuffixComponents,
        /// PublisherPublicKeyLocator, and Exclude. It does not consider the
        /// ChildSelector or MustBeFresh. This uses the given wireFormat to get the
        /// Data packet encoding for the full Name.
        /// </summary>
        ///
        /// <param name="data">The Data packet to check.</param>
        /// <param name="wireFormat"></param>
        /// <returns>True if the given Data packet can satisfy this Interest.</returns>
        public bool matchesData(Data data, WireFormat wireFormat)
        {
            // Imitate ndn-cxx Interest::matchesData.
            int  interestNameLength = getName().size();
            Name dataName           = data.getName();
            int  fullNameLength     = dataName.size() + 1;

            // Check MinSuffixComponents.
            bool hasMinSuffixComponents = getMinSuffixComponents() >= 0;
            int  minSuffixComponents    = (hasMinSuffixComponents) ? getMinSuffixComponents()
                                        : 0;

            if (!(interestNameLength + minSuffixComponents <= fullNameLength))
            {
                return(false);
            }

            // Check MaxSuffixComponents.
            bool hasMaxSuffixComponents = getMaxSuffixComponents() >= 0;

            if (hasMaxSuffixComponents &&
                !(interestNameLength + getMaxSuffixComponents() >= fullNameLength))
            {
                return(false);
            }

            // Check the prefix.
            if (interestNameLength == fullNameLength)
            {
                if (getName().get(-1).isImplicitSha256Digest())
                {
                    if (!getName().equals(data.getFullName(wireFormat)))
                    {
                        return(false);
                    }
                }
                else
                {
                    // The Interest Name is the same length as the Data full Name, but the
                    //   last component isn't a digest so there's no possibility of matching.
                    return(false);
                }
            }
            else
            {
                // The Interest Name should be a strict prefix of the Data full Name,
                if (!getName().isPrefixOf(dataName))
                {
                    return(false);
                }
            }

            // Check the Exclude.
            // The Exclude won't be violated if the Interest Name is the same as the
            //   Data full Name.
            if (getExclude().size() > 0 && fullNameLength > interestNameLength)
            {
                if (interestNameLength == fullNameLength - 1)
                {
                    // The component to exclude is the digest.
                    if (getExclude().matches(
                            data.getFullName(wireFormat).get(interestNameLength)))
                    {
                        return(false);
                    }
                }
                else
                {
                    // The component to exclude is not the digest.
                    if (getExclude().matches(dataName.get(interestNameLength)))
                    {
                        return(false);
                    }
                }
            }

            // Check the KeyLocator.
            KeyLocator publisherPublicKeyLocator = getKeyLocator();

            if (publisherPublicKeyLocator.getType() != net.named_data.jndn.KeyLocatorType.NONE)
            {
                Signature signature = data.getSignature();
                if (!net.named_data.jndn.KeyLocator.canGetFromSignature(signature))
                {
                    // No KeyLocator in the Data packet.
                    return(false);
                }
                if (!publisherPublicKeyLocator.equals(net.named_data.jndn.KeyLocator
                                                      .getFromSignature(signature)))
                {
                    return(false);
                }
            }

            return(true);
        }