<commit_before>/*
 * $Id$
 *
 * Copyright (c) OSGi Alliance (2000-2001).
 * All Rights Reserved.
 *
 * Implementation of certain elements of the OSGi
 * Specification may be subject to third party intellectual property
 * rights, including without limitation, patent rights (such a third party may
 * or may not be a member of the OSGi Alliance). The OSGi Alliance is not responsible and shall not be
 * held responsible in any manner for identifying or failing to identify any or
 * all such third party intellectual property rights.
 *
 * This document and the information contained herein are provided on an "AS
 * IS" basis and THE OSGI ALLIANCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL
 * NOT INFRINGE ANY RIGHTS AND ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL THE OSGI ALLIANCE BE LIABLE FOR ANY
 * LOSS OF PROFITS, LOSS OF BUSINESS, LOSS OF USE OF DATA, INTERRUPTION OF
 * BUSINESS, OR FOR DIRECT, INDIRECT, SPECIAL OR EXEMPLARY, INCIDENTIAL,
 * PUNITIVE OR CONSEQUENTIAL DAMAGES OF ANY KIND IN CONNECTION WITH THIS
 * DOCUMENT OR THE INFORMATION CONTAINED HEREIN, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH LOSS OR DAMAGE.
 *
 * All Company, brand and product names may be trademarks that are the sole
 * property of their respective owners. All rights reserved.
 */

package org.osgi.test.cases.provisioning.junit;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.provisioning.ProvisioningService;
import org.osgi.service.url.AbstractURLStreamHandlerService;
import org.osgi.service.url.URLConstants;
import org.osgi.service.url.URLStreamHandlerService;
import org.osgi.test.support.compatibility.DefaultTestBundleControl;
import org.osgi.util.tracker.ServiceTracker;

/**
 * A test case for the R3 Initial Provisioning service, with
 * file, http, and rsh url mappings.
 */
public class ProvisioningTests extends DefaultTestBundleControl {
	File							dir;			// Working dir for copying bundles
	ServiceTracker					provisioning;	// Tracks provisioning service
	final boolean					debug = true;	// For debug info
	ServiceTracker					bundles;		// Tracks started Ip bundles
	ServiceRegistration				registration;	// director: spid-test: stream handler
	String							query; 			// Query part from spid-test: url
	final String		httpServer;

	public ProvisioningTests() {
		httpServer = "http://" + Activator.hostName + ":" + Activator.hostPort
				+ "/";
	}

	/**
	 * Create a work directory and start a tracker for our loaded bundles.
	 *
	 * There is only one bundle that gets loaded during this test case.
	 * This bundle is simple.jar and will register its bundle in the
	 * service registry with test.case property.
	 */
	protected void setUp() throws InvalidSyntaxException {
		provisioning = new ServiceTracker(getContext(),
				ProvisioningService.class.getName(), null);
		provisioning.open();
		dir = getContext().getDataFile("work");
		dir.mkdir();
		bundles = new ServiceTracker(getContext(), getContext().createFilter(
				"(test.case=org.osgi.test.cases.provisioning)"),
			null );
		bundles.open();		
		registration = installStreamHandler();
		reset();
	}

	private void reset() {
		/*
		 * For each test case, assure we have no test bundles installed. We
		 * assure that all installed bundles end with -prov.jar so we can find
		 * them.
		 */
		Bundle bndls[] = getContext().getBundles();
		for ( int i=0; i < bndls.length; i++ ) {
			if ( bndls[i].getLocation().endsWith("-prov.jar") ) {
				if ( debug )
					log("Uninstalling? " + bndls[i].getLocation());
				try {
					Bundle bundle = bndls[i];
					//
					// Some bundles might still be in
					// the starting mode if their thread was
					// yielded to us. So give them some time
					// to leave the start() method.
					//
					int n = 30;
					while ( bundle.getState() == Bundle.STARTING && n-->0)
						Thread.sleep(Activator.TIMEOUT4);
					
					//
					// But now we kill it !
					//
					bundle.uninstall();
				}
				catch( Exception e ) { 
					log( "In uninstalling " + e ); 
				}
			}
		}
		ServiceReference refs[] = bundles.getServiceReferences();
		assertTrue( "Bundles should have been uninstalled",  refs==null || refs.length==0);
		
		//
		// Reset the provisioning service
		//
		Hashtable	information = new Hashtable(); 
		information.put( "provisioning.spid", "SPID:test%21" );
		information.put( "provisioning.rsh.secret", RSHTest.secret );
		getProvisioningService().setInformation( information );
	}
	
	/**
	 * Remove the stream handlers
	 */
	protected void tearDown() {
		reset();
		registration.unregister();
		provisioning.close();
	}

	
	private String getHttpServer() {
		return httpServer;
	}
	/*
	 * START OF TESTS
	 */
	
	/**
	 * Test if we actually have RSH stream handler installed. If
	 * read a file and check the contents. This reading will go
	 * through the RSH handler and thus passes all encryption/decryption
	 * routines. The RSH protocol is optional so this case
	 * fails silently if there is no such protocol
	 * 
	 * @spec rsh
	 */
	public void testRSHFile() throws Exception {
		if ( ! rshAvailable() )
			return;
		
		URL ws = new URL(getHttpServer());
		URL rshurl = new URL( "rsh://" + ws.getHost() + ":" + ws.getPort() + "/test/rsh");
		
		// Check if we can get data from this
		InputStream		in = rshurl.openStream();
		assertNotNull( "Stream from RSH URL could not be opened", in );

		byte r [] = collect( in, 0 );
		if ( debug ) {
			FileOutputStream tout = new FileOutputStream( "target-test.zip");
			tout.write( r );
			tout.close();
		}
		in.close();
		
		ZipInputStream	zin = new ZipInputStream( new ByteArrayInputStream(r) );
		ZipEntry entry = zin.getNextEntry();
		while ( entry != null) {<commit_msg>Place debug output from provisioning test case in generated directory.<commit_after>/*
 * $Id$
 *
 * Copyright (c) OSGi Alliance (2000-2001).
 * All Rights Reserved.
 *
 * Implementation of certain elements of the OSGi
 * Specification may be subject to third party intellectual property
 * rights, including without limitation, patent rights (such a third party may
 * or may not be a member of the OSGi Alliance). The OSGi Alliance is not responsible and shall not be
 * held responsible in any manner for identifying or failing to identify any or
 * all such third party intellectual property rights.
 *
 * This document and the information contained herein are provided on an "AS
 * IS" basis and THE OSGI ALLIANCE DISCLAIMS ALL WARRANTIES, EXPRESS OR IMPLIED, INCLUDING
 * BUT NOT LIMITED TO ANY WARRANTY THAT THE USE OF THE INFORMATION HEREIN WILL
 * NOT INFRINGE ANY RIGHTS AND ANY IMPLIED WARRANTIES OF MERCHANTABILITY OR
 * FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT WILL THE OSGI ALLIANCE BE LIABLE FOR ANY
 * LOSS OF PROFITS, LOSS OF BUSINESS, LOSS OF USE OF DATA, INTERRUPTION OF
 * BUSINESS, OR FOR DIRECT, INDIRECT, SPECIAL OR EXEMPLARY, INCIDENTIAL,
 * PUNITIVE OR CONSEQUENTIAL DAMAGES OF ANY KIND IN CONNECTION WITH THIS
 * DOCUMENT OR THE INFORMATION CONTAINED HEREIN, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH LOSS OR DAMAGE.
 *
 * All Company, brand and product names may be trademarks that are the sole
 * property of their respective owners. All rights reserved.
 */

package org.osgi.test.cases.provisioning.junit;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.Arrays;
import java.util.Dictionary;
import java.util.Hashtable;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

import org.osgi.framework.Bundle;
import org.osgi.framework.Constants;
import org.osgi.framework.InvalidSyntaxException;
import org.osgi.framework.ServiceReference;
import org.osgi.framework.ServiceRegistration;
import org.osgi.service.provisioning.ProvisioningService;
import org.osgi.service.url.AbstractURLStreamHandlerService;
import org.osgi.service.url.URLConstants;
import org.osgi.service.url.URLStreamHandlerService;
import org.osgi.test.support.compatibility.DefaultTestBundleControl;
import org.osgi.util.tracker.ServiceTracker;

/**
 * A test case for the R3 Initial Provisioning service, with
 * file, http, and rsh url mappings.
 */
public class ProvisioningTests extends DefaultTestBundleControl {
	File							dir;			// Working dir for copying bundles
	ServiceTracker					provisioning;	// Tracks provisioning service
	final boolean					debug = true;	// For debug info
	ServiceTracker					bundles;		// Tracks started Ip bundles
	ServiceRegistration				registration;	// director: spid-test: stream handler
	String							query; 			// Query part from spid-test: url
	final String		httpServer;

	public ProvisioningTests() {
		httpServer = "http://" + Activator.hostName + ":" + Activator.hostPort
				+ "/";
	}

	/**
	 * Create a work directory and start a tracker for our loaded bundles.
	 *
	 * There is only one bundle that gets loaded during this test case.
	 * This bundle is simple.jar and will register its bundle in the
	 * service registry with test.case property.
	 */
	protected void setUp() throws InvalidSyntaxException {
		provisioning = new ServiceTracker(getContext(),
				ProvisioningService.class.getName(), null);
		provisioning.open();
		dir = getContext().getDataFile("work");
		dir.mkdir();
		bundles = new ServiceTracker(getContext(), getContext().createFilter(
				"(test.case=org.osgi.test.cases.provisioning)"),
			null );
		bundles.open();		
		registration = installStreamHandler();
		reset();
	}

	private void reset() {
		/*
		 * For each test case, assure we have no test bundles installed. We
		 * assure that all installed bundles end with -prov.jar so we can find
		 * them.
		 */
		Bundle bndls[] = getContext().getBundles();
		for ( int i=0; i < bndls.length; i++ ) {
			if ( bndls[i].getLocation().endsWith("-prov.jar") ) {
				if ( debug )
					log("Uninstalling? " + bndls[i].getLocation());
				try {
					Bundle bundle = bndls[i];
					//
					// Some bundles might still be in
					// the starting mode if their thread was
					// yielded to us. So give them some time
					// to leave the start() method.
					//
					int n = 30;
					while ( bundle.getState() == Bundle.STARTING && n-->0)
						Thread.sleep(Activator.TIMEOUT4);
					
					//
					// But now we kill it !
					//
					bundle.uninstall();
				}
				catch( Exception e ) { 
					log( "In uninstalling " + e ); 
				}
			}
		}
		ServiceReference refs[] = bundles.getServiceReferences();
		assertTrue( "Bundles should have been uninstalled",  refs==null || refs.length==0);
		
		//
		// Reset the provisioning service
		//
		Hashtable	information = new Hashtable(); 
		information.put( "provisioning.spid", "SPID:test%21" );
		information.put( "provisioning.rsh.secret", RSHTest.secret );
		getProvisioningService().setInformation( information );
	}
	
	/**
	 * Remove the stream handlers
	 */
	protected void tearDown() {
		reset();
		registration.unregister();
		provisioning.close();
	}

	
	private String getHttpServer() {
		return httpServer;
	}
	/*
	 * START OF TESTS
	 */
	
	/**
	 * Test if we actually have RSH stream handler installed. If
	 * read a file and check the contents. This reading will go
	 * through the RSH handler and thus passes all encryption/decryption
	 * routines. The RSH protocol is optional so this case
	 * fails silently if there is no such protocol
	 * 
	 * @spec rsh
	 */
	public void testRSHFile() throws Exception {
		if ( ! rshAvailable() )
			return;
		
		URL ws = new URL(getHttpServer());
		URL rshurl = new URL( "rsh://" + ws.getHost() + ":" + ws.getPort() + "/test/rsh");
		
		// Check if we can get data from this
		InputStream		in = rshurl.openStream();
		assertNotNull( "Stream from RSH URL could not be opened", in );

		byte r [] = collect( in, 0 );
		if ( debug ) {
			FileOutputStream tout = new FileOutputStream("generated"
					+ File.separator
					+ "org.osgi.test.cases.provisioning.target-test.zip");
			tout.write( r );
			tout.close();
		}
		in.close();
		
		ZipInputStream	zin = new ZipInputStream( new ByteArrayInputStream(r) );
		ZipEntry entry = zin.getNextEntry();
		while ( entry != null) {
{'fraction_non_alphanumeric': 0.10054432348367029, 'fraction_numerical': 0.0027993779160186624, 'mean_word_length': 3.5221518987341773, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5941267', 'n_tokens_mistral': 4282, 'n_tokens_neox': 3672, 'n_words': 1316}