-
Notifications
You must be signed in to change notification settings - Fork 1
/
tests.py
executable file
·164 lines (128 loc) · 4.49 KB
/
tests.py
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
#!/usr/bin/python3
import sys
import os.path
sys.path.append(os.path.expanduser("~/twsmb")) # FIXME trial won't add to path
import struct
import time
import threading
import tempfile
import pdb
import io
import twsmb
import base
from realm import TestRealm
# Mike Teo's pysmb: used to make test connections
from smb.SMBConnection import SMBConnection
from twisted.cred import portal, checkers, credentials
from twisted.trial import unittest
from twisted.internet.endpoints import TCP4ServerEndpoint
from twisted.internet import reactor
from twisted.logger import globalLogBeginner, textFileLogObserver, Logger
from twisted.internet.protocol import ProcessProtocol
from twisted.internet.defer import Deferred
from twisted.python.failure import Failure
log = Logger()
observers = [textFileLogObserver(sys.stdout)]
globalLogBeginner.beginLoggingTo(observers)
class SMBTest(unittest.TestCase):
def test_base_unpack(self):
data = struct.pack("!HBH", 525, 24, 17) + b'bob'
r = base.unpack("!HBH", data)
self.assertEqual(r, (525, 24, 17, b'bob'))
def test_smb_packet_receiver(self):
pr = base.SMBPacketReceiver()
pr.transport = io.BytesIO()
def recv(x):
global rdata
rdata = x
pr.packetReceived = recv
# send fake packet
pr.sendPacket(b'bur ble')
r = pr.transport.getvalue()
self.assertEqual(r, b'\0\0\0\x07bur ble')
# receive fake packet
pr.dataReceived(b'\0\0\0\x03abc')
self.assertEqual(rdata, b'abc')
def backgd():
time.sleep(1)
conn = SMBConnection("user", "password", "client", "server",
use_ntlm_v2 = True,
sign_options = 0,
is_direct_tcp = True)
try:
conn.connect("127.0.0.1", 8445)
file_obj = tempfile.NamedTemporaryFile()
file_attributes, filesize = conn.retrieveFile('smbtest', '/foo.txt', file_obj)
file_obj.close()
conn.close()
time.sleep(1)
finally:
reactor.stop()
def run_pysmb():
endpoint = TCP4ServerEndpoint(reactor, 8445)
endpoint.listen(twsmb.SMBFactory(None))
backgd_thread = threading.Thread(target=backgd)
backgd_thread.start()
reactor.run()
class ChatNotFinished(Exception): pass
class ChatProcess(ProcessProtocol):
def __init__(self, chat):
self.chat = chat
self.d = Deferred()
self.matches = []
def outReceived(self, data):
data = data.decode("utf-8")
print(data)
if self.chat:
prompt, reply = self.chat[0]
m = re.search(prompt, data)
if m:
self.matches.append(m)
if reply:
for i in range(1, 10):
t = "\\%d" % i
if t in reply:
reply = reply.replace(t, m.group(i))
self.transport.write(reply.encode('utf-8'))
else:
self.transport.closeStdin()
del self.chat[0]
def errReceived(self, data):
print(data.decode("utf-8"))
def processEnded(self, status):
if status.value.exitCode != 0:
self.d.errback(status)
elif self.chat:
try:
raise ChatNotFinished()
except:
self.d.errback(Failure())
else:
self.d.callback(self.matches)
def spawn(chat, args, usePTY=True):
pro = ChatProcess(chat)
reactor.spawnProcess(pro, args[0], args, usePTY=usePTY)
return pro.d
class SambaClientTests(unittest.TestCase):
def setUp(self):
# Start the server
r = TestRealm()
p = portal.Portal(r)
users_checker = checkers.InMemoryUsernamePasswordDatabaseDontUse()
self.username = "user"
self.password = "test-password"
users_checker.addUser(self.username, self.password)
p.registerChecker(users_checker, credentials.IUsernameHashedPassword)
self.factory = twsmb.SMBFactory(p)
self.port = port = reactor.listenTCP(
445, self.factory)
self.addCleanup(port.stopListening)
self.addCleanup(r.shutdown)
def test_login(self):
return spawn([], ["/usr/bin/smbclient",
"\\\\mintbox\\x", self.password,
"-m", "SMB2",
"-U", self.username,
"-I", "127.0.0.1",
"-d", "10"],
usePTY=True)