You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by pl...@apache.org on 2016/01/19 08:36:19 UTC
directory-kerby git commit: DIRKRB-527 Encode and decode XDR: String
and Enumerated. Contributed by Yan.
Repository: directory-kerby
Updated Branches:
refs/heads/kadmin-remote 40782bd5c -> a3d540d4a
DIRKRB-527 Encode and decode XDR: String and Enumerated. 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/a3d540d4
Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/a3d540d4
Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/a3d540d4
Branch: refs/heads/kadmin-remote
Commit: a3d540d4a7969f771adbdb1c42b22c94f9eb4b60
Parents: 40782bd
Author: plusplusjiajia <ji...@intel.com>
Authored: Tue Jan 19 15:43:14 2016 +0800
Committer: plusplusjiajia <ji...@intel.com>
Committed: Tue Jan 19 15:43:14 2016 +0800
----------------------------------------------------------------------
.../apache/kerby/xdr/type/XdrEnumerated.java | 2 +-
.../org/apache/kerby/xdr/type/XdrString.java | 24 ++++----
.../apache/kerby/xdr/XdrEnumeratedInstance.java | 56 ++++++++++++++++++
.../org/apache/kerby/xdr/XdrEnumeratedTest.java | 58 +++++++++++++++++++
.../org/apache/kerby/xdr/XdrStringTest.java | 60 ++++++++++++++++++++
5 files changed, 189 insertions(+), 11 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a3d540d4/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrEnumerated.java
----------------------------------------------------------------------
diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrEnumerated.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrEnumerated.java
index f589064..5193ef2 100644
--- a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrEnumerated.java
+++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrEnumerated.java
@@ -35,7 +35,7 @@ public abstract class XdrEnumerated<T extends EnumType> extends XdrSimple<T> {
/**
* Constructor with a value, generally for encoding of the value
- * @param value The boolean value
+ * @param value The Enum value
*/
public XdrEnumerated(T value) {
super(XdrDataType.ENUM, value);
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a3d540d4/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrString.java
----------------------------------------------------------------------
diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrString.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrString.java
index 8874b33..26ec37c 100644
--- a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrString.java
+++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrString.java
@@ -24,6 +24,7 @@ import org.apache.kerby.xdr.XdrDataType;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
+import java.nio.ByteBuffer;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
@@ -39,7 +40,7 @@ import java.util.List;
* |<----n+r (where (n+r) mod 4 = 0)---->|
* STRING
*/
-public abstract class XdrString extends XdrSimple<String> {
+public class XdrString extends XdrSimple<String> {
private int padding;
public XdrString(XdrDataType xdrDataType) {
@@ -53,13 +54,13 @@ public abstract class XdrString extends XdrSimple<String> {
@Override
protected void toBytes() {
if (getValue() != null) {
- byte[] bytes = new byte[encodingBodyLength()];
- int length = bytes.length - padding;
+ byte[] bytes = new byte[encodingBodyLength()]; /**Default value of byte is 0. So we don't have to initialize it with 0*/
+ int length = bytes.length - padding - 4;
bytes[0] = (byte) (length >> 24);
bytes[1] = (byte) (length >> 16);
bytes[2] = (byte) (length >> 8);
bytes[3] = (byte) (length);
- System.arraycopy(getValue(), 0, bytes, 4, bytes.length - 4);
+ System.arraycopy(getValue().getBytes(), 0, bytes, 4, length);
setBytes(bytes);
}
}
@@ -67,23 +68,26 @@ public abstract class XdrString extends XdrSimple<String> {
@Override
protected int encodingBodyLength() {
if (getValue() != null) {
- return getValue().length() + 4;
+ padding = 4 - getValue().length() % 4;
+ return getValue().length() + padding + 4;
}
return 0;
}
protected void toValue() throws IOException {
byte[] bytes = getBytes();
-
- int paddingBytes = bytes.length - bytes[0];
+ byte[] header = new byte[4];
+ System.arraycopy(bytes, 0, header, 0, 4);
+ int StringLen = ByteBuffer.wrap(header).getInt();
+ int paddingBytes = bytes.length - StringLen - 4;
validatePaddingBytes(paddingBytes);
setPadding(paddingBytes);
- byte[] newBytes = new byte[bytes.length - 4];
+ byte[] content = new byte[StringLen];
if (bytes.length > 1) {
- System.arraycopy(bytes, 4, newBytes, 0, bytes.length - 4);
+ System.arraycopy(bytes, 4, content, 0, StringLen);
}
- setValue(new String(newBytes, StandardCharsets.US_ASCII));
+ setValue(new String(content, StandardCharsets.US_ASCII));
}
public void setPadding(int padding) {
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a3d540d4/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrEnumeratedInstance.java
----------------------------------------------------------------------
diff --git a/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrEnumeratedInstance.java b/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrEnumeratedInstance.java
new file mode 100644
index 0000000..a0611d2
--- /dev/null
+++ b/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrEnumeratedInstance.java
@@ -0,0 +1,56 @@
+/**
+ * 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.XdrEnumerated;
+
+enum Color implements EnumType {
+ RED(2),
+ YELLOW(3),
+ BLUE(5);
+ int value;
+ private Color(int value) {
+ this.value = value;
+ }
+
+ public int getValue() {
+ return value;
+ }
+
+ public String getName() {
+ return name();
+ }
+}
+
+public class XdrEnumeratedInstance extends XdrEnumerated<Color> {
+
+ public XdrEnumeratedInstance() {
+ super(null);
+ }
+
+ public XdrEnumeratedInstance(Color value) {
+ super(value);
+ }
+ @Override
+ protected EnumType[] getAllEnumValues() {
+ return Color.values();
+ }
+
+}
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a3d540d4/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrEnumeratedTest.java
----------------------------------------------------------------------
diff --git a/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrEnumeratedTest.java b/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrEnumeratedTest.java
new file mode 100644
index 0000000..788cd3f
--- /dev/null
+++ b/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrEnumeratedTest.java
@@ -0,0 +1,58 @@
+/**
+ * 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.XdrEnumerated;
+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 XdrEnumeratedTest {
+ @Test
+ public void testEncoding() throws IOException {
+ testEncodingWith(Color.RED, "0x00 00 00 02");
+ testEncodingWith(Color.YELLOW, "0x00 00 00 03");
+ testEncodingWith(Color.BLUE, "0x00 00 00 05");
+ }
+
+ private void testEncodingWith(Color value, String expectedEncoding) throws IOException {
+ byte[] expected = HexUtil.hex2bytesFriendly(expectedEncoding);
+ XdrEnumerated aValue = new XdrEnumeratedInstance(value);
+
+ byte[] encodingBytes = aValue.encode();
+ assertThat(encodingBytes).isEqualTo(expected);
+ }
+
+
+ @Test
+ public void testDecoding() throws IOException {
+ testDecodingWith(Color.RED, "0x00 00 00 02");
+ testDecodingWith(Color.YELLOW, "0x00 00 00 03");
+ testDecodingWith(Color.BLUE, "0x00 00 00 05");
+ }
+
+ private void testDecodingWith(Color expectedValue, String content) throws IOException {
+ XdrEnumerated decoded = new XdrEnumeratedInstance();
+ decoded.decode(HexUtil.hex2bytesFriendly(content));
+ assertThat(decoded.getValue()).isEqualTo(expectedValue);
+ }
+}
http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/a3d540d4/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrStringTest.java
----------------------------------------------------------------------
diff --git a/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrStringTest.java b/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrStringTest.java
new file mode 100644
index 0000000..c799d5a
--- /dev/null
+++ b/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrStringTest.java
@@ -0,0 +1,60 @@
+/**
+ * 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.XdrString;
+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 XdrStringTest {
+ @Test
+ public void testEncoding() throws IOException {
+ testEncodingWith("Hello, Kerby!", "0X00 00 00 0D 48 65 6C 6C 6F 2C 20 4B 65 72 62 79 21 00 00 00");
+ testEncodingWith("sillyprog", "0X00 00 00 09 73 69 6C 6C 79 70 72 6F 67 00 00 00");
+ testEncodingWith("(quit)", "0X00 00 00 06 28 71 75 69 74 29 00 00");
+ }
+
+ private void testEncodingWith(String value, String expectedEncoding) throws IOException {
+ byte[] expected = HexUtil.hex2bytesFriendly(expectedEncoding);
+ XdrString aValue = new XdrString(XdrDataType.STRING, value);
+
+ byte[] encodingBytes = aValue.encode();
+ assertThat(encodingBytes).isEqualTo(expected);
+ }
+
+
+ @Test
+ public void testDecoding() throws IOException {
+ testDecodingWith("Hello, Kerby!", "0X00 00 00 0D 48 65 6C 6C 6F 2C 20 4B 65 72 62 79 21 00 00 00");
+ testDecodingWith("sillyprog", "0X00 00 00 09 73 69 6c 6c 79 70 72 6f 67 00 00 00");
+ testDecodingWith("(quit)", "0X00 00 00 06 28 71 75 69 74 29 00 00");
+ }
+
+ private void testDecodingWith(String expectedValue, String content) throws IOException {
+ XdrString decoded = new XdrString(XdrDataType.STRING);
+
+ decoded.decode(HexUtil.hex2bytesFriendly(content));
+ assertThat(decoded.getValue()).isEqualTo(expectedValue);
+ }
+}