Skip to content

Commit 4002794

Browse files
authored
Merge pull request #9 from dalingk/master
Python 3 Support
2 parents 3c3bc43 + 3bbf15a commit 4002794

File tree

5 files changed

+76
-11
lines changed

5 files changed

+76
-11
lines changed

README.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,18 @@ Output
3030
S-1-5-21-2127521184-1604012920-1887927527-72713
3131
```
3232

33+
## Tests
34+
35+
Run tests using
36+
```
37+
python -m unittest
38+
```
39+
while in the `src/sid` directory.
40+
3341
## Changelog
42+
* 0.2
43+
* Python 3 support
44+
* Added a few tests
3445
* 0.1
3546
* Fix docstrings for documentation
3647
* Add setup.py

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
setup(
3030
name = "sid",
31-
version = "0.1",
31+
version = "0.2",
3232
author = "Sascha Spreitzer",
3333
author_email = "sspreitz@redhat.com",
3434
description = ("Python library to convert Windows SIDs"),

src/sid/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@
99
'''
1010

1111

12-
from lib import *
12+
from .lib import *

src/sid/lib.py

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
'''The lib contains exceptions, constants and the sid class'''
22

3-
43
import base64
54
import struct
6-
import string
75
import binascii
86

97

@@ -90,10 +88,10 @@ def byteToLong(cls, byte, little_endian=True):
9088
raise Exception('Bytes too long. Needs to be <= 8 or 64bit')
9189
else:
9290
if little_endian:
93-
a = string.ljust(byte, 8, '\x00')
91+
a = byte.ljust(8, b'\x00')
9492
return struct.unpack('<q', a)[0]
9593
else:
96-
a = string.rjust(byte, 8, '\x00')
94+
a = byte.rjust(8, b'\x00')
9795
return struct.unpack('>q', a)[0]
9896

9997
@classmethod
@@ -104,7 +102,7 @@ def strsid(cls, byte):
104102
'''
105103
ret = 'S'
106104
sid = []
107-
sid.append(cls.byteToLong(byte[0]))
105+
sid.append(cls.byteToLong(byte[0:1]))
108106
sid.append(cls.byteToLong(byte[2:2+6], False))
109107
for i in range(8, len(byte), 4):
110108
sid.append(cls.byteToLong(byte[i:i+4]))
@@ -118,8 +116,8 @@ def byte(cls, strsid):
118116
Convert a SID into bytes
119117
strdsid - SID to convert into bytes
120118
'''
121-
sid = string.split(strsid, '-')
122-
ret = ''
119+
sid = str.split(strsid, '-')
120+
ret = bytearray()
123121
sid.remove('S')
124122
for i in range(len(sid)):
125123
sid[i] = int(sid[i])
@@ -138,8 +136,7 @@ def byteldap(cls, strsid):
138136
strsid - SID to encode
139137
'''
140138
ret = ''
141-
a = binascii.hexlify(cls.byte(strsid))
142-
print a
139+
a = binascii.hexlify(cls.byte(strsid)).decode('utf-8')
143140
for i in range(0, len(a), 2):
144141
ret += '\\' + a[i:i+2]
145142
return ret

src/sid/test_lib.py

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""Module containing a few tests for this module"""
2+
import unittest
3+
import lib as sid
4+
5+
6+
class Testsid(unittest.TestCase):
7+
"""Class for testing sid"""
8+
sid_null = 'S-1-0-0'
9+
sid_sample = 'S-1-5-21-2127521184-1604012920-1887927527-72713'
10+
sid_null_bin = bytearray(
11+
b'\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
12+
sid_sample_bin = bytearray(
13+
b'\x01\x05\x00\x00\x00\x00\x00\x05\x15\x00\x00\x00\xa0e\xcf~xK\x9b_\xe7|\x87p\t\x1c\x01\x00')
14+
sid_null_b64 = b'AQEAAAAAAAAAAAAA'
15+
sid_sample_b64 = b'AQUAAAAAAAUVAAAAoGXPfnhLm1/nfIdwCRwBAA=='
16+
17+
def test_init_string(self):
18+
"""Test creating sids from strings"""
19+
self.assertEqual(self.sid_null, str(sid.sid(self.sid_null)))
20+
self.assertEqual(self.sid_sample, str(sid.sid(self.sid_sample)))
21+
22+
def test_init_base64(self):
23+
"""Test creating sids from base64"""
24+
self.assertEqual(self.sid_null, str(
25+
sid.sid(self.sid_null_b64, sid.SID_BASE64)))
26+
self.assertEqual(self.sid_sample, str(
27+
sid.sid(self.sid_sample_b64, sid.SID_BASE64)))
28+
29+
def test_init_binary(self):
30+
"""Test creating sids from binary"""
31+
self.assertEqual(self.sid_null, str(
32+
sid.sid(self.sid_null_bin, sid.SID_BINARY)))
33+
self.assertEqual(self.sid_sample, str(
34+
sid.sid(self.sid_sample_bin, sid.SID_BINARY)))
35+
36+
def test_ldap(self):
37+
"""Test ldap filter form of sid"""
38+
sid_null_ldap = '\\01\\01\\00\\00\\00\\00\\00\\00\\00\\00\\00\\00'
39+
sid_sample_ldap = '\\01\\05\\00\\00\\00\\00\\00\\05\\15\\00\\00\\00\\a0\\65\\cf\\7e\\78\\4b\\9b\\5f\\e7\\7c\\87\\70\\09\\1c\\01\\00'
40+
self.assertEqual(sid_null_ldap, sid.sid(self.sid_null).ldap())
41+
self.assertEqual(sid_sample_ldap, sid.sid(self.sid_sample).ldap())
42+
43+
def test_binary(self):
44+
"""Test binary form of sid"""
45+
self.assertEqual(self.sid_null_bin, sid.sid(self.sid_null).binary())
46+
self.assertEqual(self.sid_sample_bin,
47+
sid.sid(self.sid_sample).binary())
48+
49+
def test_base64(self):
50+
"""Test base64 form of sid"""
51+
self.assertEqual(self.sid_null_b64, sid.sid(self.sid_null).base64())
52+
self.assertEqual(self.sid_sample_b64,
53+
sid.sid(self.sid_sample).base64())
54+
55+
56+
if __name__ == '__main__':
57+
unittest.main()

0 commit comments

Comments
 (0)