Skip to content
Snippets Groups Projects
Commit 0fa945a7 authored by Daryl's avatar Daryl
Browse files

Updated gmail plugin and unit test, removed unused class.

parent 59dc065c
No related branches found
No related tags found
No related merge requests found
...@@ -18,6 +18,7 @@ public interface SimplexTransportReader { ...@@ -18,6 +18,7 @@ public interface SimplexTransportReader {
* argument indicates whether the reader is being closed because of an * argument indicates whether the reader is being closed because of an
* exception and the second argument indicates whether the connection was * exception and the second argument indicates whether the connection was
* recognised, which may affect how resources are disposed of. * recognised, which may affect how resources are disposed of.
* @throws IOException
*/ */
void dispose(boolean exception, boolean recognised); void dispose(boolean exception, boolean recognised) throws IOException;
} }
...@@ -25,6 +25,7 @@ public interface SimplexTransportWriter { ...@@ -25,6 +25,7 @@ public interface SimplexTransportWriter {
* Closes the writer and disposes of any associated resources. The * Closes the writer and disposes of any associated resources. The
* argument indicates whether the writer is being closed because of an * argument indicates whether the writer is being closed because of an
* exception, which may affect how resources are disposed of. * exception, which may affect how resources are disposed of.
* @throws IOException
*/ */
void dispose(boolean exception); void dispose(boolean exception) throws IOException;
} }
package net.sf.briar.plugins.email; package net.sf.briar.plugins.email;
import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
...@@ -8,6 +9,11 @@ import java.util.Collection; ...@@ -8,6 +9,11 @@ import java.util.Collection;
import java.util.Map; import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.concurrent.Executor; import java.util.concurrent.Executor;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.mail.Flags; import javax.mail.Flags;
import javax.mail.Flags.Flag; import javax.mail.Flags.Flag;
import javax.mail.Folder; import javax.mail.Folder;
...@@ -19,8 +25,11 @@ import javax.mail.Session; ...@@ -19,8 +25,11 @@ import javax.mail.Session;
import javax.mail.Store; import javax.mail.Store;
import javax.mail.Transport; import javax.mail.Transport;
import javax.mail.internet.InternetAddress; import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage; import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.search.FlagTerm; import javax.mail.search.FlagTerm;
import javax.mail.util.ByteArrayDataSource;
import javax.microedition.io.StreamConnection; import javax.microedition.io.StreamConnection;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
...@@ -44,7 +53,9 @@ public class GmailPlugin implements SimplexPlugin { ...@@ -44,7 +53,9 @@ public class GmailPlugin implements SimplexPlugin {
private static final TransportId ID = new TransportId(TRANSPORT_ID); private static final TransportId ID = new TransportId(TRANSPORT_ID);
private final Executor pluginExecutor; private final Executor pluginExecutor;
private final SimplexPluginCallback callback; private final SimplexPluginCallback callback;
// private static GmailTransportConnectionReader reader; private static final Logger LOG = Logger.getLogger(GmailPlugin.class
.getName());
// private static GmailTransportConnectionWriter writer; // private static GmailTransportConnectionWriter writer;
public GmailPlugin(Executor pluginExecutor, SimplexPluginCallback callback) { public GmailPlugin(Executor pluginExecutor, SimplexPluginCallback callback) {
...@@ -64,118 +75,100 @@ public class GmailPlugin implements SimplexPlugin { ...@@ -64,118 +75,100 @@ public class GmailPlugin implements SimplexPlugin {
}); });
} }
protected void checkUnreadEmails(Folder inbox) { private void checkUnreadEmails(Folder inbox) {
try { try {
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false); FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message msg[] = inbox.search(ft); Message msg[] = inbox.search(ft);
// System.out.println("Unread Messages: "+ msg.length); // System.out.println("Unread Messages: "+ msg.length);
for (final Message message : msg) { for (final Message message : msg) {
try { callback.readerCreated(new SimplexTransportReader() {
callback.readerCreated(new SimplexTransportReader() {
public InputStream getInputStream() throws IOException {
public InputStream getInputStream() throws IOException { try {
try { return message.getInputStream();
return message.getInputStream(); } catch (MessagingException e) {
} catch (MessagingException e) { if (LOG.isLoggable(Level.WARNING))
e.printStackTrace(); LOG.warning(e.toString());
}
return null;
} }
return null;
public void dispose(boolean exception, }
boolean recognised) {
try { public void dispose(boolean exception, boolean recognised)
message.setFlag(Flag.DELETED, recognised); throws IOException {
message.setFlag(Flag.SEEN, recognised); try {
} catch (MessagingException e) { message.setFlag(Flag.DELETED, recognised);
e.printStackTrace(); message.setFlag(Flag.SEEN, recognised);
} } catch (MessagingException e) {
if (LOG.isLoggable(Level.WARNING))
LOG.warning(e.toString());
} }
}); }
});
// This part for testing purposes only
System.out.println("DATE: "
+ message.getSentDate().toString());
System.out.println("FROM: "
+ message.getFrom()[0].toString());
System.out.println("SUBJECT: "
+ message.getSubject().toString());
System.out.println("CONTENT: "
+ message.getContent().toString());
System.out
.println("=================================================");
} catch (Exception e) {
System.out.println("No Information");
}
} }
} catch (MessagingException e) { } catch (MessagingException e) {
System.out.println(e.toString()); if (LOG.isLoggable(Level.WARNING))
LOG.warning(e.toString());
} }
} }
protected void connectIMAP() { private void connectIMAP() {
Properties props = System.getProperties(); Properties props = new Properties();
props.setProperty("mail.store.protocol", "imaps"); props.setProperty("mail.store.protocol", "imaps");
final ArrayList<String> userPass = getAuthenticationDetails(callback final ArrayList<String> userPass = getAuthenticationDetails(callback
.getConfig()); .getConfig());
if (userPass != null) { if (userPass != null) {
try { try {
Session session = Session.getDefaultInstance(props, null); Session session = Session.getInstance(props, null);
Store store = session.getStore("imaps"); Store store = session.getStore("imaps");
synchronized (this) { store.connect("imap.gmail.com", userPass.get(0),
store.connect("imap.gmail.com", userPass.get(0), userPass.get(1));
userPass.get(1));
}
Folder inbox = store.getFolder("Inbox"); Folder inbox = store.getFolder("Inbox");
inbox.open(Folder.READ_ONLY); inbox.open(Folder.READ_ONLY);
checkUnreadEmails(inbox); checkUnreadEmails(inbox);
} catch (NoSuchProviderException e) { } catch (NoSuchProviderException e) {
System.out.println(e.toString()); if (LOG.isLoggable(Level.WARNING))
System.exit(1); LOG.warning(e.toString());
} catch (MessagingException e) { } catch (MessagingException e) {
System.out.println(e.toString()); if (LOG.isLoggable(Level.WARNING))
System.exit(2); LOG.warning(e.toString());
} }
} }
} }
protected boolean connectSMTP(ContactId cid) { public boolean connectSMTP(ContactId cid) {
boolean sent = false; boolean sent = false;
if (discoverContactEmail(cid) != null) { if (discoverContactEmail(cid) != null) {
Properties prop = new Properties(); Properties props = new Properties();
prop.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.host", "smtp.gmail.com");
prop.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.port", "465");
prop.put("mail.smtp.socketFactory.class", props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory"); "javax.net.ssl.SSLSocketFactory");
prop.put("mail.smtp.auth", "true"); props.put("mail.smtp.auth", "true");
prop.put("mail.smtp.port", "465"); props.put("mail.smtp.port", "465");
final ArrayList<String> userPass = getAuthenticationDetails(callback final ArrayList<String> userPass = getAuthenticationDetails(callback
.getConfig()); .getConfig());
if (userPass != null) { if (userPass != null) {
Session session; Session session;
synchronized (this) { session = Session.getInstance(props,
session = Session.getInstance(prop, new javax.mail.Authenticator() {
new javax.mail.Authenticator() { protected PasswordAuthentication getPasswordAuthentication() {
protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(userPass
return new PasswordAuthentication(userPass .get(0), userPass.get(1));
.get(0), userPass.get(1)); }
} });
});
}
// SimplexTransportWriter writer = createWriter(cid);
sent = sendMessage(session, cid); sent = sendMessage(session, cid);
} }
} }
return sent; return sent;
} }
private synchronized boolean sendMessage(Session session, ContactId cid) { private boolean sendMessage(Session session, ContactId cid) {
boolean sent = false; boolean sent = false;
ByteArrayOutputStream outputStream = null;
try { try {
Message message = new MimeMessage(session); Message message = new MimeMessage(session);
TransportProperties props = callback.getLocalProperties(); TransportProperties props = callback.getLocalProperties();
...@@ -185,7 +178,41 @@ public class GmailPlugin implements SimplexPlugin { ...@@ -185,7 +178,41 @@ public class GmailPlugin implements SimplexPlugin {
message.setRecipients(Message.RecipientType.TO, message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(discoverContactEmail(cid))); InternetAddress.parse(discoverContactEmail(cid)));
message.setSubject("Test Subject"); message.setSubject("Test Subject");
message.setText("Test content");
outputStream = new ByteArrayOutputStream();
callback.writerCreated(cid, new SimplexTransportWriter() {
public boolean shouldFlush() {
return false;
}
public OutputStream getOutputStream() throws IOException {
return null;
}
public long getCapacity() {
return 0;
}
public void dispose(boolean exception) throws IOException {
}
});
byte[] bytes = outputStream.toByteArray();
DataSource dataSource = new ByteArrayDataSource(bytes,
"application/octet-stream");
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setDataHandler(new DataHandler(dataSource));
MimeMultipart mimeMultipart = new MimeMultipart();
mimeMultipart.addBodyPart(messageBodyPart);
message.setContent(mimeMultipart);
// message.setText("Test content");
Transport.send(message); Transport.send(message);
...@@ -193,6 +220,16 @@ public class GmailPlugin implements SimplexPlugin { ...@@ -193,6 +220,16 @@ public class GmailPlugin implements SimplexPlugin {
return sent; return sent;
} catch (MessagingException e) { } catch (MessagingException e) {
return sent; return sent;
} finally {
if (null != outputStream) {
try {
outputStream.close();
outputStream = null;
} catch (Exception e) {
if (LOG.isLoggable(Level.WARNING))
LOG.warning(e.toString());
}
}
} }
} }
...@@ -207,12 +244,12 @@ public class GmailPlugin implements SimplexPlugin { ...@@ -207,12 +244,12 @@ public class GmailPlugin implements SimplexPlugin {
return false; return false;
} }
public long getPollingInterval() throws UnsupportedOperationException { public long getPollingInterval() {
return 0; throw new UnsupportedOperationException();
} }
public void poll(Collection<ContactId> connected) public void poll(Collection<ContactId> connected) {
throws UnsupportedOperationException { throw new UnsupportedOperationException();
} }
public boolean supportsInvitations() { public boolean supportsInvitations() {
...@@ -238,7 +275,7 @@ public class GmailPlugin implements SimplexPlugin { ...@@ -238,7 +275,7 @@ public class GmailPlugin implements SimplexPlugin {
} }
/* /*
* looks up the contact's email address given the contactID * Looks up the contact's email address given the contactID
* @param ContactId * @param ContactId
* @return String email * @return String email
*/ */
...@@ -247,8 +284,11 @@ public class GmailPlugin implements SimplexPlugin { ...@@ -247,8 +284,11 @@ public class GmailPlugin implements SimplexPlugin {
Map<ContactId, TransportProperties> remote = callback Map<ContactId, TransportProperties> remote = callback
.getRemoteProperties(); .getRemoteProperties();
TransportProperties tp = remote.get(cid); TransportProperties tp = remote.get(cid);
String address = tp.get("email"); if (tp != null) {
return address; String address = tp.get("email");
return address;
} else
return null;
} catch (Exception e) { } catch (Exception e) {
return null; return null;
} }
...@@ -262,24 +302,22 @@ public class GmailPlugin implements SimplexPlugin { ...@@ -262,24 +302,22 @@ public class GmailPlugin implements SimplexPlugin {
return null; return null;
} }
public SimplexTransportWriter sendInvitation(PseudoRandom r, long timeout) public SimplexTransportWriter sendInvitation(PseudoRandom r, long timeout) {
throws UnsupportedOperationException { throw new UnsupportedOperationException();
return null;
} }
public SimplexTransportReader acceptInvitation(PseudoRandom r, long timeout) public SimplexTransportReader acceptInvitation(PseudoRandom r, long timeout) {
throws UnsupportedOperationException { throw new UnsupportedOperationException();
return null;
} }
public SimplexTransportWriter sendInvitationResponse(PseudoRandom r, public SimplexTransportWriter sendInvitationResponse(PseudoRandom r,
long timeout) throws UnsupportedOperationException { long timeout) {
return null; throw new UnsupportedOperationException();
} }
public SimplexTransportReader acceptInvitationResponse(PseudoRandom r, public SimplexTransportReader acceptInvitationResponse(PseudoRandom r,
long timeout) throws UnsupportedOperationException { long timeout) {
return null; throw new UnsupportedOperationException();
} }
} }
package net.sf.briar.plugins.email;
import java.io.IOException;
import java.io.InputStream;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.microedition.io.StreamConnection;
import net.sf.briar.api.plugins.simplex.SimplexTransportReader;
public class GmailTransportConnectionReader implements SimplexTransportReader{
private static final Logger LOG = Logger.getLogger(GmailTransportConnectionReader.class.getName());
private final StreamConnection stream;
GmailTransportConnectionReader(StreamConnection stream) {
this.stream = stream;
}
public InputStream getInputStream() throws IOException {
return stream.openInputStream();
}
public void dispose(boolean exception, boolean recognised) {
try {
stream.close();
} catch(IOException e) {
if(LOG.isLoggable(Level.WARNING)) LOG.warning(e.toString());
}
}
}
package net.sf.briar.plugins.email; package net.sf.briar.plugins.email;
import static org.junit.Assert.assertArrayEquals; import static org.junit.Assert.*;
import static org.junit.Assert.assertEquals;
import java.io.IOException; import java.io.IOException;
import java.io.OutputStream;
import java.util.Collection;
import java.util.HashMap; import java.util.HashMap;
import java.util.Map; import java.util.Map;
import java.util.Set;
import java.util.Map.Entry;
import java.util.concurrent.Executors; import java.util.concurrent.Executors;
import net.sf.briar.BriarTestCase;
import net.sf.briar.api.ContactId; import net.sf.briar.api.ContactId;
import net.sf.briar.api.TransportConfig; import net.sf.briar.api.TransportConfig;
import net.sf.briar.api.TransportProperties; import net.sf.briar.api.TransportProperties;
...@@ -15,6 +19,7 @@ import net.sf.briar.api.plugins.simplex.SimplexPluginCallback; ...@@ -15,6 +19,7 @@ import net.sf.briar.api.plugins.simplex.SimplexPluginCallback;
import net.sf.briar.api.plugins.simplex.SimplexTransportReader; import net.sf.briar.api.plugins.simplex.SimplexTransportReader;
import net.sf.briar.api.plugins.simplex.SimplexTransportWriter; import net.sf.briar.api.plugins.simplex.SimplexTransportWriter;
import org.jmock.Mockery;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
...@@ -23,104 +28,100 @@ import org.junit.Test; ...@@ -23,104 +28,100 @@ import org.junit.Test;
* and CONTACT1_EMAIL - (as recipient email address) * and CONTACT1_EMAIL - (as recipient email address)
*/ */
public class GmailPluginTester { public class GmailPluginTest {
SimplexPluginCallback callback;
TransportProperties local, props1;
TransportConfig config;
ContactId test1;
Map<ContactId, TransportProperties> map = new HashMap<ContactId, TransportProperties>();
SimplexPluginCallback callback;
TransportProperties local, props1;
TransportConfig config;
ContactId test1;
Map<ContactId,TransportProperties> map = new HashMap<ContactId, TransportProperties>();
@Before @Before
public void setup() public void setup() {
{
local = new TransportProperties(); local = new TransportProperties();
local.put("email",System.getenv("USER_GMAIL_ADDRESS")); local.put("email", System.getenv("USER_GMAIL_ADDRESS"));
config = new TransportConfig(); config = new TransportConfig();
config.put("username", System.getenv("GMAIL_USERNAME")); config.put("username", System.getenv("GMAIL_USERNAME"));
config.put("password", System.getenv("GMAIL_PASSWORD")); config.put("password", System.getenv("GMAIL_PASSWORD"));
props1 = new TransportProperties(); props1 = new TransportProperties();
props1.put("email", System.getenv("CONTACT1_EMAIL")); props1.put("email", System.getenv("CONTACT1_EMAIL"));
test1 = new ContactId(12); test1 = new ContactId(12);
map.put(test1, props1); map.put(test1, props1);
assertEquals(1,map.size()); assertEquals(1, map.size());
callback = new SimplexPluginCallback() { callback = new SimplexPluginCallback() {
public void showMessage(String... message) { public void showMessage(String... message) {}
}
public boolean showConfirmationMessage(String... message) { public boolean showConfirmationMessage(String... message) {
return false; return false;
} }
public int showChoice(String[] options, String... message) { public int showChoice(String[] options, String... message) {
return 0; return 0;
} }
public void setLocalProperties(TransportProperties p) { public void setLocalProperties(TransportProperties p) {
local = p; local = p;
} }
public void setConfig(TransportConfig c) { public void setConfig(TransportConfig c) {
config = c; config = c;
} }
public Map<ContactId, TransportProperties> getRemoteProperties() { public Map<ContactId, TransportProperties> getRemoteProperties() {
return map; return map;
} }
public TransportProperties getLocalProperties() { public TransportProperties getLocalProperties() {
return local; return local;
} }
public TransportConfig getConfig() { public TransportConfig getConfig() {
return config; return config;
} }
public void writerCreated(ContactId c, SimplexTransportWriter w) {} public void writerCreated(ContactId c, SimplexTransportWriter w) {}
public void readerCreated(SimplexTransportReader r) {} public void readerCreated(SimplexTransportReader r) {}
}; };
callback.setLocalProperties(local); callback.setLocalProperties(local);
callback.setConfig(config); callback.setConfig(config);
} }
@Test
public void testGetID()
{
GmailPlugin pluginTest = new GmailPlugin(Executors.newSingleThreadExecutor(), callback);
assertArrayEquals(GmailPlugin.TRANSPORT_ID,pluginTest.getId().getBytes());
}
@Test @Test
public void testPluginFactoryCreation() public void testGetID() throws IOException {
{ GmailPlugin pluginTest = new GmailPlugin(
GmailPluginFactory plugin = new GmailPluginFactory(); Executors.newSingleThreadExecutor(), callback);
plugin.createPlugin(Executors.newSingleThreadExecutor(), callback); assertArrayEquals(GmailPlugin.TRANSPORT_ID, pluginTest.getId()
.getBytes());
} }
@Test @Test
public void testGmailPluginIMAP() public void testGmailPluginIMAP() throws IOException {
{ GmailPlugin pluginTest = new GmailPlugin(
GmailPlugin pluginTest = new GmailPlugin(Executors.newSingleThreadExecutor(), callback); Executors.newSingleThreadExecutor(), callback);
try { try {
pluginTest.start(); pluginTest.start();
} catch (IOException e) { } catch (IOException e) {
System.out.println("IO Exception got caught"); System.out.println("IO Exception got caught");
fail();
}
finally{
pluginTest.stop();
} }
} }
@Test @Test
public void testGmailSMTP() public void testGmailSMTP() throws IOException {
{ GmailPlugin pluginTest = new GmailPlugin(
GmailPlugin pluginTest = new GmailPlugin(Executors.newSingleThreadExecutor(), callback); Executors.newSingleThreadExecutor(), callback);
assertEquals(true, pluginTest.connectSMTP(test1)); assertEquals(true, pluginTest.connectSMTP(test1));
assertEquals(false, pluginTest.connectSMTP(new ContactId(7))); assertEquals(false, pluginTest.connectSMTP(new ContactId(7)));
pluginTest.stop();
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment