package net.tomp2p.utils;

import java.io.IOException;
import java.net.InetAddress;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.security.spec.InvalidKeySpecException;
import java.util.ArrayList;
import java.util.List;

import net.tomp2p.Utils2;
import net.tomp2p.connection.DSASignatureFactory;
import net.tomp2p.message.Buffer;
import net.tomp2p.message.Message;
import net.tomp2p.peers.PeerAddress;
import net.tomp2p.peers.PeerSocketAddress;

import org.junit.Assert;
import org.junit.Test;

public class TestMessageUtils {

	private final DSASignatureFactory signatureFactory;

	public TestMessageUtils() {
		signatureFactory = new DSASignatureFactory();
	}

	@Test
	public void testEncodeDecodeRelayedMessage() throws InvalidKeyException, SignatureException, IOException, NoSuchAlgorithmException,
			InvalidKeySpecException {
		Message message = Utils2.createDummyMessage();

		List<PeerSocketAddress> relays = new ArrayList<PeerSocketAddress>();
		relays.add(new PeerSocketAddress(InetAddress.getLocalHost(), 8000, 9000));
		relays.add(new PeerSocketAddress(InetAddress.getLocalHost(), 8001, 9001));
		relays.add(new PeerSocketAddress(InetAddress.getLocalHost(), 8002, 9002));

		PeerAddress sender = Utils2.createAddress().changeRelayed(true).changePeerSocketAddresses(relays)
				.changeFirewalledTCP(true).changeFirewalledUDP(true);
		message.sender(sender);
		message.senderSocket(sender.createSocketTCP());
		
		PeerAddress receiver = Utils2.createAddress();
		message.recipient(receiver);
		message.recipientSocket(receiver.createSocketTCP());
		
		Buffer encoded = MessageUtils.encodeMessage(message, signatureFactory);
		Message decoded = MessageUtils.decodeMessage(encoded, message.recipientSocket(), message.senderSocket(),
				signatureFactory);
		Assert.assertEquals(message.peerSocketAddresses().size(), decoded.peerSocketAddresses().size());
	}
}

@@ -1830,21 +1830,17 @@
 ocket(),
-%0A%09%09%09%09
+ 
 signatur
@@ -1952,10 +1952,232 @@
 ());%0A%09%7D%0A
+%09%0A%09@Test%0A%09public void testEncodeDecodeString() %7B%0A%09%09String test = %22dummy%22;%0A%09%09Buffer encoded = MessageUtils.encodeString(test);%0A%09%09String decoded = MessageUtils.decodeString(encoded);%0A%09%09Assert.assertEquals(test, decoded);%0A%09%7D%0A
 %7D%0A

Add test for String encoding / decoding