1 package org.xmlBlaster.test.authentication;
  2 
  3 import java.util.logging.Logger;
  4 import org.xmlBlaster.util.Global;
  5 import org.xmlBlaster.util.EmbeddedXmlBlaster;
  6 import org.xmlBlaster.util.FileLocator;
  7 import org.xmlBlaster.client.qos.ConnectQos;
  8 import org.xmlBlaster.client.qos.ConnectReturnQos;
  9 import org.xmlBlaster.client.I_XmlBlasterAccess;
 10 
 11 import org.xmlBlaster.test.Util;
 12 
 13 
 14 import junit.framework.*;
 15 
 16 public class TestAuthenticationHtPassWd extends TestCase
 17 {
 18   private EmbeddedXmlBlaster serverThread = null;
 19   private final String RIGHT_USERNAME = "existingUser";
 20   private final String PARTIAL_USERNAME = "existingSomeThingElseStandsBehind";
 21   private final String RIGHT_PASSWORD = "existingUserPW";
 22   private final String WRONG_USERNAME = "notExistingUser";
 23   private final String WRONG_PASSWORD = "notExistingUserPW";
 24   private String userhome = "";
 25   private Global glob = null;
 26    private static Logger log = Logger.getLogger(TestAuthenticationHtPassWd.class.getName());
 27   private I_XmlBlasterAccess con = null;
 28   private int serverPort = 7604;
 29 
 30   public final String ME = "TestAuthenticationHtPassWd";
 31 
 32 
 33 
 34   public TestAuthenticationHtPassWd (String name)
 35   { super(name);
 36     this.glob = new Global();
 37 
 38     this.userhome = glob.getProperty().get("user.home","/home/astelzl")+java.io.File.separatorChar;
 39     try
 40     { FileLocator.writeFile(userhome+"test.htpasswd","existingUser:yZum5CYzDk.EE\n");
 41       FileLocator.writeFile(userhome+"test.htpasswd2","existing:yZum5CYzDk.EE\n");
 42       FileLocator.writeFile(userhome+"test.htpasswd1","*");
 43     }
 44     catch(Exception ex)
 45     { assertTrue("Could not create password files in directory '" + userhome + "'. Tests won't work!",false);
 46     }
 47   }                                                         
 48 
 49   protected void setUp()
 50   {
 51   }
 52 
 53   private void setupTestCase(int testcase)
 54   { 
 55     String[] ports = Util.getOtherServerPorts(serverPort);
 56     String[] args = new String[4+ports.length];
 57     switch (testcase)
 58     { case 1: args[0] = "-Security.Server.Plugin.htpasswd.secretfile";      
 59               args[1] = userhome+"test.htpasswd2";
 60               args[2] = "-Security.Server.Plugin.htpasswd.allowPartialUsername";
 61               args[3] = "true";
 62               break;
 63       case 2: args[0] = "-Security.Server.Plugin.htpasswd.secretfile";
 64               args[1] = userhome+"test.htpasswd";
 65               args[2] = "-Security.Server.Plugin.htpasswd.allowPartialUsername";
 66               args[3] = "false";
 67               break;  
 68       case 3: args[0] = "-Security.Server.Plugin.htpasswd.secretfile";
 69               args[1] = userhome+"test.htpasswd";
 70               args[2] = "-Security.Server.Plugin.htpasswd.allowPartialUsername";
 71               args[3] = "false";
 72               break;  
 73       case 4: args[0] = "-Security.Server.Plugin.htpasswd.secretfile";
 74               args[1] = userhome+"test.htpasswd1";
 75               args[2] = "-Security.Server.Plugin.htpasswd.allowPartialUsername";
 76               args[3] = "false";
 77               break;  
 78     }
 79     for (int i=0;i<ports.length ;i++ ) {
 80       args[i+4] = ports[i];
 81     }
 82     glob.init(args);
 83     serverThread = EmbeddedXmlBlaster.startXmlBlaster(glob);
 84   }
 85 
 86   protected void tearDown() {
 87      try { Thread.sleep(1000);} catch(Exception ex) {} 
 88      if (serverThread != null)
 89        serverThread.stopServer(true);
 90      glob.init(Util.getDefaultServerPorts());
 91      Util.resetPorts(glob);
 92      this.glob = null;
 93     
 94      this.con = null;
 95      Global.instance().shutdown();
 96   }
 97 
 98   public void testAuthHtPassWordCase1()
 99   { log.info("Testcase1");
100     setupTestCase(1);
101     boolean isValue=true;
102     try
103     { con = glob.getXmlBlasterAccess();
104     }
105     catch(Exception ex)
106     { log.severe("Could not initialize I_XmlBlasterAccess: " + ex.toString());
107       ex.printStackTrace();
108     }
109     try
110     { ConnectQos qos = new ConnectQos(glob,PARTIAL_USERNAME, RIGHT_PASSWORD);
111       ConnectReturnQos conRetQos = con.connect(qos, null);
112       con.disconnect(null);
113     }
114     catch(Exception ex)
115     { log.info("Could not connect: " + ex.toString());
116       ex.printStackTrace();
117       isValue = false;
118     }
119     assertTrue("Could not connect although it should have been possible with the specified beginning of username and password",isValue);
120                  
121   }
122   
123   public void testAuthHtPassWordCase2()
124   { log.info("Testcase2");
125     setupTestCase(2);
126     boolean isValue = true;
127     try
128     { con = glob.getXmlBlasterAccess();
129     }
130     catch(Exception ex)
131     { log.severe("Could not initialize I_XmlBlasterAccess");
132     }
133     try
134     { ConnectQos qos = new ConnectQos(glob,RIGHT_USERNAME, RIGHT_PASSWORD);
135       ConnectReturnQos conRetQos = con.connect(qos, null);
136       con.disconnect(null);
137     }
138     catch(Exception ex)
139     { log.info("Could not connect");
140       isValue = false;
141       ex.printStackTrace();
142     }
143     assertTrue("Could not connect although it should have been possible with the specified username and password",isValue);
144 
145   }
146 
147   public void testAuthHtPassWordCaseWrongPassword()
148   { log.info("Testcase3");
149     setupTestCase(3);
150     boolean isValue = false;
151     try
152     { con = glob.getXmlBlasterAccess();
153     }
154     catch(Exception ex)
155     { log.severe("Could not initialize I_XmlBlasterAccess");
156     }
157     try
158     { ConnectQos qos = new ConnectQos(glob,WRONG_USERNAME, WRONG_PASSWORD);
159       ConnectReturnQos conRetQos = con.connect(qos, null);
160       con.disconnect(null);
161       assertTrue("Could connect although it should not have been possible with the specified username and password",isValue);
162     }
163     catch(Exception ex)
164     { isValue = true;
165       log.info("Could not connect");
166     }
167   }
168 
169   public void testAuthHtPassWordCase3()
170   { log.info("Testcase4");
171     setupTestCase(4);
172     boolean isValue = true;
173     try
174     { con = glob.getXmlBlasterAccess();
175     }
176     catch(Exception ex)
177     { log.severe("Could not initialize I_XmlBlasterAccess");
178     }
179     try
180     { ConnectQos qos = new ConnectQos(glob,WRONG_USERNAME, WRONG_PASSWORD);
181       ConnectReturnQos conRetQos = con.connect(qos, null);
182       con.disconnect(null);
183     }
184     catch(Exception ex)
185     { log.info("Could not connect");
186       isValue = false;
187     }
188     assertTrue("Could not connect although it should have been possible as any username and password is authenticated",isValue);
189   }
190   
191   
192 }


syntax highlighted by Code2HTML, v. 0.9.1