Return-Path: X-Original-To: archive-asf-public-internal@cust-asf2.ponee.io Delivered-To: archive-asf-public-internal@cust-asf2.ponee.io Received: from cust-asf.ponee.io (cust-asf.ponee.io [163.172.22.183]) by cust-asf2.ponee.io (Postfix) with ESMTP id 3EFA7200B41 for ; Thu, 7 Jul 2016 08:41:53 +0200 (CEST) Received: by cust-asf.ponee.io (Postfix) id 3D684160A59; Thu, 7 Jul 2016 06:41:53 +0000 (UTC) Delivered-To: archive-asf-public@cust-asf.ponee.io Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by cust-asf.ponee.io (Postfix) with SMTP id BDCF3160A68 for ; Thu, 7 Jul 2016 08:41:50 +0200 (CEST) Received: (qmail 71368 invoked by uid 500); 7 Jul 2016 06:41:49 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 69632 invoked by uid 99); 7 Jul 2016 06:41:47 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 07 Jul 2016 06:41:47 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id D583CE0844; Thu, 7 Jul 2016 06:41:46 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: plusplusjiajia@apache.org To: commits@directory.apache.org Date: Thu, 07 Jul 2016 06:42:07 -0000 Message-Id: <075d77bf6299470da13086eae22b274f@git.apache.org> In-Reply-To: References: X-Mailer: ASF-Git Admin Mailer Subject: [22/27] directory-kerby git commit: DIRKRB-592 Merge kadmin-remote branch to trunk. archived-at: Thu, 07 Jul 2016 06:41:53 -0000 DIRKRB-592 Merge kadmin-remote branch to trunk. Project: http://git-wip-us.apache.org/repos/asf/directory-kerby/repo Commit: http://git-wip-us.apache.org/repos/asf/directory-kerby/commit/9f628e5a Tree: http://git-wip-us.apache.org/repos/asf/directory-kerby/tree/9f628e5a Diff: http://git-wip-us.apache.org/repos/asf/directory-kerby/diff/9f628e5a Branch: refs/heads/kpasswd Commit: 9f628e5ae9b5a10c8ee7b33fcde6fe67ed7624bc Parents: 708456f Author: plusplusjiajia Authored: Wed Jul 6 11:38:02 2016 +0800 Committer: plusplusjiajia Committed: Wed Jul 6 11:38:02 2016 +0800 ---------------------------------------------------------------------- kerby-common/kerby-asn1/pom.xml | 3 - .../java/org/apache/kerby/asn1/EnumType.java | 2 +- kerby-common/kerby-xdr/pom.xml | 29 ++ .../java/org/apache/kerby/xdr/EnumType.java | 37 ++ .../java/org/apache/kerby/xdr/XdrDataType.java | 55 +++ .../java/org/apache/kerby/xdr/XdrFieldInfo.java | 53 +++ .../apache/kerby/xdr/type/AbstractXdrType.java | 100 +++++ .../org/apache/kerby/xdr/type/XdrBoolean.java | 94 +++++ .../org/apache/kerby/xdr/type/XdrBytes.java | 45 ++ .../apache/kerby/xdr/type/XdrEnumerated.java | 66 +++ .../org/apache/kerby/xdr/type/XdrInteger.java | 85 ++++ .../org/apache/kerby/xdr/type/XdrSimple.java | 132 ++++++ .../org/apache/kerby/xdr/type/XdrString.java | 346 ++++++++++++++++ .../apache/kerby/xdr/type/XdrStructType.java | 99 +++++ .../java/org/apache/kerby/xdr/type/XdrType.java | 62 +++ .../org/apache/kerby/xdr/type/XdrUnion.java | 131 ++++++ .../kerby/xdr/type/XdrUnsignedInteger.java | 100 +++++ .../java/org/apache/kerby/xdr/util/HexUtil.java | 113 +++++ .../java/org/apache/kerby/xdr/util/IOUtil.java | 109 +++++ .../java/org/apache/kerby/xdr/util/Utf8.java | 34 ++ .../java/org/apache/kerby/xdr/util/XdrUtil.java | 26 ++ .../java/org/apache/kerby/xdr/TestUtil.java | 47 +++ .../org/apache/kerby/xdr/XdrBooleanTest.java | 60 +++ .../apache/kerby/xdr/XdrEnumeratedInstance.java | 56 +++ .../org/apache/kerby/xdr/XdrEnumeratedTest.java | 58 +++ .../org/apache/kerby/xdr/XdrIntegerTest.java | 80 ++++ .../org/apache/kerby/xdr/XdrStringTest.java | 59 +++ .../apache/kerby/xdr/XdrStructTypeInstance.java | 111 +++++ .../org/apache/kerby/xdr/XdrStructTypeTest.java | 83 ++++ .../org/apache/kerby/xdr/XdrUnionInstance.java | 170 ++++++++ .../java/org/apache/kerby/xdr/XdrUnionTest.java | 67 +++ .../kerby/xdr/XdrUnsignedIntegerTest.java | 69 ++++ kerby-common/pom.xml | 1 + kerby-dist/kdc-dist/bin/admin-server.cmd | 32 ++ kerby-dist/kdc-dist/bin/admin-server.sh | 32 ++ kerby-dist/kdc-dist/bin/remote-admin-client.cmd | 32 ++ kerby-dist/kdc-dist/bin/remote-admin-client.sh | 32 ++ kerby-dist/kdc-dist/conf/adminClient.conf | 23 ++ kerby-dist/kdc-dist/conf/adminServer.conf | 23 ++ kerby-dist/kdc-dist/pom.xml | 10 + .../kerby/kerberos/kdc/KerbyKdcServer.java | 4 +- kerby-kerb/kerb-admin-server/pom.xml | 56 +++ .../kerb/admin/server/KerbyAdminServer.java | 80 ++++ .../kerb/admin/server/kadmin/AdminServer.java | 266 ++++++++++++ .../admin/server/kadmin/AdminServerConfig.java | 105 +++++ .../server/kadmin/AdminServerConfigKey.java | 59 +++ .../admin/server/kadmin/AdminServerContext.java | 52 +++ .../admin/server/kadmin/AdminServerHandler.java | 238 +++++++++++ .../admin/server/kadmin/AdminServerOption.java | 52 +++ .../admin/server/kadmin/AdminServerSetting.java | 212 ++++++++++ .../admin/server/kadmin/AdminServerUtil.java | 165 ++++++++ .../impl/AbstractInternalAdminServer.java | 116 ++++++ .../kadmin/impl/DefaultAdminServerHandler.java | 199 +++++++++ .../impl/DefaultInternalAdminServerImpl.java | 80 ++++ .../server/kadmin/impl/InternalAdminServer.java | 60 +++ .../src/main/resources/adminServer.conf | 20 + kerby-kerb/kerb-admin/pom.xml | 5 + .../kerby/kerberos/kerb/admin/AdminHelper.java | 308 -------------- .../kerby/kerberos/kerb/admin/AuthUtil.java | 141 +++++++ .../kerby/kerberos/kerb/admin/Kadmin.java | 207 ---------- .../kerby/kerberos/kerb/admin/KadminOption.java | 76 ---- .../kerby/kerberos/kerb/admin/KadminServer.java | 144 ------- .../kerby/kerberos/kerb/admin/Krb5Conf.java | 86 ++++ .../kerby/kerberos/kerb/admin/LocalKadmin.java | 87 ---- .../kerberos/kerb/admin/LocalKadminImpl.java | 400 ------------------ .../kerb/admin/RemoteAdminClientTool.java | 263 ++++++++++++ .../kerberos/kerb/admin/RemoteKadminImpl.java | 144 ------- .../kerberos/kerb/admin/kadmin/Kadmin.java | 207 ++++++++++ .../kerb/admin/kadmin/KadminOption.java | 76 ++++ .../kerb/admin/kadmin/local/AdminHelper.java | 309 ++++++++++++++ .../kerb/admin/kadmin/local/LocalKadmin.java | 88 ++++ .../admin/kadmin/local/LocalKadminImpl.java | 407 +++++++++++++++++++ .../kerb/admin/kadmin/remote/AdminClient.java | 204 ++++++++++ .../kerb/admin/kadmin/remote/AdminConfig.java | 132 ++++++ .../admin/kadmin/remote/AdminConfigKey.java | 58 +++ .../kerb/admin/kadmin/remote/AdminContext.java | 49 +++ .../kerb/admin/kadmin/remote/AdminHandler.java | 162 ++++++++ .../kerb/admin/kadmin/remote/AdminOption.java | 102 +++++ .../kerb/admin/kadmin/remote/AdminSetting.java | 129 ++++++ .../kerb/admin/kadmin/remote/AdminUtil.java | 127 ++++++ .../admin/kadmin/remote/RemoteKadminImpl.java | 207 ++++++++++ .../command/RemoteAddPrincipalCommand.java | 65 +++ .../kadmin/remote/command/RemoteCommand.java | 41 ++ .../command/RemoteDeletePrincipalCommand.java | 83 ++++ .../remote/command/RemoteGetprincsCommand.java | 65 +++ .../remote/command/RemotePrintUsageCommand.java | 42 ++ .../command/RemoteRenamePrincipalCommand.java | 85 ++++ .../impl/AbstractInternalAdminClient.java | 71 ++++ .../kadmin/remote/impl/DefaultAdminHandler.java | 79 ++++ .../remote/impl/DefaultInternalAdminClient.java | 71 ++++ .../kadmin/remote/impl/InternalAdminClient.java | 41 ++ .../remote/request/AddPrincipalRequest.java | 114 ++++++ .../kadmin/remote/request/AdminRequest.java | 63 +++ .../remote/request/DeletePrincipalRequest.java | 70 ++++ .../kadmin/remote/request/GetprincsRequest.java | 70 ++++ .../remote/request/RenamePrincipalRequest.java | 74 ++++ .../kerb/admin/message/AddPrincipalRep.java | 30 ++ .../kerb/admin/message/AddPrincipalReq.java | 30 ++ .../kerb/admin/message/AdminMessage.java | 56 +++ .../kerb/admin/message/AdminMessageCode.java | 90 ++++ .../kerb/admin/message/AdminMessageEnum.java | 41 ++ .../kerb/admin/message/AdminMessageType.java | 73 ++++ .../kerberos/kerb/admin/message/AdminRep.java | 33 ++ .../kerberos/kerb/admin/message/AdminReq.java | 34 ++ .../kerb/admin/message/DeletePrincipalRep.java | 30 ++ .../kerb/admin/message/DeletePrincipalReq.java | 30 ++ .../kerb/admin/message/GetprincsRep.java | 26 ++ .../kerb/admin/message/GetprincsReq.java | 26 ++ .../kerberos/kerb/admin/message/KadminCode.java | 63 +++ .../kerb/admin/message/RenamePrincipalRep.java | 29 ++ .../kerb/admin/message/RenamePrincipalReq.java | 29 ++ .../kerby/kerberos/kerb/admin/KadminTest.java | 24 -- .../kerberos/kerb/transport/KdcNetwork.java | 1 - .../kerby/kerberos/kerb/server/KdcSetting.java | 2 +- .../kerberos/kerb/server/ServerSetting.java | 35 ++ .../kerberos/kerb/server/SimpleKdcServer.java | 4 +- kerby-kerb/pom.xml | 1 + kerby-tool/kdc-tool/pom.xml | 6 + .../kerby/kerberos/tool/kadmin/KadminTool.java | 6 +- .../kerby/kerberos/tool/kadmin/ToolUtil.java | 2 +- .../kadmin/command/AddPrincipalCommand.java | 4 +- .../kadmin/command/AddPrincipalsCommand.java | 4 +- .../kadmin/command/ChangePasswordCommand.java | 4 +- .../kadmin/command/DeletePrincipalCommand.java | 4 +- .../kadmin/command/GetPrincipalCommand.java | 2 +- .../tool/kadmin/command/KadminCommand.java | 2 +- .../tool/kadmin/command/KeytabAddCommand.java | 2 +- .../kadmin/command/KeytabRemoveCommand.java | 4 +- .../kadmin/command/ListPrincipalCommand.java | 2 +- .../kadmin/command/ModifyPrincipalCommand.java | 4 +- .../kadmin/command/RenamePrincipalCommand.java | 7 +- .../kerberos/tool/kdcinit/KdcInitTool.java | 19 +- 132 files changed, 9015 insertions(+), 1426 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-asn1/pom.xml ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-asn1/pom.xml b/kerby-common/kerby-asn1/pom.xml index 8b1e23c..26dbef8 100644 --- a/kerby-common/kerby-asn1/pom.xml +++ b/kerby-common/kerby-asn1/pom.xml @@ -26,7 +26,4 @@ Kerby ASN1 Project Kerby ASN1 Project - - - http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-asn1/src/main/java/org/apache/kerby/asn1/EnumType.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-asn1/src/main/java/org/apache/kerby/asn1/EnumType.java b/kerby-common/kerby-asn1/src/main/java/org/apache/kerby/asn1/EnumType.java index e0166ec..5b9a65f 100644 --- a/kerby-common/kerby-asn1/src/main/java/org/apache/kerby/asn1/EnumType.java +++ b/kerby-common/kerby-asn1/src/main/java/org/apache/kerby/asn1/EnumType.java @@ -20,7 +20,7 @@ package org.apache.kerby.asn1; /** - * A helper interface used by Asn1Enumerated. + * A helper interface for enum types. * * @author Apache Directory Project */ http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/pom.xml ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/pom.xml b/kerby-common/kerby-xdr/pom.xml new file mode 100644 index 0000000..d6ad54c --- /dev/null +++ b/kerby-common/kerby-xdr/pom.xml @@ -0,0 +1,29 @@ + + + + + + org.apache.kerby + kerby-common + 1.0.0-RC3-SNAPSHOT + + + 4.0.0 + + kerby-xdr + Kerby XDR Project + Kerby XDR Project + + http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/EnumType.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/EnumType.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/EnumType.java new file mode 100644 index 0000000..0936863 --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/EnumType.java @@ -0,0 +1,37 @@ +/** + * 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; + +/** + * A helper interface for enum types. + * + * @author Apache Directory Project + */ +public interface EnumType { + /** + * @return the Enum element value + */ + int getValue(); + + /** + * @return The enum element name + */ + String getName(); +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/XdrDataType.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/XdrDataType.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/XdrDataType.java new file mode 100644 index 0000000..34bc014 --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/XdrDataType.java @@ -0,0 +1,55 @@ +/** + * 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; + +/** + * An enumeration for every XDR type. + * + * @author Apache Directory Project + */ +public enum XdrDataType { + UNKNOWN (-1), + BOOLEAN (0x01), + INTEGER (0x02), + BYTES (0x03), + STRING (0X04), + ENUM (0x05), + OPAQUE (0x06), + UNSIGNED_INTEGER (0x07), + STRUCT (0x08), + UNION (0x09); + + /** The dataType value */ + private int value; + + /** + * Create an instance of this class + */ + XdrDataType(int value) { + this.value = value; + } + + /** + * @return The associated dataType value + */ + public int getValue() { + return value; + } +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/XdrFieldInfo.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/XdrFieldInfo.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/XdrFieldInfo.java new file mode 100644 index 0000000..2ab727c --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/XdrFieldInfo.java @@ -0,0 +1,53 @@ +/** + * 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; + +/** + * Representing a field in a XDR struct. + */ +public class XdrFieldInfo { + private int index; + private XdrDataType dataType; + private Object value; + + /** + * Constructor. + * @param index + * @param dataType + * + */ + public XdrFieldInfo(int index, XdrDataType dataType, Object value) { + this.index = index; + this.dataType = dataType; + this.value = value; + } + + public int getIndex() { + return index; + } + + public XdrDataType getDataType() { + return dataType; + } + + public Object getValue() { + return value; + } +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/AbstractXdrType.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/AbstractXdrType.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/AbstractXdrType.java new file mode 100644 index 0000000..68facec --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/AbstractXdrType.java @@ -0,0 +1,100 @@ +/** + * 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.type; + +import org.apache.kerby.xdr.XdrDataType; + +import java.io.IOException; +import java.nio.ByteBuffer; + +/** + * The abstract XDR type for all the XDR types. It provides basic + * encoding and decoding utilities. + * + * @param the type of the value encoded/decoded or wrapped by this + */ +public abstract class AbstractXdrType implements XdrType { + private XdrDataType dataType; + + // The wrapped real value. + private T value; + + /** + * Default constructor. + * @param dataType the dataType + * @param value the value + */ + public AbstractXdrType(XdrDataType dataType, T value) { + this(dataType); + this.value = value; + } + + /** + * Default constructor. + * @param dataType the dataType + */ + public AbstractXdrType(XdrDataType dataType) { + this.dataType = dataType; + } + + @Override + public byte[] encode() throws IOException { + int len = encodingLength(); + ByteBuffer byteBuffer = ByteBuffer.allocate(len); + encode(byteBuffer); + byteBuffer.flip(); + return byteBuffer.array(); + } + + @Override + public void encode(ByteBuffer buffer) throws IOException { + encodeBody(buffer); + } + + protected abstract void encodeBody(ByteBuffer buffer) throws IOException; + + @Override + public void decode(byte[] content) throws IOException { + decode(ByteBuffer.wrap(content)); + } + + @Override + public int encodingLength() throws IOException { + return encodingBodyLength(); + } + + protected abstract int encodingBodyLength() throws IOException; + + @Override + public void decode(ByteBuffer content) throws IOException { + } + + public T getValue() { + return value; + } + + public void setValue(T value) { + this.value = value; + } + + public XdrDataType getDataType() { + return dataType; + } +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBoolean.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBoolean.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBoolean.java new file mode 100644 index 0000000..e8e092f --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBoolean.java @@ -0,0 +1,94 @@ +/** + * 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.type; + +import org.apache.kerby.xdr.XdrDataType; + +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Arrays; + +/** + * Xdr Boolean type from RFC 4506 + * Boolean type has the same representation as signed integers. + */ +public class XdrBoolean extends XdrSimple { + 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); + + /** + * Default constructor, generally for decoding as a container + */ + public XdrBoolean() { + this(null); + } + + /** + * Constructor with a value, generally for encoding of the value + * @param value The boolean value + */ + public XdrBoolean(Boolean value) { + super(XdrDataType.BOOLEAN, value); + } + + /** + * The length of a signed integer is 4. + * @return Length of a boolean type. + */ + @Override + protected int encodingBodyLength() { + 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 { + if (getBytes().length != 4) { + byte[] boolBytes = ByteBuffer.allocate(4).put(getBytes(), 0, 4).array(); + /**reset bytes in case the enum type is in a struct or union*/ + setBytes(boolBytes); + } + + 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/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBytes.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBytes.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBytes.java new file mode 100644 index 0000000..105ff74 --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrBytes.java @@ -0,0 +1,45 @@ +/** + * 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.type; + +import org.apache.kerby.xdr.XdrDataType; + +import java.io.IOException; + +public class XdrBytes extends XdrSimple { + + public XdrBytes() { + this(null); + } + + 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/9f628e5a/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 new file mode 100644 index 0000000..e04b484 --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrEnumerated.java @@ -0,0 +1,66 @@ +/** + * 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.type; + +import org.apache.kerby.xdr.EnumType; +import org.apache.kerby.xdr.XdrDataType; + +import java.math.BigInteger; +import java.nio.ByteBuffer; + +public abstract class XdrEnumerated extends XdrSimple { + /** + * Default constructor, generally for decoding as a container + */ + public XdrEnumerated() { + this(null); + } + + /** + * Constructor with a value, generally for encoding of the value + * @param value The Enum value + */ + public XdrEnumerated(T value) { + super(XdrDataType.ENUM, value); + } + + protected void toBytes() { + byte[] bytes = ByteBuffer.allocate(4).putInt(getValue().getValue()).array(); + setBytes(bytes); + } + + protected void toValue() { + if (getBytes().length != 4) { + byte[] intBytes = ByteBuffer.allocate(4).put(getBytes(), 0, 4).array(); + /**reset bytes in case the enum type is in a struct or union*/ + setBytes(intBytes); + } + BigInteger biVal = new BigInteger(getBytes()); + int iVal = biVal.intValue(); + EnumType[] allValues = getAllEnumValues(); + for (EnumType val : allValues) { + if (val.getValue() == iVal) { + setValue((T) val); + } + } + } + + protected abstract EnumType[] getAllEnumValues(); +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrInteger.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrInteger.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrInteger.java new file mode 100644 index 0000000..478cf80 --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrInteger.java @@ -0,0 +1,85 @@ +/** + * 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.type; + +import org.apache.kerby.xdr.XdrDataType; +import java.nio.ByteBuffer; + +/** + * 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 { + public XdrInteger() { + this((Integer) null); + } + + public XdrInteger(Integer value) { + super(XdrDataType.INTEGER, value); + } + + /** + * The length of a signed integer is 4. + * @return Length of a signed integer 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() { + int value = getValue().intValue(); + ByteBuffer buffer = ByteBuffer.allocate(4); + buffer.putInt(value); + buffer.flip(); + setBytes(buffer.array()); + } + + /** + * Decode bytes to Integer value. + */ + @Override + protected void toValue() { + if (getBytes().length != 4) { + byte[] intBytes = ByteBuffer.allocate(4).put(getBytes(), 0, 4).array(); + /**reset bytes in case the enum type is in a struct or union*/ + setBytes(intBytes); + } + ByteBuffer buffer = ByteBuffer.wrap(getBytes()); + setValue(buffer.getInt()); + } + +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrSimple.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrSimple.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrSimple.java new file mode 100644 index 0000000..a3e13b1 --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrSimple.java @@ -0,0 +1,132 @@ +/** + * 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.type; + +import org.apache.kerby.xdr.XdrDataType; + +import java.io.IOException; +import java.nio.ByteBuffer; + +/** + * 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 extends AbstractXdrType { + private byte[] bytes; + + /** + * Default constructor, generally for decoding as a value container + * @param dataTypeNo The dataType number + */ + public XdrSimple(XdrDataType dataTypeNo) { + this(dataTypeNo, null); + } + + /** + * Constructor with a value, generally for encoding of the value + * @param xdrDataType The dataType number + * @param value The value + */ + public XdrSimple(XdrDataType xdrDataType, T value) { + super(xdrDataType, value); + } + + protected byte[] getBytes() { + return bytes; + } + + protected void setBytes(byte[] bytes) { + this.bytes = bytes; + } + + protected byte[] encodeBody() throws IOException { + if (bytes == null) { + /**Terminal step for encoding all the simple type to bytes.*/ + toBytes(); + } + return bytes; + } + + /** + * Put encoded bytes into buffer. + * @param buffer ByteBuffer to hold encoded bytes. + */ + @Override + protected void encodeBody(ByteBuffer buffer) throws IOException { + byte[] body = encodeBody(); + if (body != null) { + buffer.put(body); + } + } + + /** + * Length including null bytes to maintain an multiple of 4. + * @return + */ + @Override + protected int encodingBodyLength() throws IOException { + if (getValue() == null) { + return 0; + } + if (bytes == null) { + /**Terminal step for decoding all the simple type to bytes.*/ + toBytes(); + } + return bytes.length; + } + + @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); + /**Terminal step for decoding all the bytes into simple types.*/ + toValue(); + } + } + + /** + * Decode bytes to simple value. + */ + protected abstract void toValue() throws IOException; + + /** + * Encode simple type to bytes. + */ + protected abstract void toBytes() throws IOException; + + public static boolean isSimple(XdrDataType dataType) { + switch (dataType) { + case BOOLEAN: + case INTEGER: + case UNSIGNED_INTEGER: + case ENUM: + case STRING: + return true; + default: + return false; + } + } +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/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 new file mode 100644 index 0000000..32b2302 --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrString.java @@ -0,0 +1,346 @@ +/** + * 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.type; + +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; + +/* + * From RFC 4506 : + * + * 0 1 2 3 4 5 ... + * +-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+ + * | length n |byte0|byte1|...| n-1 | 0 |...| 0 | + * +-----+-----+-----+-----+-----+-----+...+-----+-----+...+-----+ + * |<-------4 bytes------->|<------n bytes------>|<---r bytes--->| + * |<----n+r (where (n+r) mod 4 = 0)---->| + * STRING + */ +public class XdrString extends XdrSimple { + private int padding; + + public XdrString() { + this((String) null); + } + + public XdrString(String value) { + super(XdrDataType.STRING, value); + } + + @Override + protected void toBytes() { + if (getValue() != null) { + /**Default value of byte is 0. So we don't have to initialize it with 0*/ + byte[] bytes = new byte[encodingBodyLength()]; + 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().getBytes(), 0, bytes, 4, length); + setBytes(bytes); + } + } + + @Override + protected int encodingBodyLength() { + if (getValue() != null) { + padding = (4 - getValue().length() % 4) % 4; + return getValue().length() + padding + 4; + } + return 0; + } + + protected void toValue() throws IOException { + byte[] bytes = getBytes(); + byte[] header = new byte[4]; + System.arraycopy(bytes, 0, header, 0, 4); + int stringLen = ByteBuffer.wrap(header).getInt(); + int paddingBytes = (4 - (stringLen % 4)) % 4; + validatePaddingBytes(paddingBytes); + setPadding(paddingBytes); + + if (bytes.length != stringLen + 4 + paddingBytes) { + int totalLength = stringLen + paddingBytes + 4; + byte[] stringBytes = ByteBuffer.allocate(totalLength).put(getBytes(), + 0, totalLength).array(); + /**reset bytes in case the enum type is in a struct or union*/ + setBytes(stringBytes); + } + + byte[] content = new byte[stringLen]; + if (bytes.length > 1) { + System.arraycopy(bytes, 4, content, 0, stringLen); + } + setValue(new String(content, StandardCharsets.US_ASCII)); + } + + public void setPadding(int padding) { + this.padding = padding; + } + + public int getPadding() { + return padding; + } + + public static String fromUTF8ByteArray(byte[] bytes) { + int i = 0; + int length = 0; + + while (i < bytes.length) { + length++; + if ((bytes[i] & 0xf0) == 0xf0) { + // surrogate pair + length++; + i += 4; + } else if ((bytes[i] & 0xe0) == 0xe0) { + i += 3; + } else if ((bytes[i] & 0xc0) == 0xc0) { + i += 2; + } else { + i += 1; + } + } + + char[] cs = new char[length]; + i = 0; + length = 0; + + while (i < bytes.length) { + char ch; + + if ((bytes[i] & 0xf0) == 0xf0) { + int codePoint = ((bytes[i] & 0x03) << 18) | ((bytes[i + 1] & 0x3F) << 12) + | ((bytes[i + 2] & 0x3F) << 6) | (bytes[i + 3] & 0x3F); + int u = codePoint - 0x10000; + char w1 = (char) (0xD800 | (u >> 10)); + char w2 = (char) (0xDC00 | (u & 0x3FF)); + cs[length++] = w1; + ch = w2; + i += 4; + } else if ((bytes[i] & 0xe0) == 0xe0) { + ch = (char) (((bytes[i] & 0x0f) << 12) + | ((bytes[i + 1] & 0x3f) << 6) | (bytes[i + 2] & 0x3f)); + i += 3; + } else if ((bytes[i] & 0xd0) == 0xd0) { + ch = (char) (((bytes[i] & 0x1f) << 6) | (bytes[i + 1] & 0x3f)); + i += 2; + } else if ((bytes[i] & 0xc0) == 0xc0) { + ch = (char) (((bytes[i] & 0x1f) << 6) | (bytes[i + 1] & 0x3f)); + i += 2; + } else { + ch = (char) (bytes[i] & 0xff); + i += 1; + } + + cs[length++] = ch; + } + + return new String(cs); + } + + public static byte[] toUTF8ByteArray(String string) { + return toUTF8ByteArray(string.toCharArray()); + } + + public static byte[] toUTF8ByteArray(char[] string) { + ByteArrayOutputStream bOut = new ByteArrayOutputStream(); + + try { + toUTF8ByteArray(string, bOut); + } catch (IOException e) { + throw new IllegalStateException("cannot encode string to byte array!"); + } + + return bOut.toByteArray(); + } + + public static void toUTF8ByteArray(char[] string, OutputStream sOut) throws IOException { + char[] c = string; + int i = 0; + + while (i < c.length) { + char ch = c[i]; + + if (ch < 0x0080) { + sOut.write(ch); + } else if (ch < 0x0800) { + sOut.write(0xc0 | (ch >> 6)); + sOut.write(0x80 | (ch & 0x3f)); + } else if (ch >= 0xD800 && ch <= 0xDFFF) { + // in error - can only happen, if the Java String class has a + // bug. + if (i + 1 >= c.length) { + throw new IllegalStateException("invalid UTF-16 codepoint"); + } + char w1 = ch; + ch = c[++i]; + char w2 = ch; + // in error - can only happen, if the Java String class has a + // bug. + if (w1 > 0xDBFF) { + throw new IllegalStateException("invalid UTF-16 codepoint"); + } + int codePoint = ((w1 & 0x03FF) << 10) | (w2 & 0x03FF) + 0x10000; + sOut.write(0xf0 | (codePoint >> 18)); + sOut.write(0x80 | ((codePoint >> 12) & 0x3F)); + sOut.write(0x80 | ((codePoint >> 6) & 0x3F)); + sOut.write(0x80 | (codePoint & 0x3F)); + } else { + sOut.write(0xe0 | (ch >> 12)); + sOut.write(0x80 | ((ch >> 6) & 0x3F)); + sOut.write(0x80 | (ch & 0x3F)); + } + + i++; + } + } + + /** + * A locale independent version of toUpperCase. + * + * @param string input to be converted + * @return a US Ascii uppercase version + */ + public static String toUpperCase(String string) { + boolean changed = false; + char[] chars = string.toCharArray(); + + for (int i = 0; i != chars.length; i++) { + char ch = chars[i]; + if ('a' <= ch && 'z' >= ch) { + changed = true; + chars[i] = (char) (ch - 'a' + 'A'); + } + } + + if (changed) { + return new String(chars); + } + + return string; + } + + /** + * A locale independent version of toLowerCase. + * + * @param string input to be converted + * @return a US ASCII lowercase version + */ + public static String toLowerCase(String string) { + boolean changed = false; + char[] chars = string.toCharArray(); + + for (int i = 0; i != chars.length; i++) { + char ch = chars[i]; + if ('A' <= ch && 'Z' >= ch) { + changed = true; + chars[i] = (char) (ch - 'A' + 'a'); + } + } + + if (changed) { + return new String(chars); + } + + return string; + } + + public static byte[] toByteArray(char[] chars) { + byte[] bytes = new byte[chars.length]; + + for (int i = 0; i != bytes.length; i++) { + bytes[i] = (byte) chars[i]; + } + + return bytes; + } + + public static byte[] toByteArray(String string) { + byte[] bytes = new byte[string.length()]; + + for (int i = 0; i != bytes.length; i++) { + char ch = string.charAt(i); + + bytes[i] = (byte) ch; + } + + return bytes; + } + + /** + * Convert an array of 8 bit characters into a string. + * + * @param bytes 8 bit characters. + * @return resulting String. + */ + public static String fromByteArray(byte[] bytes) { + return new String(asCharArray(bytes)); + } + + /** + * Do a simple conversion of an array of 8 bit characters into a string. + * + * @param bytes 8 bit characters. + * @return resulting String. + */ + public static char[] asCharArray(byte[] bytes) { + char[] chars = new char[bytes.length]; + + for (int i = 0; i != chars.length; i++) { + chars[i] = (char) (bytes[i] & 0xff); + } + + return chars; + } + + public static String[] split(String input, char delimiter) { + List v = new ArrayList(); + boolean moreTokens = true; + String subString; + + while (moreTokens) { + int tokenLocation = input.indexOf(delimiter); + if (tokenLocation > 0) { + subString = input.substring(0, tokenLocation); + v.add(subString); + input = input.substring(tokenLocation + 1); + } else { + moreTokens = false; + v.add(input); + } + } + + return v.toArray(new String[v.size()]); + } + + private void validatePaddingBytes(int paddingBytes) throws IOException { + if (paddingBytes < 0 || paddingBytes > 3) { + throw new IOException("Bad padding number: " + paddingBytes + ", should be in [0, 3]"); + } + } +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrStructType.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrStructType.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrStructType.java new file mode 100644 index 0000000..6bb74a5 --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrStructType.java @@ -0,0 +1,99 @@ +/** + * 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.type; + +import org.apache.kerby.xdr.XdrDataType; +import org.apache.kerby.xdr.XdrFieldInfo; + +import java.io.IOException; +import java.nio.ByteBuffer; + +/** + * For collection type that may consist of dataTypeged fields + */ +public abstract class XdrStructType extends AbstractXdrType { + private XdrFieldInfo[] fieldInfos; + private XdrType[] fields; + + public XdrStructType(XdrDataType xdrDataType) { + super(xdrDataType); + this.fieldInfos = null; + this.fields = null; + } + + public XdrStructType(XdrDataType xdrDataType, + final XdrFieldInfo[] fieldInfos) { + super(xdrDataType); + this.fieldInfos = fieldInfos; + this.fields = new XdrType[fieldInfos.length]; + + getStructTypeInstance(this.fields, fieldInfos); + } + + protected abstract void getStructTypeInstance(final XdrType[] fields, final XdrFieldInfo[] fieldInfos); + + public XdrFieldInfo[] getXdrFieldInfos() { + return fieldInfos; + } + + @Override + protected int encodingBodyLength() throws IOException { + int allLen = 0; + for (int i = 0; i < fields.length; ++i) { + AbstractXdrType field = (AbstractXdrType) fields[i]; + if (field != null) { + allLen += field.encodingLength(); + } + } + return allLen; + } + + @Override + protected void encodeBody(ByteBuffer buffer) throws IOException { + for (int i = 0; i < fields.length; ++i) { + XdrType field = fields[i]; + if (field != null) { + field.encode(buffer); + } + } + } + + @Override + public void decode(ByteBuffer content) throws IOException { + AbstractXdrType[] fields = getAllFields(); + Object[] value; + for (int i = 0; i < fields.length; i++) { + if (fields[i] != null) { + fields[i].decode(content); + int length = fields[i].encodingLength(); + byte[] array = content.array(); + byte[] newArray = new byte[array.length - length]; + System.arraycopy(array, length, newArray, 0, array.length - length); + content = ByteBuffer.wrap(newArray); + } + } + this.fields = fields; + setValue(fieldsToValues(fields)); + } + + protected abstract XdrStructType fieldsToValues(AbstractXdrType[] fields); + + protected abstract AbstractXdrType[] getAllFields(); +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrType.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrType.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrType.java new file mode 100644 index 0000000..6840e59 --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrType.java @@ -0,0 +1,62 @@ +/** + * 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.type; + +import java.io.IOException; +import java.nio.ByteBuffer; + +/** + * The ASN1 type interface for all ASN1 types. + */ +public interface XdrType { + + /** + * Get length of encoding bytes by just calculating without real encoding. + * Generally it's called to prepare for the encoding buffer. + * @return length of encoding bytes + */ + int encodingLength() throws IOException; + + /** + * Encode the type, by recursively. + * @return encoded bytes + */ + byte[] encode() throws IOException; + + /** + * Encode the type, by recursively, using the provided buffer. + * @param buffer The byte buffer + */ + void encode(ByteBuffer buffer) throws IOException; + + /** + * Decode the content bytes into this type. + * @param content The content bytes + * @throws IOException e + */ + void decode(byte[] content) throws IOException; + + /** + * Decode the content bytes into this type. + * @param content The content bytes + * @throws IOException e + */ + void decode(ByteBuffer content) throws IOException; +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrUnion.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrUnion.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrUnion.java new file mode 100644 index 0000000..b7dc59c --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrUnion.java @@ -0,0 +1,131 @@ +/** + * 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.type; + +import org.apache.kerby.xdr.XdrDataType; +import org.apache.kerby.xdr.XdrFieldInfo; + +import java.io.IOException; +import java.nio.ByteBuffer; + +/** + * A discriminated union is a type composed of a discriminant followed + * by a type selected from a set of prearranged types according to the + * value of the discriminant. The type of discriminant is either "int", + * "unsigned int", or an enumerated type, such as "bool". The component + * types are called "arms" of the union and are preceded by the value of + * the discriminant that implies their encoding. Discriminated unions + * are declared as follows: + * + * union switch (discriminant-declaration) { + * case discriminant-value-A: + * arm-declaration-A; + * case discriminant-value-B: + * arm-declaration-B; + * ... + * default: default-declaration; + * } identifier; + * Each "case" keyword is followed by a legal value of the discriminant. + * The default arm is optional. If it is not specified, then a valid + * encoding of the union cannot take on unspecified discriminant values. + * The size of the implied arm is always a multiple of four bytes. + * + * The discriminated union is encoded as its discriminant followed by + * the encoding of the implied arm. + * 0 1 2 3 + * +---+---+---+---+---+---+---+---+ + * | discriminant | implied arm | + * +---+---+---+---+---+---+---+---+ + * |<---4 bytes--->| + */ +public abstract class XdrUnion extends AbstractXdrType { + /** + * [0] is the discriminant + * index, XdrDataType, value; + * [1] is the implied arm + */ + private XdrFieldInfo[] fieldInfos; + private XdrType[] fields; + + public XdrUnion(XdrDataType xdrDataType) { + super(xdrDataType); + this.fieldInfos = null; + this.fields = null; + } + + public XdrUnion(XdrDataType xdrDataType, + final XdrFieldInfo[] fieldInfos) { + super(xdrDataType); + this.fieldInfos = fieldInfos; + this.fields = new XdrType[fieldInfos.length]; + + getUnionInstance(this.fields, fieldInfos); + } + + protected abstract void getUnionInstance(final XdrType[] fields, final XdrFieldInfo[] fieldInfos); + + public XdrFieldInfo[] getXdrFieldInfos() { + return fieldInfos; + } + + @Override + protected int encodingBodyLength() throws IOException { + int allLen = 0; + for (int i = 0; i < fields.length; i++) { + AbstractXdrType field = (AbstractXdrType) fields[i]; + if (field != null) { + allLen += field.encodingLength(); + } + } + return allLen; + } + + @Override + protected void encodeBody(ByteBuffer buffer) throws IOException { + for (int i = 0; i < fields.length; ++i) { + XdrType field = fields[i]; + if (field != null) { + field.encode(buffer); + } + } + } + + @Override + public void decode(ByteBuffer content) throws IOException { + AbstractXdrType[] fields = getAllFields(); + Object[] value; + for (int i = 0; i < fields.length; i++) { + if (fields[i] != null) { + fields[i].decode(content); + int length = fields[i].encodingLength(); + byte[] array = content.array(); + byte[] newArray = new byte[array.length - length]; + System.arraycopy(array, length, newArray, 0, array.length - length); + content = ByteBuffer.wrap(newArray); + } + } + this.fields = fields; + setValue(fieldsToValues(fields)); + } + + protected abstract XdrUnion fieldsToValues(AbstractXdrType[] fields); + + protected abstract AbstractXdrType[] getAllFields(); +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrUnsignedInteger.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrUnsignedInteger.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrUnsignedInteger.java new file mode 100644 index 0000000..ad1df69 --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/type/XdrUnsignedInteger.java @@ -0,0 +1,100 @@ +/** + * 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.type; + +import org.apache.kerby.xdr.XdrDataType; + +import java.io.IOException; +import java.nio.ByteBuffer; + +/** + * Xdr Unsigned Integer type from RFC 4506 + * An XDR unsigned integer is a 32-bit datum that encodes + * a non-negative integer in the range [0,4294967295]. + * It is represented by an unsigned binary number whose most + * and least significant bytes are 0 and 3, respectively. + * An unsigned integer is declared as follows: + * unsigned int identifier; + * + * (MSB) (LSB) + * +-------+-------+-------+-------+ + * |byte 0 |byte 1 |byte 2 |byte 3 | + * +-------+-------+-------+-------+ + * <------------32 bits------------> + */ +public class XdrUnsignedInteger extends XdrSimple { + public XdrUnsignedInteger() { + this((Long) null); + } + + public XdrUnsignedInteger(String value) { + this(Long.valueOf(value)); + } + + public XdrUnsignedInteger(Long value) { + super(XdrDataType.UNSIGNED_INTEGER, value); + } + + /** + * The length of an unsigned integer is 4. + * @return Length of a unsigned integer type. + */ + @Override + protected int encodingBodyLength() { + return 4; /**Length of XdrInteger is fixed as 4 bytes*/ + } + + /** + * Encode Unsigned Integer type to bytes. + */ + @Override + protected void toBytes() throws IOException { + Long value = getValue(); + validateUnsignedInteger(value); /**Check whether the long value is valid unsigned int*/ + ByteBuffer buffer = ByteBuffer.allocate(8); + buffer.putLong(value); + byte[] bytes = new byte[4]; /**The encoding length is 4*/ + System.arraycopy(buffer.array(), 4, bytes, 0, 4); + setBytes(bytes); + } + + private void validateUnsignedInteger(Long value) throws IOException { + if (value < 0 || value > 4294967295L) { + throw new IOException("Invalid unsigned integer: " + value); + } + } + + /** + * Decode bytes to Unsigned Integer value. + */ + @Override + protected void toValue() { + if (getBytes().length != 4) { + byte[] bytes = ByteBuffer.allocate(4).put(getBytes(), 0, 4).array(); + setBytes(bytes); /**reset bytes in case the enum type is in a struct or union*/ + } + + byte[] longBytes = {(byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00, + (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x00}; + System.arraycopy(getBytes(), 0, longBytes, 4, 4); + ByteBuffer buffer = ByteBuffer.wrap(longBytes); + setValue(buffer.getLong()); + } +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/HexUtil.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/HexUtil.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/HexUtil.java new file mode 100644 index 0000000..70a2b1c --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/HexUtil.java @@ -0,0 +1,113 @@ +/** + * 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.util; + +/** + * This is only for test, be careful when use in production codes. + */ +public class HexUtil { + + static final String HEX_CHARS_STR = "0123456789ABCDEF"; + static final char[] HEX_CHARS = HEX_CHARS_STR.toCharArray(); + + /** + * Convert bytes into friendly format as: + * 0x02 02 00 80 + */ + public static String bytesToHexFriendly(byte[] bytes) { + int len = bytes.length * 2; + len += bytes.length; // for ' ' appended for each char + len += 2; // for '0x' prefix + char[] hexChars = new char[len]; + hexChars[0] = '0'; + hexChars[1] = 'x'; + for (int j = 0; j < bytes.length; j++) { + int v = bytes[j] & 0xFF; + hexChars[j * 3 + 2] = HEX_CHARS[v >>> 4]; + hexChars[j * 3 + 3] = HEX_CHARS[v & 0x0F]; + hexChars[j * 3 + 4] = ' '; + } + + return new String(hexChars); + } + + /** + * Convert friendly hex string like follows into byte array + * 0x02 02 00 80 + */ + public static byte[] hex2bytesFriendly(String hexString) { + hexString = hexString.toUpperCase(); + String hexStr = hexString; + if (hexString.startsWith("0X")) { + hexStr = hexString.substring(2); + } + String[] hexParts = hexStr.split(" "); + + byte[] bytes = new byte[hexParts.length]; + char[] hexPart; + for (int i = 0; i < hexParts.length; ++i) { + hexPart = hexParts[i].toCharArray(); + if (hexPart.length != 2) { + throw new IllegalArgumentException("Invalid hex string to convert"); + } + bytes[i] = (byte) ((HEX_CHARS_STR.indexOf(hexPart[0]) << 4) + + HEX_CHARS_STR.indexOf(hexPart[1])); + } + + return bytes; + } + + /** + * Convert bytes into format as: + * 02020080 + * @param bytes The bytes + * @return The hex string + */ + public static String bytesToHex(byte[] bytes) { + int len = bytes.length * 2; + char[] hexChars = new char[len]; + for (int j = 0; j < bytes.length; j++) { + int v = bytes[j] & 0xFF; + hexChars[j * 2] = HEX_CHARS[v >>> 4]; + hexChars[j * 2 + 1] = HEX_CHARS[v & 0x0F]; + } + + return new String(hexChars); + } + + /** + * Convert hex string like follows into byte array + * 02020080 + * @param hexString The hex string + * @return The bytes + */ + public static byte[] hex2bytes(String hexString) { + hexString = hexString.toUpperCase(); + int len = hexString.length() / 2; + byte[] bytes = new byte[len]; + char[] hexChars = hexString.toCharArray(); + for (int i = 0, j = 0; i < len; ++i) { + bytes[i] = (byte) ((HEX_CHARS_STR.indexOf(hexChars[j++]) << 4) + + HEX_CHARS_STR.indexOf(hexChars[j++])); + } + + return bytes; + } +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/IOUtil.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/IOUtil.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/IOUtil.java new file mode 100644 index 0000000..2136511 --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/IOUtil.java @@ -0,0 +1,109 @@ +/** + * 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.util; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.ByteBuffer; +import java.nio.channels.FileChannel; + +/** + * Some IO and file related utilities. + */ +public final class IOUtil { + private IOUtil() { } + + public static byte[] readInputStream(InputStream in) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + byte[] buffer = new byte[1024]; + int length = 0; + while ((length = in.read(buffer)) != -1) { + baos.write(buffer, 0, length); + } + in.close(); + return baos.toByteArray(); + } + + public static void readInputStream(InputStream in, + byte[] buf) throws IOException { + int toRead = buf.length; + int off = 0; + while (toRead > 0) { + int ret = in.read(buf, off, toRead); + if (ret < 0) { + throw new IOException("Bad inputStream, premature EOF"); + } + toRead -= ret; + off += ret; + } + in.close(); + } + + /** + * Read an input stream and return the content as string assuming UTF8. + * @param in The input stream + * @return The content + * @throws IOException e + */ + public static String readInput(InputStream in) throws IOException { + byte[] content = readInputStream(in); + return Utf8.toString(content); + } + + /** + * Read a file and return the content as string assuming UTF8. + * @param file The file to read + * @return The content + * @throws IOException e + */ + public static String readFile(File file) throws IOException { + long len = 0; + if (file.length() >= Integer.MAX_VALUE) { + throw new IOException("Too large file, unexpected!"); + } else { + len = file.length(); + } + byte[] buf = new byte[(int) len]; + + InputStream is = new FileInputStream(file); + readInputStream(is, buf); + + return Utf8.toString(buf); + } + + /** + * Write a file with the content assuming UTF8. + * @param content The content + * @param file The file to write + * @throws IOException e + */ + public static void writeFile(String content, File file) throws IOException { + FileOutputStream outputStream = new FileOutputStream(file); + FileChannel fc = outputStream.getChannel(); + + ByteBuffer buffer = ByteBuffer.wrap(Utf8.toBytes(content)); + fc.write(buffer); + outputStream.close(); + } +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/Utf8.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/Utf8.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/Utf8.java new file mode 100644 index 0000000..374c16f --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/Utf8.java @@ -0,0 +1,34 @@ +/** + * 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.util; + +import java.nio.charset.StandardCharsets; + +public final class Utf8 { + private Utf8() { } + + public static String toString(byte[] bytes) { + return new String(bytes, StandardCharsets.UTF_8); + } + + public static byte[] toBytes(String s) { + return s.getBytes(StandardCharsets.UTF_8); + } +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/XdrUtil.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/XdrUtil.java b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/XdrUtil.java new file mode 100644 index 0000000..880bbb0 --- /dev/null +++ b/kerby-common/kerby-xdr/src/main/java/org/apache/kerby/xdr/util/XdrUtil.java @@ -0,0 +1,26 @@ +/** + * 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.util; + +public final class XdrUtil { + private XdrUtil() { + + } +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/TestUtil.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/TestUtil.java b/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/TestUtil.java new file mode 100644 index 0000000..6a3d9e8 --- /dev/null +++ b/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/TestUtil.java @@ -0,0 +1,47 @@ +/** + * 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.util.HexUtil; +import org.apache.kerby.xdr.util.IOUtil; + +import java.io.IOException; +import java.io.InputStream; + +public final class TestUtil { + private TestUtil() { + + } + + static byte[] readBytesFromTxtFile(String resource) throws IOException { + String hexStr = readStringFromTxtFile(resource); + return HexUtil.hex2bytes(hexStr); + } + + static String readStringFromTxtFile(String resource) throws IOException { + InputStream is = TestUtil.class.getResourceAsStream(resource); + return IOUtil.readInput(is); + } + + static byte[] readBytesFromBinFile(String resource) throws IOException { + InputStream is = TestUtil.class.getResourceAsStream(resource); + return IOUtil.readInputStream(is); + } +} http://git-wip-us.apache.org/repos/asf/directory-kerby/blob/9f628e5a/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrBooleanTest.java ---------------------------------------------------------------------- diff --git a/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrBooleanTest.java b/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrBooleanTest.java new file mode 100644 index 0000000..0d7b0f2 --- /dev/null +++ b/kerby-common/kerby-xdr/src/test/java/org/apache/kerby/xdr/XdrBooleanTest.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.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/9f628e5a/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..e35d03c --- /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; + Color(int value) { + this.value = value; + } + + public int getValue() { + return value; + } + + public String getName() { + return name(); + } +} + +public class XdrEnumeratedInstance extends XdrEnumerated { + + 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/9f628e5a/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..2a3096a --- /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); + } +}