<commit_before>/**
 * A CCNx library test.
 *
 * Copyright (C) 2008, 2009 Palo Alto Research Center, Inc.
 *
 * This work is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the
 * Free Software Foundation. 
 * This work is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details. You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

package org.ccnx.ccn.test.profiles.access;

import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.SortedSet;

import javax.xml.stream.XMLStreamException;

import org.bouncycastle.crypto.CryptoException;
import org.ccnx.ccn.CCNHandle;
import org.ccnx.ccn.KeyManager;
import org.ccnx.ccn.config.ConfigurationException;
import org.ccnx.ccn.config.UserConfiguration;
import org.ccnx.ccn.impl.support.Log;
import org.ccnx.ccn.io.content.ContentNotReadyException;
import org.ccnx.ccn.io.content.Link;
import org.ccnx.ccn.profiles.access.AccessControlManager;
import org.ccnx.ccn.profiles.access.AccessControlProfile;
import org.ccnx.ccn.profiles.access.Group;
import org.ccnx.ccn.profiles.access.GroupManager;
import org.ccnx.ccn.profiles.nameenum.EnumeratedNameList;
import org.ccnx.ccn.protocol.ContentName;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;



public class GroupTestRepo {
	
	static boolean USE_REPO = true;
	static int NUM_USERS = 10;
	static char [] USER_PASSWORD = new String("password").toCharArray();
	
	static ContentName testStorePrefix = null;
	static ContentName userNamespace = null;
	static ContentName groupStore = null;
	static ContentName userKeyStore = null;
	
	static EnumeratedNameList _userList = null;
	static CCNHandle _handle = null;
	
	static String myUserName = null;
	
	/**
	 * Have to make a bunch of users.
	 * @throws Exception
	 */
	static TestUserData users = null;
	static CCNHandle userHandle = null;
	static AccessControlManager _acm = null;
	static GroupManager _gm = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception { 
		try {
			myUserName = UserConfiguration.userName();
			System.out.println("Username = " + myUserName);
			testStorePrefix = UserConfiguration.defaultNamespace();
			userNamespace = ContentName.fromNative(testStorePrefix, "home");
			userKeyStore = ContentName.fromNative(testStorePrefix, "_access_");
			groupStore = AccessControlProfile.groupNamespaceName(testStorePrefix);

			System.out.println("prefix: " + testStorePrefix);
			System.out.println("group store: " + groupStore);
			System.out.println("user store: " + userNamespace);

			_handle = CCNHandle.open();
			userHandle = _handle;
	
			
			users = new TestUserData(userKeyStore, NUM_USERS,
					USE_REPO,
					USER_PASSWORD, userHandle);
			users.saveUserPK2Repo(userNamespace);
			
			_acm = new AccessControlManager(testStorePrefix, groupStore, userNamespace);
			_acm.publishMyIdentity(myUserName, KeyManager.getDefaultKeyManager().getDefaultPublicKey());
			_userList = _acm.userList();
			_gm = _acm.groupManager();
		} catch (Exception e) {
			Log.warning("Exception in setupBeforeClass: " + e);
			Log.warningStackTrace(e);
			throw e;
		}
	}

	private void testRemoveUsers(ArrayList<Link> removeMembers, Group grp) throws InvalidKeyException, CryptoException, XMLStreamException, IOException, ConfigurationException{
		boolean succeed = false;
		int retries = 0;
		while(!succeed){
			retries ++;
			System.out.print("................trying to remove user...........");
			try{
					grp.removeUsers(removeMembers);
					succeed = true;
			}catch(ContentNotReadyException e){
				succeed = false;
			}
		}
		System.out.println(".....................removed user, number of retries:..............." + retries);
	}
	
	private void testAddUsers(ArrayList<Link> removeMembers, Group grp) throws InvalidKeyException, CryptoException, XMLStreamException, IOException, ConfigurationException{
		boolean succeed = false;
		int retries = 0;
		while(!succeed){
			retries ++;
			System.out.print("................trying to add user...........");
			try{
					grp.addUsers(removeMembers);
					succeed = true;
			}catch(ContentNotReadyException e){
				succeed = false;
			}
		}
		System.out.println(".....................added user, number of retries:..............." + retries);
	}
	
	
	@Test<commit_msg>refs #398 GroupTestRepo retries when faced with AccessDeniedException<commit_after>/**
 * A CCNx library test.
 *
 * Copyright (C) 2008, 2009 Palo Alto Research Center, Inc.
 *
 * This work is free software; you can redistribute it and/or modify it under
 * the terms of the GNU General Public License version 2 as published by the
 * Free Software Foundation. 
 * This work is distributed in the hope that it will be useful, but WITHOUT ANY
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details. You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 * Boston, MA 02110-1301, USA.
 */

package org.ccnx.ccn.test.profiles.access;

import static org.junit.Assert.assertTrue;

import java.io.IOException;
import java.security.InvalidKeyException;
import java.security.PrivateKey;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Random;
import java.util.SortedSet;

import javax.xml.stream.XMLStreamException;

import org.bouncycastle.crypto.CryptoException;
import org.ccnx.ccn.CCNHandle;
import org.ccnx.ccn.KeyManager;
import org.ccnx.ccn.config.ConfigurationException;
import org.ccnx.ccn.config.UserConfiguration;
import org.ccnx.ccn.impl.support.Log;
import org.ccnx.ccn.io.content.ContentNotReadyException;
import org.ccnx.ccn.io.content.Link;
import org.ccnx.ccn.profiles.access.AccessControlManager;
import org.ccnx.ccn.profiles.access.AccessControlProfile;
import org.ccnx.ccn.profiles.access.AccessDeniedException;
import org.ccnx.ccn.profiles.access.Group;
import org.ccnx.ccn.profiles.access.GroupManager;
import org.ccnx.ccn.profiles.nameenum.EnumeratedNameList;
import org.ccnx.ccn.protocol.ContentName;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Test;



public class GroupTestRepo {
	
	static boolean USE_REPO = true;
	static int NUM_USERS = 10;
	static char [] USER_PASSWORD = new String("password").toCharArray();
	
	static ContentName testStorePrefix = null;
	static ContentName userNamespace = null;
	static ContentName groupStore = null;
	static ContentName userKeyStore = null;
	
	static EnumeratedNameList _userList = null;
	static CCNHandle _handle = null;
	
	static String myUserName = null;
	
	/**
	 * Have to make a bunch of users.
	 * @throws Exception
	 */
	static TestUserData users = null;
	static CCNHandle userHandle = null;
	static AccessControlManager _acm = null;
	static GroupManager _gm = null;

	@BeforeClass
	public static void setUpBeforeClass() throws Exception { 
		try {
			myUserName = UserConfiguration.userName();
			System.out.println("Username = " + myUserName);
			testStorePrefix = UserConfiguration.defaultNamespace();
			userNamespace = ContentName.fromNative(testStorePrefix, "home");
			userKeyStore = ContentName.fromNative(testStorePrefix, "_access_");
			groupStore = AccessControlProfile.groupNamespaceName(testStorePrefix);

			System.out.println("prefix: " + testStorePrefix);
			System.out.println("group store: " + groupStore);
			System.out.println("user store: " + userNamespace);

			_handle = CCNHandle.open();
			userHandle = _handle;
	
			
			users = new TestUserData(userKeyStore, NUM_USERS,
					USE_REPO,
					USER_PASSWORD, userHandle);
			users.saveUserPK2Repo(userNamespace);
			
			_acm = new AccessControlManager(testStorePrefix, groupStore, userNamespace);
			_acm.publishMyIdentity(myUserName, KeyManager.getDefaultKeyManager().getDefaultPublicKey());
			_userList = _acm.userList();
			_gm = _acm.groupManager();
		} catch (Exception e) {
			Log.warning("Exception in setupBeforeClass: " + e);
			Log.warningStackTrace(e);
			throw e;
		}
	}

	private void testRemoveUsers(ArrayList<Link> removeMembers, Group grp) throws InvalidKeyException, CryptoException, XMLStreamException, IOException, ConfigurationException{
		boolean succeed = false;
		int retries = 0;
		while(!succeed){
			retries ++;
			System.out.print("................trying to remove user...........");
			try{
					grp.removeUsers(removeMembers);
					succeed = true;
			}catch(ContentNotReadyException e){
				succeed = false;
			}catch(AccessDeniedException e){
				succeed = false;
			}
		}
		System.out.println(".....................removed user, number of retries:..............." + retries);
	}
	
	private void testAddUsers(ArrayList<Link> removeMembers, Group grp) throws InvalidKeyException, CryptoException, XMLStreamException, IOException, ConfigurationException{
		boolean succeed = false;
		int retries = 0;
		while(!succeed){
			retries ++;
			System.out.print("................trying to add user...........");
			try{
					grp.addUsers(removeMembers);
					succeed = true;
			}catch(ContentNotReadyException e){
				succeed = false;
			}catch(AccessDeniedException e){
				succeed = false;
			}
		}
		System.out.println(".....................added user, number of retries:..............." + retries);
	}
	
	
	@Test
{'fraction_non_alphanumeric': 0.11993533393957764, 'fraction_numerical': 0.005355158128725877, 'mean_word_length': 4.037150127226463, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 2}, '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': '374287', 'n_tokens_mistral': 3200, 'n_tokens_neox': 2855, 'n_words': 776}