tests/test_core_stanza_router.lua
author Matthew Wild <mwild1@gmail.com>
Sat, 15 Nov 2008 19:12:23 +0000
changeset 273 b4c6b124c06f
child 302 8aedb3766ec7
permissions -rw-r--r--
Add tests for core.stanza_router
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
273
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     1
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     2
function core_process_stanza(core_process_stanza)
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     3
	local s2sout_session = { to_host = "remotehost", from_host = "localhost", type = "s2sout" }
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     4
	local local_host_session = { host = "localhost", type = "local" }
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     5
	local local_user_session = { username = "user", host = "localhost", resource = "resource", full_jid = "user@localhost/resource", type = "c2s" }
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     6
	local hosts = {
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     7
			["localhost"] = local_host_session;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     8
			}
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
     9
				
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    10
	-- Test message routing
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    11
	local function test_message_full_jid()
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    12
		local env = testlib_new_env();
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    13
		local msg = stanza.stanza("message", { to = "user@localhost/resource", type = "chat" }):tag("body"):text("Hello world");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    14
		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    15
		local target_routed;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    16
		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    17
		function env.core_route_stanza(p_origin, p_stanza)
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    18
			assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    19
			assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print());
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    20
			target_routed = true;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    21
		end
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    22
		env.hosts = hosts;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    23
		setfenv(core_process_stanza, env);
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    24
		assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    25
		assert_equal(target_routed, true, "stanza was not routed successfully");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    26
	end
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    27
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    28
	local function test_message_bare_jid()
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    29
		local env = testlib_new_env();
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    30
		local msg = stanza.stanza("message", { to = "user@localhost", type = "chat" }):tag("body"):text("Hello world");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    31
		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    32
		local target_routed;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    33
		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    34
		function env.core_route_stanza(p_origin, p_stanza)
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    35
			assert_equal(p_origin, local_user_session, "origin of routed stanza is not correct");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    36
			assert_equal(p_stanza, msg, "routed stanza is not correct one: "..p_stanza:pretty_print());
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    37
			target_routed = true;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    38
		end
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    39
		env.hosts = hosts;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    40
		setfenv(core_process_stanza, env);
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    41
		assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    42
		assert_equal(target_routed, true, "stanza was not routed successfully");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    43
	end
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    44
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    45
	local function test_message_no_to()
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    46
		local env = testlib_new_env();
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    47
		local msg = stanza.stanza("message", { type = "chat" }):tag("body"):text("Hello world");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    48
		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    49
		local target_handled;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    50
		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    51
		function env.core_route_stanza(p_origin, p_stanza)
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    52
		end
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    53
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    54
		function env.core_handle_stanza(p_origin, p_stanza)
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    55
			assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    56
			assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    57
			target_handled = true;		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    58
		end
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    59
		env.hosts = hosts;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    60
		setfenv(core_process_stanza, env);
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    61
		assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    62
		assert_equal(target_handled, true, "stanza was not handled successfully");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    63
	end
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    64
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    65
	local function test_message_to_remote_bare()
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    66
		local env = testlib_new_env();
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    67
		local msg = stanza.stanza("message", { to = "user@remotehost", type = "chat" }):tag("body"):text("Hello world");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    68
		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    69
		local target_routed;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    70
		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    71
		function env.core_route_stanza(p_origin, p_stanza)
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    72
			assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    73
			assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    74
			target_routed = true;		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    75
		end
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    76
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    77
		env.hosts = hosts;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    78
		setfenv(core_process_stanza, env);
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    79
		assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    80
		assert_equal(target_routed, true, "stanza was not routed successfully");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    81
	end
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    82
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    83
	local function test_message_to_remote_server()
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    84
		local env = testlib_new_env();
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    85
		local msg = stanza.stanza("message", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    86
		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    87
		local target_routed;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    88
		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    89
		function env.core_route_stanza(p_origin, p_stanza)
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    90
			assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    91
			assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    92
			target_routed = true;		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    93
		end
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    94
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    95
		env.hosts = hosts;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    96
		setfenv(core_process_stanza, env);
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    97
		assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    98
		assert_equal(target_routed, true, "stanza was not routed successfully");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
    99
	end
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   100
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   101
	--IQ tests
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   102
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   103
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   104
	local function test_iq_to_remote_server()
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   105
		local env = testlib_new_env();
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   106
		local msg = stanza.stanza("iq", { to = "remotehost", type = "chat" }):tag("body"):text("Hello world");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   107
		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   108
		local target_routed;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   109
		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   110
		function env.core_route_stanza(p_origin, p_stanza)
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   111
			assert_equal(p_origin, local_user_session, "origin of handled stanza is not correct");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   112
			assert_equal(p_stanza, msg, "handled stanza is not correct one: "..p_stanza:pretty_print());
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   113
			target_routed = true;		
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   114
		end
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   115
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   116
		function env.core_handle_stanza(p_origin, p_stanza)
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   117
			
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   118
		end
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   119
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   120
		env.hosts = hosts;
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   121
		setfenv(core_process_stanza, env);
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   122
		assert_equal(core_process_stanza(local_user_session, msg), nil, "core_process_stanza returned incorrect value");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   123
		assert_equal(target_routed, true, "stanza was not routed successfully");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   124
	end
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   125
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   126
	runtest(test_message_full_jid, "Messages with full JID destinations get routed");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   127
	runtest(test_message_bare_jid, "Messages with bare JID destinations get routed");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   128
	runtest(test_message_no_to, "Messages with no destination are handled by the server");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   129
	runtest(test_message_to_remote_bare, "Messages to a remote user are routed by the server");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   130
	runtest(test_message_to_remote_server, "Messages to a remote server's JID are routed");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   131
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   132
	runtest(test_iq_to_remote_server, "iq to a remote server's JID are routed");
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   133
b4c6b124c06f Add tests for core.stanza_router
Matthew Wild <mwild1@gmail.com>
parents:
diff changeset
   134
end