import unittest
import time

import dbus
import dbus.service
import dbus.glib

class TestCase(unittest.TestCase):

    def setUp(self):
        # WRITE SOMETHING IN THE FILE AND
        # close it
        f = open("/tmp/fd-test", "w")
        f.write("Hello, World!")
        f.close();


    def testHelloService(self):
        bus = dbus.SessionBus()
        service = bus.get_object('test.readFDservice', '/test/readFDservice')
        method = service.get_dbus_method('readFD', 'test.readFDservice')
        # Have written the hello world in the fd-test file, open the file and
        # send the fd over D-bus.
        f = open('/tmp/fd-test')
        print ("fd = %d" % f.fileno())
        fds = {0:dbus.types.UnixFd(f)}
        r = method("Test", fds)
        time.sleep(60) #To invoke another prg to try to read the shared fd.
        f.close()

        print ("r = %s" % r)
        assert r == "Hello, World!" # we got the same string


if __name__ == '__main__':
    unittest.main()
