Skip to content

Commit

Permalink
Fixed issue #130
Browse files Browse the repository at this point in the history
  • Loading branch information
oshoukry committed Jul 26, 2019
1 parent 3cf2b06 commit fc4fd77
Show file tree
Hide file tree
Showing 12 changed files with 219 additions and 94 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,55 +18,58 @@

package com.openpojo.random.generator.security;

import java.util.Arrays;
import java.util.Collection;

import com.openpojo.random.RandomFactory;
import com.openpojo.random.RandomGenerator;
import sun.security.krb5.Credentials;
import sun.security.krb5.EncryptionKey;
import sun.security.krb5.PrincipalName;
import sun.security.krb5.internal.HostAddresses;
import sun.security.krb5.internal.KerberosTime;
import sun.security.krb5.internal.Ticket;
import sun.security.krb5.internal.TicketFlags;
import com.openpojo.reflection.PojoClass;
import com.openpojo.reflection.construct.InstanceFactory;

import java.util.*;

import static com.openpojo.random.RandomFactory.getRandomValue;
import static com.openpojo.reflection.impl.PojoClassFactory.getPojoClass;
import static com.openpojo.reflection.java.load.ClassUtil.loadClass;

/**
* @author oshoukry
*/
public class CredentialsRandomGenerator implements RandomGenerator {

private static final Class<?>[] TYPES = new Class<?>[] { Credentials.class };
private static final String TYPE = "sun.security.krb5.Credentials";
private static final CredentialsRandomGenerator INSTANCE = new CredentialsRandomGenerator();
private final Class<?> credentialsClass;

private CredentialsRandomGenerator() {
credentialsClass = loadClass(TYPE);
}

public static RandomGenerator getInstance() {
return INSTANCE;
}

public Collection<Class<?>> getTypes() {
return Arrays.asList(TYPES);
List<Class<?>> supported = new ArrayList<Class<?>>();
if (credentialsClass != null)
supported.add(credentialsClass);
return supported;
}

public Object doGenerate(Class<?> type) {
Ticket var1 = RandomFactory.getRandomValue(Ticket.class);
Object var1 = getRandomValue(loadClass("sun.security.krb5.internal.Ticket"));

PrincipalName var2 = RandomFactory.getRandomValue(PrincipalName.class);
PrincipalName var3 = RandomFactory.getRandomValue(PrincipalName.class);
Object var2 = getRandomValue(loadClass("sun.security.krb5.PrincipalName"));
Object var3 = getRandomValue(loadClass("sun.security.krb5.PrincipalName"));

EncryptionKey var4 = RandomFactory.getRandomValue(EncryptionKey.class);
Object var4 = getRandomValue(loadClass("sun.security.krb5.EncryptionKey"));

TicketFlags var5 = RandomFactory.getRandomValue(TicketFlags.class);
Object var5 = getRandomValue(loadClass("sun.security.krb5.internal.TicketFlags"));

KerberosTime var6 = RandomFactory.getRandomValue(KerberosTime.class);
KerberosTime var7 = RandomFactory.getRandomValue(KerberosTime.class);
KerberosTime var8 = RandomFactory.getRandomValue(KerberosTime.class);
KerberosTime var9 = RandomFactory.getRandomValue(KerberosTime.class);
Object var6 = getRandomValue(loadClass("sun.security.krb5.internal.KerberosTime"));
Object var7 = getRandomValue(loadClass("sun.security.krb5.internal.KerberosTime"));
Object var8 = getRandomValue(loadClass("sun.security.krb5.internal.KerberosTime"));
Object var9 = getRandomValue(loadClass("sun.security.krb5.internal.KerberosTime"));

HostAddresses var10 = RandomFactory.getRandomValue(HostAddresses.class);
Object var10 = getRandomValue(loadClass("sun.security.krb5.internal.HostAddresses"));

return new Credentials(var1, var2, var3, var4, var5, var6, var7, var8, var9, var10);
PojoClass pojoClass = getPojoClass(credentialsClass);
return InstanceFactory.getInstance(pojoClass, var1, var2, var3, var4, var5, var6, var7, var8, var9, var10);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,43 @@

package com.openpojo.random.generator.security;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;

import com.openpojo.random.RandomFactory;
import com.openpojo.random.RandomGenerator;
import sun.security.krb5.EncryptionKey;
import com.openpojo.reflection.PojoClass;
import com.openpojo.reflection.construct.InstanceFactory;
import com.openpojo.reflection.impl.PojoClassFactory;

import static com.openpojo.reflection.java.load.ClassUtil.loadClass;

/**
* @author oshoukry
*/
public class EncryptionKeyRandomGenerator implements RandomGenerator {
private static final Class<?>[] TYPES = new Class<?>[] { EncryptionKey.class };
private static final RandomGenerator INSTANCE = new EncryptionKeyRandomGenerator();
private static final String TYPE = "sun.security.krb5.EncryptionKey";
private final Class<?> encryptionKeyClass;
private static final EncryptionKeyRandomGenerator INSTANCE = new EncryptionKeyRandomGenerator();

private EncryptionKeyRandomGenerator() {
encryptionKeyClass = loadClass(TYPE);
}

public static RandomGenerator getInstance() {
return INSTANCE;
}

public Collection<Class<?>> getTypes() {
return Arrays.asList(TYPES);
List<Class<?>> supported = new ArrayList<Class<?>>();
if (encryptionKeyClass != null)
supported.add(encryptionKeyClass);
return supported;
}

public Object doGenerate(Class<?> type) {
//noinspection ConstantConditions
return new EncryptionKey(RandomFactory.getRandomValue(byte[].class), RandomFactory.getRandomValue(Integer.class), null);
PojoClass pojoClass = PojoClassFactory.getPojoClass(encryptionKeyClass);
return InstanceFactory.getInstance(pojoClass, RandomFactory.getRandomValue(byte[].class), RandomFactory.getRandomValue(Integer.class), null);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,42 @@

package com.openpojo.random.generator.security;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;

import com.openpojo.random.RandomFactory;
import com.openpojo.random.RandomGenerator;
import sun.security.krb5.internal.KerberosTime;
import com.openpojo.reflection.PojoClass;
import com.openpojo.reflection.construct.InstanceFactory;
import com.openpojo.reflection.impl.PojoClassFactory;
import com.openpojo.reflection.java.load.ClassUtil;

/**
* @author oshoukry
*/
public class KerberosTimeRandomGenerator implements RandomGenerator {
private static final Class<?>[] TYPES = new Class<?>[] { KerberosTime.class };
private static final String TYPE = "sun.security.krb5.internal.KerberosTime";
private final Class<?> kerberosTimeClass;
private static final KerberosTimeRandomGenerator INSTANCE = new KerberosTimeRandomGenerator();

private KerberosTimeRandomGenerator() {
kerberosTimeClass = ClassUtil.loadClass(TYPE);
}

public static RandomGenerator getInstance() {
return INSTANCE;
}

public Collection<Class<?>> getTypes() {
return Arrays.asList(TYPES);
List<Class<?>> supported = new ArrayList<Class<?>>();
if (kerberosTimeClass != null)
supported.add(kerberosTimeClass);
return supported;
}

public Object doGenerate(Class<?> type) {
return new KerberosTime(RandomFactory.getRandomValue(long.class));
PojoClass pojoClass = PojoClassFactory.getPojoClass(kerberosTimeClass);
return InstanceFactory.getInstance(pojoClass, RandomFactory.getRandomValue(long.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,40 +18,48 @@

package com.openpojo.random.generator.security;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.ArrayList;

import com.openpojo.random.RandomFactory;
import com.openpojo.random.RandomGenerator;
import com.openpojo.random.exception.RandomGeneratorException;
import sun.security.krb5.PrincipalName;
import com.openpojo.reflection.PojoClass;
import com.openpojo.reflection.construct.InstanceFactory;
import com.openpojo.reflection.impl.PojoClassFactory;
import com.openpojo.reflection.java.load.ClassUtil;

/**
* @author oshoukry
*/
public class PrincipalNameRandomGenerator implements RandomGenerator {

private static final Class<?>[] TYPES = new Class<?>[] { PrincipalName.class };
private static final String TYPE = "sun.security.krb5.PrincipalName";
private final Class<?> principalNameClass;
private static final PrincipalNameRandomGenerator INSTANCE = new PrincipalNameRandomGenerator();

private PrincipalNameRandomGenerator() {
principalNameClass = ClassUtil.loadClass(TYPE);
}

public static RandomGenerator getInstance() {
return INSTANCE;
}

public Collection<Class<?>> getTypes() {
return Arrays.asList(TYPES);
List<Class<?>> supported = new ArrayList<Class<?>>();
if (principalNameClass != null)
supported.add(principalNameClass);
return supported;
}

public Object doGenerate(Class<?> type) {
try {
//noinspection ConstantConditions
return new PrincipalName(getPrincipleParsableString()
);
PojoClass pojoClass = PojoClassFactory.getPojoClass(principalNameClass);
return InstanceFactory.getInstance(pojoClass, getPrincipleParsableString());
} catch (Exception e) {
throw RandomGeneratorException.getInstance("Failed to generate " + TYPES[0].getName() + " instance.", e);
throw RandomGeneratorException.getInstance("Failed to generate " + TYPE + " instance.", e);
}
}

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
* Copyright (c) 2010-2018 Osman Shoukry
*
* Licensed 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 com.openpojo.issues.issue84;

import org.objectweb.asm.AnnotationVisitor;
import org.objectweb.asm.ClassWriter;
import org.objectweb.asm.FieldVisitor;
import org.objectweb.asm.MethodVisitor;

import static org.objectweb.asm.Opcodes.*;
//@formatter:off
/**
* Creates this class:
*{@code
* package com.openpojo.issues.issue84;
* import sun.security.krb5.Credentials;
*
* public class ClassWithCredentials {
* private Credentials credentials;
* public Credentials getCredentials() {
* return this.credentials;
* }
*
* public void setCredentials(Credentials credentials) {
* this.credentials = credentials;
* }
* }
*
* @author oshoukry
*/
//@formatter:on
final class ClassWithCredentialsDumper {

private final static String CLASS_PACKAGE = "com/openpojo/issues/issue84/ClassWithCredentials";
private static final String SUN_SECURITY_KRB_5_CREDENTIALS = "sun/security/krb5/Credentials";

static byte[] dump() {

ClassWriter cw = new ClassWriter(0);
FieldVisitor fv;
MethodVisitor mv;
AnnotationVisitor av0;

cw.visit(52, ACC_FINAL + ACC_SUPER, CLASS_PACKAGE, null, "java/lang/Object", null);

{
fv = cw.visitField(ACC_PRIVATE, "credentials", "L" + SUN_SECURITY_KRB_5_CREDENTIALS + ";", null, null);
fv.visitEnd();
}
{
mv = cw.visitMethod(0, "<init>", "()V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitMethodInsn(INVOKESPECIAL, "java/lang/Object", "<init>", "()V", false);
mv.visitInsn(RETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "getCredentials", "()L" + SUN_SECURITY_KRB_5_CREDENTIALS + ";", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitFieldInsn(GETFIELD, CLASS_PACKAGE, "credentials", "L" + SUN_SECURITY_KRB_5_CREDENTIALS + ";");
mv.visitInsn(ARETURN);
mv.visitMaxs(1, 1);
mv.visitEnd();
}
{
mv = cw.visitMethod(ACC_PUBLIC, "setCredentials", "(L" + SUN_SECURITY_KRB_5_CREDENTIALS + ";)V", null, null);
mv.visitCode();
mv.visitVarInsn(ALOAD, 0);
mv.visitVarInsn(ALOAD, 1);
mv.visitFieldInsn(PUTFIELD, CLASS_PACKAGE, "credentials", "L" + SUN_SECURITY_KRB_5_CREDENTIALS + ";");
mv.visitInsn(RETURN);
mv.visitMaxs(2, 2);
mv.visitEnd();
}
cw.visitEnd();

return cw.toByteArray();
}
}
Loading

0 comments on commit fc4fd77

Please sign in to comment.