|
| 1 | +package net.sharksystem.asap; |
| 2 | + |
| 3 | +import net.sharksystem.asap.util.ASAPPeerHandleConnectionThread; |
| 4 | +import net.sharksystem.cmdline.ExampleASAPChunkReceivedListener; |
| 5 | +import net.sharksystem.cmdline.TCPStream; |
| 6 | +import org.junit.Assert; |
| 7 | +import org.junit.Test; |
| 8 | + |
| 9 | +import java.io.IOException; |
| 10 | +import java.util.List; |
| 11 | + |
| 12 | +public class CryptoTests { |
| 13 | + public static final String WORKING_SUB_DIRECTORY = "cryptoTests/"; |
| 14 | + public static final String ALICE_PEER_NAME = "Alice"; |
| 15 | + public static final String BOB_PEER_NAME = "Bob"; |
| 16 | + public static final String CLARA_PEER_NAME = "Clara"; |
| 17 | + public static final String APPNAME = "encryptedChat"; |
| 18 | + public static final String CHAT_TOPIC = "topicA"; |
| 19 | + public static final int EXAMPLE_PORT = 7070; |
| 20 | + public static final String EXAMPLE_MESSAGE_STRING = "Hi"; |
| 21 | + |
| 22 | + @Test |
| 23 | + public void noExchangeNotSigned() throws IOException, ASAPException, InterruptedException { |
| 24 | + ASAPEngineFS.removeFolder(WORKING_SUB_DIRECTORY); // clean previous version before |
| 25 | + |
| 26 | + ///// Prepare Alice |
| 27 | + String aliceFolder = WORKING_SUB_DIRECTORY + ALICE_PEER_NAME; |
| 28 | + |
| 29 | + // ASAPChunkReceivedListener - an example |
| 30 | + ExampleASAPChunkReceivedListener aliceChunkListener = new ExampleASAPChunkReceivedListener(aliceFolder); |
| 31 | + |
| 32 | + // setup alice peer |
| 33 | + ASAPPeer alicePeer = ASAPPeerFS.createASAPPeer(ALICE_PEER_NAME, aliceFolder, aliceChunkListener); |
| 34 | + |
| 35 | + // setup chat on alice peer |
| 36 | + ASAPEngine aliceChatEngine = alicePeer.createEngineByFormat(APPNAME); |
| 37 | + // false is default but makes test more obvious |
| 38 | + aliceChatEngine.getASAPCommunicationControl().setSendEncryptedMessages(false); |
| 39 | + aliceChatEngine.getASAPCommunicationControl().setSendSignedMessages(false); |
| 40 | + |
| 41 | + // create a message |
| 42 | + String messageAlice = EXAMPLE_MESSAGE_STRING; |
| 43 | + |
| 44 | + // transform to bytes - there are more elaborate ways to produce a byte array of course |
| 45 | + byte[] messageBytes = messageAlice.getBytes(); |
| 46 | + |
| 47 | + // write a message - we are still offline |
| 48 | + aliceChatEngine.add(CHAT_TOPIC, messageBytes); |
| 49 | + |
| 50 | + ///// Prepare Bob |
| 51 | + String bobFolder = WORKING_SUB_DIRECTORY + BOB_PEER_NAME; |
| 52 | + |
| 53 | + // ASAPChunkReceivedListener - an example |
| 54 | + ExampleASAPChunkReceivedListener bobChunkListener = new ExampleASAPChunkReceivedListener(bobFolder); |
| 55 | + |
| 56 | + // setup bob peer |
| 57 | + ASAPPeer bobPeer = ASAPPeerFS.createASAPPeer(BOB_PEER_NAME, bobFolder, bobChunkListener); |
| 58 | + |
| 59 | + // setup chat on alice peer |
| 60 | + ASAPEngine bobChatEngine = bobPeer.createEngineByFormat(APPNAME); |
| 61 | + // bob expects signed and encrypted what Alice not provides |
| 62 | + bobChatEngine.getASAPEnginePermissionSettings().setReceivedMessagesMustBeEncrypted(true); |
| 63 | + bobChatEngine.getASAPEnginePermissionSettings().setReceivedMessagesMustBeSigned(true); |
| 64 | + |
| 65 | + /////////////// create a connection - in real apps it is presumably a bluetooth wifi direct etc. connection |
| 66 | + // TCPStream is a helper class for connection establishment |
| 67 | + TCPStream aliceStream = new TCPStream(EXAMPLE_PORT, true, "alice2bob"); |
| 68 | + TCPStream bobStream = new TCPStream(EXAMPLE_PORT, false, "b2a"); |
| 69 | + |
| 70 | + // start tcp server or client and try to connect |
| 71 | + aliceStream.start(); |
| 72 | + bobStream.start(); |
| 73 | + |
| 74 | + // wait until connection is established |
| 75 | + aliceStream.waitForConnection(); |
| 76 | + bobStream.waitForConnection(); |
| 77 | + //////////////// end of connection establishment - a simulation in some way - but real enough. It is real tcp. |
| 78 | + |
| 79 | + // let both asap peers run an asap session |
| 80 | + ASAPPeerHandleConnectionThread aliceThread = new ASAPPeerHandleConnectionThread(alicePeer, |
| 81 | + aliceStream.getInputStream(), aliceStream.getOutputStream()); |
| 82 | + |
| 83 | + // alice is up and running in a thread |
| 84 | + aliceThread.start(); |
| 85 | + |
| 86 | + // run bob in this test thread |
| 87 | + bobPeer.handleConnection(bobStream.getInputStream(), bobStream.getOutputStream()); |
| 88 | + |
| 89 | + // at this point give both asap engines some time to run their asap session - then we check what happened. |
| 90 | + Thread.sleep(1000); |
| 91 | + |
| 92 | + // we assume the asap session was performed |
| 93 | + |
| 94 | + // bob chunk received listener must have received something |
| 95 | + List<ExampleASAPChunkReceivedListener.ASAPChunkReceivedParameters> receivedList = |
| 96 | + bobChunkListener.getReceivedList(); |
| 97 | + Assert.assertTrue(receivedList.isEmpty()); |
| 98 | + |
| 99 | + } |
| 100 | +} |
0 commit comments