1   package com.atlassian.seraph.cookie;
2   
3   import junit.framework.TestCase;
4   
5   public class TestEncryptedCookieEncoder extends TestCase
6   {
7       private CookieEncoder cookieEncoder;
8   
9       protected void setUp() throws Exception
10      {
11          cookieEncoder = new EncryptedCookieEncoder("some password");
12      }
13  
14      protected void tearDown() throws Exception
15      {
16          cookieEncoder = null;
17      }
18  
19      public void testEncodePasswordCookieAndDecode()
20      {
21          final String cookieValue = cookieEncoder.encodePasswordCookie(TestUser.LAGONIL.getUsername(), TestUser.LAGONIL.getPassword(), null);
22          assertNotNull(cookieValue);
23          assertTrue(cookieValue.indexOf("%3C") != -1); // checking for encoded "<"
24          assertTrue(cookieValue.indexOf("%3E") != -1); // checking for encoded ">"
25  
26          final String[] vals = cookieEncoder.decodePasswordCookie(cookieValue, null);
27          assertEquals(TestUser.LAGONIL.getUsername(), vals[0]);
28          assertEquals(TestUser.LAGONIL.getPassword(), vals[1]);
29      }
30  
31      public void testDecodePasswordCookieBadText()
32      {
33          String[] vals = cookieEncoder.decodePasswordCookie("asthasasdf", null);
34          assertNotNull(vals);
35          assertEquals("", vals[0]);
36          assertEquals("", vals[1]);
37      }
38  
39      public void testCannotCreateEncryptedCookieEncoderWithNullPassword()
40      {
41          try
42          {
43              new EncryptedCookieEncoder(null);
44              fail();
45          }
46          catch (IllegalArgumentException ex)
47          {
48              // do nothing, test passed
49          }
50      }
51  
52      private static class TestUser
53      {
54          // This user seem to consistently get '<' and '>' in its encoded value, see SER-117
55          static final TestUser LAGONIL = new TestUser("lagonil", "sphere");
56  
57          private final String username;
58  
59          private final String password;
60  
61          private TestUser(String username, String password)
62          {
63              this.password = password;
64              this.username = username;
65          }
66  
67          String getUsername()
68          {
69              return username;
70          }
71  
72          String getPassword()
73          {
74              return password;
75          }
76      }
77  }