~ubuntu-branches/ubuntu/precise/triplea/precise

« back to all changes in this revision

Viewing changes to test/games/strategy/net/MessengerLoginTest.java

  • Committer: Package Import Robot
  • Author(s): Scott Howard
  • Date: 2011-11-11 21:40:11 UTC
  • Revision ID: package-import@ubuntu.com-20111111214011-sehf2rwat36o2xqf
Tags: upstream-1.3.2.2
ImportĀ upstreamĀ versionĀ 1.3.2.2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
package games.strategy.net;
 
2
 
 
3
import games.strategy.engine.EngineVersion;
 
4
import games.strategy.engine.framework.startup.login.*;
 
5
import games.strategy.test.TestUtil;
 
6
import games.strategy.util.MD5Crypt;
 
7
 
 
8
import java.net.SocketAddress;
 
9
import java.util.*;
 
10
 
 
11
import junit.framework.TestCase;
 
12
 
 
13
public class MessengerLoginTest extends TestCase
 
14
{
 
15
    private int SERVER_PORT = -1;
 
16
    
 
17
    @Override
 
18
    public void setUp()
 
19
    {
 
20
        SERVER_PORT = TestUtil.getUniquePort();
 
21
    }
 
22
    
 
23
    
 
24
    
 
25
    public void testSimple() throws Exception
 
26
    {
 
27
        ILoginValidator validator = new ILoginValidator()
 
28
        {        
 
29
            public String verifyConnection(Map<String, String> propertiesSentToClient, Map<String, String> propertiesReadFromClient, String clientName, String mac, SocketAddress remoteAddress)
 
30
            {
 
31
                return null;
 
32
            }
 
33
        
 
34
            public Map<String,String> getChallengeProperties(String userName, SocketAddress remoteAddress)
 
35
            {
 
36
                return new HashMap<String,String>();
 
37
            }        
 
38
        };
 
39
        
 
40
        IConnectionLogin login = new IConnectionLogin()
 
41
        {
 
42
        
 
43
            public void notifyFailedLogin(String message)
 
44
            {
 
45
                 fail();
 
46
            }
 
47
        
 
48
            public Map<String, String> getProperties(Map<String,String> challengProperties)
 
49
            {
 
50
                return new HashMap<String,String>();
 
51
            }
 
52
        
 
53
        };
 
54
        
 
55
        
 
56
        ServerMessenger server = new ServerMessenger("test", SERVER_PORT);
 
57
        try
 
58
        {
 
59
            server.setLoginValidator(validator);
 
60
            
 
61
            server.setAcceptNewConnections(true);
 
62
            
 
63
            String mac = MacFinder.GetHashedMacAddress();
 
64
            ClientMessenger client = new ClientMessenger("localhost", SERVER_PORT, "fee", mac, new DefaultObjectStreamFactory(), login);
 
65
            
 
66
            client.shutDown();
 
67
        }
 
68
        finally
 
69
        {
 
70
            server.shutDown();            
 
71
        }
 
72
 
 
73
        
 
74
    }
 
75
    
 
76
 
 
77
    
 
78
    public void testRefused() throws Exception
 
79
    {
 
80
        ILoginValidator validator = new ILoginValidator()
 
81
        {        
 
82
            public String verifyConnection(Map<String, String> propertiesSentToClient, Map<String, String> propertiesReadFromClient, String clientName, String mac, SocketAddress remoteAddress)
 
83
            {
 
84
                return "error";
 
85
            }
 
86
        
 
87
            public Map<String,String> getChallengeProperties(String userName, SocketAddress remoteAddress)
 
88
            {
 
89
                return new HashMap<String,String>();
 
90
            }
 
91
        };
 
92
        
 
93
        IConnectionLogin login = new IConnectionLogin()
 
94
        {
 
95
        
 
96
            public void notifyFailedLogin(String message)
 
97
            {
 
98
                 
 
99
            }
 
100
        
 
101
            public Map<String, String> getProperties(Map<String,String> challengProperties)
 
102
            {
 
103
                return new HashMap<String,String>();
 
104
            }
 
105
        
 
106
        };
 
107
        
 
108
        
 
109
        ServerMessenger server = new ServerMessenger("test", SERVER_PORT);
 
110
        try
 
111
        {
 
112
            server.setLoginValidator(validator);
 
113
            
 
114
            server.setAcceptNewConnections(true);
 
115
            
 
116
            try
 
117
            {
 
118
                String mac = MacFinder.GetHashedMacAddress();
 
119
                new ClientMessenger("localhost", SERVER_PORT, "fee", mac, new DefaultObjectStreamFactory(), login);
 
120
                fail("we should not have logged in");
 
121
            }
 
122
            catch(CouldNotLogInException expected)
 
123
            {
 
124
                //we expect this exception
 
125
            }
 
126
            
 
127
            
 
128
        }
 
129
        finally
 
130
        {
 
131
            server.shutDown();            
 
132
        }
 
133
 
 
134
        
 
135
    }
 
136
 
 
137
    public void testGetMagic()
 
138
    {
 
139
        String salt = "falafel";
 
140
        String password = "king";
 
141
        String encrypted = MD5Crypt.crypt(password, salt, MD5Crypt.MAGIC);
 
142
        assertEquals(salt,MD5Crypt.getSalt(MD5Crypt.MAGIC, encrypted));
 
143
        
 
144
    }
 
145
    
 
146
    
 
147
    public void testPassword() throws Exception
 
148
    {
 
149
      
 
150
        ClientLoginValidator validator = new ClientLoginValidator();
 
151
        validator.setGamePassword("foo");
 
152
        
 
153
        IConnectionLogin login = new IConnectionLogin()
 
154
        {
 
155
        
 
156
            public void notifyFailedLogin(String message)
 
157
            {
 
158
                 fail();
 
159
            }
 
160
        
 
161
            public Map<String, String> getProperties(Map<String,String> challengProperties)
 
162
            {
 
163
                String salt = challengProperties.get(ClientLoginValidator.SALT_PROPERTY);
 
164
                
 
165
                HashMap<String,String> rVal = new HashMap<String,String>();
 
166
                rVal.put(ClientLogin.PASSWORD_PROPERTY, MD5Crypt.crypt("foo", salt));
 
167
                rVal.put(ClientLogin.ENGINE_VERSION_PROPERTY, EngineVersion.VERSION.toString());
 
168
                return rVal;
 
169
            }
 
170
        
 
171
        };
 
172
        
 
173
        
 
174
        ServerMessenger server = new ServerMessenger("test", SERVER_PORT);
 
175
        try
 
176
        {
 
177
            server.setLoginValidator(validator);
 
178
            
 
179
            server.setAcceptNewConnections(true);
 
180
            
 
181
            String mac = MacFinder.GetHashedMacAddress();
 
182
            ClientMessenger client = new ClientMessenger("localhost", SERVER_PORT, "fee", mac, new DefaultObjectStreamFactory(), login);
 
183
            
 
184
            client.shutDown();
 
185
        }
 
186
        finally
 
187
        {
 
188
            server.shutDown();            
 
189
        }
 
190
 
 
191
        
 
192
        
 
193
    }
 
194
 
 
195
}
 
196
 
 
197