legion-server 0.1.0

🏛️ Legion Server - Secure IRC server with Legion Protocol support
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
use super::*;
use crate::state::{ServerState, Connection, Channel};
use crate::protocol::{Message, Reply};
use std::sync::Arc;
use tokio::sync::{RwLock, mpsc};
use std::net::{SocketAddr, IpAddr, Ipv4Addr};

async fn setup_test_state() -> (Arc<RwLock<ServerState>>, u64, u64) {
    let state = Arc::new(RwLock::new(ServerState::new()));
    
    let (tx1, _) = mpsc::channel(100);
    let (tx2, _) = mpsc::channel(100);
    
    let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 12345);
    
    let requester_id = 1;
    let member_id = 2;
    
    let mut state_guard = state.write().await;
    
    // Add connections
    let mut requester = Connection::new(requester_id, addr, tx1);
    requester.nickname = Some("requester".to_string());
    requester.username = Some("requester".to_string());
    requester.hostname = "test.host".to_string();
    requester.registered = true;
    state_guard.connections.insert(requester_id, requester);
    state_guard.nicknames.insert("requester".to_string(), requester_id);
    
    let mut member = Connection::new(member_id, addr, tx2);
    member.nickname = Some("member".to_string());
    member.username = Some("member".to_string());
    member.hostname = "member.host".to_string();
    member.registered = true;
    state_guard.connections.insert(member_id, member);
    state_guard.nicknames.insert("member".to_string(), member_id);
    
    // Create test channels
    let mut public_channel = Channel::new("#public".to_string());
    public_channel.add_member(requester_id, false);
    public_channel.add_member(member_id, false);
    public_channel.topic = Some("Public channel topic".to_string());
    state_guard.channels.insert("#public".to_string(), public_channel);
    
    let mut secret_channel = Channel::new("#secret".to_string());
    secret_channel.add_member(member_id, true);
    secret_channel.modes.push('s'); // secret mode
    secret_channel.topic = Some("Secret channel topic".to_string());
    state_guard.channels.insert("#secret".to_string(), secret_channel);
    
    let mut private_channel = Channel::new("#private".to_string());
    private_channel.add_member(member_id, true);
    private_channel.modes.push('p'); // private mode
    private_channel.topic = Some("Private channel topic".to_string());
    state_guard.channels.insert("#private".to_string(), private_channel);
    
    let mut empty_channel = Channel::new("#empty".to_string());
    state_guard.channels.insert("#empty".to_string(), empty_channel);
    
    let mut no_topic_channel = Channel::new("#notopic".to_string());
    no_topic_channel.add_member(requester_id, false);
    state_guard.channels.insert("#notopic".to_string(), no_topic_channel);
    
    drop(state_guard);
    
    (state, requester_id, member_id)
}

#[tokio::test]
async fn test_list_all_channels() {
    let (state, requester_id, _) = setup_test_state().await;
    
    let params = vec![];
    let result = handle_list(state, requester_id, params).await;
    
    assert!(result.is_ok());
    let messages = result.unwrap();
    
    // Should have: start (321) + visible channels (322) + end (323)
    // Visible channels: #public, #empty, #notopic (requester is member of #public and #notopic)
    assert!(messages.len() >= 3); // At least start + end + some channels
    
    // Check list start
    assert_eq!(messages[0].command, "321");
    assert_eq!(messages[0].params[0], "requester");
    
    // Check list end
    let last_msg = messages.last().unwrap();
    assert_eq!(last_msg.command, "323");
    assert_eq!(last_msg.params[0], "requester");
    assert_eq!(last_msg.params[1], "End of LIST");
    
    // Check that we have channel listings
    let channel_messages: Vec<_> = messages.iter()
        .filter(|msg| msg.command == "322")
        .collect();
    
    // Should see at least public channels
    assert!(!channel_messages.is_empty());
    
    // Check for public channel
    let public_listing = channel_messages.iter()
        .find(|msg| msg.params[1] == "#public");
    assert!(public_listing.is_some());
    let public_msg = public_listing.unwrap();
    assert_eq!(public_msg.params[2], "2"); // 2 members
    assert_eq!(public_msg.params[3], "Public channel topic");
}

#[tokio::test]
async fn test_list_specific_channel() {
    let (state, requester_id, _) = setup_test_state().await;
    
    let params = vec!["#public".to_string()];
    let result = handle_list(state, requester_id, params).await;
    
    assert!(result.is_ok());
    let messages = result.unwrap();
    
    // Should have: start (321) + channel (322) + end (323)
    assert_eq!(messages.len(), 3);
    
    // Check list start
    assert_eq!(messages[0].command, "321");
    
    // Check channel listing
    assert_eq!(messages[1].command, "322");
    assert_eq!(messages[1].params[0], "requester");
    assert_eq!(messages[1].params[1], "#public");
    assert_eq!(messages[1].params[2], "2"); // 2 members
    assert_eq!(messages[1].params[3], "Public channel topic");
    
    // Check list end
    assert_eq!(messages[2].command, "323");
}

#[tokio::test]
async fn test_list_multiple_channels() {
    let (state, requester_id, _) = setup_test_state().await;
    
    let params = vec!["#public,#notopic".to_string()];
    let result = handle_list(state, requester_id, params).await;
    
    assert!(result.is_ok());
    let messages = result.unwrap();
    
    // Should have: start (321) + 2 channels (322) + end (323)
    assert_eq!(messages.len(), 4);
    
    // Check list start
    assert_eq!(messages[0].command, "321");
    
    // Check channel listings
    let channel_names: Vec<String> = messages.iter()
        .filter(|msg| msg.command == "322")
        .map(|msg| msg.params[1].clone())
        .collect();
    
    assert!(channel_names.contains(&"#public".to_string()));
    assert!(channel_names.contains(&"#notopic".to_string()));
    
    // Check list end
    let last_msg = messages.last().unwrap();
    assert_eq!(last_msg.command, "323");
}

#[tokio::test]
async fn test_list_nonexistent_channel() {
    let (state, requester_id, _) = setup_test_state().await;
    
    let params = vec!["#nonexistent".to_string()];
    let result = handle_list(state, requester_id, params).await;
    
    assert!(result.is_ok());
    let messages = result.unwrap();
    
    // Should have: start (321) + end (323) only (no channel listing)
    assert_eq!(messages.len(), 2);
    
    assert_eq!(messages[0].command, "321");
    assert_eq!(messages[1].command, "323");
    
    // No 322 messages for nonexistent channels
    let channel_listings = messages.iter()
        .filter(|msg| msg.command == "322")
        .count();
    assert_eq!(channel_listings, 0);
}

#[tokio::test]
async fn test_list_secret_channel_not_member() {
    let (state, requester_id, _) = setup_test_state().await;
    
    let params = vec!["#secret".to_string()];
    let result = handle_list(state, requester_id, params).await;
    
    assert!(result.is_ok());
    let messages = result.unwrap();
    
    // Should have: start (321) + end (323) only (secret channel not visible)
    assert_eq!(messages.len(), 2);
    
    assert_eq!(messages[0].command, "321");
    assert_eq!(messages[1].command, "323");
    
    // No channel listing for secret channel when not a member
    let secret_listings = messages.iter()
        .filter(|msg| msg.command == "322" && msg.params[1] == "#secret")
        .count();
    assert_eq!(secret_listings, 0);
}

#[tokio::test]
async fn test_list_secret_channel_as_member() {
    let (state, _, member_id) = setup_test_state().await;
    
    let params = vec!["#secret".to_string()];
    let result = handle_list(state, member_id, params).await;
    
    assert!(result.is_ok());
    let messages = result.unwrap();
    
    // Should have: start (321) + channel (322) + end (323)
    assert_eq!(messages.len(), 3);
    
    // Check channel listing is visible to member
    assert_eq!(messages[1].command, "322");
    assert_eq!(messages[1].params[1], "#secret");
    assert_eq!(messages[1].params[2], "1"); // 1 member
    assert_eq!(messages[1].params[3], "Secret channel topic");
}

#[tokio::test]
async fn test_list_private_channel_not_member() {
    let (state, requester_id, _) = setup_test_state().await;
    
    let params = vec!["#private".to_string()];
    let result = handle_list(state, requester_id, params).await;
    
    assert!(result.is_ok());
    let messages = result.unwrap();
    
    // Should have: start (321) + end (323) only (private channel not visible)
    assert_eq!(messages.len(), 2);
    
    assert_eq!(messages[0].command, "321");
    assert_eq!(messages[1].command, "323");
    
    // No channel listing for private channel when not a member
    let private_listings = messages.iter()
        .filter(|msg| msg.command == "322" && msg.params[1] == "#private")
        .count();
    assert_eq!(private_listings, 0);
}

#[tokio::test]
async fn test_list_private_channel_as_member() {
    let (state, _, member_id) = setup_test_state().await;
    
    let params = vec!["#private".to_string()];
    let result = handle_list(state, member_id, params).await;
    
    assert!(result.is_ok());
    let messages = result.unwrap();
    
    // Should have: start (321) + channel (322) + end (323)
    assert_eq!(messages.len(), 3);
    
    // Check channel listing is visible to member
    assert_eq!(messages[1].command, "322");
    assert_eq!(messages[1].params[1], "#private");
    assert_eq!(messages[1].params[2], "1"); // 1 member
    assert_eq!(messages[1].params[3], "Private channel topic");
}

#[tokio::test]
async fn test_list_channel_with_no_topic() {
    let (state, requester_id, _) = setup_test_state().await;
    
    let params = vec!["#notopic".to_string()];
    let result = handle_list(state, requester_id, params).await;
    
    assert!(result.is_ok());
    let messages = result.unwrap();
    
    // Should have: start (321) + channel (322) + end (323)
    assert_eq!(messages.len(), 3);
    
    // Check channel listing has empty topic
    assert_eq!(messages[1].command, "322");
    assert_eq!(messages[1].params[1], "#notopic");
    assert_eq!(messages[1].params[2], "1"); // 1 member
    assert_eq!(messages[1].params[3], ""); // Empty topic
}

#[tokio::test]
async fn test_list_empty_channel() {
    let (state, requester_id, _) = setup_test_state().await;
    
    let params = vec!["#empty".to_string()];
    let result = handle_list(state, requester_id, params).await;
    
    assert!(result.is_ok());
    let messages = result.unwrap();
    
    // Should have: start (321) + channel (322) + end (323)
    assert_eq!(messages.len(), 3);
    
    // Check channel listing shows 0 members
    assert_eq!(messages[1].command, "322");
    assert_eq!(messages[1].params[1], "#empty");
    assert_eq!(messages[1].params[2], "0"); // 0 members
    assert_eq!(messages[1].params[3], ""); // No topic
}

#[tokio::test]
async fn test_list_invalid_connection() {
    let state = Arc::new(RwLock::new(ServerState::new()));
    let invalid_id = 999;
    
    let params = vec![];
    let result = handle_list(state, invalid_id, params).await;
    
    assert!(result.is_ok());
    let messages = result.unwrap();
    assert_eq!(messages.len(), 0); // No messages when connection doesn't exist
}

#[tokio::test]
async fn test_list_requester_without_nickname() {
    let state = Arc::new(RwLock::new(ServerState::new()));
    let requester_id = 1;
    
    // Create connection without nickname
    let (tx, _) = mpsc::channel(100);
    let addr = SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 12345);
    let connection = Connection::new(requester_id, addr, tx);
    
    // Add a test channel
    let channel = Channel::new("#test".to_string());
    
    {
        let mut state_guard = state.write().await;
        state_guard.connections.insert(requester_id, connection);
        state_guard.channels.insert("#test".to_string(), channel);
    }
    
    let params = vec![];
    let result = handle_list(state, requester_id, params).await;
    
    assert!(result.is_ok());
    let messages = result.unwrap();
    
    // Should use "*" as nick for connection without nickname
    assert!(messages.len() >= 2); // At least start and end
    assert_eq!(messages[0].params[0], "*"); // Start message with "*" nick
    
    let last_msg = messages.last().unwrap();
    assert_eq!(last_msg.params[0], "*"); // End message with "*" nick
}

#[tokio::test]
async fn test_list_mixed_channel_visibility() {
    let (state, requester_id, _) = setup_test_state().await;
    
    let params = vec!["#public,#secret,#private,#notopic".to_string()];
    let result = handle_list(state, requester_id, params).await;
    
    assert!(result.is_ok());
    let messages = result.unwrap();
    
    // Should show only channels requester can see (#public and #notopic)
    let channel_listings: Vec<_> = messages.iter()
        .filter(|msg| msg.command == "322")
        .collect();
    
    assert_eq!(channel_listings.len(), 2);
    
    let channel_names: Vec<String> = channel_listings.iter()
        .map(|msg| msg.params[1].clone())
        .collect();
    
    assert!(channel_names.contains(&"#public".to_string()));
    assert!(channel_names.contains(&"#notopic".to_string()));
    assert!(!channel_names.contains(&"#secret".to_string()));
    assert!(!channel_names.contains(&"#private".to_string()));
}

#[tokio::test]
async fn test_list_all_excludes_hidden_channels() {
    let (state, requester_id, _) = setup_test_state().await;
    
    let params = vec![];
    let result = handle_list(state, requester_id, params).await;
    
    assert!(result.is_ok());
    let messages = result.unwrap();
    
    let channel_listings: Vec<_> = messages.iter()
        .filter(|msg| msg.command == "322")
        .collect();
    
    let channel_names: Vec<String> = channel_listings.iter()
        .map(|msg| msg.params[1].clone())
        .collect();
    
    // Should not see secret or private channels where not a member
    assert!(!channel_names.contains(&"#secret".to_string()));
    assert!(!channel_names.contains(&"#private".to_string()));
    
    // Should see public channels
    assert!(channel_names.contains(&"#public".to_string()));
    assert!(channel_names.contains(&"#notopic".to_string()));
    assert!(channel_names.contains(&"#empty".to_string()));
}