usr/src/test/java/src/client/PAMTestBase.java
author devjani.ray@oracle.com <devjani.ray@oracle.com>
Thu, 02 Feb 2012 11:25:01 -0500
changeset 798 a7deccd6492f
parent 766 46dbc6163ccd
child 811 c65c58b7a1f0
permissions -rw-r--r--
CR 7121230 Rad unix transport paths should be created under /system/volatile/rad/

/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

/*
 * Copyright (c) 2011, 2012, Oracle and/or its affiliates. All rights reserved.
 */

package client;

import com.oracle.solaris.smf.Instance;
import com.oracle.solaris.smf.MasterMXBean;
import com.oracle.solaris.smf.SMFState;
import common.MBeanTestCommon;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import javax.management.MBeanServerConnection;
import javax.management.ObjectName;
import javax.management.remote.JMXConnector;
import javax.management.remote.JMXConnectorFactory;
import javax.management.remote.JMXServiceURL;
import org.junit.After;
import org.junit.Before;
import org.opensolaris.os.adr.Stability;
import org.opensolaris.os.rad.jmx.RadJMX;
import org.opensolaris.os.rad.api.pam.AuthenticatorMXBean;
import org.opensolaris.os.rad.api.pam.Block;
import org.opensolaris.os.rad.api.pam.Message;

import static org.junit.Assert.*;

public abstract class PAMTestBase extends MBeanTestCommon {
    public static final String RADTEST_SVC = "svc:/system/rad:test";
    public static final String RADCONVTEST_SVC = "svc:/system/rad-conv-test";
    public static final String RAD_PATH_AFUNIX_AUTH =
	"/system/volatile/rad/test-radsocket";
    public static final String RAD_PATH_AFUNIX_UNAUTH =
	"/system/volatile/rad/test-radsocket-unauth";

    private JMXConnector conn_;
    protected AuthenticatorMXBean bean_;
    protected String locale_ = Locale.getDefault().getLanguage();

    /**
     * determine if the rad-test and rad-conv-test services are runnning.
     */
    protected void checkRequiredServices() throws Exception {
	setUpCommon("/usr/lib/rad/module/mod_smf.so");

	final String name = "com.oracle.solaris.smf:type=master";
	MasterMXBean smfBean = RadJMX.newMXBeanProxy(getMBSC(), strToON(name),
            MasterMXBean.class, Stability.PRIVATE);
	if (smfBean == null)
	    throw new Exception("Unable to create SMF Master MX Bean.");

	List<Instance> instances = smfBean.getinstances();
	boolean radtest = false;
	boolean radconvtest = false;

	for (Instance instance : instances) {
	    if (instance.getFmri().startsWith(RADTEST_SVC))
		radtest = instance.getState() == SMFState.ONLINE;
	    if (instance.getFmri().startsWith(RADCONVTEST_SVC))
		radconvtest = instance.getState() == SMFState.ONLINE;
	}

	if (!radtest)
	    throw new Exception("The Service '" + RADTEST_SVC +
		"' Required by this test is not running.");
	if (!radconvtest)
	    throw new Exception("The Service '" + RADCONVTEST_SVC +
		"' Required by this test is not running.");

	super.tearDown();
    }

    /**
     * connect to the rad-test instance.
     */
    protected boolean isRadReady(boolean auth) throws Exception {
	checkRequiredServices();

	String path = auth ? RAD_PATH_AFUNIX_AUTH : RAD_PATH_AFUNIX_UNAUTH;

	final String url = "service:jmx:radunix://" + path;
	final String name = "org.opensolaris.os.rad:type=authentication";

	conn_ = JMXConnectorFactory.connect(new JMXServiceURL(url));
	MBeanServerConnection mbsc = conn_.getMBeanServerConnection();
	if (mbsc == null) {
	    conn_.close();
	    return false;
	}

	bean_ = RadJMX.newMXBeanProxy(mbsc, new ObjectName(name),
	    AuthenticatorMXBean.class, Stability.PRIVATE);
	if (bean_ == null) {
	    conn_.close();
	    return false;
	}

	return true;
    }

    @After
    public void tearDown() throws IOException {
	if (conn_ != null)
	    conn_.close();
    }
}