View Javadoc
1   /*
2    * Copyright (C) 2003-2014 eXo Platform SAS.
3    *
4    * This is free software; you can redistribute it and/or modify it
5    * under the terms of the GNU Lesser General Public License as
6    * published by the Free Software Foundation; either version 3 of
7    * the License, or (at your option) any later version.
8    *
9    * This software is distributed in the hope that it will be useful,
10   * but WITHOUT ANY WARRANTY; without even the implied warranty of
11   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12   * Lesser General Public License for more details.
13   *
14   * You should have received a copy of the GNU Lesser General Public
15   * License along with this software; if not, write to the Free
16   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
17   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
18   */
19  package org.exoplatform.mobile.tests;
20  
21  import static org.junit.Assert.assertFalse;
22  import static org.junit.Assert.assertTrue;
23  import static org.junit.Assert.assertEquals;
24  
25  import org.exoplatform.utils.ExoUtils;
26  import org.robolectric.annotation.Config;
27  
28  import android.os.Build;
29  
30  import org.junit.After;
31  import org.junit.Before;
32  import org.junit.Test;
33  import org.junit.runner.RunWith;
34  import org.robolectric.RobolectricTestRunner;
35  
36  /**
37   * Created by The eXo Platform SAS Author : Philippe Aristote
38   * paristote@exoplatform.com Oct 28, 2014
39   */
40  @RunWith(RobolectricTestRunner.class)
41  @Config(sdk=Build.VERSION_CODES.LOLLIPOP) // API LEVEL 21
42  public class ExoUtilsTest {
43  
44      @Before
45      public void setUp() throws Exception {
46      }
47  
48      @After
49      public void tearDown() throws Exception {
50      }
51  
52      @Test
53      public void testUrlValidationFailed() {
54          String[] validUrls = { "http://www.mycompany.", "shttps://safe.company.com",
55                  "www.mycompany.com!", "http://int.my.company_com.vn", "http://1192.168.4.42" };
56  
57          for (String url : validUrls) {
58              assertFalse(url + " should NOT be valid.", ExoUtils.isUrlValid(url));
59          }
60      }
61  
62      @Test
63      public void testUrlValidationPassed() {
64          String[] validUrls = { "http://www.mycompany.com", "https://safe.company.com",
65                  "www.mycompany.com", "http://int.my.company.com.vn", "http://192.168.4.42",
66                  "http://192.168.4.42:8080", "http://my-company.com" };
67  
68          for (String url : validUrls) {
69              assertTrue(url + " should be valid.", ExoUtils.isUrlValid(url));
70          }
71      }
72  
73      // @Test
74      public void testDocumentUrlValidationFailed() {
75  
76      }
77  
78      // @Test
79      public void testDocumentUrlValidationPassed() {
80  
81      }
82  
83      // @Test
84      public void testDocumentUrlEncoding() {
85  
86      }
87  
88      @Test
89      public void testStripUrl() {
90          String url1 = "http://www.mycompany.com";
91          String url2 = "https://www.mycompany.com";
92          String url3 = "http://www.mycompany.com:8080";
93          String url4 = "http://www.mycompany.com/some/path";
94          String url5 = "http://www.mycompany.com?foo=bar";
95          String url6 = "www.mycompany.com";
96  
97          assertEquals("http://www.mycompany.com", ExoUtils.stripUrl(url1));
98          assertEquals("https://www.mycompany.com", ExoUtils.stripUrl(url2));
99          assertEquals("http://www.mycompany.com:8080", ExoUtils.stripUrl(url3));
100         assertEquals("http://www.mycompany.com", ExoUtils.stripUrl(url4));
101         assertEquals("http://www.mycompany.com", ExoUtils.stripUrl(url5));
102         assertEquals("http://www.mycompany.com", ExoUtils.stripUrl(url6));
103     }
104 
105     @Test
106     public void testAccountNameValidationFailed() {
107         String incorrectChars = "~ ` ! @ # $ % ^ & * ( ) = { } [ ] | \\ : ; \" ' , < > ? / - _ .";
108         String[] chars = incorrectChars.split(" ");
109         for (String c : chars) {
110             String incorrectName = "Account " + c;
111             assertFalse(incorrectName + " should be an invalid account name",
112                         ExoUtils.isServerNameValid(incorrectName));
113         }
114         assertFalse("null should be invalid", ExoUtils.isServerNameValid(null));
115     }
116 
117     @Test
118     public void testAccountNameValidationPassed() {
119         String[] correctNames = { "Account", "Account 123", "My Account", "my long account name" };
120         for (String name : correctNames) {
121             assertTrue(name + " should be a valid account name", ExoUtils.isServerNameValid(name));
122         }
123     }
124 
125     @Test
126     public void testAccountUsernameValidationFailed() {
127         // list all forbidden characters here, separated by a space
128         String incorrectChars = "~ ` ! @ # $ % ^ & * ( ) = { } [ ] | \\ : ; \" ' , < > ? /";
129         String[] chars = incorrectChars.split(" ");
130         for (String c : chars) {
131             String wrongUsername = "john" + c + "doe";
132             assertFalse("Username '" + wrongUsername + "' should not have been validated",
133                         ExoUtils.isUsernameValid(wrongUsername));
134         }
135         // test username that contains a space
136         assertFalse("Username 'john doe' should not have been validated",
137                     ExoUtils.isUsernameValid("john doe"));
138     }
139 
140     @Test
141     public void testAccountUsernameValidationPassed() {
142         String[] testUsernames = { "johndoe", "john.doe", "john-doe", "john_doe", "john+doe",
143                 "JohnDoe", "johndoe1234" };
144         for (String username : testUsernames) {
145             assertTrue("Username '" + username + "' should have been validated",
146                        ExoUtils.isUsernameValid(username));
147         }
148     }
149 
150     @Test
151     public void testEmailValidationFailed() {
152         String[] invalidEmails = { "test@@example.com", "test@example@example.com",
153                 "test@example.", "test@example+com" };
154 
155         for (String email : invalidEmails) {
156             assertFalse(email + " should be an invalid email address", ExoUtils.isEmailValid(email));
157         }
158     }
159 
160     @Test
161     public void testEmailValidationPassed() {
162         String[] validEmails = { "test@example.com", "test@example.com.vn", "test.foo@example.com" };
163         for (String email : validEmails) {
164             assertTrue(email + " should be a valid email address", ExoUtils.isEmailValid(email));
165         }
166     }
167 
168     @Test
169     public void testForbiddenUrls() {
170         String[] forbiddenUrls = { "http://exoplatform.net", "http://wks-acc.exoplatform.org",
171                 "http://netstg.exoplatform.org" };
172         for (String url : forbiddenUrls) {
173             assertTrue(url + " should be forbidden", ExoUtils.isURLForbidden(url));
174         }
175     }
176 
177     @Test
178     public void testAccountNameFromUrlExtraction() {
179         String url1 = "http://mycompany.com";
180         assertEquals("Mycompany", ExoUtils.getAccountNameFromURL(url1, ""));
181 
182         String url2 = "http://int.mycompany.com";
183         assertEquals("Int", ExoUtils.getAccountNameFromURL(url2, ""));
184 
185         String url3 = "http://intranet.secure.mycompany.com.vn";
186         assertEquals("Intranet", ExoUtils.getAccountNameFromURL(url3, ""));
187 
188         String url4 = "https://mycompany.com"; // HTTPS
189         assertEquals("Mycompany", ExoUtils.getAccountNameFromURL(url4, ""));
190 
191         String url5 = "http://mycompany.com:8080"; // Port 8080
192         assertEquals("Mycompany", ExoUtils.getAccountNameFromURL(url5, ""));
193 
194         String url6 = "http://localhost";
195         assertEquals("Localhost", ExoUtils.getAccountNameFromURL(url6, ""));
196 
197         String url7 = "http://192.168.4.42";
198         assertEquals("192.168.4.42", ExoUtils.getAccountNameFromURL(url7, ""));
199     }
200 
201     @Test
202     public void testIpAddressCheck() {
203         String[] correctIPs = { "192.168.4.42", "1.1.1.1", "255.255.255.255" };
204         String[] incorrectIPs = { "z.a.b.c", "0123.45.67.89", "192.168.4.42:8080" };
205         for (String ip : correctIPs) {
206             assertTrue("IP " + ip + " should be correct", ExoUtils.isCorrectIPAddress(ip));
207         }
208         for (String ip : incorrectIPs) {
209             assertFalse("IP " + ip + " should be incorrect", ExoUtils.isCorrectIPAddress(ip));
210         }
211     }
212 
213     @Test
214     public void testCapitalizeString() {
215         String string = "word";
216         assertEquals("Word", ExoUtils.capitalize(string));
217 
218         string = "two words";
219         assertEquals("Two words", ExoUtils.capitalize(string));
220 
221         string = "w";
222         assertEquals("W", ExoUtils.capitalize(string));
223 
224         string = "two Words";
225         assertEquals("Two Words", ExoUtils.capitalize(string));
226 
227         string = "Two words";
228         assertEquals("Two words", ExoUtils.capitalize(string));
229     }
230 
231 }