<commit_before>package org.subethamail.smtp.test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import junit.framework.TestCase;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;

import com.sun.mail.smtp.SMTPTransport;

public class BigAttachmentTest  extends TestCase
{
	private final static Logger log = LoggerFactory.getLogger(BigAttachmentTest.class);
	private final static int SMTP_PORT = 1081;
	private final static String TO_CHANGE = "<path>/<your_bigfile.ext>";
		
	// Set the full path name of the big file to use for the test.
	private final static String BIGFILE_PATH = TO_CHANGE;
	
	private Wiser server;
	
	public BigAttachmentTest(String name) 
	{
		super(name);
	}

	protected void setUp() throws Exception
	{
		super.setUp();
		server = new Wiser();
		server.setPort(SMTP_PORT);
		server.start();
	}

	protected void tearDown() throws Exception
	{
		super.tearDown();
		try 
		{ 
			server.stop(); 
		} catch (Exception e)
		{
			e.printStackTrace();
		};
	}
	
	public void testAttachments() throws Exception
	{	
		assertNotSame("To complete this test you must change the BIGFILE_PATH var to point out a file on your disk !", TO_CHANGE, BIGFILE_PATH);
		Properties props = System.getProperties();
		props.setProperty("mail.smtp.host", "localhost");
		props.setProperty("mail.smtp.port", SMTP_PORT+"");
		Session session = Session.getDefaultInstance(props, null);

		MimeMessage baseMsg = new MimeMessage(session);
		MimeBodyPart bp1 = new MimeBodyPart();
		bp1.setHeader("Content-Type", "text/plain");
		bp1.setContent("Hello World!!!", "text/plain; charset=\"ISO-8859-1\"");
		
		// Attach the file
		MimeBodyPart bp2 = new MimeBodyPart();
		FileDataSource fileAttachment = new FileDataSource(BIGFILE_PATH);
		DataHandler dh = new DataHandler(fileAttachment);
		bp2.setDataHandler(dh);
		bp2.setFileName(fileAttachment.getName());

		Multipart multipart = new MimeMultipart();
		multipart.addBodyPart(bp1);
		multipart.addBodyPart(bp2);

		baseMsg.setFrom(new InternetAddress("Ted <<EMAIL>>"));
		baseMsg.setRecipient(Message.RecipientType.TO, new InternetAddress(
				"<EMAIL>"));
		baseMsg.setSubject("Test Big attached file message");
		baseMsg.setContent(multipart);
        baseMsg.saveChanges();        
        
        log.debug("Send started");        
        Transport t = new SMTPTransport(session, new URLName("smtp://localhost:"+SMTP_PORT));
        t.connect();
        t.sendMessage(baseMsg, new Address[] {new InternetAddress(
				"<EMAIL>")});
        t.close();
        
        WiserMessage msg = server.getMessages().get(0);
        
        assertEquals(1, server.getMessages().size());		
		assertEquals("<EMAIL>", msg.getEnvelopeReceiver());
		
		File compareFile = File.createTempFile("attached", ".tmp");
		log.debug("Writing received attachment ...");
		FileOutputStream fos = new FileOutputStream(compareFile);
		((MimeMultipart) msg.getMimeMessage().getContent()).getBodyPart(1).getDataHandler().writeTo(fos);
		fos.close();
		log.debug("Checking integrity ...");
		assertTrue(checkIntegrity(new File(BIGFILE_PATH), compareFile));
		log.debug("Checking integrity DONE");
		compareFile.delete();
		msg.dispose();
	}
	
	private boolean checkIntegrity(File src, File dest) throws IOException, NoSuchAlgorithmException
	{
		BufferedInputStream ins = new BufferedInputStream(new FileInputStream(src));
		BufferedInputStream ind = new BufferedInputStream(new FileInputStream(dest));
		MessageDigest md1 = MessageDigest.getInstance("MD5");
		MessageDigest md2 = MessageDigest.getInstance("MD5");
		
		if (ins == null || ind == null)
			return false;
		
		int r = 0;
		byte[] buf1 = new byte[32768];
		byte[] buf2 = new byte[32768];
		
		while (r !=-1)
		{
			r = ins.read(buf1);
			ind.read(buf2);
			
			md1.update(buf1);
			md2.update(buf2);
		}
		
		ins.close();
		ind.close();
		return MessageDigest.isEqual(md1.digest(), md2.digest());
	}
}<commit_msg>Fixed to be tester friendly ;p<commit_after>package org.subethamail.smtp.test;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.URLName;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

import junit.framework.TestCase;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.subethamail.wiser.Wiser;
import org.subethamail.wiser.WiserMessage;

import com.sun.mail.smtp.SMTPTransport;

/**
 * This class tests the transfer speed of emails that carry 
 * attached files.
 * 
 * @author <NAME> &lt;<EMAIL>&gt;
 */
public class BigAttachmentTest extends TestCase
{
	private final static Logger log = LoggerFactory.getLogger(BigAttachmentTest.class);
	private final static int SMTP_PORT = 1081;
	private final static String TO_CHANGE = "<path>/<your_bigfile.ext>";
	private final static int BUFFER_SIZE = 32768;
		
	// Set the full path name of the big file to use for the test.
	private final static String BIGFILE_PATH = TO_CHANGE;
	
	private Wiser server;
	
	public BigAttachmentTest(String name) 
	{
		super(name);
	}

	protected void setUp() throws Exception
	{
		super.setUp();
		server = new Wiser();
		server.setPort(SMTP_PORT);
		server.setReceiveBufferSize(BUFFER_SIZE);
		server.start();		
	}

	protected void tearDown() throws Exception
	{
		super.tearDown();
		try 
		{ 
			server.stop(); 
		} catch (Exception e)
		{
			e.printStackTrace();
		};
	}
	
	public void testAttachments() throws Exception
	{	
		assertNotSame("To complete this test you must change the BIGFILE_PATH var to point out a file on your disk !", TO_CHANGE, BIGFILE_PATH);
		Properties props = System.getProperties();
		props.setProperty("mail.smtp.host", "localhost");
		props.setProperty("mail.smtp.port", SMTP_PORT+"");
		Session session = Session.getInstance(props);

		MimeMessage baseMsg = new MimeMessage(session);
		MimeBodyPart bp1 = new MimeBodyPart();
		bp1.setHeader("Content-Type", "text/plain");
		bp1.setContent("Hello World!!!", "text/plain; charset=\"ISO-8859-1\"");
		
		// Attach the file
		MimeBodyPart bp2 = new MimeBodyPart();
		FileDataSource fileAttachment = new FileDataSource(BIGFILE_PATH);
		DataHandler dh = new DataHandler(fileAttachment);
		bp2.setDataHandler(dh);
		bp2.setFileName(fileAttachment.getName());

		Multipart multipart = new MimeMultipart();
		multipart.addBodyPart(bp1);
		multipart.addBodyPart(bp2);

		baseMsg.setFrom(new InternetAddress("Ted <<EMAIL>>"));
		baseMsg.setRecipient(Message.RecipientType.TO, new InternetAddress(
				"<EMAIL>"));
		baseMsg.setSubject("Test Big attached file message");
		baseMsg.setContent(multipart);
        baseMsg.saveChanges();
        
        log.debug("Send started");        
        Transport t = new SMTPTransport(session, new URLName("smtp://localhost:"+SMTP_PORT));
		long started = System.currentTimeMillis();
        t.connect();
        t.sendMessage(baseMsg, new Address[] {new InternetAddress(
				"<EMAIL>")});
        t.close();
        started = System.currentTimeMillis() - started;
        log.info("Elapsed ms = "+started);
        
        WiserMessage msg = server.getMessages().get(0);
        
        assertEquals(1, server.getMessages().size());		
		assertEquals("<EMAIL>", msg.getEnvelopeReceiver());
		
		File compareFile = File.createTempFile("attached", ".tmp");
		log.debug("Writing received attachment ...");

		FileOutputStream fos = new FileOutputStream(compareFile);
		((MimeMultipart) msg.getMimeMessage().getContent()).getBodyPart(1).getDataHandler().writeTo(fos);
		fos.close();
		log.debug("Checking integrity ...");
		assertTrue(checkIntegrity(new File(BIGFILE_PATH), compareFile));
		log.debug("Checking integrity DONE");
		compareFile.delete();
		msg.dispose();
	}
	
	private boolean checkIntegrity(File src, File dest) throws IOException, NoSuchAlgorithmException
	{
		BufferedInputStream ins = new BufferedInputStream(new FileInputStream(src));
		BufferedInputStream ind = new BufferedInputStream(new FileInputStream(dest));
		MessageDigest md1 = MessageDigest.getInstance("MD5");
		MessageDigest md2 = MessageDigest.getInstance("MD5");
		
		if (ins == null || ind == null)
			return false;
		
		int r = 0;
		byte[] buf1 = new byte[BUFFER_SIZE];
		byte[] buf2 = new byte[BUFFER_SIZE];
		
		while (r !=-1)
		{
			r = ins.read(buf1);
			ind.read(buf2);
			
			md1.update(buf1);
			md2.update(buf2);
		}
		
		ins.close();
		ind.close();
		return MessageDigest.isEqual(md1.digest(), md2.digest());
	}
}
{'fraction_non_alphanumeric': 0.12150570188717327, 'fraction_numerical': 0.0091835704914724, 'mean_word_length': 3.7805113362276894, 'pattern_counts': {'":': 0, '<': 19, '<?xml version=': 0, '>': 19, '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': '371393', 'n_tokens_mistral': 3338, 'n_tokens_neox': 3048, 'n_words': 669}