/tests/test_context.py

https://github.com/ioggstream/pysmbc
Python | 195 lines | 133 code | 33 blank | 29 comment | 19 complexity | 18a932352fb3beb1d6e9283b31c58641 MD5 | raw file
  1. #!/usr/bin/env python
  2. import os
  3. import smbc
  4. import settings
  5. import nose
  6. from nose.plugins.skip import SkipTest
  7. baseurl = 'smb://' + settings.SERVER + "/" + settings.SHARE +"/"
  8. # a nice map from/to smbc constants
  9. smbcType = {
  10. 'WORKGROUP' : smbc.WORKGROUP,
  11. 'SERVER' : smbc.SERVER,
  12. 'FILE_SHARE' : smbc.FILE_SHARE,
  13. 'PRINTER_SHARE' : smbc.PRINTER_SHARE,
  14. 'IPC_SHARE' : smbc.IPC_SHARE,
  15. smbc.WORKGROUP : 'WORKGROUP',
  16. smbc.SERVER : 'SERVER',
  17. smbc.FILE_SHARE : 'FILE_SHARE',
  18. smbc.PRINTER_SHARE : 'PRINTER_SHARE',
  19. smbc.IPC_SHARE : 'IPC_SHARE'
  20. }
  21. # another map for system errors TODO can you find them in another module?
  22. EINVAL = 22
  23. def setUp():
  24. global ctx
  25. ctx = smbc.Context()
  26. cb = lambda se, sh, w, u, p: (w, settings.USERNAME, settings.PASSWORD)
  27. ctx.functionAuthData = cb
  28. def tearDown():
  29. global ctx
  30. del ctx
  31. def touch_file(name):
  32. """
  33. create a file containing "sample test file" in the test baseurl
  34. """
  35. tmpfile_name = baseurl + name
  36. dfile = ctx.open(tmpfile_name, os.O_CREAT | os.O_TRUNC | os.O_WRONLY)
  37. dfile.write("sample test file")
  38. dfile.close
  39. return tmpfile_name
  40. def test_xattr_constants():
  41. assert smbc.XATTR_ACL
  42. assert smbc.XATTR_OWNER
  43. assert smbc.XATTR_GROUP
  44. def test_xattr_get():
  45. """
  46. system.nt_sec_desc.<attribute name>
  47. * system.nt_sec_desc.*
  48. * system.nt_sec_desc.*+
  49. *
  50. * where <attribute name> is one of:
  51. *
  52. * revision
  53. * owner
  54. * owner+
  55. * group
  56. * group+
  57. * acl:<name or sid>
  58. * acl+:<name or sid
  59. """
  60. print "test_xattr"
  61. furl = touch_file("tmpfile.out")
  62. # create all combinations of attribute strings
  63. plus_xattrs = ["%s%s" % (i,j) for i in ["owner", "group", "*"] for j in ["","+"]]
  64. plus_xattrs.append("revision")
  65. valid_xatts = ["system.nt_sec_desc." +i for i in plus_xattrs]
  66. # check their existence
  67. for xattr in valid_xatts:
  68. print "\ttesting %s with %s" % (furl, xattr)
  69. assert(ctx.getxattr(furl, xattr))
  70. ctx.open(furl)
  71. def test_xattr_get_error():
  72. """ Verify that a RuntimeError is raised when passing bad arguments to getxattr()
  73. Bad arguments include malformed xattrs and unexistent file
  74. """
  75. print "test_xattr"
  76. furl = touch_file("tmpfile.out")
  77. # create all combinations of attribute strings
  78. plus_xattrs = ["%s%s" % (i,j) for i in ["owner", "pluto", "*"] for j in ["x","-"]]
  79. plus_xattrs.append("revisionX")
  80. invalid_xatts = ["system.nt_sec_desc." +i for i in plus_xattrs]
  81. try:
  82. ctx.getxattr("UNEXISTENT", smbc.XATTR_OWNER)
  83. assert False, "getxattr should fail with an unexistent file"
  84. except ValueError as e:
  85. (errno,strerror) = e.args
  86. assert errno == EINVAL # TODO is it possible to trap an unexistent entity error from smbclient?
  87. pass
  88. # check their existence
  89. for xattr in invalid_xatts:
  90. print "\ttesting %s with %s" % (furl, xattr)
  91. try:
  92. ctx.getxattr(furl, xattr)
  93. assert False, "getxattr should fail with %s" % xattr
  94. except ValueError as e:
  95. (errno,strerror) = e.args
  96. assert errno == EINVAL # invalid arguments
  97. ctx.open(furl)
  98. def test_xattr_set():
  99. #raise SkipTest("xattr_set to be implemented")
  100. print "test_xattr_put"
  101. furl = touch_file("tmpfile_set.out")
  102. attr_name = smbc.XATTR_ALL
  103. attrs = ctx.getxattr(furl, attr_name)
  104. print "attrs(%s): %s" % (attr_name, attrs)
  105. ctx.setxattr(furl, attr_name, attrs, smbc.XATTR_FLAG_REPLACE)
  106. attrs1 = ctx.getxattr(furl, attr_name)
  107. print "attrs1(%s): %s" % (attr_name, attrs1)
  108. assert attrs1 == attrs
  109. @SkipTest
  110. def test_xattr_set_2():
  111. furl = touch_file("tmpfile_set.out")
  112. attrs_new = u'REVISION:1,OWNER:RPOLLI\\babel" \
  113. + ",GROUP:Unix Group\\babel" \
  114. + ",ACL:RPOLLI\\babel:0/0/0x001e01ff" \
  115. + ",ACL:Unix Group\\babel:0/0/0x00120089" \
  116. + ",ACL:Unix Group\\games:0/0/0x001e01ff" \
  117. + ",ACL:\\Everyone:0/0/0x00120089'
  118. attr_name = smbc.XATTR_ALL_SID
  119. attrs_0 = ctx.getxattr(furl, attr_name)
  120. print "original attrs(%s)" % attrs_0
  121. assert attrs_0 != attrs_new, "Old and new attributes are the same:\n%s\n%s\n" % (attrs_0, attrs_new)
  122. ctx.setxattr(furl, attr_name, attrs_new, smbc.XATTR_FLAG_REPLACE)
  123. attrs_1 = ctx.getxattr(furl, attr_name)
  124. print "attrs_1(%s): %s" % (attr_name, attrs_1)
  125. assert attrs_1 == attrs_new
  126. def test_xattr_set_error():
  127. #raise SkipTest("xattr_set to be implemented")
  128. print "test_xattr_set_error"
  129. furl = touch_file("tmpfile_set.out")
  130. attr_name = smbc.XATTR_ALL_SID
  131. attrs_ok = ctx.getxattr(furl, attr_name)
  132. attrs = "BAD_VALUE" # causes segfault
  133. for xa in ["BAD_VALUE", u'REVISION:1,OWNER:RPOLLI\\babel,GROUP:', 0, None]:
  134. try:
  135. ctx.setxattr(furl, attr_name, xa, smbc.XATTR_FLAG_REPLACE)
  136. except ValueError as e:
  137. (errno,strerror) = e.args
  138. assert errno == EINVAL # invalid arguments
  139. print "setxattr(%s) raises %s" % (xa, e)
  140. pass
  141. except TypeError as e:
  142. print "setxattr(%s) raises %s" % (xa, e)
  143. pass
  144. def test_Workgroup():
  145. l_entries = ctx.opendir('smb://').getdents()
  146. assert(len(l_entries) > 0)
  147. for entry in l_entries:
  148. assert(entry.smbc_type == smbc.WORKGROUP), "Entry %s of type %s, expected %s" % (entry.name, smbcType[entry.smbc_type], smbcType[smbc.WORKGROUP])
  149. def test_Server():
  150. uri = 'smb://' + settings.WORKGROUP
  151. l_entries = ctx.opendir(uri).getdents()
  152. assert(len(l_entries) > 0)
  153. for entry in l_entries:
  154. assert(entry.smbc_type == smbc.SERVER), "Entry %s of type %s, expected %s" % (entry.name, smbcType[entry.smbc_type], smbcType[smbc.SERVER])
  155. def test_Share():
  156. uri = 'smb://' + settings.SERVER
  157. l_entries = ctx.opendir(uri).getdents()
  158. allowed_shares = [smbc.FILE_SHARE, smbc.PRINTER_SHARE, smbc.IPC_SHARE, smbc.COMMS_SHARE]
  159. assert(len(l_entries) > 0)
  160. for entry in l_entries:
  161. assert (entry.smbc_type in allowed_shares), "Entry was %s (%d), expected values: %s" % (smbcType[entry.smbc_type], entry.smbc_type, allowed_shares)