<commit_before>/*
 * Part of the CCNx Java Library.
 *
 * Copyright (C) 2012 Palo Alto Research Center, Inc.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation.
 * This library 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
 * Lesser General Public License for more details. You should have received
 * a copy of the GNU Lesser General Public License along with this library;
 * 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.sync;

import java.lang.reflect.Array;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Random;
import java.util.Vector;

import junit.framework.Assert;

import org.ccnx.ccn.CCNSync;
import org.ccnx.ccn.CCNSyncHandler;
import org.ccnx.ccn.config.SystemConfiguration;
import org.ccnx.ccn.impl.security.crypto.CCNDigestHelper;
import org.ccnx.ccn.impl.support.Log;
import org.ccnx.ccn.io.RepositoryFileOutputStream;
import org.ccnx.ccn.io.content.ConfigSlice;
import org.ccnx.ccn.io.content.SyncNodeComposite;
import org.ccnx.ccn.profiles.SegmentationProfile;
import org.ccnx.ccn.profiles.metadata.MetadataProfile;
import org.ccnx.ccn.profiles.sync.Sync;
import org.ccnx.ccn.protocol.ContentName;
import org.ccnx.ccn.protocol.ContentObject;
import org.ccnx.ccn.protocol.Interest;
import org.ccnx.ccn.test.CCNTestBase;
import org.ccnx.ccn.test.CCNTestHelper;
import org.junit.Before;
import org.junit.Test;

public class CCNSyncTest extends CCNTestBase implements CCNSyncHandler {
	
	public static CCNTestHelper testHelper = new CCNTestHelper(CCNSyncTest.class);
	
	ContentName prefix;
	ContentName topo;
	int BUF_SIZE = 1024;
	int maxBytes = 10 * BUF_SIZE;
	Vector<ContentName> callbackNames = new Vector<ContentName>();
	String errorMessage = null;
	
	@Before
	public void setUpNameSpace() {
		prefix = testHelper.getTestNamespace("ccnSyncTest");
		topo = testHelper.getTestNamespace("topoPrefix");
		Log.fine(Log.FAC_TEST, "setting up namespace for sync test  data: {0} syncControlTraffic: {1}", prefix, topo);
	}
	
	@Test
	public void testSyncStartWithHandle() throws Exception {
		Log.info(Log.FAC_TEST, "Starting testSyncStartWithHandle");
		ContentName prefix1;
		prefix1 = prefix.append("slice1");

		CCNSync sync1 = new CCNSync();
		ConfigSlice slice1 = sync1.startSync(getHandle, topo, prefix1, this);
		
		sync1.stopSync(this, slice1);
			
		Log.info(Log.FAC_TEST, "Completed testSyncStartWithHandle");
	}
	
	@Test
	public void testSyncStartWithoutHandle() throws Exception {
		Log.info(Log.FAC_TEST, "Starting testSyncStartWithoutHandle");
		
		ContentName prefix1;
		prefix1 = prefix.append("slice2");
		CCNSync sync1 = new CCNSync();
		ConfigSlice slice2 = sync1.startSync(getHandle, topo, prefix1, this);
		
		sync1.stopSync(this, slice2);

		Log.info(Log.FAC_TEST,"Finished running testSyncStartWithoutHandle");
	}

	@Test
	public void testSyncClose() throws Exception {
		Log.info(Log.FAC_TEST, "Starting testSyncClose");
		
		ContentName prefix1;
		prefix1 = prefix.append("slice3");
		CCNSync sync1 = new CCNSync();
		ConfigSlice slice3 = sync1.startSync(getHandle, topo, prefix1, this);
		
		//the slice should be written..  now save content and get a callback.
		Log.fine(Log.FAC_TEST, "writing out file: {0}", prefix1);
		
		// Write a 100 block file to test a true sync tree
		int segments = writeFile(prefix1, false, SystemConfiguration.BLOCK_SIZE * 100);
		int segmentCheck = checkCallbacks(prefix1, segments, 0);
		if (segmentCheck!=0)
			Assert.fail("Did not receive all of the callbacks");
		else
			Log.fine(Log.FAC_TEST, "I got all the callbacks for part 1 of testSyncClose!");
		
		//now close the callback interface
		sync1.stopSync(this, slice3);
		
		//then close and make sure we don't get a callback
		prefix1 = prefix1.append("round2");
		Log.fine(Log.FAC_TEST, "writing out file: {0}", prefix1);
		segments = writeFile(prefix1, true, 0);  //this should be a new version
		segmentCheck = checkCallbacks(prefix1, segments, 0);
		if (segmentCheck != segments) {
			//we must have gotten callbacks...  bad.
			Assert.fail("received callbacks after interface was closed.  ERROR");
		}
		Log.fine(Log.FAC_TEST, "I didn't get callbacks after I stopped sync for myself!");

		Log.info(Log.FAC_TEST,"Finished running testSyncStop");
	}
	
	@Test
	public void testCallbackRegistration() throws Exception {
		Log.info(Log.FAC_TEST, "Starting testCallbackRegistration");
		
		ContentName prefix1;
		ContentName prefix2;
		prefix1 = prefix.append("slice4");
		CCNSync sync1 = new CCNSync();
		ConfigSlice slice4 = sync1.startSync(getHandle, topo, prefix1, this);
		prefix2 = prefix.append("slice5");
		ConfigSlice slice5 = sync1.startSync(getHandle, topo, prefix2, this);
		
		//the slice should be written..  now save content and get a callback.
		Log.fine(Log.FAC_TEST, "writing out file: {0}", prefix1);
		int segments = writeFile(prefix1, true, 0);
		int segments2 = writeFile(prefix2, true, 0);
		int segmentCheck = checkCallbacks(prefix1, segments, 0);
		if (segmentCheck!=0)
			Assert.fail("Did not receive all of the callbacks");
		segmentCheck = checkCallbacks(prefix2, segments2, 0);
		if (segmentCheck!=0)
			Assert.fail("Did not receive all of the callbacks");
		
		//now close the callback interface
		sync1.stopSync(this, slice4);
		sync1.stopSync(this, slice5);
			
		Log.info(Log.FAC_TEST,"Finished running testSyncStop");
	}
	

	@Test
	public void testSyncRestart() throws Exception {
		Log.info(Log.FAC_TEST, "Starting testSyncRestart");
		
		ContentName prefix1;
		prefix1 = prefix.append("slice6");
		CCNSync sync1 = new CCNSync();
		ConfigSlice slice6 = sync1.startSync(getHandle, topo, prefix1, this);
		
		//the slice should be written..  now save content and get a callback.
		Log.fine(Log.FAC_TEST, "writing out file: {0}", prefix1);
		int segments = writeFile(prefix1, true, 0);
		int segmentCheck = checkCallbacks(prefix1, segments, 0);
		if (segmentCheck!=0)
			Assert.fail("Did not receive all of the callbacks");
		else
			Log.fine(Log.FAC_TEST, "I got all the callbacks for part 1 of testSyncRestart!");
		
		//now close the callback interface
		sync1.stopSync(this, slice6);
		callbackNames.clear();
		
		//then close and make sure we don't get a callback
		ContentName prefix1a = prefix1.append("round2");
		Log.fine(Log.FAC_TEST, "writing out file: {0}", prefix1a);
		segments = writeFile(prefix1a, true, 0);  //this should be a new version
		segmentCheck = checkCallbacks(prefix1a, segments, 0);
		if (segmentCheck != segments) {
			//we must have gotten callbacks...  bad.
			Assert.fail("received callbacks after interface was closed.  ERROR");
		}
		Log.fine(Log.FAC_TEST, "I didn't get callbacks after I stopped sync for myself!");
		
		//now restart sync and make sure i get everything
		ContentName prefix1b = prefix1.append("round3");
		//ConfigSlice slice3 = sync1.startSync(topo, prefix1, null, this);
		ConfigSlice slice6b = sync1.startSync(getHandle, topo, prefix1, this);
		
		Log.fine(Log.FAC_TEST, "check if slice 6 == slice 6b, they should be equal!");<commit_msg>add code to delete slices created during the test after the test runs.  refs #100809<commit_after>/*
 * Part of the CCNx Java Library.
 *
 * Copyright (C) 2012 Palo Alto Research Center, Inc.
 *
 * This library is free software; you can redistribute it and/or modify it
 * under the terms of the GNU Lesser General Public License version 2.1
 * as published by the Free Software Foundation.
 * This library 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
 * Lesser General Public License for more details. You should have received
 * a copy of the GNU Lesser General Public License along with this library;
 * 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.sync;

import java.io.IOException;
import java.lang.reflect.Array;
import java.security.DigestOutputStream;
import java.security.MessageDigest;
import java.util.Arrays;
import java.util.Random;
import java.util.Vector;

import junit.framework.Assert;

import org.ccnx.ccn.CCNHandle;
import org.ccnx.ccn.CCNSync;
import org.ccnx.ccn.CCNSyncHandler;
import org.ccnx.ccn.config.SystemConfiguration;
import org.ccnx.ccn.impl.security.crypto.CCNDigestHelper;
import org.ccnx.ccn.impl.support.Log;
import org.ccnx.ccn.io.RepositoryFileOutputStream;
import org.ccnx.ccn.io.content.ConfigSlice;
import org.ccnx.ccn.io.content.SyncNodeComposite;
import org.ccnx.ccn.profiles.SegmentationProfile;
import org.ccnx.ccn.profiles.metadata.MetadataProfile;
import org.ccnx.ccn.profiles.sync.Sync;
import org.ccnx.ccn.protocol.ContentName;
import org.ccnx.ccn.protocol.ContentObject;
import org.ccnx.ccn.protocol.Interest;
import org.ccnx.ccn.test.CCNTestBase;
import org.ccnx.ccn.test.CCNTestHelper;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.Test;

public class CCNSyncTest extends CCNTestBase implements CCNSyncHandler {
	
	public static CCNTestHelper testHelper = new CCNTestHelper(CCNSyncTest.class);
	
	ContentName prefix;
	ContentName topo;
	int BUF_SIZE = 1024;
	int maxBytes = 10 * BUF_SIZE;
	Vector<ContentName> callbackNames = new Vector<ContentName>();
	String errorMessage = null;
	static Vector<ConfigSlice> slices = new Vector<ConfigSlice>();
	
	@Before
	public void setUpNameSpace() {
		prefix = testHelper.getTestNamespace("ccnSyncTest");
		topo = testHelper.getTestNamespace("topoPrefix");
		Log.fine(Log.FAC_TEST, "setting up namespace for sync test  data: {0} syncControlTraffic: {1}", prefix, topo);
	}
	
	@AfterClass
	public static void cleanup() {
		//need to delete the slices to clean up after the test.
		for(ConfigSlice s: slices)
			try {
				s.deleteSlice(CCNHandle.getHandle());
			} catch (IOException e) {
				Log.warning(Log.FAC_TEST, "failed to get handle to clean up sync slices:{0}", e.getMessage());
			}
		
	}
	
	@Test
	public void testSyncStartWithHandle() throws Exception {
		Log.info(Log.FAC_TEST, "Starting testSyncStartWithHandle");
		ContentName prefix1;
		prefix1 = prefix.append("slice1");

		CCNSync sync1 = new CCNSync();
		ConfigSlice slice1 = sync1.startSync(getHandle, topo, prefix1, this);
		slices.add(slice1);
		
		sync1.stopSync(this, slice1);
			
		Log.info(Log.FAC_TEST, "Completed testSyncStartWithHandle");
	}
	
	@Test
	public void testSyncStartWithoutHandle() throws Exception {
		Log.info(Log.FAC_TEST, "Starting testSyncStartWithoutHandle");
		
		ContentName prefix1;
		prefix1 = prefix.append("slice2");
		CCNSync sync1 = new CCNSync();
		ConfigSlice slice2 = sync1.startSync(getHandle, topo, prefix1, this);
		slices.add(slice2);
		
		sync1.stopSync(this, slice2);

		Log.info(Log.FAC_TEST,"Finished running testSyncStartWithoutHandle");
	}

	@Test
	public void testSyncClose() throws Exception {
		Log.info(Log.FAC_TEST, "Starting testSyncClose");
		
		ContentName prefix1;
		prefix1 = prefix.append("slice3");
		CCNSync sync1 = new CCNSync();
		ConfigSlice slice3 = sync1.startSync(getHandle, topo, prefix1, this);
		slices.add(slice3);
		
		//the slice should be written..  now save content and get a callback.
		Log.fine(Log.FAC_TEST, "writing out file: {0}", prefix1);
		
		// Write a 100 block file to test a true sync tree
		int segments = writeFile(prefix1, false, SystemConfiguration.BLOCK_SIZE * 100);
		int segmentCheck = checkCallbacks(prefix1, segments, 0);
		if (segmentCheck!=0)
			Assert.fail("Did not receive all of the callbacks");
		else
			Log.fine(Log.FAC_TEST, "I got all the callbacks for part 1 of testSyncClose!");
		
		//now close the callback interface
		sync1.stopSync(this, slice3);
		
		//then close and make sure we don't get a callback
		prefix1 = prefix1.append("round2");
		Log.fine(Log.FAC_TEST, "writing out file: {0}", prefix1);
		segments = writeFile(prefix1, true, 0);  //this should be a new version
		segmentCheck = checkCallbacks(prefix1, segments, 0);
		if (segmentCheck != segments) {
			//we must have gotten callbacks...  bad.
			Assert.fail("received callbacks after interface was closed.  ERROR");
		}
		Log.fine(Log.FAC_TEST, "I didn't get callbacks after I stopped sync for myself!");

		Log.info(Log.FAC_TEST,"Finished running testSyncStop");
	}
	
	@Test
	public void testCallbackRegistration() throws Exception {
		Log.info(Log.FAC_TEST, "Starting testCallbackRegistration");
		
		ContentName prefix1;
		ContentName prefix2;
		prefix1 = prefix.append("slice4");
		CCNSync sync1 = new CCNSync();
		ConfigSlice slice4 = sync1.startSync(getHandle, topo, prefix1, this);
		slices.add(slice4);
		prefix2 = prefix.append("slice5");
		ConfigSlice slice5 = sync1.startSync(getHandle, topo, prefix2, this);
		
		//the slice should be written..  now save content and get a callback.
		Log.fine(Log.FAC_TEST, "writing out file: {0}", prefix1);
		int segments = writeFile(prefix1, true, 0);
		int segments2 = writeFile(prefix2, true, 0);
		int segmentCheck = checkCallbacks(prefix1, segments, 0);
		if (segmentCheck!=0)
			Assert.fail("Did not receive all of the callbacks");
		segmentCheck = checkCallbacks(prefix2, segments2, 0);
		if (segmentCheck!=0)
			Assert.fail("Did not receive all of the callbacks");
		
		//now close the callback interface
		sync1.stopSync(this, slice4);
		sync1.stopSync(this, slice5);

			
		Log.info(Log.FAC_TEST,"Finished running testSyncStop");
	}
	

	@Test
	public void testSyncRestart() throws Exception {
		Log.info(Log.FAC_TEST, "Starting testSyncRestart");
		
		ContentName prefix1;
		prefix1 = prefix.append("slice6");
		CCNSync sync1 = new CCNSync();
		ConfigSlice slice6 = sync1.startSync(getHandle, topo, prefix1, this);
		slices.add(slice6);
		
		//the slice should be written..  now save content and get a callback.
		Log.fine(Log.FAC_TEST, "writing out file: {0}", prefix1);
		int segments = writeFile(prefix1, true, 0);
		int segmentCheck = checkCallbacks(prefix1, segments, 0);
		if (segmentCheck!=0)
			Assert.fail("Did not receive all of the callbacks");
		else
			Log.fine(Log.FAC_TEST, "I got all the callbacks for part 1 of testSyncRestart!");
		
		//now close the callback interface
		sync1.stopSync(this, slice6);
		callbackNames.clear();
		
		//then close and make sure we don't get a callback
		ContentName prefix1a = prefix1.append("round2");
		Log.fine(Log.FAC_TEST, "writing out file: {0}", prefix1a);
		segments = writeFile(prefix1a, true, 0);  //this should be a new version
		segmentCheck = checkCallbacks(prefix1a, segments, 0);
		if (segmentCheck != segments) {
			//we must have gotten callbacks...  bad.
			Assert.fail("received callbacks after interface was closed.  ERROR");
		}
		Log.fine(Log.FAC_TEST, "I didn't get callbacks after I stopped sync for myself!");
		
		//now restart sync and make sure i get everything
		ContentName prefix1b = prefix1.append("round3");
		//ConfigSlice slice3 = sync1.startSync(topo, prefix1, null, this);
		ConfigSlice slice6b = sync1.startSync(getHandle, topo, prefix1, this);
		
		Log.fine(Log.FAC_TEST, "check if slice 6 == slice 6b, they should be equal!");
{'fraction_non_alphanumeric': 0.10866761253708242, 'fraction_numerical': 0.019089384754288664, 'mean_word_length': 4.475635593220339, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, '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': '3433955', 'n_tokens_mistral': 5385, 'n_tokens_neox': 4859, 'n_words': 1480}