[gvfs] gvfs-test: Make Smb tests run without gvfs-testbed



commit 996f0513897816130fd50377b8093b57fd47bb41
Author: Martin Pitt <martinpitt gnome org>
Date:   Tue Oct 16 14:31:28 2012 +0200

    gvfs-test: Make Smb tests run without gvfs-testbed
    
    When not running under gvfs-testbed, start smbd as normal user with temporary
    configuration and state files on a high port (1445).
    
    Set $LIBSMB_PROG to "netcat to port 1445" to convince libsmbclient and the smb
    gvfs backend to talk to this port instead (unfortunately libsmbclient has no
    API to set the port).
    
    As in this mode smbd cannot change to other users it does not prevent access to
    files which are only accessible to the test user, so adjust the test case
    accordingly. This should be cleaned up later on by using two separate shares.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=686006

 test/Makefile.am |    6 ++-
 test/gvfs-test   |   91 ++++++++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 83 insertions(+), 14 deletions(-)
---
diff --git a/test/Makefile.am b/test/Makefile.am
index 91b0c28..840eeb1 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -63,12 +63,14 @@ endif
 noinst_DATA= $(CONFIG_FILES)
 
 # run tests against build tree
+# $LIBSMB_PROG needs to be set in the local dbus-daemon, but only if we
+# run tests; it doesn't help on the client-side
 check: $(CONFIG_FILES) gvfs-test
-	$(srcdir)/run-in-tree.sh $(srcdir)/gvfs-test $(TEST_NAMES)
+	env LIBSMB_PROG="nc localhost 1445" $(srcdir)/run-in-tree.sh $(srcdir)/gvfs-test $(TEST_NAMES)
 
 # run tests against the installed system packages
 installcheck-local: gvfs-test
-	$(srcdir)/gvfs-test $(TEST_NAMES)
+	env LIBSMB_PROG="nc localhost 1445" $(srcdir)/gvfs-test $(TEST_NAMES)
 
 CLEANFILES=$(CONFIG_FILES)
 
diff --git a/test/gvfs-test b/test/gvfs-test
index cfccb3c..1000d17 100755
--- a/test/gvfs-test
+++ b/test/gvfs-test
@@ -446,20 +446,85 @@ class Ftp(GvfsTestCase):
         finally:
             self.unmount(uri)
 
- unittest skipUnless(in_testbed, 'not running under gvfs-testbed')
+ unittest skipUnless(in_testbed or 'LIBSMB_PROG' in os.environ,
+                     'not running under gvfs-testbed or LIBSMB_PROG="nc localhost 1445"')
 class Smb(GvfsTestCase):
+    def setUp(self):
+        '''start local smbd as user if we are not in test bed'''
+        super().setUp()
 
-    @classmethod
-    def setUpClass(klass):
         # create a few test files
-        myfiles = os.path.expanduser('~/myfiles')
-        os.makedirs(os.path.join(myfiles, 'mydir'))
-        with open(os.path.join(myfiles, 'myfile.txt'), 'w') as f:
-            f.write('hello world\n')
-        secret_path = os.path.join(myfiles, 'mydir', 'onlyme.txt')
-        with open(secret_path, 'w') as f:
-            f.write('secret\n')
-        os.chmod(secret_path, 0o600)
+        if in_testbed:
+            myfiles = os.path.expanduser('~/myfiles')
+        else:
+            myfiles = os.path.join(self.workdir, 'myfiles')
+        if not os.path.exists(myfiles):
+            # only run this once
+            os.makedirs(os.path.join(myfiles, 'mydir'))
+            with open(os.path.join(myfiles, 'myfile.txt'), 'w') as f:
+                f.write('hello world\n')
+            secret_path = os.path.join(myfiles, 'mydir', 'onlyme.txt')
+            with open(secret_path, 'w') as f:
+                f.write('secret\n')
+            os.chmod(secret_path, 0o600)
+
+        if in_testbed:
+            return
+
+        # smbpasswd file with password "foo"
+        smbpasswd = os.path.join(self.workdir, 'smbpasswd')
+        with open(smbpasswd, 'w') as f:
+            f.write(os.environ['USER'])
+            f.write(':2:XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX:AC8E657F83DF82BEEA5D43BDAF7800CC:[U          ]:LCT-507C14C7:\n')
+        
+        # create local samba configuration
+        smbdir = os.path.join(self.workdir, 'samba')
+        os.mkdir(smbdir, 0o755)
+        smbconf = os.path.join(self.workdir, 'smb.conf')
+        with open(smbconf, 'w') as f:
+            f.write('''[global]
+workgroup = TESTGROUP
+interfaces = lo 127.0.0.0/8
+smb ports = 1445
+log level = 2
+map to guest = Bad User
+passdb backend = smbpasswd
+smb passwd file = %(workdir)s/smbpasswd
+lock directory = %(workdir)s/samba
+state directory = %(workdir)s/samba
+cache directory = %(workdir)s/samba
+pid directory = %(workdir)s/samba
+private directory = %(workdir)s/samba
+ncalrpc dir = %(workdir)s/samba
+
+[myfiles]
+  path = %(workdir)s/myfiles
+  guest ok = yes
+  read only = no
+''' % {'workdir': self.workdir})
+
+        # start smbd
+        self.smbd = subprocess.Popen(['smbd', '-iFS', '-s', smbconf],
+                                     universal_newlines=True,
+                                     stdout=subprocess.PIPE)
+        time.sleep(1)
+
+    def tearDown(self):
+        # stop smbd
+        if hasattr(self, 'smbd') and self.smbd.returncode is None:
+            self.smbd.terminate()
+            self.smbd.wait()
+        super().tearDown()
+
+    def run(self, result=None):
+        '''Show smbd log output on failed tests'''
+
+        if result:
+            orig_err_fail = result.errors + result.failures
+        super().run(result)
+        if hasattr(self, 'smbd'):
+            if result and result.errors + result.failures > orig_err_fail:
+                print('\n----- smbd log -----\n%s\n------\n' % self.smbd.stdout.read())
 
     def test_anonymous(self):
         '''smb:// anonymous'''
@@ -521,7 +586,9 @@ class Smb(GvfsTestCase):
             out = self.program_out_success(['gvfs-cat', uri + '/myfile.txt'])
             self.assertEqual(out, 'hello world\n')
 
-            if auth:
+            # FIXME: when running smbd as user it cannot change user to guest
+            # and prevent access
+            if auth or not in_testbed:
                 out = self.program_out_success(['gvfs-cat', uri + '/mydir/onlyme.txt'])
                 self.assertEqual(out, 'secret\n')
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]