directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From plusplusjia...@apache.org
Subject directory-kerby git commit: DIRKRB-525 Encode and decode XDR: Boolean and Integer types. Contributed by Yan.
Date Thu, 14 Jan 2016 02:25:02 GMT
Repository: directory-kerby
Updated Branches:
  refs/heads/kadmin-remote a0c088108 -> 5f54df40c


DIRKRB-525 Encode and decode XDR: Boolean and Integer types. Contributed by Yan.


Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo
Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/5f54df40
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/5f54df40
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/5f54df40

Branch: refs/heads/kadmin-remote
Commit: 5f54df40ccea4ed12465e44ff1e40cba860d6424
Parents: a0c0881
Author: plusplusjiajia <jiajia.li@intel.com>
Authored: Thu Jan 14 10:31:45 2016 +0800
Committer: plusplusjiajia <jiajia.li@intel.com>
Committed: Thu Jan 14 10:31:45 2016 +0800

----------------------------------------------------------------------
 .../apache/kerby/xdr/type/AbstractXdrType.java  | 14 ++--
 .../org/apache/kerby/xdr/type/XdrBoolean.java   | 37 ++++++++--
 .../org/apache/kerby/xdr/type/XdrBytes.java     | 12 ++++
 .../org/apache/kerby/xdr/type/XdrInteger.java   | 54 ++++++++++++--
 .../org/apache/kerby/xdr/type/XdrSimple.java    | 39 +++++++++--
 .../apache/kerby/xdr/type/XdrStructType.java    |  2 +-
 .../org/apache/kerby/xdr/XdrBooleanTest.java    | 37 ++++++++++
 .../org/apache/kerby/xdr/XdrIntegerTest.java    | 74 ++++++++++++++++++++
 8 files changed, 245 insertions(+), 24 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/5f54df40/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/AbstractXdrType.java
----------------------------------------------------------------------
diff --git a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/AbstractXdrType.java b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/AbstractXdrType.java
index 2b956a0..7e03c85 100644
--- a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/AbstractXdrType.java
+++ b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/AbstractXdrType.java
@@ -67,11 +67,10 @@ public abstract class AbstractXdrType<T> implements XdrType {
 
     @Override
     public void encode(ByteBuffer buffer) throws IOException {
-
+        encodeBody(buffer);
     }
 
-    protected void encodeBody(ByteBuffer buffer) throws IOException {
-    }
+    protected abstract void encodeBody(ByteBuffer buffer) throws IOException;
 
     @Override
     public void decode(byte[] content) throws IOException {
@@ -79,19 +78,14 @@ public abstract class AbstractXdrType<T> implements XdrType {
     }
 
     @Override
-    public int encodingLength() {
-        return -1;
-    }
-
-    protected int encodingHeaderLength() throws IOException {
-        return -1;
+    public int encodingLength() throws IOException {
+        return encodingBodyLength();
     }
 
     protected abstract int encodingBodyLength() throws IOException;
 
     @Override
     public void decode(ByteBuffer content) throws IOException {
-
     }
 
     public T getValue() {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/5f54df40/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBoolean.java
----------------------------------------------------------------------
diff --git a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBoolean.java b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBoolean.java
index b6d8d04..8ed8e91 100644
--- a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBoolean.java
+++ b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBoolean.java
@@ -21,12 +21,16 @@ package org.apache.kerby.xdr.type;
 
 import org.apache.kerby.xdr.XdrDataType;
 
+import java.io.IOException;
+import java.util.Arrays;
+
 /**
- * ASN1 Boolean type
+ * Xdr Boolean type from RFC 4506
+ * Boolean type has the same representation as signed integers.
  */
 public class XdrBoolean extends XdrSimple<Boolean> {
-    private static final byte[] TRUE_BYTE = new byte[] {(byte) 0xff};
-    private static final byte[] FALSE_BYTE = new byte[] {(byte) 0x00};
+    private static final byte[] TRUE_BYTE = new byte[] {(byte) 0x00, (byte) 0x00, (byte)
0x00, (byte) 0x01};
+    private static final byte[] FALSE_BYTE = new byte[] {(byte) 0x00, (byte) 0x00, (byte)
0x00, (byte) 0x00};
 
     public static final XdrBoolean TRUE = new XdrBoolean(true);
     public static final XdrBoolean FALSE = new XdrBoolean(false);
@@ -46,13 +50,38 @@ public class XdrBoolean extends XdrSimple<Boolean> {
         super(XdrDataType.BOOLEAN, value);
     }
 
+    /**
+     * The length of a signed integer is 4.
+     * @return Length of a boolean type.
+     */
     @Override
     protected int encodingBodyLength() {
-        return 1;
+        return 4;
     }
 
+    /**
+     * Encode boolean type to bytes.
+     */
     @Override
     protected void toBytes() {
         setBytes(getValue() ? TRUE_BYTE : FALSE_BYTE);
     }
+
+    /**
+     * Decode bytes to boolean value.
+     * @throws IOException Wrong bytes for boolean.
+     */
+    @Override
+    protected void toValue() throws IOException {
+        byte[] bytes = getBytes();
+        if (Arrays.equals(bytes, TRUE_BYTE)) {
+            setValue(true);
+        }
+        else if (Arrays.equals(bytes, FALSE_BYTE)) {
+            setValue(false);
+        }
+        else {
+            throw new IOException("Fail to decode boolean type: " + bytes.toString());
+        }
+    }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/5f54df40/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBytes.java
----------------------------------------------------------------------
diff --git a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBytes.java b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBytes.java
index dcceb91..073b962 100644
--- a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBytes.java
+++ b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBytes.java
@@ -21,6 +21,8 @@ package org.apache.kerby.xdr.type;
 
 import org.apache.kerby.xdr.XdrDataType;
 
+import java.io.IOException;
+
 public class XdrBytes extends XdrSimple<byte[]> {
     private int padding;
 
@@ -31,4 +33,14 @@ public class XdrBytes extends XdrSimple<byte[]> {
     public XdrBytes(byte[] value) {
         super(XdrDataType.BYTES, value);
     }
+
+    @Override
+    protected void toValue() throws IOException {
+
+    }
+
+    @Override
+    protected void toBytes() {
+
+    }
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/5f54df40/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrInteger.java
----------------------------------------------------------------------
diff --git a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrInteger.java b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrInteger.java
index e1099a4..fb78242 100644
--- a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrInteger.java
+++ b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrInteger.java
@@ -20,10 +20,23 @@
 package org.apache.kerby.xdr.type;
 
 import org.apache.kerby.xdr.XdrDataType;
-
-import java.io.IOException;
 import java.math.BigInteger;
 
+/**
+ * Xdr Integer type from RFC 4506
+ * An XDR signed integer is a 32-bit datum
+ * that encodes an integer in the range [-2147483648,2147483647].
+ * The integer is represented in two's complement notation.
+ * The most and least significant bytes are0 and 3, respectively.
+ * Integers are declared as follows:
+ * int identifier;
+ *
+ *      (MSB)                   (LSB)
+ *      +-------+-------+-------+-------+
+ *      |byte 0 |byte 1 |byte 2 |byte 3 |
+ *      +-------+-------+-------+-------+
+ *      <------------32 bits------------>
+ */
 public class XdrInteger extends XdrSimple<BigInteger> {
     public XdrInteger() {
         this((BigInteger) null);
@@ -41,11 +54,44 @@ public class XdrInteger extends XdrSimple<BigInteger> {
         super(XdrDataType.INTEGER, value);
     }
 
+    /**
+     * The length of a signed integer is 4.
+     * @return Length of a boolean type.
+     */
+    @Override
+    protected int encodingBodyLength() {
+        return 4; /**Length of XdrInteger is fixed as 4 bytes*/
+    }
+
+    /**
+     * Encode Integer type to bytes.
+     * Cannot only use toByteArray() because of fixed 4 bytes length.
+     */
+    @Override
     protected void toBytes() {
-        setBytes(getValue().toByteArray());
+        BigInteger bi = getValue();
+        byte[] complement = new byte[4];
+        byte[] array = bi.toByteArray(); /**array[0] has full 8 bits with top bits are signed
bits*/
+        int length = array.length;
+
+        /**fit for top bytes*/
+        for (int i = 0; i < 4 - length; i++) {
+            complement[i] = (byte) (array[0] >> (8 * (4 - length - i)));
+        }
+        /**copy lower bytes*/
+        for (int i = 0; i < length; i++) {
+            complement[4 - length + i] = array[i];
+        }
+
+        setBytes(complement);
     }
 
-    protected void toValue() throws IOException {
+    /**
+     * Decode bytes to Integer value.
+     */
+    @Override
+    protected void toValue() {
         setValue(new BigInteger(getBytes()));
     }
+
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/5f54df40/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrSimple.java
----------------------------------------------------------------------
diff --git a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrSimple.java b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrSimple.java
index b9526bd..1b46cf9 100644
--- a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrSimple.java
+++ b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrSimple.java
@@ -25,7 +25,9 @@ import java.io.IOException;
 import java.nio.ByteBuffer;
 
 /**
- * ASN1 simple type, of single value other than complex type of multiple values.
+ * Xdr simple type, of single value other than complex type of multiple values.
+ * Including: Bytes, Integer, Boolean, String.
+ * Use toBytes() for encoding, toValue() for decoding.
  */
 public abstract class XdrSimple<T> extends AbstractXdrType<T> {
     private byte[] bytes;
@@ -57,11 +59,15 @@ public abstract class XdrSimple<T> extends AbstractXdrType<T>
{
 
     protected byte[] encodeBody() {
         if (bytes == null) {
-            toBytes();
+            toBytes();  /**Terminal step for encoding all the simple type to bytes.*/
         }
         return bytes;
     }
 
+    /**
+     * Put encoded bytes into buffer.
+     * @param buffer ByteBuffer to hold encoded bytes.
+     */
     @Override
     protected void encodeBody(ByteBuffer buffer) {
         byte[] body = encodeBody();
@@ -70,20 +76,43 @@ public abstract class XdrSimple<T> extends AbstractXdrType<T>
{
         }
     }
 
+    /**
+     * Length including null bytes to maintain an multiple of 4.
+     * @return
+     */
     @Override
     protected int encodingBodyLength() {
         if (getValue() == null) {
             return 0;
         }
         if (bytes == null) {
-            toBytes();
+            toBytes(); /**Terminal step for decoding all the simple type to bytes.*/
         }
         return bytes.length;
     }
 
-    protected void toValue() throws IOException { }
+    @Override
+    public void decode(ByteBuffer content) throws IOException {
+        decodeBody(content);
+    }
+
+    protected void decodeBody(ByteBuffer body) throws IOException {
+        byte[] result = body.array();
+        if (result.length > 0) {
+            setBytes(result);
+            toValue(); /**Terminal step for decoding all the bytes into simple types.*/
+        }
+    }
 
-    protected void toBytes() { }
+    /**
+     * Decode bytes to simple value.
+     */
+    protected abstract void toValue() throws IOException;
+
+    /**
+     * Encode simple type to bytes.
+     */
+    protected abstract void toBytes();
 
     public static boolean isSimple(XdrDataType dataType) {
         switch (dataType) {

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/5f54df40/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrStructType.java
----------------------------------------------------------------------
diff --git a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrStructType.java b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrStructType.java
index 584e84a..bea0788 100644
--- a/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrStructType.java
+++ b/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrStructType.java
@@ -42,7 +42,7 @@ public abstract class XdrStructType extends AbstractXdrType<XdrStructType>
{
     }
 
     @Override
-    protected int encodingBodyLength() {
+    protected int encodingBodyLength() throws IOException {
         int allLen = 0;
         for (int i = 0; i < fields.length; ++i) {
             AbstractXdrType field = (AbstractXdrType) fields[i];

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/5f54df40/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrBooleanTest.java
----------------------------------------------------------------------
diff --git a/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrBooleanTest.java b/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrBooleanTest.java
index 003403b..ff0b0fb 100644
--- a/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrBooleanTest.java
+++ b/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrBooleanTest.java
@@ -19,6 +19,43 @@
  */
 package org.apache.kerby.xdr;
 
+import org.apache.kerby.xdr.type.XdrBoolean;
+import org.apache.kerby.xdr.util.HexUtil;
+import org.junit.Test;
+
+import java.io.IOException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
 public class XdrBooleanTest {
+    @Test
+    public void testEncoding() throws IOException {
+        testEncodingWith(true, "0x00 00 00 01");
+        testEncodingWith(false, "0x00 00 00 00");
+        //what about undefined codeBytes?
+    }
+
+    private void testEncodingWith(Boolean value, String expectedEncoding) throws IOException
{
+        byte[] expected = HexUtil.hex2bytesFriendly(expectedEncoding);
+        XdrBoolean aValue = new XdrBoolean(value);
+
+        byte[] encodingBytes = aValue.encode();
+        assertThat(encodingBytes).isEqualTo(expected);
+    }
+
+
+    @Test
+    public void testDecoding() throws IOException {
+        testDecodingWith(true, "0x00 00 00 01");
+        testDecodingWith(false, "0x00 00 00 00");
+        //what about undefined codeBytes?
+    }
+
+    private void testDecodingWith(Boolean expectedValue, String content) throws IOException
{
+        XdrBoolean decoded = new XdrBoolean();
+
+        decoded.decode(HexUtil.hex2bytesFriendly(content));
+        assertThat(decoded.getValue()).isEqualTo(expectedValue);
+    }
 
 }

http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/5f54df40/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrIntegerTest.java
----------------------------------------------------------------------
diff --git a/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrIntegerTest.java b/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrIntegerTest.java
new file mode 100644
index 0000000..731e70a
--- /dev/null
+++ b/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrIntegerTest.java
@@ -0,0 +1,74 @@
+/**
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ *
+ */
+package org.apache.kerby.xdr;
+
+import org.apache.kerby.xdr.type.XdrInteger;
+import org.apache.kerby.xdr.util.HexUtil;
+import org.junit.Test;
+
+import java.io.IOException;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class XdrIntegerTest {
+    @Test
+    public void testEncoding() throws IOException {
+        testEncodingWith(0, "0x00 00 00 00");
+        testEncodingWith(1, "0x00 00 00 01");
+        testEncodingWith(2, "0x00 00 00 02");
+        testEncodingWith(127, "0x00 00 00 7F");
+        testEncodingWith(128, "0x00 00 00 80");
+        testEncodingWith(-1, "0xFF FF FF FF");
+        testEncodingWith(-127, "0xFF FF FF 81");
+        testEncodingWith(-255, "0xFF FF FF 01");
+        testEncodingWith(-32768, "0xFF FF 80 00");
+        testEncodingWith(1234567890, "0x49 96 02 D2");
+    }
+
+    private void testEncodingWith(int value, String expectedEncoding) throws IOException
{
+        byte[] expected = HexUtil.hex2bytesFriendly(expectedEncoding);
+        XdrInteger aValue = new XdrInteger(value);
+
+        byte[] encodingBytes = aValue.encode();
+        assertThat(encodingBytes).isEqualTo(expected);
+    }
+
+
+    @Test
+    public void testDecoding() throws IOException {
+        testDecodingWith(0, "0x00 00 00 00");
+        testDecodingWith(1, "0x00 00 00 01");
+        testDecodingWith(2, "0x00 00 00 02");
+        testDecodingWith(127, "0x00 00 00 7F");
+        testDecodingWith(128, "0x00 00 00 80");
+        testDecodingWith(-1, "0xFF FF FF FF");
+        testDecodingWith(-127, "0xFF FF FF 81");
+        testDecodingWith(-255, "0xFF FF FF 01");
+        testDecodingWith(-32768, "0xFF FF 80 00");
+        testDecodingWith(1234567890, "0x49 96 02 D2");
+    }
+
+    private void testDecodingWith(int expectedValue, String content) throws IOException {
+        XdrInteger decoded = new XdrInteger();
+
+        decoded.decode(HexUtil.hex2bytesFriendly(content));
+        assertThat(decoded.getValue().intValue()).isEqualTo(expectedValue);
+    }
+}


Mime
View raw message