示例#1
0
        public void testImplicitSha256Digest()
        {
            Name name = new Name();

            ByteBuffer digest = toBuffer(new int[] { 0x28, 0xba, 0xd4, 0xb5, 0x27,
                    0x5b, 0xd3, 0x92, 0xdb, 0xb6, 0x70, 0xc7, 0x5c, 0xf0, 0xb6,
                    0x6f, 0x13, 0xf7, 0x94, 0x2b, 0x21, 0xe8, 0x0f, 0x55, 0xc0,
                    0xe8, 0x6b, 0x37, 0x47, 0x53, 0xa5, 0x48, 0x00, 0x00 });

            digest.limit(32);
            name.appendImplicitSha256Digest(new Blob(digest, true));
            name.appendImplicitSha256Digest(new Blob(digest, true)
                    .getImmutableArray());
            Assert.AssertEquals(name.get(0), name.get(1));

            digest.limit(34);
            bool gotError = true;
            try {
                name.appendImplicitSha256Digest(new Blob(digest, true));
                gotError = false;
            } catch (Exception ex) {
            }
            if (!gotError)
                Assert.Fail("Expected error in appendImplicitSha256Digest");

            digest.limit(30);
            gotError = true;
            try {
                name.appendImplicitSha256Digest(new Blob(digest, true));
                gotError = false;
            } catch (Exception ex_0) {
            }
            if (!gotError)
                Assert.Fail("Expected error in appendImplicitSha256Digest");

            // Add name.get(2) as a generic component.
            digest.limit(32);
            name.append(new Blob(digest, true));
            Assert.AssertTrue(name.get(0).compare(name.get(2)) < 0);
            Assert.AssertTrue(name.get(0).getValue().equals(name.get(2).getValue()));

            // Add name.get(3) as a generic component whose first byte is greater.
            digest.position(1);
            digest.limit(33);
            name.append(new Blob(digest, true));
            Assert.AssertTrue(name.get(0).compare(name.get(3)) < 0);

            Assert.AssertEquals(
                    "sha256digest="
                            + "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548",
                    name.get(0).toEscapedString());

            Assert.AssertEquals(true, name.get(0).isImplicitSha256Digest());
            Assert.AssertEquals(false, name.get(2).isImplicitSha256Digest());

            gotError = true;
            try {
                new Name("/hello/sha256digest=hmm");
                gotError = false;
            } catch (Exception ex_1) {
            }
            if (!gotError)
                Assert.Fail("Expected error in new Name from URI");

            // Check canonical URI encoding (lower case).
            Name name2 = new Name(
                    "/hello/sha256digest="
                            + "28bad4b5275bd392dbb670c75cf0b66f13f7942b21e80f55c0e86b374753a548");
            Assert.AssertEquals(name.get(0), name2.get(1));

            // Check that it will accept a hex value in upper case too.
            name2 = new Name(
                    "/hello/sha256digest="
                            + "28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548");
            Assert.AssertEquals(name.get(0), name2.get(1));

            // This is not valid sha256digest component. It should be treated as generic.
            name2 = new Name(
                    "/hello/SHA256DIGEST="
                            + "28BAD4B5275BD392DBB670C75CF0B66F13F7942B21E80F55C0E86B374753A548");
            Assert.AssertFalse(name.get(0).equals(name2.get(1)));
            Assert.AssertTrue(name2.get(1).isGeneric());
        }
示例#2
0
        /// <summary>
        /// Get the Data packet's full name, which includes the final
        /// ImplicitSha256Digest component based on the wire encoding for a particular
        /// wire format.
        /// </summary>
        ///
        /// <param name="wireFormat">A WireFormat object used to encode the Data packet.</param>
        /// <returns>The full name. You must not change the Name object - if you need
        /// to change it then make a copy.</returns>
        public Name getFullName(WireFormat wireFormat)
        {
            // The default full name depends on the default wire encoding.
            if (!getDefaultWireEncoding().isNull() && defaultFullName_.size() > 0
                    && getDefaultWireEncodingFormat() == wireFormat)
                // We already have a full name. A non-null default wire encoding means
                // that the Data packet fields have not changed.
                return defaultFullName_;

            Name fullName = new Name(getName());
            // wireEncode will use the cached encoding if possible.
            fullName.appendImplicitSha256Digest(net.named_data.jndn.util.Common.digestSha256(wireEncode(
                    wireFormat).buf()));

            if (wireFormat == net.named_data.jndn.encoding.WireFormat.getDefaultWireFormat())
                // wireEncode has already set defaultWireEncodingFormat_.
                defaultFullName_ = fullName;

            return fullName;
        }