-
Notifications
You must be signed in to change notification settings - Fork 635
/
test_native_hooks.rb
92 lines (78 loc) · 2.33 KB
/
test_native_hooks.rb
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
$:.unshift File.expand_path(File.dirname(__FILE__) + "/../lib")
require 'eventmachine'
require 'test/unit'
require File.dirname(__FILE__) + '/emtestext/testext'
class TestNativeHooks < Test::Unit::TestCase
HookTestHost = "127.0.0.1"
HookTestPort = 12345
module HookTestServer
def post_init
send_data "legit data from the server yo, untouched"
end
def receive_data(data)
$hook_test_received_data = data
EM.stop
end
end
module HookTestRecvClient
def initialize
self.recv_hook = EmTestext.em_recv_hook(self)
end
def receive_data(data)
$hook_test_received_data = data
EM.stop
end
def unbind
EM.stop
end
end
module HookTestSendClient
def initialize
self.send_hook = EmTestext.em_send_hook(self)
end
def post_init
send_special "legit data from the client yo, untouched"
end
def receive_data(data)
end
end
def test_receive_hook
$hook_test_received_data = nil
EmTestext.reset
assert !EmTestext.recv_hook_called
assert EmTestext.test_data.nil?
assert EmTestext.real_data.nil?
EM.run {
EM.start_server(HookTestHost, HookTestPort, HookTestServer)
EM.connect(HookTestHost, HookTestPort, HookTestRecvClient)
}
assert EmTestext.recv_hook_called
assert_equal "zomg manipulated inbound dataz", EmTestext.test_data
assert_equal "legit data from the server yo, untouched", EmTestext.real_data
end
def test_send_hook
$hook_test_received_data = nil
EmTestext.reset
assert !EmTestext.send_hook_called
EM.run {
EM.start_server(HookTestHost, HookTestPort, HookTestServer)
c = EM.connect(HookTestHost, HookTestPort, HookTestSendClient)
}
assert EmTestext.send_hook_called
assert_equal "zomg manipulated outbound dataz", $hook_test_received_data
end
def test_disable_recv_hook
$hook_test_received_data = nil
EmTestext.reset
assert !EmTestext.recv_hook_called
EM.run {
EM.start_server(HookTestHost, HookTestPort, HookTestServer)
c = EM.connect(HookTestHost, HookTestPort, HookTestRecvClient)
c.recv_hook.enabled = false
}
assert !EmTestext.recv_hook_called
assert EmTestext.test_data.nil?
assert EmTestext.real_data.nil?
assert_equal "legit data from the server yo, untouched", $hook_test_received_data
end
end