testController = TestI.TestIntfControllerI(adapter) adapter.add(TestI.TestIntfI(), communicator.stringToIdentity("test")) adapter.activate() adapter2.add(testController, communicator.stringToIdentity("testController")) adapter2.activate() communicator.waitForShutdown() return True try: initData = Ice.InitializationData() initData.properties = Ice.createProperties(sys.argv) # # This test kills connections, so we don't want warnings. # initData.properties.setProperty("Ice.Warn.Connections", "0") # # Limit the recv buffer size, this test relies on the socket # send() blocking after sending a given amount of data. # initData.properties.setProperty("Ice.TCP.RcvSize", "50000") communicator = Ice.initialize(sys.argv, initData) status = run(sys.argv, communicator)
def do_main_program(): # # --- Authenticator implementation # All of this has to go in here so we can correctly daemonize the tool # without losing the file descriptors opened by the Ice module slicedir = Ice.getSliceDir() if not slicedir: slicedir = ["-I/usr/share/Ice/slice", "-I/usr/share/slice"] else: slicedir = ["-I" + slicedir] Ice.loadSlice("", slicedir + [cfg.ice.slice]) import Murmur class LDAPAuthenticatorApp(Ice.Application): def run(self, args): self.shutdownOnInterrupt() if not self.initializeIceConnection(): return 1 if cfg.ice.watchdog > 0: self.failedWatch = True self.checkConnection() # Serve till we are stopped self.communicator().waitForShutdown() self.watchdog.cancel() if self.interrupted(): warning("Caught interrupt, shutting down") return 0 def initializeIceConnection(self): """ Establishes the two-way Ice connection and adds the authenticator to the configured servers """ ice = self.communicator() if cfg.ice.secret: debug("Using shared ice secret") ice.getImplicitContext().put("secret", cfg.ice.secret) elif not cfg.glacier.enabled: warning("Consider using an ice secret to improve security") if cfg.glacier.enabled: # info("Connecting to Glacier2 server (%s:%d)", glacier_host, glacier_port) error("Glacier support not implemented yet") # TODO: Implement this info("Connecting to Ice server (%s:%d)", cfg.ice.host, cfg.ice.port) base = ice.stringToProxy("Meta:tcp -h %s -p %d" % (cfg.ice.host, cfg.ice.port)) self.meta = Murmur.MetaPrx.uncheckedCast(base) adapter = ice.createObjectAdapterWithEndpoints( "Callback.Client", "tcp -h %s" % cfg.ice.host) adapter.activate() metacbprx = adapter.addWithUUID(metaCallback(self)) self.metacb = Murmur.MetaCallbackPrx.uncheckedCast(metacbprx) authprx = adapter.addWithUUID(LDAPAuthenticator()) self.auth = Murmur.ServerUpdatingAuthenticatorPrx.uncheckedCast( authprx) return self.attachCallbacks() def attachCallbacks(self, quiet=False): """ Attaches all callbacks for meta and authenticators """ # Ice.ConnectionRefusedException # debug("Attaching callbacks") try: if not quiet: info("Attaching meta callback") self.meta.addCallback(self.metacb) for server in self.meta.getBootedServers(): if not cfg.murmur.servers or server.id( ) in cfg.murmur.servers: if not quiet: info( "Setting authenticator for virtual server %d", server.id(), ) server.setAuthenticator(self.auth) except ( Murmur.InvalidSecretException, Ice.UnknownUserException, Ice.ConnectionRefusedException, ) as e: if isinstance(e, Ice.ConnectionRefusedException): error("Server refused connection") elif (isinstance(e, Murmur.InvalidSecretException) or isinstance(e, Ice.UnknownUserException) and (e.unknown == "Murmur::InvalidSecretException")): error("Invalid ice secret") else: # We do not actually want to handle this one, re-raise it raise e self.connected = False return False self.connected = True return True def checkConnection(self): """ Tries reapplies all callbacks to make sure the authenticator survives server restarts and disconnects. """ # debug("Watchdog run") try: if not self.attachCallbacks(quiet=not self.failedWatch): self.failedWatch = True else: self.failedWatch = False except Ice.Exception as e: error( "Failed connection check, will retry in next watchdog run (%ds)", cfg.ice.watchdog, ) debug(str(e)) self.failedWatch = True # Renew the timer self.watchdog = Timer(cfg.ice.watchdog, self.checkConnection) self.watchdog.start() def checkSecret(func): """ Decorator that checks whether the server transmitted the right secret if a secret is supposed to be used. """ if not cfg.ice.secret: return func def newfunc(*args, **kws): if "current" in kws: current = kws["current"] else: current = args[-1] if (not current or "secret" not in current.ctx or current.ctx["secret"] != cfg.ice.secret): error( "Server transmitted invalid secret. Possible injection attempt." ) raise Murmur.InvalidSecretException() return func(*args, **kws) return newfunc def fortifyIceFu(retval=None, exceptions=(Ice.Exception, )): """ Decorator that catches exceptions,logs them and returns a safe retval value. This helps preventing the authenticator getting stuck in critical code paths. Only exceptions that are instances of classes given in the exceptions list are not caught. The default is to catch all non-Ice exceptions. """ def newdec(func): def newfunc(*args, **kws): try: return func(*args, **kws) except Exception as e: catch = True for ex in exceptions: if isinstance(e, ex): catch = False break if catch: critical("Unexpected exception caught") exception(e) return retval raise return newfunc return newdec class metaCallback(Murmur.MetaCallback): def __init__(self, app): Murmur.MetaCallback.__init__(self) self.app = app @fortifyIceFu() @checkSecret def started(self, server, current=None): """ This function is called when a virtual server is started and makes sure an authenticator gets attached if needed. """ if not cfg.murmur.servers or server.id() in cfg.murmur.servers: info("Setting authenticator for virtual server %d", server.id()) try: server.setAuthenticator(app.auth) # Apparently this server was restarted without us noticing except (Murmur.InvalidSecretException, Ice.UnknownUserException) as e: if (hasattr(e, "unknown") and e.unknown != "Murmur::InvalidSecretException"): # Special handling for Murmur 1.2.2 servers with invalid slice files raise e error("Invalid ice secret") return else: debug("Virtual server %d got started", server.id()) @fortifyIceFu() @checkSecret def stopped(self, server, current=None): """ This function is called when a virtual server is stopped """ if self.app.connected: # Only try to output the server id if we think we are still connected to prevent # flooding of our thread pool try: if not cfg.murmur.servers or server.id( ) in cfg.murmur.servers: info("Authenticated virtual server %d got stopped", server.id()) else: debug("Virtual server %d got stopped", server.id()) return except Ice.ConnectionRefusedException: self.app.connected = False debug("Server shutdown stopped a virtual server") if cfg.user.reject_on_error: # Python 2.4 compat authenticateFortifyResult = (-1, None, None) else: authenticateFortifyResult = (-2, None, None) class LDAPAuthenticator(Murmur.ServerUpdatingAuthenticator): def __init__(self): Murmur.ServerUpdatingAuthenticator.__init__(self) self.name_uid_cache = dict() @fortifyIceFu(authenticateFortifyResult) @checkSecret def authenticate(self, name, pw, certlist, certhash, strong, current=None, groups=None): """ This function is called to authenticate a user """ # Search for the user in the database FALL_THROUGH = -2 AUTH_REFUSED = -1 # SuperUser is a special login. if name == "SuperUser": debug("Forced fall through for SuperUser") return (FALL_THROUGH, None, None) # Otherwise, let's check the LDAP server. uid = None if cfg.ldap.use_start_tls: # try StartTLS: global options debug( "use_start_tls is set, setting global option TLS_REQCERT = never" ) ldap.set_option(ldap.OPT_X_TLS_REQUIRE_CERT, ldap.OPT_X_TLS_NEVER) ldap_trace = 0 # Change to 1 for more verbose trace ldap_conn = ldap.initialize(cfg.ldap.ldap_uri, ldap_trace) if cfg.ldap.use_start_tls: # try StartTLS: connection specific options debug( "use_start_tls is set, setting connection options X_TLS_*") ldap_conn.set_option(ldap.OPT_PROTOCOL_VERSION, 3) ldap_conn.set_option(ldap.OPT_X_TLS, ldap.OPT_X_TLS_DEMAND) ldap_conn.set_option(ldap.OPT_X_TLS_DEMAND, True) try: ldap_conn.start_tls_s() except Exception as e: warning("could not initiate StartTLS, e = " + str(e)) return (AUTH_REFUSED, None, None) if cfg.ldap.bind_dn: # Bind the functional account to search the directory. bind_dn = cfg.ldap.bind_dn bind_pass = cfg.ldap.bind_pass try: debug("try to connect to ldap (bind_dn will be used)") ldap_conn.bind_s(bind_dn, bind_pass) except ldap.INVALID_CREDENTIALS: ldap_conn.unbind() warning("Invalid credentials for bind_dn=" + bind_dn) return (AUTH_REFUSED, None, None) elif cfg.ldap.discover_dn: # Use anonymous bind to discover the DN try: ldap_conn.bind_s() except ldap.INVALID_CREDENTIALS: ldap_conn.unbind() warning("Failed anomymous bind for discovering DN") return (AUTH_REFUSED, None, None) else: # Prevent anonymous authentication. if not pw: warning("No password supplied for user " + name) return (AUTH_REFUSED, None, None) # Bind the user account to search the directory. bind_dn = "%s=%s,%s" % (cfg.ldap.username_attr, name, cfg.ldap.users_dn) bind_pass = pw try: ldap_conn.bind_s(bind_dn, bind_pass) except ldap.INVALID_CREDENTIALS: ldap_conn.unbind() warning("User " + name + " failed with invalid credentials") return (AUTH_REFUSED, None, None) # Search for the user. res = ldap_conn.search_s( cfg.ldap.users_dn, ldap.SCOPE_SUBTREE, "(%s=%s)" % (cfg.ldap.username_attr, name), [cfg.ldap.number_attr, cfg.ldap.display_attr], ) if len(res) == 0: warning("User " + name + " not found") if cfg.user.reject_on_miss: return (AUTH_REFUSED, None, None) else: return (FALL_THROUGH, None, None) match = res[ 0] # Only interested in the first result, as there should only be one match # Parse the user information. uid = int(match[1][cfg.ldap.number_attr][0]) displayName = str( (match[1][cfg.ldap.display_attr][0]).decode("utf-8")) user_dn = match[0] debug("User match found, display '" + displayName + "' with UID " + repr(uid)) # Optionally check groups. if cfg.ldap.group_dn != "": debug("Checking group membership for " + name) # Search for user in group res = ldap_conn.search_s( cfg.ldap.group_dn, ldap.SCOPE_SUBTREE, "(%s=%s)" % (cfg.ldap.group_attr, user_dn), [cfg.ldap.number_attr, cfg.ldap.display_attr], ) # Check if the user is a member of the group if len(res) < 1: debug("User " + name + " failed with no group membership") return (AUTH_REFUSED, None, None) # Optionally check admin. is_admin = False if cfg.ldap.admin_dn != "": debug("Checking admin membership for " + name) # Search for user in group res = ldap_conn.search_s( cfg.ldap.admin_dn, ldap.SCOPE_SUBTREE, "(%s=%s)" % (cfg.ldap.group_attr, user_dn), [cfg.ldap.number_attr, cfg.ldap.display_attr], ) # Check if the user is a member of the group if len(res) > 0: debug("User " + name + " have admin membership") is_admin = True # Second bind to test user credentials if using bind_dn or discover_dn. if cfg.ldap.bind_dn or cfg.ldap.discover_dn: # Prevent anonymous authentication. if not pw: warning("No password supplied for user " + name) return (AUTH_REFUSED, None, None) bind_dn = user_dn bind_pass = pw try: ldap_conn.bind_s(bind_dn, bind_pass) except ldap.INVALID_CREDENTIALS: ldap_conn.unbind() warning("User " + name + " failed with wrong password") return (AUTH_REFUSED, None, None) # Unbind and close connection. ldap_conn.unbind() # If we get here, the login is correct. # Add the user/id combo to cache, then accept: self.name_uid_cache[displayName] = uid debug("Login accepted for " + name) # displayName = '[{group}] {displayName}'.format(group=('A' if is_admin == True else 'U'), displayName=displayName) return (uid + cfg.user.id_offset, displayName, []) @fortifyIceFu((False, None)) @checkSecret def getInfo(self, id, current=None): """ Gets called to fetch user specific information """ if not cfg.ldap.provide_info: # We do not expose any additional information so always fall through debug("getInfo for %d -> denied", id) return (False, None) ldap_conn = ldap.initialize(cfg.ldap.ldap_uri, 0) # Bind if configured, else do explicit anonymous bind if cfg.ldap.bind_dn and cfg.ldap.bind_pass: ldap_conn.simple_bind_s(cfg.ldap.bind_dn, cfg.ldap.bind_pass) else: ldap_conn.simple_bind_s() name = self.idToName(id, current) res = ldap_conn.search_s( cfg.ldap.users_dn, ldap.SCOPE_SUBTREE, "(%s=%s)" % (cfg.ldap.display_attr, name), [cfg.ldap.display_attr, cfg.ldap.mail_attr], ) # If user found, return info if len(res) == 1: info = {} if cfg.ldap.mail_attr in res[0][1]: info[Murmur.UserInfo.UserEmail] = res[0][1][ cfg.ldap.mail_attr][0] debug("getInfo %s -> %s", name, repr(info)) return (True, info) else: debug("getInfo %s -> ?", name) return (False, None) @fortifyIceFu(-2) @checkSecret def nameToId(self, name, current=None): """ Gets called to get the id for a given username """ FALL_THROUGH = -2 if name == "SuperUser": debug("nameToId SuperUser -> forced fall through") return FALL_THROUGH if name in self.name_uid_cache: uid = self.name_uid_cache[name] + cfg.user.id_offset debug("nameToId %s (cache) -> %d", name, uid) return uid ldap_conn = ldap.initialize(cfg.ldap.ldap_uri, 0) # Bind if configured, else do explicit anonymous bind if cfg.ldap.bind_dn and cfg.ldap.bind_pass: ldap_conn.simple_bind_s(cfg.ldap.bind_dn, cfg.ldap.bind_pass) else: ldap_conn.simple_bind_s() res = ldap_conn.search_s( cfg.ldap.users_dn, ldap.SCOPE_SUBTREE, "(%s=%s)" % (cfg.ldap.display_attr, name), [cfg.ldap.number_attr], ) # If user found, return the ID if len(res) == 1: uid = int( res[0][1][cfg.ldap.number_attr][0]) + cfg.user.id_offset debug("nameToId %s -> %d", name, uid) else: debug("nameToId %s -> ?", name) return FALL_THROUGH return uid @fortifyIceFu("") @checkSecret def idToName(self, id, current=None): """ Gets called to get the username for a given id """ FALL_THROUGH = "" # Make sure the ID is in our range and transform it to the actual LDAP user id if id < cfg.user.id_offset: debug("idToName %d -> fall through", id) return FALL_THROUGH ldapid = id - cfg.user.id_offset for name, uid in self.name_uid_cache.items(): if uid == ldapid: if name == "SuperUser": debug("idToName %d -> 'SuperUser' catched", id) return FALL_THROUGH debug("idToName %d -> '%s'", id, name) return name debug("idToName %d -> ?", id) return FALL_THROUGH @fortifyIceFu("") @checkSecret def idToTexture(self, id, current=None): """ Gets called to get the corresponding texture for a user """ FALL_THROUGH = "" debug("idToTexture %d -> fall through", id) return FALL_THROUGH @fortifyIceFu(-2) @checkSecret def registerUser(self, name, current=None): """ Gets called when the server is asked to register a user. """ debug("registerUser.name: {}".format(dir(name))) debug("registerUser.current: {}".format(dir(current))) FAILURE = -1 FALL_THROUGH = -2 debug("registerUser '%s' -> fall through", name) return FAILURE @fortifyIceFu(-1) @checkSecret def unregisterUser(self, id, current=None): """ Gets called when the server is asked to unregister a user. """ FALL_THROUGH = -1 # Return -1 to fall through to internal server database, we will not modify the LDAP directory # but we can make murmur delete all additional information it got this way. debug("unregisterUser %d -> fall through", id) return FALL_THROUGH @fortifyIceFu({}) @checkSecret def getRegisteredUsers(self, filter, current=None): """ Returns a list of usernames in the LDAP directory which contain filter as a substring. """ FALL_THROUGH = {} if not cfg.ldap.provide_users: # Fall through if not configured to provide user list debug("getRegisteredUsers -> fall through") return FALL_THROUGH ldap_conn = ldap.initialize(cfg.ldap.ldap_uri, 0) # Bind if configured, else do explicit anonymous bind if cfg.ldap.bind_dn and cfg.ldap.bind_pass: ldap_conn.simple_bind_s(cfg.ldap.bind_dn, cfg.ldap.bind_pass) else: ldap_conn.simple_bind_s() if filter: res = ldap_conn.search_s( cfg.ldap.users_dn, ldap.SCOPE_SUBTREE, "(&(uid=*)(%s=*%s*))" % (cfg.ldap.display_attr, filter), [cfg.ldap.number_attr, cfg.ldap.display_attr], ) else: res = ldap_conn.search_s( cfg.ldap.users_dn, ldap.SCOPE_SUBTREE, "(uid=*)", [cfg.ldap.number_attr, cfg.ldap.display_attr], ) # Build result dict users = {} for dn, attrs in res: if cfg.ldap.number_attr in attrs and cfg.ldap.display_attr in attrs: uid = int( attrs[cfg.ldap.number_attr][0]) + cfg.user.id_offset name = attrs[cfg.ldap.display_attr][0] users[uid] = name debug("getRegisteredUsers %s -> %s", filter, repr(users)) return users @fortifyIceFu(-1) @checkSecret def setInfo(self, id, info, current=None): """ Gets called when the server is supposed to save additional information about a user to his database """ FALL_THROUGH = -1 # Return -1 to fall through to the internal server handler. We do not store # any information in LDAP debug("setInfo %d -> fall through", id) return FALL_THROUGH @fortifyIceFu(-1) @checkSecret def setTexture(self, id, texture, current=None): """ Gets called when the server is asked to update the user texture of a user """ FALL_THROUGH = -1 # We do not store textures in LDAP debug("setTexture %d -> fall through", id) return FALL_THROUGH class CustomLogger(Ice.Logger): """ Logger implementation to pipe Ice log messages into out own log """ def __init__(self): Ice.Logger.__init__(self) self._log = getLogger("Ice") def _print(self, message): self._log.info(message) def trace(self, category, message): self._log.debug("Trace %s: %s", category, message) def warning(self, message): self._log.warning(message) def error(self, message): self._log.error(message) # # --- Start of authenticator # info("Starting LDAP mumble authenticator") initdata = Ice.InitializationData() initdata.properties = Ice.createProperties([], initdata.properties) for prop, val in cfg.iceraw: initdata.properties.setProperty(prop, val) initdata.properties.setProperty("Ice.ImplicitContext", "Shared") initdata.properties.setProperty("Ice.Default.EncodingVersion", "1.0") initdata.logger = CustomLogger() app = LDAPAuthenticatorApp() app.main(sys.argv[:1], initData=initdata) info("Shutdown complete")
def twowaysFuture(communicator, p): f = p.ice_pingAsync() test(f.result() is None) f = p.ice_isAAsync(Test._MyClassDisp.ice_staticId()) test(f.result()) f = p.ice_idAsync() test(f.result() == "::Test::MyDerivedClass") f = p.ice_idsAsync() test(len(f.result()) == 3) f = p.opVoidAsync() test(f.result() is None) cb = Callback() p.opVoidAsync().add_done_callback(lambda f: cb.called()) cb.check() f = p.opByteAsync(0xff, 0x0f) (ret, p3) = f.result() test(p3 == 0xf0) test(ret == 0xff) cb = Callback() p.opByteAsync(0xff, 0x0f).add_done_callback(cb.opByte) cb.check() cb = Callback() p.opBoolAsync(True, False).add_done_callback(cb.opBool) cb.check() cb = Callback() p.opShortIntLongAsync(10, 11, 12).add_done_callback(cb.opShortIntLong) cb.check() cb = Callback() p.opFloatDoubleAsync(3.14, 1.1E10).add_done_callback(cb.opFloatDouble) cb.check() cb = Callback() p.opStringAsync("hello", "world").add_done_callback(cb.opString) cb.check() cb = Callback() p.opMyEnumAsync(Test.MyEnum.enum2).add_done_callback(cb.opMyEnum) cb.check() cb = Callback(communicator) p.opMyClassAsync(p).add_done_callback(cb.opMyClass) cb.check() si1 = Test.Structure() si1.p = p si1.e = Test.MyEnum.enum3 si1.s = Test.AnotherStruct() si1.s.s = "abc" si2 = Test.Structure() si2.p = None si2.e = Test.MyEnum.enum2 si2.s = Test.AnotherStruct() si2.s.s = "def" cb = Callback(communicator) p.opStructAsync(si1, si2).add_done_callback(cb.opStruct) cb.check() bsi1 = (0x01, 0x11, 0x12, 0x22) bsi2 = (0xf1, 0xf2, 0xf3, 0xf4) cb = Callback() p.opByteSAsync(bsi1, bsi2).add_done_callback(cb.opByteS) cb.check() bsi1 = (True, True, False) bsi2 = (False, ) cb = Callback() p.opBoolSAsync(bsi1, bsi2).add_done_callback(cb.opBoolS) cb.check() ssi = (1, 2, 3) isi = (5, 6, 7, 8) lsi = (10, 30, 20) cb = Callback() p.opShortIntLongSAsync(ssi, isi, lsi).add_done_callback(cb.opShortIntLongS) cb.check() fsi = (3.14, 1.11) dsi = (1.1E10, 1.2E10, 1.3E10) cb = Callback() p.opFloatDoubleSAsync(fsi, dsi).add_done_callback(cb.opFloatDoubleS) cb.check() ssi1 = ('abc', 'de', 'fghi') ssi2 = ('xyz', ) cb = Callback() p.opStringSAsync(ssi1, ssi2).add_done_callback(cb.opStringS) cb.check() bsi1 = ((0x01, 0x11, 0x12), (0xff, )) bsi2 = ((0x0e, ), (0xf2, 0xf1)) cb = Callback() p.opByteSSAsync(bsi1, bsi2).add_done_callback(cb.opByteSS) cb.check() bsi1 = ( (True, ), (False, ), (True, True), ) bsi2 = ((False, False, True), ) cb = Callback() p.opBoolSSAsync(bsi1, bsi2).add_done_callback(cb.opBoolSS) cb.check() ssi = ((1, 2, 5), (13, ), ()) isi = ((24, 98), (42, )) lsi = ((496, 1729), ) cb = Callback() p.opShortIntLongSSAsync(ssi, isi, lsi).add_done_callback(cb.opShortIntLongSS) cb.check() fsi = ((3.14, ), (1.11, ), ()) dsi = ((1.1E10, 1.2E10, 1.3E10), ) cb = Callback() p.opFloatDoubleSSAsync(fsi, dsi).add_done_callback(cb.opFloatDoubleSS) cb.check() ssi1 = (('abc', ), ('de', 'fghi')) ssi2 = ((), (), ('xyz', )) cb = Callback() p.opStringSSAsync(ssi1, ssi2).add_done_callback(cb.opStringSS) cb.check() di1 = {10: True, 100: False} di2 = {10: True, 11: False, 101: True} cb = Callback() p.opByteBoolDAsync(di1, di2).add_done_callback(cb.opByteBoolD) cb.check() di1 = {110: -1, 1100: 123123} di2 = {110: -1, 111: -100, 1101: 0} cb = Callback() p.opShortIntDAsync(di1, di2).add_done_callback(cb.opShortIntD) cb.check() di1 = {999999110: -1.1, 999999111: 123123.2} di2 = {999999110: -1.1, 999999120: -100.4, 999999130: 0.5} cb = Callback() p.opLongFloatDAsync(di1, di2).add_done_callback(cb.opLongFloatD) cb.check() di1 = {'foo': 'abc -1.1', 'bar': 'abc 123123.2'} di2 = {'foo': 'abc -1.1', 'FOO': 'abc -100.4', 'BAR': 'abc 0.5'} cb = Callback() p.opStringStringDAsync(di1, di2).add_done_callback(cb.opStringStringD) cb.check() di1 = {'abc': Test.MyEnum.enum1, '': Test.MyEnum.enum2} di2 = { 'abc': Test.MyEnum.enum1, 'qwerty': Test.MyEnum.enum3, 'Hello!!': Test.MyEnum.enum2 } cb = Callback() p.opStringMyEnumDAsync(di1, di2).add_done_callback(cb.opStringMyEnumD) cb.check() di1 = {Test.MyEnum.enum1: 'abc'} di2 = {Test.MyEnum.enum2: 'Hello!!', Test.MyEnum.enum3: 'qwerty'} cb = Callback() p.opMyEnumStringDAsync(di1, di2).add_done_callback(cb.opMyEnumStringD) cb.check() s11 = Test.MyStruct() s11.i = 1 s11.j = 1 s12 = Test.MyStruct() s12.i = 1 s12.j = 2 s22 = Test.MyStruct() s22.i = 2 s22.j = 2 s23 = Test.MyStruct() s23.i = 2 s23.j = 3 di1 = {s11: Test.MyEnum.enum1, s12: Test.MyEnum.enum2} di2 = { s11: Test.MyEnum.enum1, s22: Test.MyEnum.enum3, s23: Test.MyEnum.enum2 } cb = Callback() p.opMyStructMyEnumDAsync(di1, di2).add_done_callback(cb.opMyStructMyEnumD) cb.check() dsi1 = ({10: True, 100: False}, {10: True, 11: False, 101: True}) dsi2 = ({100: False, 101: False}, ) cb = Callback() p.opByteBoolDSAsync(dsi1, dsi2).add_done_callback(cb.opByteBoolDS) cb.check() dsi1 = ({110: -1, 1100: 123123}, {110: -1, 111: -100, 1101: 0}) dsi2 = ({100: -1001}, ) cb = Callback() p.opShortIntDSAsync(dsi1, dsi2).add_done_callback(cb.opShortIntDS) cb.called() dsi1 = ({ 999999110: -1.1, 999999111: 123123.2 }, { 999999110: -1.1, 999999120: -100.4, 999999130: 0.5 }) dsi2 = ({999999140: 3.14}, ) cb = Callback() p.opLongFloatDSAsync(dsi1, dsi2).add_done_callback(cb.opLongFloatDS) cb.called() dsi1 = ({ "foo": "abc -1.1", "bar": "abc 123123.2" }, { "foo": "abc -1.1", "FOO": "abc -100.4", "BAR": "abc 0.5" }) dsi2 = ({"f00": "ABC -3.14"}, ) cb = Callback() p.opStringStringDSAsync(dsi1, dsi2).add_done_callback(cb.opStringStringDS) cb.called() dsi1 = ({ "abc": Test.MyEnum.enum1, "": Test.MyEnum.enum2 }, { "abc": Test.MyEnum.enum1, "qwerty": Test.MyEnum.enum3, "Hello!!": Test.MyEnum.enum2 }) dsi2 = ({"Goodbye": Test.MyEnum.enum1}, ) cb = Callback() p.opStringMyEnumDSAsync(dsi1, dsi2).add_done_callback(cb.opStringMyEnumDS) cb.called() dsi1 = ({ Test.MyEnum.enum1: 'abc' }, { Test.MyEnum.enum2: 'Hello!!', Test.MyEnum.enum3: 'qwerty' }) dsi2 = ({Test.MyEnum.enum1: 'Goodbye'}, ) cb = Callback() p.opMyEnumStringDSAsync(dsi1, dsi2).add_done_callback(cb.opMyEnumStringDS) cb.called() s11 = Test.MyStruct(1, 1) s12 = Test.MyStruct(1, 2) s22 = Test.MyStruct(2, 2) s23 = Test.MyStruct(2, 3) dsi1 = ({ s11: Test.MyEnum.enum1, s12: Test.MyEnum.enum2 }, { s11: Test.MyEnum.enum1, s22: Test.MyEnum.enum3, s23: Test.MyEnum.enum2 }) dsi2 = ({s23: Test.MyEnum.enum3}, ) cb = Callback() p.opMyStructMyEnumDSAsync(dsi1, dsi2).add_done_callback(cb.opMyStructMyEnumDS) cb.called() sdi1 = {0x01: (0x01, 0x11), 0x22: (0x12, )} sdi2 = {0xf1: (0xf2, 0xf3)} cb = Callback() p.opByteByteSDAsync(sdi1, sdi2).add_done_callback(cb.opByteByteSD) cb.called() sdi1 = {False: (True, False), True: (False, True, True)} sdi2 = {False: (True, False)} cb = Callback() p.opBoolBoolSDAsync(sdi1, sdi2).add_done_callback(cb.opBoolBoolSD) cb.called() sdi1 = {1: (1, 2, 3), 2: (4, 5)} sdi2 = {4: (6, 7)} cb = Callback() p.opShortShortSDAsync(sdi1, sdi2).add_done_callback(cb.opShortShortSD) cb.called() sdi1 = {100: (100, 200, 300), 200: (400, 500)} sdi2 = {400: (600, 700)} cb = Callback() p.opIntIntSDAsync(sdi1, sdi2).add_done_callback(cb.opIntIntSD) cb.called() sdi1 = { 999999990: (999999110, 999999111, 999999110), 999999991: (999999120, 999999130) } sdi2 = {999999992: (999999110, 999999120)} cb = Callback() p.opLongLongSDAsync(sdi1, sdi2).add_done_callback(cb.opLongLongSD) cb.called() sdi1 = {"abc": (-1.1, 123123.2, 100.0), "ABC": (42.24, -1.61)} sdi2 = {"aBc": (-3.14, 3.14)} cb = Callback() p.opStringFloatSDAsync(sdi1, sdi2).add_done_callback(cb.opStringFloatSD) cb.called() sdi1 = {"Hello!!": (1.1E10, 1.2E10, 1.3E10), "Goodbye": (1.4E10, 1.5E10)} sdi2 = {"": (1.6E10, 1.7E10)} cb = Callback() p.opStringDoubleSDAsync(sdi1, sdi2).add_done_callback(cb.opStringDoubleSD) cb.called() sdi1 = {"abc": ("abc", "de", "fghi"), "def": ("xyz", "or")} sdi2 = {"ghi": ("and", "xor")} cb = Callback() p.opStringStringSDAsync(sdi1, sdi2).add_done_callback(cb.opStringStringSD) cb.called() sdi1 = { Test.MyEnum.enum3: (Test.MyEnum.enum1, Test.MyEnum.enum1, Test.MyEnum.enum2), Test.MyEnum.enum2: (Test.MyEnum.enum1, Test.MyEnum.enum2) } sdi2 = {Test.MyEnum.enum1: (Test.MyEnum.enum3, Test.MyEnum.enum3)} cb = Callback() p.opMyEnumMyEnumSDAsync(sdi1, sdi2).add_done_callback(cb.opMyEnumMyEnumSD) cb.called() lengths = (0, 1, 2, 126, 127, 128, 129, 253, 254, 255, 256, 257, 1000) for l in lengths: s = [] for i in range(l): s.append(i) cb = Callback(l) p.opIntSAsync(s).add_done_callback(cb.opIntS) cb.check() ctx = {'one': 'ONE', 'two': 'TWO', 'three': 'THREE'} test(len(p.ice_getContext()) == 0) f = p.opContextAsync() c = f.result() test(c != ctx) test(len(p.ice_getContext()) == 0) f = p.opContextAsync(context=ctx) c = f.result() test(c == ctx) p2 = Test.MyClassPrx.checkedCast(p.ice_context(ctx)) test(p2.ice_getContext() == ctx) f = p2.opContextAsync() c = f.result() test(c == ctx) f = p2.opContextAsync(context=ctx) c = f.result() test(c == ctx) # # Test implicit context propagation # if p.ice_getConnection(): impls = ('Shared', 'PerThread') for i in impls: initData = Ice.InitializationData() initData.properties = communicator.getProperties().clone() initData.properties.setProperty('Ice.ImplicitContext', i) ic = Ice.initialize(data=initData) ctx = {'one': 'ONE', 'two': 'TWO', 'three': 'THREE'} p3 = Test.MyClassPrx.uncheckedCast( ic.stringToProxy("test:default -p 12010")) ic.getImplicitContext().setContext(ctx) test(ic.getImplicitContext().getContext() == ctx) f = p3.opContextAsync() c = f.result() test(c == ctx) ic.getImplicitContext().put('zero', 'ZERO') ctx = ic.getImplicitContext().getContext() f = p3.opContextAsync() c = f.result() test(c == ctx) prxContext = {'one': 'UN', 'four': 'QUATRE'} combined = {} combined.update(ctx) combined.update(prxContext) test(combined['one'] == 'UN') p3 = Test.MyClassPrx.uncheckedCast(p3.ice_context(prxContext)) ic.getImplicitContext().setContext({}) f = p3.opContextAsync() c = f.result() test(c == prxContext) ic.getImplicitContext().setContext(ctx) f = p3.opContextAsync() c = f.result() test(c == combined) ic.destroy() cb = Callback() p.opIdempotentAsync().add_done_callback(cb.opIdempotent) cb.check() cb = Callback() p.opNonmutatingAsync().add_done_callback(cb.opNonmutating) cb.check() derived = Test.MyDerivedClassPrx.checkedCast(p) test(derived) cb = Callback() derived.opDerivedAsync().add_done_callback(cb.opDerived) cb.check() f = p.opByte1Async(0xFF) test(f.result() == 0xFF) f = p.opShort1Async(0x7FFF) test(f.result() == 0x7FFF) f = p.opInt1Async(0x7FFFFFFF) test(f.result() == 0x7FFFFFFF) f = p.opLong1Async(0x7FFFFFFFFFFFFFFF) test(f.result() == 0x7FFFFFFFFFFFFFFF) f = p.opFloat1Async(1.0) test(f.result() == 1.0) f = p.opDouble1Async(1.0) test(f.result() == 1.0) f = p.opString1Async("opString1") test(f.result() == "opString1") f = p.opStringS1Async(None) test(len(f.result()) == 0) f = p.opByteBoolD1Async(None) test(len(f.result()) == 0) f = p.opStringS2Async(None) test(len(f.result()) == 0) f = p.opByteBoolD2Async(None) test(len(f.result()) == 0)
def batchOneways(p): if sys.version_info[0] == 2: bs1 = [] bs1[0:10 * 1024] = range(0, 10 * 1024) # add 100,000 entries. bs1 = ['\x00' for x in bs1] # set them all to \x00 bs1 = ''.join(bs1) # make into a byte array else: bs1 = bytes([0 for x in range(0, 10 * 1024)]) try: p.opByteSOneway(bs1) except Ice.MemoryLimitException: test(False) batch = Test.MyClassPrx.uncheckedCast(p.ice_batchOneway()) batch.ice_flushBatchRequests() # Empty flush p.opByteSOnewayCallCount() # Reset the call count for i in range(30): batch.opByteSOneway(bs1) count = 0 while count < 27: # 3 * 9 requests auto-flushed. count += p.opByteSOnewayCallCount() time.sleep(0.01) if p.ice_getConnection(): batch1 = Test.MyClassPrx.uncheckedCast(p.ice_batchOneway()) batch2 = Test.MyClassPrx.uncheckedCast(p.ice_batchOneway()) batch1.ice_ping() batch2.ice_ping() batch1.ice_flushBatchRequests() batch1.ice_getConnection().close(False) batch1.ice_ping() batch2.ice_ping() batch1.ice_getConnection() batch2.ice_getConnection() batch1.ice_ping() batch1.ice_getConnection().close(False) batch1.ice_ping() batch2.ice_ping() identity = Ice.Identity() identity.name = "invalid"; batch3 = batch.ice_identity(identity) batch3.ice_ping() batch3.ice_flushBatchRequests() # Make sure that a bogus batch request doesn't cause troubles to other ones. batch3.ice_ping() batch.ice_ping() batch.ice_flushBatchRequests() batch.ice_ping() if batch.ice_getConnection(): initData = Ice.InitializationData() initData.properties = p.ice_getCommunicator().getProperties().clone() interceptor = BatchRequestInterceptorI() initData.batchRequestInterceptor = interceptor ic = Ice.initialize(data=initData) batch = Test.MyClassPrx.uncheckedCast(ic.stringToProxy(p.ice_toString())).ice_batchOneway() test(interceptor.count() == 0) batch.ice_ping() batch.ice_ping() batch.ice_ping() test(interceptor.count() == 0) interceptor.setEnabled(True) batch.ice_ping() batch.ice_ping() batch.ice_ping() test(interceptor.count() == 3) batch.ice_flushBatchRequests() batch.ice_ping() test(interceptor.count() == 1) batch.opByteSOneway(bs1) test(interceptor.count() == 2) batch.opByteSOneway(bs1) test(interceptor.count() == 3) batch.opByteSOneway(bs1) # This should trigger the flush batch.ice_ping() test(interceptor.count() == 2) ic.destroy()
def allTests(communicator, collocated): sref = "test:default -p 12010" obj = communicator.stringToProxy(sref) test(obj) p = Test.TestIntfPrx.uncheckedCast(obj) sref = "testController:default -p 12011" obj = communicator.stringToProxy(sref) test(obj) testController = Test.TestIntfControllerPrx.uncheckedCast(obj) sys.stdout.write("testing begin/end invocation... ") sys.stdout.flush() ctx = {} result = p.begin_ice_isA("::Test::TestIntf") test(p.end_ice_isA(result)) result = p.begin_ice_isA("::Test::TestIntf", _ctx=ctx) test(p.end_ice_isA(result)) result = p.begin_ice_ping() p.end_ice_ping(result) result = p.begin_ice_ping(_ctx=ctx) p.end_ice_ping(result) result = p.begin_ice_id() test(p.end_ice_id(result) == "::Test::TestIntf") result = p.begin_ice_id(_ctx=ctx) test(p.end_ice_id(result) == "::Test::TestIntf") result = p.begin_ice_ids() test(len(p.end_ice_ids(result)) == 2) result = p.begin_ice_ids(_ctx=ctx) test(len(p.end_ice_ids(result)) == 2) if not collocated: result = p.begin_ice_getConnection() test(p.end_ice_getConnection(result) != None) result = p.begin_op() p.end_op(result) result = p.begin_op(_ctx=ctx) p.end_op(result) result = p.begin_opWithResult() test(p.end_opWithResult(result) == 15) result = p.begin_opWithResult(_ctx=ctx) test(p.end_opWithResult(result) == 15) result = p.begin_opWithUE() try: p.end_opWithUE(result) test(False) except Test.TestIntfException: pass result = p.begin_opWithUE(_ctx=ctx) try: p.end_opWithUE(result) test(False) except Test.TestIntfException: pass print("ok") sys.stdout.write("testing response callback... ") sys.stdout.flush() ctx = {} cb = ResponseCallback() cookie = 5 cbWC = ResponseCallbackWC(cookie) p.begin_ice_isA(Test.TestIntf.ice_staticId(), cb.isA, cb.ex) cb.check() p.begin_ice_isA(Test.TestIntf.ice_staticId(), lambda r: cbWC.isA(r, cookie), lambda ex: cbWC.ex(ex, cookie)) cbWC.check() p.begin_ice_isA(Test.TestIntf.ice_staticId(), cb.isA, cb.ex, _ctx=ctx) cb.check() p.begin_ice_isA(Test.TestIntf.ice_staticId(), lambda r: cbWC.isA(r, cookie), lambda ex: cbWC.ex(ex, cookie), _ctx=ctx) cbWC.check() p.begin_ice_ping(cb.ping, cb.ex) cb.check() p.begin_ice_ping(lambda: cbWC.ping(cookie), lambda ex: cbWC.ex(ex, cookie)) cbWC.check() p.begin_ice_ping(cb.ping, cb.ex, _ctx=ctx) cb.check() p.begin_ice_ping(lambda: cbWC.ping(cookie), lambda: cbWC.ex(ex, cookie), _ctx=ctx) cbWC.check() p.begin_ice_id(cb.id, cb.ex) cb.check() p.begin_ice_id(lambda id: cbWC.id(id, cookie), lambda ex: cbWC.ex(ex, cookie)) cbWC.check() p.begin_ice_id(cb.id, cb.ex, _ctx=ctx) cb.check() p.begin_ice_id(lambda id: cbWC.id(id, cookie), lambda ex: cbWC.ex(ex, cookie), _ctx=ctx) cbWC.check() p.begin_ice_ids(cb.ids, cb.ex) cb.check() p.begin_ice_ids(lambda ids: cbWC.ids(ids, cookie), lambda ex: cbWC.ex(ex, cookie)) cbWC.check() p.begin_ice_ids(cb.ids, cb.ex, _ctx=ctx) cb.check() p.begin_ice_ids(lambda ids: cbWC.ids(ids, cookie), lambda ex: cbWC.ex(ex, cookie), _ctx=ctx) cbWC.check() if not collocated: p.begin_ice_getConnection(cb.connection, cb.ex) cb.check() p.begin_ice_getConnection(lambda conn: cbWC.connection(conn, cookie), lambda ex: cbWC.ex(ex, cookie)) cbWC.check() p.begin_op(cb.op, cb.ex) cb.check() p.begin_op(lambda: cbWC.op(cookie), lambda ex: cbWC.ex(ex, cookie)) cbWC.check() p.begin_op(cb.op, cb.ex, _ctx=ctx) cb.check() p.begin_op(lambda: cbWC.op(cookie), lambda ex: cbWC.ex(ex, cookie), _ctx=ctx) cbWC.check() p.begin_opWithResult(cb.opWithResult, cb.ex) cb.check() p.begin_opWithResult(lambda r: cbWC.opWithResult(r, cookie), lambda ex: cbWC.ex(ex, cookie)) cbWC.check() p.begin_opWithResult(cb.opWithResult, cb.ex, _ctx=ctx) cb.check() p.begin_opWithResult(lambda r: cbWC.opWithResult(r, cookie), lambda ex: cbWC.ex(ex, cookie), _ctx=ctx) cbWC.check() p.begin_opWithUE(cb.op, cb.opWithUE) cb.check() p.begin_opWithUE(lambda: cbWC.op(cookie), lambda ex: cbWC.opWithUE(ex, cookie)) cbWC.check() p.begin_opWithUE(cb.op, cb.opWithUE, _ctx=ctx) cb.check() p.begin_opWithUE(lambda: cbWC.op(cookie), lambda ex: cbWC.opWithUE(ex, cookie), _ctx=ctx) cbWC.check() print("ok") sys.stdout.write("testing local exceptions... ") sys.stdout.flush() indirect = Test.TestIntfPrx.uncheckedCast(p.ice_adapterId("dummy")) r = indirect.begin_op() try: indirect.end_op(r) test(False) except Ice.NoEndpointException: pass try: p.ice_oneway().begin_opWithResult() test(False) except RuntimeError: pass # # Check that CommunicatorDestroyedException is raised directly. # if p.ice_getConnection(): initData = Ice.InitializationData() initData.properties = communicator.getProperties().clone() ic = Ice.initialize(initData) obj = ic.stringToProxy(p.ice_toString()) p2 = Test.TestIntfPrx.checkedCast(obj) ic.destroy() try: p2.begin_op() test(False) except Ice.CommunicatorDestroyedException: pass print("ok") sys.stdout.write("testing local exceptions with response callback... ") sys.stdout.flush() i = Test.TestIntfPrx.uncheckedCast(p.ice_adapterId("dummy")) cb = ExceptionCallback() cookie = 5 cbWC = ExceptionCallbackWC(cookie) i.begin_ice_isA(Test.TestIntf.ice_staticId(), cb.response, cb.ex) cb.check() i.begin_ice_isA(Test.TestIntf.ice_staticId(), lambda b: cbWC.response(b, cookie), lambda ex: cbWC.ex(ex, cookie)) cbWC.check() i.begin_ice_ping(cb.response, cb.ex) cb.check() i.begin_ice_ping(lambda: cbWC.response(cookie), lambda ex: cbWC.ex(ex, cookie)) cbWC.check() i.begin_ice_id(cb.response, cb.ex) cb.check() i.begin_ice_id(lambda id: cbWC.response(id, cookie), lambda ex: cbWC.ex(ex, cookie)) cbWC.check() i.begin_ice_ids(cb.response, cb.ex) cb.check() i.begin_ice_ids(lambda ids: cbWC.response(ids, cookie), lambda ex: cbWC.ex(ex, cookie)) cbWC.check() if not collocated: i.begin_ice_getConnection(cb.response, cb.ex) cb.check() i.begin_ice_getConnection(lambda conn: cbWC.response(conn, cookie), lambda ex: cbWC.ex(ex, cookie)) cbWC.check() i.begin_op(cb.response, cb.ex) cb.check() i.begin_op(lambda: cbWC.response(cookie), lambda ex: cbWC.ex(ex, cookie)) cbWC.check() print("ok") sys.stdout.write("testing exception callback... ") sys.stdout.flush() cb = ExceptionCallback() cookie = 5 cbWC = ExceptionCallbackWC(cookie) # Ensures no exception is called when response is received. p.begin_ice_isA(Test.TestIntf.ice_staticId(), cb.nullResponse, cb.noEx) p.begin_ice_isA(Test.TestIntf.ice_staticId(), lambda b: cbWC.nullResponse(b, cookie), lambda ex: cbWC.noEx(ex, cookie)) p.begin_op(cb.nullResponse, cb.noEx) p.begin_op(lambda: cbWC.nullResponse(cookie), lambda ex: cbWC.noEx(ex, cookie)) # If response is a user exception, it should be received. p.begin_opWithUE(cb.nullResponse, cb.opWithUE) cb.check() p.begin_opWithUE(lambda: cbWC.nullResponse(cookie), lambda ex: cbWC.opWithUE(ex, cookie)) cbWC.check() print("ok") sys.stdout.write("testing sent callback... ") sys.stdout.flush() cb = SentCallback() cookie = 5 cbWC = SentCallbackWC(cookie) p.begin_ice_isA("", cb.response, cb.ex, cb.sent) cb.check() p.begin_ice_isA("", lambda b: cbWC.response(b, cookie), lambda ex: cbWC.ex(ex, cookie), lambda ss: cbWC.sent(ss, cookie)) cbWC.check() p.begin_ice_ping(cb.response, cb.ex, cb.sent) cb.check() p.begin_ice_ping(lambda: cbWC.response(cookie), lambda ex: cbWC.ex(ex, cookie), lambda ss: cbWC.sent(ss, cookie)) cbWC.check() p.begin_ice_id(cb.response, cb.ex, cb.sent) cb.check() p.begin_ice_id(lambda id: cbWC.response(id, cookie), lambda ex: cbWC.ex(ex, cookie), lambda ss: cbWC.sent(ss, cookie)) cbWC.check() p.begin_ice_ids(cb.response, cb.ex, cb.sent) cb.check() p.begin_ice_ids(lambda ids: cbWC.response(ids, cookie), lambda ex: cbWC.ex(ex, cookie), lambda ss: cbWC.sent(ss, cookie)) cbWC.check() p.begin_op(cb.response, cb.ex, cb.sent) cb.check() p.begin_op(lambda: cbWC.response(cookie), lambda ex: cbWC.ex(ex, cookie), lambda ss: cbWC.sent(ss, cookie)) cbWC.check() cbs = [] if sys.version_info[0] == 2: b = [chr(random.randint(0, 255)) for x in range(0, 1024)] seq = ''.join(b) else: b = [random.randint(0, 255) for x in range(0, 1024)] seq = bytes(b) testController.holdAdapter() try: cb = SentCallback() while (p.begin_opWithPayload(seq, None, cb.ex, cb.sent).sentSynchronously()): cbs.append(cb) cb = SentCallback() except Exception as ex: testController.resumeAdapter() raise ex testController.resumeAdapter() for r in cbs: r.check() print("ok") sys.stdout.write("testing illegal arguments... ") sys.stdout.flush() result = p.begin_op() p.end_op(result) try: p.end_op(result) test(False) except RuntimeError: pass result = p.begin_op() try: p.end_opWithResult(result) test(False) except RuntimeError: pass print("ok") sys.stdout.write("testing unexpected exceptions from callback... ") sys.stdout.flush() q = Test.TestIntfPrx.uncheckedCast(p.ice_adapterId("dummy")) throwTypes = [LocalException, UserException, OtherException] for t in throwTypes: cb = Thrower(t) cookie = 5 p.begin_op(cb.op, cb.noEx) cb.check() p.begin_op(lambda: cb.opWC(cookie), lambda ex: cb.noExWC(ex, cookie)) cb.check() q.begin_op(cb.op, cb.ex) cb.check() q.begin_op(lambda: cb.opWC(cookie), lambda ex: cb.exWC(ex, cookie)) cb.check() p.begin_op(cb.noOp, cb.ex, cb.sent) cb.check() p.begin_op(lambda: cb.noOpWC(cookie), lambda ex: cb.exWC(ex, cookie), lambda ss: cb.sentWC(ss, cookie)) cb.check() q.begin_op(None, cb.ex) cb.check() q.begin_op(None, lambda ex: cb.exWC(ex, cookie)) cb.check() print("ok") sys.stdout.write("testing batch requests with proxy... ") sys.stdout.flush() cookie = 5 # # Without cookie. # test(p.opBatchCount() == 0) b1 = p.ice_batchOneway() b1.opBatch() b1.opBatch() cb = FlushCallback() r = b1.begin_ice_flushBatchRequests(cb.exception, cb.sent) cb.check() test(r.isSent()) test(r.isCompleted()) test(p.waitForBatch(2)) # # With cookie. # test(p.opBatchCount() == 0) b1 = p.ice_batchOneway() b1.opBatch() b1.opBatch() cb = FlushCallback(cookie) r = b1.begin_ice_flushBatchRequests(lambda ex: cb.exceptionWC(ex, cookie), lambda ss: cb.sentWC(ss, cookie)) cb.check() test(p.waitForBatch(2)) if p.ice_getConnection(): # No collocation optimization # # Exception without cookie. # test(p.opBatchCount() == 0) b1 = p.ice_batchOneway() b1.opBatch() b1.ice_getConnection().close(False) cb = FlushCallback() r = b1.begin_ice_flushBatchRequests(cb.exception, cb.sent) cb.check() test(r.isSent()) test(r.isCompleted()) test(p.waitForBatch(1)) # # Exception with cookie. # test(p.opBatchCount() == 0) b1 = p.ice_batchOneway() b1.opBatch() b1.ice_getConnection().close(False) cb = FlushCallback(cookie) r = b1.begin_ice_flushBatchRequests( lambda ex: cb.exceptionWC(ex, cookie), lambda ss: cb.sentWC(ss, cookie)) cb.check() test(p.waitForBatch(1)) print("ok") if p.ice_getConnection(): # No collocation optimization sys.stdout.write("testing batch requests with connection... ") sys.stdout.flush() cookie = 5 # # Without cookie. # test(p.opBatchCount() == 0) b1 = Test.TestIntfPrx.uncheckedCast(p.ice_getConnection().createProxy( p.ice_getIdentity()).ice_batchOneway()) b1.opBatch() b1.opBatch() cb = FlushCallback() r = b1.ice_getConnection().begin_flushBatchRequests( cb.exception, cb.sent) cb.check() test(r.isSent()) test(r.isCompleted()) test(p.waitForBatch(2)) # # With cookie. # test(p.opBatchCount() == 0) b1 = Test.TestIntfPrx.uncheckedCast(p.ice_getConnection().createProxy( p.ice_getIdentity()).ice_batchOneway()) b1.opBatch() b1.opBatch() cb = FlushCallback(cookie) r = b1.ice_getConnection().begin_flushBatchRequests( lambda ex: cb.exceptionWC(ex, cookie), lambda ss: cb.sentWC(ss, cookie)) cb.check() test(p.waitForBatch(2)) # # Exception without cookie. # test(p.opBatchCount() == 0) b1 = Test.TestIntfPrx.uncheckedCast(p.ice_getConnection().createProxy( p.ice_getIdentity()).ice_batchOneway()) b1.opBatch() b1.ice_getConnection().close(False) cb = FlushExCallback() r = b1.ice_getConnection().begin_flushBatchRequests( cb.exception, cb.sent) cb.check() test(not r.isSent()) test(r.isCompleted()) test(p.opBatchCount() == 0) # # Exception with cookie. # test(p.opBatchCount() == 0) b1 = Test.TestIntfPrx.uncheckedCast(p.ice_getConnection().createProxy( p.ice_getIdentity()).ice_batchOneway()) b1.opBatch() b1.ice_getConnection().close(False) cb = FlushExCallback(cookie) r = b1.ice_getConnection().begin_flushBatchRequests( lambda ex: cb.exceptionWC(ex, cookie), lambda ss: cb.sentWC(ss, cookie)) cb.check() test(p.opBatchCount() == 0) print("ok") sys.stdout.write("testing batch requests with communicator... ") sys.stdout.flush() # # 1 connection. # test(p.opBatchCount() == 0) b1 = Test.TestIntfPrx.uncheckedCast(p.ice_getConnection().createProxy( p.ice_getIdentity()).ice_batchOneway()) b1.opBatch() b1.opBatch() cb = FlushCallback() r = communicator.begin_flushBatchRequests(cb.exception, cb.sent) cb.check() test(r.isSent()) test(r.isCompleted()) test(p.waitForBatch(2)) # # 1 connection. # test(p.opBatchCount() == 0) b1 = Test.TestIntfPrx.uncheckedCast(p.ice_getConnection().createProxy( p.ice_getIdentity()).ice_batchOneway()) b1.opBatch() b1.ice_getConnection().close(False) cb = FlushCallback() r = communicator.begin_flushBatchRequests(cb.exception, cb.sent) cb.check() test(r.isSent()) # Exceptions are ignored! test(r.isCompleted()) test(p.opBatchCount() == 0) # # 2 connections. # test(p.opBatchCount() == 0) b1 = Test.TestIntfPrx.uncheckedCast(p.ice_getConnection().createProxy( p.ice_getIdentity()).ice_batchOneway()) b2 = Test.TestIntfPrx.uncheckedCast( p.ice_connectionId("2").ice_getConnection().createProxy( p.ice_getIdentity()).ice_batchOneway()) b2.ice_getConnection() # Ensure connection is established. b1.opBatch() b1.opBatch() b2.opBatch() b2.opBatch() cb = FlushCallback() r = communicator.begin_flushBatchRequests(cb.exception, cb.sent) cb.check() test(r.isSent()) test(r.isCompleted()) test(p.waitForBatch(4)) # # 2 connections - 1 failure. # # All connections should be flushed even if there are failures on some connections. # Exceptions should not be reported. # test(p.opBatchCount() == 0) b1 = Test.TestIntfPrx.uncheckedCast(p.ice_getConnection().createProxy( p.ice_getIdentity()).ice_batchOneway()) b2 = Test.TestIntfPrx.uncheckedCast( p.ice_connectionId("2").ice_getConnection().createProxy( p.ice_getIdentity()).ice_batchOneway()) b2.ice_getConnection() # Ensure connection is established. b1.opBatch() b2.opBatch() b1.ice_getConnection().close(False) cb = FlushCallback() r = communicator.begin_flushBatchRequests(cb.exception, cb.sent) cb.check() test(r.isSent()) # Exceptions are ignored! test(r.isCompleted()) test(p.waitForBatch(1)) # # 2 connections - 2 failures. # # The sent callback should be invoked even if all connections fail. # test(p.opBatchCount() == 0) b1 = Test.TestIntfPrx.uncheckedCast(p.ice_getConnection().createProxy( p.ice_getIdentity()).ice_batchOneway()) b2 = Test.TestIntfPrx.uncheckedCast( p.ice_connectionId("2").ice_getConnection().createProxy( p.ice_getIdentity()).ice_batchOneway()) b2.ice_getConnection() # Ensure connection is established. b1.opBatch() b2.opBatch() b1.ice_getConnection().close(False) b2.ice_getConnection().close(False) cb = FlushCallback() r = communicator.begin_flushBatchRequests(cb.exception, cb.sent) cb.check() test(r.isSent()) # Exceptions are ignored! test(r.isCompleted()) test(p.opBatchCount() == 0) print("ok") sys.stdout.write("testing AsyncResult operations... ") sys.stdout.flush() indirect = Test.TestIntfPrx.uncheckedCast(p.ice_adapterId("dummy")) r = indirect.begin_op() try: r.waitForCompleted() r.throwLocalException() test(False) except Ice.NoEndpointException: pass testController.holdAdapter() r1 = None r2 = None try: r1 = p.begin_op() if sys.version_info[0] == 2: b = [chr(random.randint(0, 255)) for x in range(0, 1024)] seq = ''.join(b) else: b = [random.randint(0, 255) for x in range(0, 1024)] seq = bytes(b) while (True): r2 = p.begin_opWithPayload(seq) if not r2.sentSynchronously(): break test(r1 == r1) test(r1 != r2) if p.ice_getConnection(): test((r1.sentSynchronously() and r1.isSent() and not r1.isCompleted()) or (not r1.sentSynchronously() and not r1.isCompleted())) test(not r2.sentSynchronously() and not r2.isCompleted()) except Exception as ex: testController.resumeAdapter() raise ex testController.resumeAdapter() r1.waitForSent() test(r1.isSent()) r2.waitForSent() test(r2.isSent()) r1.waitForCompleted() test(r1.isCompleted()) r2.waitForCompleted() test(r2.isCompleted()) test(r1.getOperation() == "op") test(r2.getOperation() == "opWithPayload") # # Twoway # r = p.begin_ice_ping() test(r.getOperation() == "ice_ping") test(r.getConnection() == None) # Expected test(r.getCommunicator() == communicator) test(r.getProxy() == p) p.end_ice_ping(r) # # Oneway # p2 = p.ice_oneway() r = p2.begin_ice_ping() test(r.getOperation() == "ice_ping") test(r.getConnection() == None) # Expected test(r.getCommunicator() == communicator) test(r.getProxy() == p2) # # Batch request via proxy # p2 = p.ice_batchOneway() p2.ice_ping() r = p2.begin_ice_flushBatchRequests() test(r.getConnection() == None) # Expected test(r.getCommunicator() == communicator) test(r.getProxy() == p2) p2.end_ice_flushBatchRequests(r) if p.ice_getConnection(): # # Batch request via connection # con = p.ice_getConnection() p2 = p.ice_batchOneway() p2.ice_ping() r = con.begin_flushBatchRequests() test(r.getConnection() == con) test(r.getCommunicator() == communicator) test(r.getProxy() == None) # Expected con.end_flushBatchRequests(r) # # Batch request via communicator # p2 = p.ice_batchOneway() p2.ice_ping() r = communicator.begin_flushBatchRequests() test(r.getConnection() == None) # Expected test(r.getCommunicator() == communicator) test(r.getProxy() == None) # Expected communicator.end_flushBatchRequests(r) if (p.ice_getConnection()): r1 = None r2 = None if sys.version_info[0] == 2: b = [chr(random.randint(0, 255)) for x in range(0, 10024)] seq = ''.join(b) else: b = [random.randint(0, 255) for x in range(0, 10024)] seq = bytes(b) testController.holdAdapter() for x in range(0, 200): # 2MB r = p.begin_opWithPayload(seq) test(not r.isSent()) r1 = p.begin_ice_ping() r2 = p.begin_ice_id() r1.cancel() r2.cancel() try: p.end_ice_ping(r1) test(false) except (Ice.InvocationCanceledException): pass try: p.end_ice_id(r2) test(false) except (Ice.InvocationCanceledException): pass testController.resumeAdapter() p.ice_ping() test(not r1.isSent() and r1.isCompleted()) test(not r2.isSent() and r2.isCompleted()) testController.holdAdapter() r1 = p.begin_op() r2 = p.begin_ice_id() r1.waitForSent() r2.waitForSent() r1.cancel() r2.cancel() try: p.end_op(r1) test(false) except: pass try: p.end_ice_id(r2) test(false) except: pass testController.resumeAdapter() print("ok") if p.ice_getConnection(): sys.stdout.write("testing close connection with sending queue... ") sys.stdout.flush() if sys.version_info[0] == 2: b = [chr(random.randint(0, 255)) for x in range(0, 10 * 1024)] seq = ''.join(b) else: b = [random.randint(0, 255) for x in range(0, 10 * 1024)] seq = bytes(b) # # Send multiple opWithPayload, followed by a close and followed by multiple opWithPaylod. # The goal is to make sure that none of the opWithPayload fail even if the server closes # the connection gracefully in between. # maxQueue = 2 done = False while not done and maxQueue < 50: done = True p.ice_ping() results = [] for i in range(0, maxQueue): results.append(p.begin_opWithPayload(seq)) if not p.begin_close(False).isSent(): for i in range(0, maxQueue): r = p.begin_opWithPayload(seq) results.append(r) if r.isSent(): done = False maxQueue = maxQueue * 2 break else: maxQueue = maxQueue * 2 done = False for r in results: r.waitForCompleted() try: r.throwLocalException() except Ice.LocalException: test(False) print("ok") p.shutdown()
def __init__(self, args=None, id=None, host=None, port=None, pmap=None): """ Constructor which takes one sys.argv-style list, one initialization data, one host string, one port integer, and one properties map, in that order. *However*, to simplify use, we reassign values based on their type with a warning printed. A cleaner approach is to use named parameters. :: c1 = omero.client(None, None, "host", myPort) # Correct c2 = omero.client(host = "host", port = myPort) # Correct # Works with warning c3 = omero.client("host", myPort) Both "Ice" and "omero" prefixed properties will be parsed. Defines the state variables:: __previous : InitializationData from any previous communicator, if any. Used to re-initialization the client post-closeSession() __ic : communicator. Nullness => init() needed on createSession() __sf : current session. Nullness => createSession() needed. __resources: if non-null, hs access to this client instance and will periodically call sf.keepAlive(None) in order to keep any session alive. This can be enabled either via the omero.keep_alive configuration property, or by calling the enableKeepAlive() method. Once enabled, the period cannot be adjusted during a single session. Modifying these variables outside of the accessors can lead to undefined behavior. Equivalent to all OmeroJava and OmeroCpp constructors """ # Setting all protected values to prevent AttributeError self.__agent = "OMERO.py" #: See setAgent self.__ip = None #: See setIP self.__insecure = False self.__previous = None self.__ic = None self.__oa = None self.__cb = None self.__sf = None self.__uuid = None self.__resources = None self.__lock = threading.RLock() # Logging self.__logger = logging.getLogger("omero.client") logging.basicConfig() # Does nothing if already configured # Reassigning based on argument type args, id, host, port, pmap = self._repair(args, id, host, port, pmap) # Copying args since we don't really want them edited if not args: args = [] else: # See ticket:5516 To prevent issues on systems where the base # class of path.path is unicode, we will encode all unicode # strings here. for idx, arg in enumerate(args): if isinstance(arg, unicode): arg = arg.encode("utf-8") args[idx] = arg # Equiv to multiple constructors. ####################### if id is None: id = Ice.InitializationData() if id.properties is None: id.properties = Ice.createProperties(args) id.properties.parseCommandLineOptions("omero", args) if host: id.properties.setProperty("omero.host", str(host)) if not port: port = id.properties.getPropertyWithDefault( "omero.port", str(omero.constants.GLACIER2PORT)) id.properties.setProperty("omero.port", str(port)) if pmap: for k, v in pmap.items(): id.properties.setProperty(str(k), str(v)) self._initData(id)
print action print p print session print chanid if (session != 0): server.sendMessage(session, "Bouncy") elif (chanid >= 0): server.sendMessageChannel(chanid, 0, "Channel Bouncy") if __name__ == "__main__": global contextR prop = Ice.createProperties(sys.argv) prop.setProperty("Ice.ImplicitContext", "Shared") idd = Ice.InitializationData() idd.properties = prop ice = Ice.initialize(idd) print "Creating callbacks...", # If icesecret is set, we need to set it here as well. ice.getImplicitContext().put("secret", "fourtytwo") meta = Murmur.MetaPrx.checkedCast(ice.stringToProxy('Meta:tcp -h 127.0.0.1 -p 6502')) adapter = ice.createObjectAdapterWithEndpoints("Callback.Client", "tcp -h 127.0.0.1") metaR=Murmur.MetaCallbackPrx.uncheckedCast(adapter.addWithUUID(MetaCallbackI())) adapter.activate()
def do_main_program(): # # --- Authenticator implementation # All of this has to go in here so we can correctly daemonize the tool # without loosing the file descriptors opened by the Ice module slicedir = Ice.getSliceDir() if not slicedir: slicedir = ["-I/usr/share/Ice/slice", "-I/usr/share/slice"] else: slicedir = ['-I' + slicedir] Ice.loadSlice('', slicedir + [cfg.ice.slice]) import Murmur class allianceauthauthenticatorApp(Ice.Application): def run(self, args): self.shutdownOnInterrupt() if not self.initializeIceConnection(): return 1 if cfg.ice.watchdog > 0: self.failedWatch = True self.checkConnection() # Serve till we are stopped self.communicator().waitForShutdown() self.watchdog.cancel() if self.interrupted(): warning('Caught interrupt, shutting down') threadDB.disconnect() return 0 def initializeIceConnection(self): """ Establishes the two-way Ice connection and adds the authenticator to the configured servers """ ice = self.communicator() if cfg.ice.secret: debug('Using shared ice secret') ice.getImplicitContext().put("secret", cfg.ice.secret) elif not cfg.glacier.enabled: warning('Consider using an ice secret to improve security') if cfg.glacier.enabled: # info('Connecting to Glacier2 server (%s:%d)', glacier_host, glacier_port) error('Glacier support not implemented yet') # TODO: Implement this info('Connecting to Ice server (%s:%d)', cfg.ice.host, cfg.ice.port) base = ice.stringToProxy('Meta:tcp -h %s -p %d' % (cfg.ice.host, cfg.ice.port)) self.meta = Murmur.MetaPrx.uncheckedCast(base) adapter = ice.createObjectAdapterWithEndpoints('Callback.Client', 'tcp -h %s' % cfg.ice.host) adapter.activate() metacbprx = adapter.addWithUUID(metaCallback(self)) self.metacb = Murmur.MetaCallbackPrx.uncheckedCast(metacbprx) authprx = adapter.addWithUUID(allianceauthauthenticator()) self.auth = Murmur.ServerUpdatingAuthenticatorPrx.uncheckedCast(authprx) return self.attachCallbacks() def attachCallbacks(self, quiet=False): """ Attaches all callbacks for meta and authenticators """ # Ice.ConnectionRefusedException # debug('Attaching callbacks') try: if not quiet: info('Attaching meta callback') self.meta.addCallback(self.metacb) for server in self.meta.getBootedServers(): if not cfg.murmur.servers or server.id() in cfg.murmur.servers: if not quiet: info('Setting authenticator for virtual server %d', server.id()) server.setAuthenticator(self.auth) except (Murmur.InvalidSecretException, Ice.UnknownUserException, Ice.ConnectionRefusedException) as e: if isinstance(e, Ice.ConnectionRefusedException): error('Server refused connection') elif isinstance(e, Murmur.InvalidSecretException) or \ isinstance(e, Ice.UnknownUserException) and ( e.unknown == 'Murmur::InvalidSecretException'): error('Invalid ice secret') else: # We do not actually want to handle this one, re-raise it raise e self.connected = False return False self.connected = True return True def checkConnection(self): """ Tries reapplies all callbacks to make sure the authenticator survives server restarts and disconnects. """ # debug('Watchdog run') try: if not self.attachCallbacks(quiet=not self.failedWatch): self.failedWatch = True else: self.failedWatch = False except Ice.Exception as e: error('Failed connection check, will retry in next watchdog run (%ds)', cfg.ice.watchdog) debug(str(e)) self.failedWatch = True # Renew the timer self.watchdog = Timer(cfg.ice.watchdog, self.checkConnection) self.watchdog.start() def checkSecret(func): """ Decorator that checks whether the server transmitted the right secret if a secret is supposed to be used. """ if not cfg.ice.secret: return func def newfunc(*args, **kws): if 'current' in kws: current = kws["current"] else: current = args[-1] if not current or 'secret' not in current.ctx or current.ctx['secret'] != cfg.ice.secret: error('Server transmitted invalid secret. Possible injection attempt.') raise Murmur.InvalidSecretException() return func(*args, **kws) return newfunc def fortifyIceFu(retval=None, exceptions=(Ice.Exception,)): """ Decorator that catches exceptions,logs them and returns a safe retval value. This helps preventing the authenticator getting stuck in critical code paths. Only exceptions that are instances of classes given in the exceptions list are not caught. The default is to catch all non-Ice exceptions. """ def newdec(func): def newfunc(*args, **kws): try: return func(*args, **kws) except Exception as e: catch = True for ex in exceptions: if isinstance(e, ex): catch = False break if catch: critical('Unexpected exception caught') exception(e) return retval raise return newfunc return newdec class metaCallback(Murmur.MetaCallback): def __init__(self, app): Murmur.MetaCallback.__init__(self) self.app = app @fortifyIceFu() @checkSecret def started(self, server, current=None): """ This function is called when a virtual server is started and makes sure an authenticator gets attached if needed. """ if not cfg.murmur.servers or server.id() in cfg.murmur.servers: info('Setting authenticator for virtual server %d', server.id()) try: server.setAuthenticator(app.auth) # Apparently this server was restarted without us noticing except (Murmur.InvalidSecretException, Ice.UnknownUserException) as e: if hasattr(e, "unknown") and e.unknown != "Murmur::InvalidSecretException": # Special handling for Murmur 1.2.2 servers with invalid slice files raise e error('Invalid ice secret') return else: debug('Virtual server %d got started', server.id()) @fortifyIceFu() @checkSecret def stopped(self, server, current=None): """ This function is called when a virtual server is stopped """ if self.app.connected: # Only try to output the server id if we think we are still connected to prevent # flooding of our thread pool try: if not cfg.murmur.servers or server.id() in cfg.murmur.servers: info('Authenticated virtual server %d got stopped', server.id()) else: debug('Virtual server %d got stopped', server.id()) return except Ice.ConnectionRefusedException: self.app.connected = False debug('Server shutdown stopped a virtual server') if cfg.user.reject_on_error: # Python 2.4 compat authenticateFortifyResult = (-1, None, None) else: authenticateFortifyResult = (-2, None, None) class allianceauthauthenticator(Murmur.ServerUpdatingAuthenticator): texture_cache = {} def __init__(self): Murmur.ServerUpdatingAuthenticator.__init__(self) @fortifyIceFu(authenticateFortifyResult) @checkSecret def authenticate(self, name, pw, certlist, certhash, strong, current=None): """ This function is called to authenticate a user """ # Search for the user in the database FALL_THROUGH = -2 AUTH_REFUSED = -1 if name == 'SuperUser': debug('Forced fall through for SuperUser') return (FALL_THROUGH, None, None) try: sql = 'SELECT user_id, pwhash, groups, hashfn ' \ 'FROM %smumble_mumbleuser ' \ 'WHERE username = %%s' % cfg.database.prefix cur = threadDB.execute(sql, [name]) except threadDbException: return (FALL_THROUGH, None, None) res = cur.fetchone() cur.close() if not res: info('Fall through for unknown user "%s"', name) return (FALL_THROUGH, None, None) uid, upwhash, ugroups, uhashfn = res if ugroups: groups = ugroups.split(',') else: groups = [] debug('checking password with hash function: %s' % uhashfn) if allianceauth_check_hash(pw, upwhash, uhashfn): info('User authenticated: "%s" (%d)', name, uid + cfg.user.id_offset) debug('Group memberships: %s', str(groups)) return (uid + cfg.user.id_offset, entity_decode(name), groups) info('Failed authentication attempt for user: "******" (%d)', name, uid + cfg.user.id_offset) return (AUTH_REFUSED, None, None) @fortifyIceFu((False, None)) @checkSecret def getInfo(self, id, current=None): """ Gets called to fetch user specific information """ # We do not expose any additional information so always fall through debug('getInfo for %d -> denied', id) return (False, None) @fortifyIceFu(-2) @checkSecret def nameToId(self, name, current=None): """ Gets called to get the id for a given username """ FALL_THROUGH = -2 if name == 'SuperUser': debug('nameToId SuperUser -> forced fall through') return FALL_THROUGH try: sql = 'SELECT user_id FROM %smumble_mumbleuser WHERE username = %%s' % cfg.database.prefix cur = threadDB.execute(sql, [name]) except threadDbException: return FALL_THROUGH res = cur.fetchone() cur.close() if not res: debug('nameToId %s -> ?', name) return FALL_THROUGH debug('nameToId %s -> %d', name, (res[0] + cfg.user.id_offset)) return res[0] + cfg.user.id_offset @fortifyIceFu("") @checkSecret def idToName(self, id, current=None): """ Gets called to get the username for a given id """ FALL_THROUGH = "" # Make sure the ID is in our range and transform it to the actual smf user id if id < cfg.user.id_offset: return FALL_THROUGH bbid = id - cfg.user.id_offset # Fetch the user from the database try: sql = 'SELECT username FROM %smumble_mumbleuser WHERE user_id = %%s' % cfg.database.prefix cur = threadDB.execute(sql, [bbid]) except threadDbException: return FALL_THROUGH res = cur.fetchone() cur.close() if res: if res[0] == 'SuperUser': debug('idToName %d -> "SuperUser" catched') return FALL_THROUGH debug('idToName %d -> "%s"', id, res[0]) return res[0] debug('idToName %d -> ?', id) return FALL_THROUGH @fortifyIceFu("") @checkSecret def idToTexture(self, id, current=None): """ Gets called to get the corresponding texture for a user """ FALL_THROUGH = "" debug('idToTexture "%s" -> fall through', id) return FALL_THROUGH @fortifyIceFu(-2) @checkSecret def registerUser(self, name, current=None): """ Gets called when the server is asked to register a user. """ FALL_THROUGH = -2 debug('registerUser "%s" -> fall through', name) return FALL_THROUGH @fortifyIceFu(-1) @checkSecret def unregisterUser(self, id, current=None): """ Gets called when the server is asked to unregister a user. """ FALL_THROUGH = -1 # Return -1 to fall through to internal server database, we will not modify the smf database # but we can make murmur delete all additional information it got this way. debug('unregisterUser %d -> fall through', id) return FALL_THROUGH @fortifyIceFu({}) @checkSecret def getRegisteredUsers(self, filter, current=None): """ Returns a list of usernames in the AllianceAuth database which contain filter as a substring. """ if not filter: filter = '%' try: sql = 'SELECT user_id, username FROM %smumble_mumbleuser WHERE username LIKE %%s' % cfg.database.prefix cur = threadDB.execute(sql, [filter]) except threadDbException: return {} res = cur.fetchall() cur.close() if not res: debug('getRegisteredUsers -> empty list for filter "%s"', filter) return {} debug('getRegisteredUsers -> %d results for filter "%s"', len(res), filter) return dict([(a + cfg.user.id_offset, b) for a, b in res]) @fortifyIceFu(-1) @checkSecret def setInfo(self, id, info, current=None): """ Gets called when the server is supposed to save additional information about a user to his database """ FALL_THROUGH = -1 # Return -1 to fall through to the internal server handler. We must not modify # the smf database so the additional information is stored in murmurs database debug('setInfo %d -> fall through', id) return FALL_THROUGH @fortifyIceFu(-1) @checkSecret def setTexture(self, id, texture, current=None): """ Gets called when the server is asked to update the user texture of a user """ FALL_THROUGH = -1 debug('setTexture %d -> fall through', id) return FALL_THROUGH class CustomLogger(Ice.Logger): """ Logger implementation to pipe Ice log messages into our own log """ def __init__(self): Ice.Logger.__init__(self) self._log = getLogger('Ice') def _print(self, message): self._log.info(message) def trace(self, category, message): self._log.debug('Trace %s: %s', category, message) def warning(self, message): self._log.warning(message) def error(self, message): self._log.error(message) # # --- Start of authenticator # info('Starting AllianceAuth mumble authenticator') initdata = Ice.InitializationData() initdata.properties = Ice.createProperties([], initdata.properties) for prop, val in cfg.iceraw: initdata.properties.setProperty(prop, val) initdata.properties.setProperty('Ice.ImplicitContext', 'Shared') initdata.properties.setProperty('Ice.Default.EncodingVersion', '1.0') initdata.logger = CustomLogger() app = allianceauthauthenticatorApp() state = app.main(sys.argv[:1], initData=initdata) info('Shutdown complete')
oldIc = self.__ic self.__ic = None # Only possible if improperly configured. if not oldIc: return if oldOa: try: oldOa.deactivate() except Exception, e: self.__logger.warning("While deactivating adapter: " + str(e.message)) self.__previous = Ice.InitializationData() self.__previous.properties = oldIc.getProperties().clone() try: try: self.getRouter(oldIc).destroySession() except Glacier2.SessionNotExistException: # ok. We don't want it to exist pass except Ice.ConnectionLostException: # ok. Exception will always be thrown pass except Ice.ConnectionRefusedException: # ok. Server probably went down pass except Ice.ConnectTimeoutException:
def allTests(helper, communicator): sys.stdout.write("testing communicator operations... ") sys.stdout.flush() # # Test: Exercise addAdminFacet, findAdminFacet, removeAdminFacet with a typical configuration. # init = Ice.InitializationData() init.properties = Ice.createProperties() init.properties.setProperty("Ice.Admin.Endpoints", "tcp -h 127.0.0.1") init.properties.setProperty("Ice.Admin.InstanceName", "Test") init.properties.setProperty("Ice.ProgramName", "MyTestProgram") com = Ice.initialize(init) testFacets(com) test(com.getLogger().getPrefix() == "MyTestProgram") com.destroy() # # Test: Verify that the operations work correctly in the presence of facet filters. # init = Ice.InitializationData() init.properties = Ice.createProperties() init.properties.setProperty("Ice.Admin.Endpoints", "tcp -h 127.0.0.1") init.properties.setProperty("Ice.Admin.InstanceName", "Test") init.properties.setProperty("Ice.Admin.Facets", "Properties") com = Ice.initialize(init) testFacets(com, False) com.destroy() # # Test: Verify that the operations work correctly with the Admin object disabled. # com = Ice.initialize() testFacets(com, False) com.destroy() # # Test: Verify that the operations work correctly when Ice.Admin is enabled. # init = Ice.InitializationData() init.properties = Ice.createProperties() init.properties.setProperty("Ice.Admin.Enabled", "1") com = Ice.initialize(init) test(com.getAdmin() == None) identity = Ice.stringToIdentity("test-admin") try: com.createAdmin(None, identity) test(False) except Ice.InitializationException: pass adapter = com.createObjectAdapter("") test(com.createAdmin(adapter, identity) != None) test(com.getAdmin() != None) testFacets(com) com.destroy() # # Test: Verify that the operations work correctly when creation of the Admin object is delayed. # init = Ice.InitializationData() init.properties = Ice.createProperties() init.properties.setProperty("Ice.Admin.Endpoints", "tcp -h 127.0.0.1") init.properties.setProperty("Ice.Admin.InstanceName", "Test") init.properties.setProperty("Ice.Admin.DelayCreation", "1") com = Ice.initialize(init) testFacets(com) com.getAdmin() testFacets(com) com.destroy() print("ok") ref = "factory:{0} -t 10000".format(helper.getTestEndpoint()) factory = Test.RemoteCommunicatorFactoryPrx.uncheckedCast( communicator.stringToProxy(ref)) sys.stdout.write("testing process facet... ") sys.stdout.flush() # # Test: Verify that Process::shutdown() operation shuts down the communicator. # props = {} props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1" props["Ice.Admin.InstanceName"] = "Test" com = factory.createCommunicator(props) obj = com.getAdmin() proc = Ice.ProcessPrx.checkedCast(obj, "Process") proc.shutdown() com.waitForShutdown() com.destroy() print("ok") sys.stdout.write("testing properties facet... ") sys.stdout.flush() props = {} props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1" props["Ice.Admin.InstanceName"] = "Test" props["Prop1"] = "1" props["Prop2"] = "2" props["Prop3"] = "3" com = factory.createCommunicator(props) obj = com.getAdmin() pa = Ice.PropertiesAdminPrx.checkedCast(obj, "Properties") # # Test: PropertiesAdmin::getProperty() # test(pa.getProperty("Prop2") == "2") test(pa.getProperty("Bogus") == "") # # Test: PropertiesAdmin::getProperties() # pd = pa.getPropertiesForPrefix("") test(len(pd) == 5) test(pd["Ice.Admin.Endpoints"] == "tcp -h 127.0.0.1") test(pd["Ice.Admin.InstanceName"] == "Test") test(pd["Prop1"] == "1") test(pd["Prop2"] == "2") test(pd["Prop3"] == "3") changes = {} # # Test: PropertiesAdmin::setProperties() # setProps = {} setProps["Prop1"] = "10" # Changed setProps["Prop2"] = "20" # Changed setProps["Prop3"] = "" # Removed setProps["Prop4"] = "4" # Added setProps["Prop5"] = "5" # Added pa.setProperties(setProps) test(pa.getProperty("Prop1") == "10") test(pa.getProperty("Prop2") == "20") test(pa.getProperty("Prop3") == "") test(pa.getProperty("Prop4") == "4") test(pa.getProperty("Prop5") == "5") changes = com.getChanges() test(len(changes) == 5) test(changes["Prop1"] == "10") test(changes["Prop2"] == "20") test(changes["Prop3"] == "") test(changes["Prop4"] == "4") test(changes["Prop5"] == "5") pa.setProperties(setProps) changes = com.getChanges() test(len(changes) == 0) com.destroy() print("ok") sys.stdout.write("testing custom facet... ") sys.stdout.flush() # # Test: Verify that the custom facet is present. # props = {} props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1" props["Ice.Admin.InstanceName"] = "Test" com = factory.createCommunicator(props) obj = com.getAdmin() tf = Test.TestFacetPrx.checkedCast(obj, "TestFacet") tf.op() com.destroy() print("ok") sys.stdout.write("testing facet filtering... ") sys.stdout.flush() # # Test: Set Ice.Admin.Facets to expose only the Properties facet, # meaning no other facet is available. # props = {} props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1" props["Ice.Admin.InstanceName"] = "Test" props["Ice.Admin.Facets"] = "Properties" com = factory.createCommunicator(props) obj = com.getAdmin() proc = Ice.ProcessPrx.checkedCast(obj, "Process") test(proc == None) tf = Test.TestFacetPrx.checkedCast(obj, "TestFacet") test(tf == None) com.destroy() # # Test: Set Ice.Admin.Facets to expose only the Process facet, # meaning no other facet is available. # props = {} props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1" props["Ice.Admin.InstanceName"] = "Test" props["Ice.Admin.Facets"] = "Process" com = factory.createCommunicator(props) obj = com.getAdmin() pa = Ice.PropertiesAdminPrx.checkedCast(obj, "Properties") test(pa == None) tf = Test.TestFacetPrx.checkedCast(obj, "TestFacet") test(tf == None) com.destroy() # # Test: Set Ice.Admin.Facets to expose only the TestFacet facet, # meaning no other facet is available. # props = {} props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1" props["Ice.Admin.InstanceName"] = "Test" props["Ice.Admin.Facets"] = "TestFacet" com = factory.createCommunicator(props) obj = com.getAdmin() pa = Ice.PropertiesAdminPrx.checkedCast(obj, "Properties") test(pa == None) proc = Ice.ProcessPrx.checkedCast(obj, "Process") test(proc == None) com.destroy() # # Test: Set Ice.Admin.Facets to expose two facets. Use whitespace to separate the # facet names. # props = {} props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1" props["Ice.Admin.InstanceName"] = "Test" props["Ice.Admin.Facets"] = "Properties TestFacet" com = factory.createCommunicator(props) obj = com.getAdmin() pa = Ice.PropertiesAdminPrx.checkedCast(obj, "Properties") test(pa.getProperty("Ice.Admin.InstanceName") == "Test") tf = Test.TestFacetPrx.checkedCast(obj, "TestFacet") tf.op() proc = Ice.ProcessPrx.checkedCast(obj, "Process") test(proc == None) com.destroy() # # Test: Set Ice.Admin.Facets to expose two facets. Use a comma to separate the # facet names. # props = {} props["Ice.Admin.Endpoints"] = "tcp -h 127.0.0.1" props["Ice.Admin.InstanceName"] = "Test" props["Ice.Admin.Facets"] = "TestFacet, Process" com = factory.createCommunicator(props) obj = com.getAdmin() pa = Ice.PropertiesAdminPrx.checkedCast(obj, "Properties") test(pa == None) tf = Test.TestFacetPrx.checkedCast(obj, "TestFacet") tf.op() proc = Ice.ProcessPrx.checkedCast(obj, "Process") proc.shutdown() com.waitForShutdown() com.destroy() print("ok") factory.shutdown()
def twoways(helper, p): communicator = helper.communicator() literals = p.opStringLiterals() test(Test.s0 == "\\") test(Test.s0 == Test.sw0) test(Test.s0 == literals[0]) test(Test.s0 == literals[11]) test(Test.s1 == "A") test(Test.s1 == Test.sw1) test(Test.s1 == literals[1]) test(Test.s1 == literals[12]) test(Test.s2 == "Ice") test(Test.s2 == Test.sw2) test(Test.s2 == literals[2]) test(Test.s2 == literals[13]) test(Test.s3 == "A21") test(Test.s3 == Test.sw3) test(Test.s3 == literals[3]) test(Test.s3 == literals[14]) test(Test.s4 == "\\u0041 \\U00000041") test(Test.s4 == Test.sw4) test(Test.s4 == literals[4]) test(Test.s4 == literals[15]) test(Test.s5 == "\xc3\xbf" if version_info[0] < 3 else b"\xc3\xbf".decode("utf-8")) test(Test.s5 == Test.sw5) test(Test.s5 == literals[5]) test(Test.s5 == literals[16]) test(Test.s6 == "\xcf\xbf" if version_info[0] < 3 else b"\xcf\xbf".decode("utf-8")) test(Test.s6 == Test.sw6) test(Test.s6 == literals[6]) test(Test.s6 == literals[17]) test(Test.s7 == "\xd7\xb0" if version_info[0] < 3 else b"\xd7\xb0".decode("utf-8")) test(Test.s7 == Test.sw7) test(Test.s7 == literals[7]) test(Test.s7 == literals[18]) test(Test.s8 == "\xf0\x90\x80\x80" if version_info[0] < 3 else b"\xf0\x90\x80\x80".decode("utf-8")) test(Test.s8 == Test.sw8) test(Test.s8 == literals[8]) test(Test.s8 == literals[19]) test(Test.s9 == "\xf0\x9f\x8d\x8c" if version_info[0] < 3 else b"\xf0\x9f\x8d\x8c".decode("utf-8")) test(Test.s9 == Test.sw9) test(Test.s9 == literals[9]) test(Test.s9 == literals[20]) test(Test.s10 == "\xe0\xb6\xa7" if version_info[0] < 3 else b"\xe0\xb6\xa7".decode("utf-8")) test(Test.s10 == Test.sw10) test(Test.s10 == literals[10]) test(Test.s10 == literals[21]) test(Test.ss0 == "\'\"\x3f\\\a\b\f\n\r\t\v\x06") test(Test.ss0 == Test.ss1) test(Test.ss0 == Test.ss2) test(Test.ss0 == literals[22]) test(Test.ss0 == literals[23]) test(Test.ss0 == literals[24]) test(Test.ss3 == "\\\\U\\u\\") test(Test.ss3 == literals[25]) test(Test.ss4 == "\\A\\") test(Test.ss4 == literals[26]) test(Test.ss5 == "\\u0041\\") test(Test.ss5 == literals[27]) test(Test.su0 == Test.su1) test(Test.su0 == Test.su2) test(Test.su0 == literals[28]) test(Test.su0 == literals[29]) test(Test.su0 == literals[30]) # # ice_ping # p.ice_ping() # # ice_isA # test(p.ice_isA(Test.MyClass.ice_staticId())) # # ice_ids # ids = p.ice_ids() test(len(ids) == 3) test(ids[0] == "::Ice::Object") test(ids[1] == "::Test::MyClass") test(ids[2] == "::Test::MyDerivedClass") # # ice_id # test(p.ice_id() == Test.MyDerivedClass.ice_staticId()) # # Prx ice_staticId # test(Test.MyClassPrx.ice_staticId() == Test.MyClass.ice_staticId()) test(Test.MyDerivedClassPrx.ice_staticId() == Test.MyDerivedClass.ice_staticId()) test(Ice.ObjectPrx.ice_staticId() == Ice.Object.ice_staticId()) # # opVoid # p.opVoid() # # opByte # r, b = p.opByte(0xff, 0x0f) test(b == 0xf0) test(r == 0xff) # # opBool # r, b = p.opBool(True, False) test(b) test(not r) # # opShortIntLong # r, s, i, l = p.opShortIntLong(10, 11, 12) test(s == 10) test(i == 11) test(l == 12) test(r == 12) r, s, i, l = p.opShortIntLong(-32768, -2147483648, -9223372036854775808) test(s == -32768) test(i == -2147483648) test(l == -9223372036854775808) test(r == -9223372036854775808) r, s, i, l = p.opShortIntLong(32767, 2147483647, 9223372036854775807) test(s == 32767) test(i == 2147483647) test(l == 9223372036854775807) test(r == 9223372036854775807) # # opFloatDouble # r, f, d = p.opFloatDouble(3.14, 1.1E10) test(f - 3.14 < 0.001) test(d == 1.1E10) test(r == 1.1E10) # # Test invalid ranges for numbers # try: r, b = p.opByte(0x01ff, 0x01ff) test(False) except ValueError: pass try: r, s, i, l = p.opShortIntLong(32767 + 1, 0, 0) test(False) except ValueError: pass try: r, s, i, l = p.opShortIntLong(-32768 - 1, 0, 0) test(False) except ValueError: pass try: r, s, i, l = p.opShortIntLong(0, 2147483647 + 1, 0) test(False) except ValueError: pass try: r, s, i, l = p.opShortIntLong(0, -2147483648 - 1, 0) test(False) except ValueError: pass try: r, s, i, l = p.opShortIntLong(0, 0, 9223372036854775807 + 1) test(False) except ValueError: pass try: r, s, i, l = p.opShortIntLong(0, 0, -9223372036854775808 - 1) test(False) except ValueError: pass r, f, d = p.opFloatDouble(3.402823466E38, 0.0) r, f, d = p.opFloatDouble(-3.402823466E38, 0.0) for val in ('inf', '-inf'): r, f, d = p.opFloatDouble(float(val), float(val)) test(math.isinf(r) and math.isinf(f) and math.isinf(d)) for val in ('nan', '-nan'): r, f, d = p.opFloatDouble(float(val), float(val)) test(math.isnan(r) and math.isnan(f) and math.isnan(d)) try: r, f, d = p.opFloatDouble(3.402823466E38 * 2, 0.0) test(False) except ValueError: pass try: r, f, d = p.opFloatDouble(-3.402823466E38 * 2, 0.0) test(False) except ValueError: pass # # opString # r, s = p.opString("hello", "world") test(s == "world hello") test(r == "hello world") if sys.version_info[0] == 2: r, s = p.opString(unicode("hello"), unicode("world")) test(s == "world hello") test(r == "hello world") # # opMyEnum # r, e = p.opMyEnum(Test.MyEnum.enum2) test(e == Test.MyEnum.enum2) test(r == Test.MyEnum.enum3) # # opMyClass # r, c1, c2 = p.opMyClass(p) test(Ice.proxyIdentityAndFacetEqual(c1, p)) test(not Ice.proxyIdentityAndFacetEqual(c2, p)) test(Ice.proxyIdentityAndFacetEqual(r, p)) test(c1.ice_getIdentity() == Ice.stringToIdentity("test")) test(c2.ice_getIdentity() == Ice.stringToIdentity("noSuchIdentity")) test(r.ice_getIdentity() == Ice.stringToIdentity("test")) r.opVoid() c1.opVoid() try: c2.opVoid() test(False) except Ice.ObjectNotExistException: pass r, c1, c2 = p.opMyClass(None) test(not c1) test(c2) test(Ice.proxyIdentityAndFacetEqual(r, p)) r.opVoid() # # opStruct # si1 = Test.Structure() si1.p = p si1.e = Test.MyEnum.enum3 si1.s = Test.AnotherStruct() si1.s.s = "abc" si2 = Test.Structure() si2.p = None si2.e = Test.MyEnum.enum2 si2.s = Test.AnotherStruct() si2.s.s = "def" rso, so = p.opStruct(si1, si2) test(not rso.p) test(rso.e == Test.MyEnum.enum2) test(rso.s.s == "def") test(so.p == p) test(so.e == Test.MyEnum.enum3) test(so.s.s == "a new string") so.p.opVoid() # Test marshalling of null structs and structs with null members. si1 = Test.Structure() si2 = None rso, so = p.opStruct(si1, si2) test(rso.p is None) test(rso.e == Test.MyEnum.enum1) test(rso.s.s == "") test(so.p is None) test(so.e == Test.MyEnum.enum1) test(so.s.s == "a new string") # # opByteS # bsi1 = (0x01, 0x11, 0x12, 0x22) bsi2 = (0xf1, 0xf2, 0xf3, 0xf4) rso, bso = p.opByteS(bsi1, bsi2) test(len(bso) == 4) test(len(rso) == 8) if sys.version_info[0] == 2: test(bso[0] == '\x22') test(bso[1] == '\x12') test(bso[2] == '\x11') test(bso[3] == '\x01') test(rso[0] == '\x01') test(rso[1] == '\x11') test(rso[2] == '\x12') test(rso[3] == '\x22') test(rso[4] == '\xf1') test(rso[5] == '\xf2') test(rso[6] == '\xf3') test(rso[7] == '\xf4') else: test(bso[0] == 0x22) test(bso[1] == 0x12) test(bso[2] == 0x11) test(bso[3] == 0x01) test(rso[0] == 0x01) test(rso[1] == 0x11) test(rso[2] == 0x12) test(rso[3] == 0x22) test(rso[4] == 0xf1) test(rso[5] == 0xf2) test(rso[6] == 0xf3) test(rso[7] == 0xf4) # # opByteS (array) # bsi1 = array.array('B') bsi1.fromlist([0x01, 0x11, 0x12, 0x22]) bsi2 = array.array('B') bsi2.fromlist([0xf1, 0xf2, 0xf3, 0xf4]) rso, bso = p.opByteS(bsi1, bsi2) test(len(bso) == 4) test(len(rso) == 8) if sys.version_info[0] == 2: test(bso[0] == '\x22') test(bso[1] == '\x12') test(bso[2] == '\x11') test(bso[3] == '\x01') test(rso[0] == '\x01') test(rso[1] == '\x11') test(rso[2] == '\x12') test(rso[3] == '\x22') test(rso[4] == '\xf1') test(rso[5] == '\xf2') test(rso[6] == '\xf3') test(rso[7] == '\xf4') else: test(bso[0] == 0x22) test(bso[1] == 0x12) test(bso[2] == 0x11) test(bso[3] == 0x01) test(rso[0] == 0x01) test(rso[1] == 0x11) test(rso[2] == 0x12) test(rso[3] == 0x22) test(rso[4] == 0xf1) test(rso[5] == 0xf2) test(rso[6] == 0xf3) test(rso[7] == 0xf4) # # opBoolS # bsi1 = (True, True, False) bsi2 = (False, ) rso, bso = p.opBoolS(bsi1, bsi2) test(len(bso) == 4) test(bso[0]) test(bso[1]) test(not bso[2]) test(not bso[3]) test(len(rso) == 3) test(not rso[0]) test(rso[1]) test(rso[2]) # # opBoolS (array) # bsi1 = array.array('B') bsi1.fromlist([1, 1, 0]) bsi2 = array.array('B') bsi2.fromlist([0]) rso, bso = p.opBoolS(bsi1, bsi2) test(len(bso) == 4) test(bso[0]) test(bso[1]) test(not bso[2]) test(not bso[3]) test(len(rso) == 3) test(not rso[0]) test(rso[1]) test(rso[2]) # # opShortIntLongS # ssi = (1, 2, 3) isi = (5, 6, 7, 8) lsi = (10, 30, 20) rso, sso, iso, lso = p.opShortIntLongS(ssi, isi, lsi) test(len(sso) == 3) test(sso[0] == 1) test(sso[1] == 2) test(sso[2] == 3) test(len(iso) == 4) test(iso[0] == 8) test(iso[1] == 7) test(iso[2] == 6) test(iso[3] == 5) test(len(lso) == 6) test(lso[0] == 10) test(lso[1] == 30) test(lso[2] == 20) test(lso[3] == 10) test(lso[4] == 30) test(lso[5] == 20) test(len(rso) == 3) test(rso[0] == 10) test(rso[1] == 30) test(rso[2] == 20) # # opShortIntLongS (array) # ssi = array.array('h') ssi.fromlist([1, 2, 3]) isi = array.array('i') isi.fromlist([5, 6, 7, 8]) lsi = (10, 30, 20) # Can't store Ice::Long in an array. rso, sso, iso, lso = p.opShortIntLongS(ssi, isi, lsi) test(len(sso) == 3) test(sso[0] == 1) test(sso[1] == 2) test(sso[2] == 3) test(len(iso) == 4) test(iso[0] == 8) test(iso[1] == 7) test(iso[2] == 6) test(iso[3] == 5) test(len(lso) == 6) test(lso[0] == 10) test(lso[1] == 30) test(lso[2] == 20) test(lso[3] == 10) test(lso[4] == 30) test(lso[5] == 20) test(len(rso) == 3) test(rso[0] == 10) test(rso[1] == 30) test(rso[2] == 20) # # opFloatDoubleS # fsi = (3.14, 1.11) dsi = (1.1E10, 1.2E10, 1.3E10) rso, fso, dso = p.opFloatDoubleS(fsi, dsi) test(len(fso) == 2) test(fso[0] - 3.14 < 0.001) test(fso[1] - 1.11 < 0.001) test(len(dso) == 3) test(dso[0] == 1.3E10) test(dso[1] == 1.2E10) test(dso[2] == 1.1E10) test(len(rso) == 5) test(rso[0] == 1.1E10) test(rso[1] == 1.2E10) test(rso[2] == 1.3E10) test(rso[3] - 3.14 < 0.001) test(rso[4] - 1.11 < 0.001) # # opFloatDoubleS (array) # fsi = array.array('f') fsi.fromlist([3.14, 1.11]) dsi = array.array('d') dsi.fromlist([1.1E10, 1.2E10, 1.3E10]) rso, fso, dso = p.opFloatDoubleS(fsi, dsi) test(len(fso) == 2) test(fso[0] - 3.14 < 0.001) test(fso[1] - 1.11 < 0.001) test(len(dso) == 3) test(dso[0] == 1.3E10) test(dso[1] == 1.2E10) test(dso[2] == 1.1E10) test(len(rso) == 5) test(rso[0] == 1.1E10) test(rso[1] == 1.2E10) test(rso[2] == 1.3E10) test(rso[3] - 3.14 < 0.001) test(rso[4] - 1.11 < 0.001) # # opStringS # ssi1 = ('abc', 'de', 'fghi') ssi2 = ('xyz', ) rso, sso = p.opStringS(ssi1, ssi2) test(len(sso) == 4) test(sso[0] == "abc") test(sso[1] == "de") test(sso[2] == "fghi") test(sso[3] == "xyz") test(len(rso) == 3) test(rso[0] == "fghi") test(rso[1] == "de") test(rso[2] == "abc") # # opByteSS # bsi1 = ((0x01, 0x11, 0x12), (0xff, )) bsi2 = ((0x0e, ), (0xf2, 0xf1)) rso, bso = p.opByteSS(bsi1, bsi2) test(len(bso) == 2) test(len(bso[0]) == 1) test(len(bso[1]) == 3) test(len(rso) == 4) test(len(rso[0]) == 3) test(len(rso[1]) == 1) test(len(rso[2]) == 1) test(len(rso[3]) == 2) if sys.version_info[0] == 2: test(bso[0][0] == '\xff') test(bso[1][0] == '\x01') test(bso[1][1] == '\x11') test(bso[1][2] == '\x12') test(rso[0][0] == '\x01') test(rso[0][1] == '\x11') test(rso[0][2] == '\x12') test(rso[1][0] == '\xff') test(rso[2][0] == '\x0e') test(rso[3][0] == '\xf2') test(rso[3][1] == '\xf1') else: test(bso[0][0] == 0xff) test(bso[1][0] == 0x01) test(bso[1][1] == 0x11) test(bso[1][2] == 0x12) test(rso[0][0] == 0x01) test(rso[0][1] == 0x11) test(rso[0][2] == 0x12) test(rso[1][0] == 0xff) test(rso[2][0] == 0x0e) test(rso[3][0] == 0xf2) test(rso[3][1] == 0xf1) # # opBoolSS # bsi1 = ( (True, ), (False, ), (True, True), ) bsi2 = ((False, False, True), ) rso, bso = p.opBoolSS(bsi1, bsi2) test(len(bso) == 4) test(len(bso[0]) == 1) test(bso[0][0]) test(len(bso[1]) == 1) test(not bso[1][0]) test(len(bso[2]) == 2) test(bso[2][0]) test(bso[2][1]) test(len(bso[3]) == 3) test(not bso[3][0]) test(not bso[3][1]) test(bso[3][2]) test(len(rso) == 3) test(len(rso[0]) == 2) test(rso[0][0]) test(rso[0][1]) test(len(rso[1]) == 1) test(not rso[1][0]) test(len(rso[2]) == 1) test(rso[2][0]) # # opShortIntLongSS # ssi = ((1, 2, 5), (13, ), ()) isi = ((24, 98), (42, )) lsi = ((496, 1729), ) rso, sso, iso, lso = p.opShortIntLongSS(ssi, isi, lsi) test(len(rso) == 1) test(len(rso[0]) == 2) test(rso[0][0] == 496) test(rso[0][1] == 1729) test(len(sso) == 3) test(len(sso[0]) == 3) test(sso[0][0] == 1) test(sso[0][1] == 2) test(sso[0][2] == 5) test(len(sso[1]) == 1) test(sso[1][0] == 13) test(len(sso[2]) == 0) test(len(iso) == 2) test(len(iso[0]) == 1) test(iso[0][0] == 42) test(len(iso[1]) == 2) test(iso[1][0] == 24) test(iso[1][1] == 98) test(len(lso) == 2) test(len(lso[0]) == 2) test(lso[0][0] == 496) test(lso[0][1] == 1729) test(len(lso[1]) == 2) test(lso[1][0] == 496) test(lso[1][1] == 1729) # # opFloatDoubleSS # fsi = ((3.14, ), (1.11, ), ()) dsi = ((1.1E10, 1.2E10, 1.3E10), ) rso, fso, dso = p.opFloatDoubleSS(fsi, dsi) test(len(fso) == 3) test(len(fso[0]) == 1) test(fso[0][0] - 3.14 < 0.001) test(len(fso[1]) == 1) test(fso[1][0] - 1.11 < 0.001) test(len(fso[2]) == 0) test(len(dso) == 1) test(len(dso[0]) == 3) test(dso[0][0] == 1.1E10) test(dso[0][1] == 1.2E10) test(dso[0][2] == 1.3E10) test(len(rso) == 2) test(len(rso[0]) == 3) test(rso[0][0] == 1.1E10) test(rso[0][1] == 1.2E10) test(rso[0][2] == 1.3E10) test(len(rso[1]) == 3) test(rso[1][0] == 1.1E10) test(rso[1][1] == 1.2E10) test(rso[1][2] == 1.3E10) # # opStringSS # ssi1 = (('abc', ), ('de', 'fghi')) ssi2 = ((), (), ('xyz', )) rso, sso = p.opStringSS(ssi1, ssi2) test(len(sso) == 5) test(len(sso[0]) == 1) test(sso[0][0] == "abc") test(len(sso[1]) == 2) test(sso[1][0] == "de") test(sso[1][1] == "fghi") test(len(sso[2]) == 0) test(len(sso[3]) == 0) test(len(sso[4]) == 1) test(sso[4][0] == "xyz") test(len(rso) == 3) test(len(rso[0]) == 1) test(rso[0][0] == "xyz") test(len(rso[1]) == 0) test(len(rso[2]) == 0) # # opStringSSS # sssi1 = ((('abc', 'de'), ('xyz', )), (('hello', ), )) sssi2 = ((('', ''), ('abcd', )), (('', ), ), ()) rsso, ssso = p.opStringSSS(sssi1, sssi2) test(len(ssso) == 5) test(len(ssso[0]) == 2) test(len(ssso[0][0]) == 2) test(len(ssso[0][1]) == 1) test(len(ssso[1]) == 1) test(len(ssso[1][0]) == 1) test(len(ssso[2]) == 2) test(len(ssso[2][0]) == 2) test(len(ssso[2][1]) == 1) test(len(ssso[3]) == 1) test(len(ssso[3][0]) == 1) test(len(ssso[4]) == 0) test(ssso[0][0][0] == "abc") test(ssso[0][0][1] == "de") test(ssso[0][1][0] == "xyz") test(ssso[1][0][0] == "hello") test(ssso[2][0][0] == "") test(ssso[2][0][1] == "") test(ssso[2][1][0] == "abcd") test(ssso[3][0][0] == "") test(len(rsso) == 3) test(len(rsso[0]) == 0) test(len(rsso[1]) == 1) test(len(rsso[1][0]) == 1) test(len(rsso[2]) == 2) test(len(rsso[2][0]) == 2) test(len(rsso[2][1]) == 1) test(rsso[1][0][0] == "") test(rsso[2][0][0] == "") test(rsso[2][0][1] == "") test(rsso[2][1][0] == "abcd") # # opByteBoolD # di1 = {10: True, 100: False} di2 = {10: True, 11: False, 101: True} ro, do = p.opByteBoolD(di1, di2) test(do == di1) test(len(ro) == 4) test(ro[10]) test(not ro[11]) test(not ro[100]) test(ro[101]) # # opShortIntD # di1 = {110: -1, 1100: 123123} di2 = {110: -1, 111: -100, 1101: 0} ro, do = p.opShortIntD(di1, di2) test(do == di1) test(len(ro) == 4) test(ro[110] == -1) test(ro[111] == -100) test(ro[1100] == 123123) test(ro[1101] == 0) # # opLongFloatD # di1 = {999999110: -1.1, 999999111: 123123.2} di2 = {999999110: -1.1, 999999120: -100.4, 999999130: 0.5} ro, do = p.opLongFloatD(di1, di2) for k in do: test(math.fabs(do[k] - di1[k]) < 0.01) test(len(ro) == 4) test(ro[999999110] - -1.1 < 0.01) test(ro[999999120] - -100.4 < 0.01) test(ro[999999111] - 123123.2 < 0.01) test(ro[999999130] - 0.5 < 0.01) # # opStringStringD # di1 = {'foo': 'abc -1.1', 'bar': 'abc 123123.2'} di2 = {'foo': 'abc -1.1', 'FOO': 'abc -100.4', 'BAR': 'abc 0.5'} ro, do = p.opStringStringD(di1, di2) test(do == di1) test(len(ro) == 4) test(ro["foo"] == "abc -1.1") test(ro["FOO"] == "abc -100.4") test(ro["bar"] == "abc 123123.2") test(ro["BAR"] == "abc 0.5") # # opStringMyEnumD # di1 = {'abc': Test.MyEnum.enum1, '': Test.MyEnum.enum2} di2 = { 'abc': Test.MyEnum.enum1, 'qwerty': Test.MyEnum.enum3, 'Hello!!': Test.MyEnum.enum2 } ro, do = p.opStringMyEnumD(di1, di2) test(do == di1) test(len(ro) == 4) test(ro["abc"] == Test.MyEnum.enum1) test(ro["qwerty"] == Test.MyEnum.enum3) test(ro[""] == Test.MyEnum.enum2) test(ro["Hello!!"] == Test.MyEnum.enum2) # # opMyEnumStringD # di1 = {Test.MyEnum.enum1: 'abc'} di2 = {Test.MyEnum.enum2: 'Hello!!', Test.MyEnum.enum3: 'qwerty'} ro, do = p.opMyEnumStringD(di1, di2) test(do == di1) test(len(ro) == 3) test(ro[Test.MyEnum.enum1] == "abc") test(ro[Test.MyEnum.enum2] == "Hello!!") test(ro[Test.MyEnum.enum3] == "qwerty") # # opMyStructMyEnumD # s11 = Test.MyStruct() s11.i = 1 s11.j = 1 s12 = Test.MyStruct() s12.i = 1 s12.j = 2 s22 = Test.MyStruct() s22.i = 2 s22.j = 2 s23 = Test.MyStruct() s23.i = 2 s23.j = 3 di1 = {s11: Test.MyEnum.enum1, s12: Test.MyEnum.enum2} di2 = { s11: Test.MyEnum.enum1, s22: Test.MyEnum.enum3, s23: Test.MyEnum.enum2 } ro, do = p.opMyStructMyEnumD(di1, di2) test(do == di1) test(len(ro) == 4) test(ro[s11] == Test.MyEnum.enum1) test(ro[s12] == Test.MyEnum.enum2) test(ro[s22] == Test.MyEnum.enum3) test(ro[s23] == Test.MyEnum.enum2) # # opByteBoolDS # dsi1 = ({10: True, 100: False}, {10: True, 11: False, 101: True}) dsi2 = ({100: False, 101: False}, ) ro, do = p.opByteBoolDS(dsi1, dsi2) test(len(ro) == 2) test(len(ro[0]) == 3) test(ro[0][10]) test(not ro[0][11]) test(ro[0][101]) test(len(ro[1]) == 2) test(ro[1][10]) test(not ro[1][100]) test(len(do) == 3) test(len(do[0]) == 2) test(not do[0][100]) test(not do[0][101]) test(len(do[1]) == 2) test(do[1][10]) test(not do[1][100]) test(len(do[2]) == 3) test(do[2][10]) test(not do[2][11]) test(do[2][101]) # # opShortIntDS # dsi1 = ({110: -1, 1100: 123123}, {110: -1, 111: -100, 1101: 0}) dsi2 = ({100: -1001}, ) ro, do = p.opShortIntDS(dsi1, dsi2) test(len(ro) == 2) test(len(ro[0]) == 3) test(ro[0][110] == -1) test(ro[0][111] == -100) test(ro[0][1101] == 0) test(len(ro[1]) == 2) test(ro[1][110] == -1) test(ro[1][1100] == 123123) test(len(do) == 3) test(len(do[0]) == 1) test(do[0][100] == -1001) test(len(do[1]) == 2) test(do[1][110] == -1) test(do[1][1100] == 123123) test(len(do[2]) == 3) test(do[2][110] == -1) test(do[2][111] == -100) test(do[2][1101] == 0) # # opLongFloatDS # dsi1 = ({ 999999110: -1.1, 999999111: 123123.2 }, { 999999110: -1.1, 999999120: -100.4, 999999130: 0.5 }) dsi2 = ({999999140: 3.14}, ) ro, do = p.opLongFloatDS(dsi1, dsi2) test(len(ro) == 2) test(len(ro[0]) == 3) test(ro[0][999999110] - -1.1 < 0.01) test(ro[0][999999120] - -100.4 < 0.01) test(ro[0][999999130] - 0.5 < 0.01) test(len(ro[1]) == 2) test(ro[1][999999110] - -1.1 < 0.01) test(ro[1][999999111] - 123123.2 < 0.01) test(len(do) == 3) test(len(do[0]) == 1) test(do[0][999999140] - 3.14 < 0.01) test(len(do[1]) == 2) test(do[1][999999110] - -1.1 < 0.01) test(do[1][999999111] - 123123.2 < 0.01) test(len(do[2]) == 3) test(do[2][999999110] - -1.1 < 0.01) test(do[2][999999120] - -100.4 < 0.01) test(do[2][999999130] - 0.5 < 0.01) # # opStringStringDS # dsi1 = ({ "foo": "abc -1.1", "bar": "abc 123123.2" }, { "foo": "abc -1.1", "FOO": "abc -100.4", "BAR": "abc 0.5" }) dsi2 = ({"f00": "ABC -3.14"}, ) ro, do = p.opStringStringDS(dsi1, dsi2) test(len(ro) == 2) test(len(ro[0]) == 3) test(ro[0]["foo"] == "abc -1.1") test(ro[0]["FOO"] == "abc -100.4") test(ro[0]["BAR"] == "abc 0.5") test(len(ro[1]) == 2) test(ro[1]["foo"] == "abc -1.1") test(ro[1]["bar"] == "abc 123123.2") test(len(do) == 3) test(len(do[0]) == 1) test(do[0]["f00"] == "ABC -3.14") test(len(do[1]) == 2) test(do[1]["foo"] == "abc -1.1") test(do[1]["bar"] == "abc 123123.2") test(len(do[2]) == 3) test(do[2]["foo"] == "abc -1.1") test(do[2]["FOO"] == "abc -100.4") test(do[2]["BAR"] == "abc 0.5") # # opStringMyEnumDS # dsi1 = ({ "abc": Test.MyEnum.enum1, "": Test.MyEnum.enum2 }, { "abc": Test.MyEnum.enum1, "qwerty": Test.MyEnum.enum3, "Hello!!": Test.MyEnum.enum2 }) dsi2 = ({"Goodbye": Test.MyEnum.enum1}, ) ro, do = p.opStringMyEnumDS(dsi1, dsi2) test(len(ro) == 2) test(len(ro[0]) == 3) test(ro[0]["abc"] == Test.MyEnum.enum1) test(ro[0]["qwerty"] == Test.MyEnum.enum3) test(ro[0]["Hello!!"] == Test.MyEnum.enum2) test(len(ro[1]) == 2) test(ro[1]["abc"] == Test.MyEnum.enum1) test(ro[1][""] == Test.MyEnum.enum2) test(len(do) == 3) test(len(do[0]) == 1) test(do[0]["Goodbye"] == Test.MyEnum.enum1) test(len(do[1]) == 2) test(do[1]["abc"] == Test.MyEnum.enum1) test(do[1][""] == Test.MyEnum.enum2) test(len(do[2]) == 3) test(do[2]["abc"] == Test.MyEnum.enum1) test(do[2]["qwerty"] == Test.MyEnum.enum3) test(do[2]["Hello!!"] == Test.MyEnum.enum2) # # opMyEnumStringDS # dsi1 = ({ Test.MyEnum.enum1: 'abc' }, { Test.MyEnum.enum2: 'Hello!!', Test.MyEnum.enum3: 'qwerty' }) dsi2 = ({Test.MyEnum.enum1: 'Goodbye'}, ) ro, do = p.opMyEnumStringDS(dsi1, dsi2) test(len(ro) == 2) test(len(ro[0]) == 2) test(ro[0][Test.MyEnum.enum2] == "Hello!!") test(ro[0][Test.MyEnum.enum3] == "qwerty") test(len(ro[1]) == 1) test(ro[1][Test.MyEnum.enum1] == "abc") test(len(do) == 3) test(len(do[0]) == 1) test(do[0][Test.MyEnum.enum1] == "Goodbye") test(len(do[1]) == 1) test(do[1][Test.MyEnum.enum1] == "abc") test(len(do[2]) == 2) test(do[2][Test.MyEnum.enum2] == "Hello!!") test(do[2][Test.MyEnum.enum3] == "qwerty") # # opMyStructMyEnumDS # s11 = Test.MyStruct(1, 1) s12 = Test.MyStruct(1, 2) s22 = Test.MyStruct(2, 2) s23 = Test.MyStruct(2, 3) dsi1 = ({ s11: Test.MyEnum.enum1, s12: Test.MyEnum.enum2 }, { s11: Test.MyEnum.enum1, s22: Test.MyEnum.enum3, s23: Test.MyEnum.enum2 }) dsi2 = ({s23: Test.MyEnum.enum3}, ) ro, do = p.opMyStructMyEnumDS(dsi1, dsi2) test(len(ro) == 2) test(len(ro[0]) == 3) test(ro[0][s11] == Test.MyEnum.enum1) test(ro[0][s22] == Test.MyEnum.enum3) test(ro[0][s23] == Test.MyEnum.enum2) test(len(ro[1]) == 2) test(ro[1][s11] == Test.MyEnum.enum1) test(ro[1][s12] == Test.MyEnum.enum2) test(len(do) == 3) test(len(do[0]) == 1) test(do[0][s23] == Test.MyEnum.enum3) test(len(do[1]) == 2) test(do[1][s11] == Test.MyEnum.enum1) test(do[1][s12] == Test.MyEnum.enum2) test(len(do[2]) == 3) test(do[2][s11] == Test.MyEnum.enum1) test(do[2][s22] == Test.MyEnum.enum3) test(do[2][s23] == Test.MyEnum.enum2) # #opByteByteSD # sdi1 = {0x01: (0x01, 0x11), 0x22: (0x12, )} sdi2 = {0xf1: (0xf2, 0xf3)} ro, do = p.opByteByteSD(sdi1, sdi2) if sys.version_info[0] == 2: test(len(do) == 1) test(len(do[0xf1]) == 2) test(do[0xf1][0] == '\xf2') test(do[0xf1][1] == '\xf3') test(len(ro) == 3) test(len(ro[0x01]) == 2) test(ro[0x01][0] == '\x01') test(ro[0x01][1] == '\x11') test(len(ro[0x22]) == 1) test(ro[0x22][0] == '\x12') test(len(ro[0xf1]) == 2) test(ro[0xf1][0] == '\xf2') test(ro[0xf1][1] == '\xf3') else: test(len(do) == 1) test(len(do[0xf1]) == 2) test(do[0xf1][0] == 0xf2) test(do[0xf1][1] == 0xf3) test(len(ro) == 3) test(len(ro[0x01]) == 2) test(ro[0x01][0] == 0x01) test(ro[0x01][1] == 0x11) test(len(ro[0x22]) == 1) test(ro[0x22][0] == 0x12) test(len(ro[0xf1]) == 2) test(ro[0xf1][0] == 0xf2) test(ro[0xf1][1] == 0xf3) # # opBoolBoolSD # sdi1 = {False: (True, False), True: (False, True, True)} sdi2 = {False: (True, False)} ro, do = p.opBoolBoolSD(sdi1, sdi2) test(len(do) == 1) test(len(do[False]) == 2) test(do[False][0]) test(not do[False][1]) test(len(ro) == 2) test(len(ro[False]) == 2) test(ro[False][0]) test(not ro[False][1]) test(len(ro[True]) == 3) test(not ro[True][0]) test(ro[True][1]) test(ro[True][2]) # # opShortShortSD # sdi1 = {1: (1, 2, 3), 2: (4, 5)} sdi2 = {4: (6, 7)} ro, do = p.opShortShortSD(sdi1, sdi2) test(len(do) == 1) test(len(do[4]) == 2) test(do[4][0] == 6) test(do[4][1] == 7) test(len(ro) == 3) test(len(ro[1]) == 3) test(ro[1][0] == 1) test(ro[1][1] == 2) test(ro[1][2] == 3) test(len(ro[2]) == 2) test(ro[2][0] == 4) test(ro[2][1] == 5) test(len(ro[4]) == 2) test(ro[4][0] == 6) test(ro[4][1] == 7) # # opIntIntSD # sdi1 = {100: (100, 200, 300), 200: (400, 500)} sdi2 = {400: (600, 700)} ro, do = p.opIntIntSD(sdi1, sdi2) test(len(do) == 1) test(len(do[400]) == 2) test(do[400][0] == 600) test(do[400][1] == 700) test(len(ro) == 3) test(len(ro[100]) == 3) test(ro[100][0] == 100) test(ro[100][1] == 200) test(ro[100][2] == 300) test(len(ro[200]) == 2) test(ro[200][0] == 400) test(ro[200][1] == 500) test(len(ro[400]) == 2) test(ro[400][0] == 600) test(ro[400][1] == 700) # # opLongLongSD # sdi1 = { 999999990: (999999110, 999999111, 999999110), 999999991: (999999120, 999999130) } sdi2 = {999999992: (999999110, 999999120)} ro, do = p.opLongLongSD(sdi1, sdi2) test(len(do) == 1) test(len(do[999999992]) == 2) test(do[999999992][0] == 999999110) test(do[999999992][1] == 999999120) test(len(ro) == 3) test(len(ro[999999990]) == 3) test(ro[999999990][0] == 999999110) test(ro[999999990][1] == 999999111) test(ro[999999990][2] == 999999110) test(len(ro[999999991]) == 2) test(ro[999999991][0] == 999999120) test(ro[999999991][1] == 999999130) test(len(ro[999999992]) == 2) test(ro[999999992][0] == 999999110) test(ro[999999992][1] == 999999120) # # opStringFloatSD # sdi1 = {"abc": (-1.1, 123123.2, 100.0), "ABC": (42.24, -1.61)} sdi2 = {"aBc": (-3.14, 3.14)} ro, do = p.opStringFloatSD(sdi1, sdi2) test(len(do) == 1) test(len(do["aBc"]) == 2) test(do["aBc"][0] - -3.14 < 0.01) test(do["aBc"][1] - 3.14 < 0.01) test(len(ro) == 3) test(len(ro["abc"]) == 3) test(ro["abc"][0] - -1.1 < 0.01) test(ro["abc"][1] - 123123.2 < 0.01) test(ro["abc"][2] - 100.0 < 0.01) test(len(ro["ABC"]) == 2) test(ro["ABC"][0] - 42.24 < 0.01) test(ro["ABC"][1] - -1.61 < 0.01) test(len(ro["aBc"]) == 2) test(ro["aBc"][0] - -3.14 < 0.01) test(ro["aBc"][1] - 3.14 < 0.01) # # opStringDoubleSD # sdi1 = {"Hello!!": (1.1E10, 1.2E10, 1.3E10), "Goodbye": (1.4E10, 1.5E10)} sdi2 = {"": (1.6E10, 1.7E10)} ro, do = p.opStringDoubleSD(sdi1, sdi2) test(len(do) == 1) test(len(do[""]) == 2) test(do[""][0] == 1.6E10) test(do[""][1] == 1.7E10) test(len(ro) == 3) test(len(ro["Hello!!"]) == 3) test(ro["Hello!!"][0] == 1.1E10) test(ro["Hello!!"][1] == 1.2E10) test(ro["Hello!!"][2] == 1.3E10) test(len(ro["Goodbye"]) == 2) test(ro["Goodbye"][0] == 1.4E10) test(ro["Goodbye"][1] == 1.5E10) test(len(ro[""]) == 2) test(ro[""][0] == 1.6E10) test(ro[""][1] == 1.7E10) # # opStringStringSD # sdi1 = {"abc": ("abc", "de", "fghi"), "def": ("xyz", "or")} sdi2 = {"ghi": ("and", "xor")} ro, do = p.opStringStringSD(sdi1, sdi2) test(len(do) == 1) test(len(do["ghi"]) == 2) test(do["ghi"][0] == "and") test(do["ghi"][1] == "xor") test(len(ro) == 3) test(len(ro["abc"]) == 3) test(ro["abc"][0] == "abc") test(ro["abc"][1] == "de") test(ro["abc"][2] == "fghi") test(len(ro["def"]) == 2) test(ro["def"][0] == "xyz") test(ro["def"][1] == "or") test(len(ro["ghi"]) == 2) test(ro["ghi"][0] == "and") test(ro["ghi"][1] == "xor") # # opMyEnumMyEnumSD # sdi1 = { Test.MyEnum.enum3: (Test.MyEnum.enum1, Test.MyEnum.enum1, Test.MyEnum.enum2), Test.MyEnum.enum2: (Test.MyEnum.enum1, Test.MyEnum.enum2) } sdi2 = {Test.MyEnum.enum1: (Test.MyEnum.enum3, Test.MyEnum.enum3)} ro, do = p.opMyEnumMyEnumSD(sdi1, sdi2) test(len(do) == 1) test(len(do[Test.MyEnum.enum1]) == 2) test(do[Test.MyEnum.enum1][0] == Test.MyEnum.enum3) test(do[Test.MyEnum.enum1][1] == Test.MyEnum.enum3) test(len(ro) == 3) test(len(ro[Test.MyEnum.enum3]) == 3) test(ro[Test.MyEnum.enum3][0] == Test.MyEnum.enum1) test(ro[Test.MyEnum.enum3][1] == Test.MyEnum.enum1) test(ro[Test.MyEnum.enum3][2] == Test.MyEnum.enum2) test(len(ro[Test.MyEnum.enum2]) == 2) test(ro[Test.MyEnum.enum2][0] == Test.MyEnum.enum1) test(ro[Test.MyEnum.enum2][1] == Test.MyEnum.enum2) test(len(ro[Test.MyEnum.enum1]) == 2) test(ro[Test.MyEnum.enum1][0] == Test.MyEnum.enum3) test(ro[Test.MyEnum.enum1][1] == Test.MyEnum.enum3) # # opIntS # lengths = (0, 1, 2, 126, 127, 128, 129, 253, 254, 255, 256, 257, 1000) for l in lengths: s = [] for i in range(l): s.append(i) r = p.opIntS(s) test(len(r) == l) for j in range(len(r)): test(r[j] == -j) # # opContext # ctx = {'one': 'ONE', 'two': 'TWO', 'three': 'THREE'} r = p.opContext() test(len(p.ice_getContext()) == 0) test(r != ctx) r = p.opContext(ctx) test(len(p.ice_getContext()) == 0) test(r == ctx) p2 = Test.MyClassPrx.checkedCast(p.ice_context(ctx)) test(p2.ice_getContext() == ctx) r = p2.opContext() test(r == ctx) r = p2.opContext(ctx) test(r == ctx) # # Test implicit context propagation # if p.ice_getConnection(): impls = ('Shared', 'PerThread') for i in impls: initData = Ice.InitializationData() initData.properties = communicator.getProperties().clone() initData.properties.setProperty('Ice.ImplicitContext', i) ic = Ice.initialize(data=initData) ctx = {'one': 'ONE', 'two': 'TWO', 'three': 'THREE'} p1 = Test.MyClassPrx.uncheckedCast( ic.stringToProxy('test:{0}'.format(helper.getTestEndpoint()))) ic.getImplicitContext().setContext(ctx) test(ic.getImplicitContext().getContext() == ctx) test(p1.opContext() == ctx) test(ic.getImplicitContext().containsKey('zero') == False) r = ic.getImplicitContext().put('zero', 'ZERO') test(r == '') test(ic.getImplicitContext().containsKey('zero') == True) test(ic.getImplicitContext().get('zero') == 'ZERO') ctx = ic.getImplicitContext().getContext() test(p1.opContext() == ctx) prxContext = {'one': 'UN', 'four': 'QUATRE'} combined = ctx.copy() combined.update(prxContext) test(combined['one'] == 'UN') p2 = Test.MyClassPrx.uncheckedCast(p1.ice_context(prxContext)) ic.getImplicitContext().setContext({}) test(p2.opContext() == prxContext) ic.getImplicitContext().setContext(ctx) test(p2.opContext() == combined) test(ic.getImplicitContext().remove('one') == 'ONE') ic.destroy() d = 1278312346.0 / 13.0 ds = [] for i in range(5): ds.append(d) p.opDoubleMarshaling(d, ds) # # opIdempotent # p.opIdempotent() # # opNonmutating # p.opNonmutating() test(p.opByte1(0xFF) == 0xFF) test(p.opShort1(0x7FFF) == 0x7FFF) test(p.opInt1(0x7FFFFFFF) == 0x7FFFFFFF) test(p.opLong1(0x7FFFFFFFFFFFFFFF) == 0x7FFFFFFFFFFFFFFF) test(p.opFloat1(1.0) == 1.0) test(p.opDouble1(1.0) == 1.0) test(p.opString1("opString1") == "opString1") test(len(p.opStringS1(None)) == 0) test(len(p.opByteBoolD1(None)) == 0) test(len(p.opStringS2(None)) == 0) test(len(p.opByteBoolD2(None)) == 0) d = Test.MyDerivedClassPrx.uncheckedCast(p) s = Test.MyStruct1() s.tesT = "Test.MyStruct1.s" s.myClass = None s.myStruct1 = "Test.MyStruct1.myStruct1" s = d.opMyStruct1(s) test(s.tesT == "Test.MyStruct1.s") test(s.myClass == None) test(s.myStruct1 == "Test.MyStruct1.myStruct1") c = Test.MyClass1() c.tesT = "Test.MyClass1.testT" c.myClass = None c.myClass1 = "Test.MyClass1.myClass1" c = d.opMyClass1(c) test(c.tesT == "Test.MyClass1.testT") test(c.myClass == None) test(c.myClass1 == "Test.MyClass1.myClass1") p1 = p.opMStruct1() p1.e = Test.MyEnum.enum3 (p3, p2) = p.opMStruct2(p1) test(p2 == p1 and p3 == p1) p.opMSeq1() p1 = ["test"] (p3, p2) = p.opMSeq2(p1) test(p2[0] == "test" and p3[0] == "test") p.opMDict1() p1 = {"test": "test"} (p3, p2) = p.opMDict2(p1) test(p3["test"] == "test" and p2["test"] == "test")
def allTests(communicator): sref = "timeout:default -p 12010" obj = communicator.stringToProxy(sref) test(obj != None) timeout = Test.TimeoutPrx.checkedCast(obj) test(timeout != None) controller = Test.ControllerPrx.checkedCast( communicator.stringToProxy("controller:default -p 12011")) test(controller != None) sys.stdout.write("testing connect timeout... ") sys.stdout.flush() # # Expect ConnectTimeoutException. # to = Test.TimeoutPrx.uncheckedCast(obj.ice_timeout(100)) controller.holdAdapter(-1) try: to.op() test(False) except Ice.ConnectTimeoutException: pass # Expected. controller.resumeAdapter() timeout.op() # Ensure adapter is active. # # Expect success. # to = Test.TimeoutPrx.uncheckedCast(obj.ice_timeout(2000)) controller.holdAdapter(100) try: to.op() except Ice.ConnectTimeoutException: test(False) print("ok") sys.stdout.write("testing connection timeout... ") sys.stdout.flush() # # Expect TimeoutException. # if sys.version_info[0] == 2: seq = [] seq[0:10000000] = range(0, 10000000) # add 10,000,000 entries. seq = ['\x00' for x in seq] # set them all to \x00 seq = ''.join(seq) # make into a byte array else: seq = bytes([0 for x in range(0, 10000000)]) to = Test.TimeoutPrx.uncheckedCast(obj.ice_timeout(250)) connect(to) controller.holdAdapter(-1) try: to.sendData(seq) test(False) except Ice.TimeoutException: pass # Expected. controller.resumeAdapter() timeout.op() # Ensure adapter is active. # # Expect success. # to = Test.TimeoutPrx.uncheckedCast(obj.ice_timeout(2000)) controller.holdAdapter(100) try: if sys.version_info[0] == 2: seq2 = [] seq2[0:1000000] = range(0, 1000000) # add 1,000,000 entries. seq2 = ['\x00' for x in seq2] # set them all to \x00 seq2 = ''.join(seq2) # make into a byte array else: seq2 = bytes([0 for x in range(0, 1000000)]) to.sendData(seq2) except Ice.TimeoutException: test(False) print("ok") sys.stdout.write("testing invocation timeout... ") sys.stdout.flush() connection = obj.ice_getConnection() to = Test.TimeoutPrx.uncheckedCast(obj.ice_invocationTimeout(100)) test(connection == to.ice_getConnection()) try: to.sleep(500) test(False) except Ice.InvocationTimeoutException: pass obj.ice_ping() to = Test.TimeoutPrx.uncheckedCast(obj.ice_invocationTimeout(1000)) test(connection == to.ice_getConnection()) try: to.sleep(100) except Ice.InvocationTimeoutException: test(False) test(connection == to.ice_getConnection()) # # # # Expect InvocationTimeoutException. # # # to = Test.TimeoutPrx.uncheckedCast(obj.ice_invocationTimeout(250)) # cb = new Callback() # to.begin_sleep(500, newCallback_Timeout_sleep(cb, &Callback.responseEx, &Callback.exceptionEx)) # cb.check() # # # # Expect success. # # # to = Test.TimeoutPrx.uncheckedCast(obj.ice_invocationTimeout(1000)) # cb = new Callback() # to.begin_sleep(100, newCallback_Timeout_sleep(cb, &Callback.response, &Callback.exception)) # cb.check() print("ok") sys.stdout.write("testing close timeout... ") sys.stdout.flush() to = Test.TimeoutPrx.uncheckedCast(obj.ice_timeout(250)) connection = connect(to) controller.holdAdapter(-1) connection.close(Ice.ConnectionClose.GracefullyWithWait) try: connection.getInfo() # getInfo() doesn't throw in the closing state. except Ice.LocalException: test(False) while True: try: connection.getInfo() time.sleep(0.001) except Ice.ConnectionManuallyClosedException as ex: # Expected. test(ex.graceful) break controller.resumeAdapter() timeout.op() # Ensure adapter is active. print("ok") sys.stdout.write("testing timeout overrides... ") sys.stdout.flush() # # Test Ice.Override.Timeout. This property overrides all # endpoint timeouts. # initData = Ice.InitializationData() initData.properties = communicator.getProperties().clone() initData.properties.setProperty("Ice.Override.ConnectTimeout", "250") initData.properties.setProperty("Ice.Override.Timeout", "100") comm = Ice.initialize(initData) to = Test.TimeoutPrx.uncheckedCast(comm.stringToProxy(sref)) connect(to) controller.holdAdapter(-1) try: to.sendData(seq) test(False) except Ice.TimeoutException: pass # Expected. controller.resumeAdapter() timeout.op() # Ensure adapter is active. # # Calling ice_timeout() should have no effect. # to = Test.TimeoutPrx.uncheckedCast(to.ice_timeout(1000)) connect(to) controller.holdAdapter(-1) try: to.sendData(seq) test(False) except Ice.TimeoutException: pass # Expected. controller.resumeAdapter() timeout.op() # Ensure adapter is active. comm.destroy() # # Test Ice.Override.ConnectTimeout. # initData = Ice.InitializationData() initData.properties = communicator.getProperties().clone() initData.properties.setProperty("Ice.Override.ConnectTimeout", "250") comm = Ice.initialize(initData) controller.holdAdapter(-1) to = Test.TimeoutPrx.uncheckedCast(comm.stringToProxy(sref)) try: to.op() test(False) except Ice.ConnectTimeoutException: pass # Expected. controller.resumeAdapter() timeout.op() # Ensure adapter is active. # # Calling ice_timeout() should have no effect on the connect timeout. # controller.holdAdapter(-1) to = Test.TimeoutPrx.uncheckedCast(to.ice_timeout(1000)) try: to.op() test(False) except Ice.ConnectTimeoutException: pass # Expected. controller.resumeAdapter() timeout.op() # Ensure adapter is active. # # Verify that timeout set via ice_timeout() is still used for requests. # to = Test.TimeoutPrx.uncheckedCast(to.ice_timeout(250)) connect(to) controller.holdAdapter(-1) try: to.sendData(seq) test(False) except Ice.TimeoutException: pass # Expected. controller.resumeAdapter() timeout.op() # Ensure adapter is active. comm.destroy() # # Test Ice.Override.CloseTimeout. # initData = Ice.InitializationData() initData.properties = communicator.getProperties().clone() initData.properties.setProperty("Ice.Override.CloseTimeout", "100") comm = Ice.initialize(initData) connection = comm.stringToProxy(sref).ice_getConnection() controller.holdAdapter(-1) now = time.clock() comm.destroy() test((time.clock() - now) < 0.7) controller.resumeAdapter() print("ok") controller.shutdown()
def allTests(communicator): sys.stdout.write("testing stringToProxy... ") sys.stdout.flush() base = communicator.stringToProxy("test:default -p 12010") test(base) print("ok") sys.stdout.write("testing checked cast... ") sys.stdout.flush() obj = Test.TestIntfPrx.checkedCast(base) test(obj) test(obj == base) print("ok") sys.stdout.write("creating/destroying/recreating object adapter... ") sys.stdout.flush() adapter = communicator.createObjectAdapterWithEndpoints( "TransientTestAdapter", "default") try: communicator.createObjectAdapterWithEndpoints("TransientTestAdapter", "default") test(False) except Ice.LocalException: pass adapter.destroy() adapter = communicator.createObjectAdapterWithEndpoints( "TransientTestAdapter", "default") adapter.destroy() print("ok") sys.stdout.write( "creating/activating/deactivating object adapter in one operation... ") sys.stdout.flush() obj.transient() print("ok") sys.stdout.write("deactivating object adapter in the server... ") sys.stdout.flush() obj.deactivate() print("ok") sys.stdout.write("testing connection closure... ") sys.stdout.flush() for x in range(10): initData = Ice.InitializationData() initData.properties = communicator.getProperties().clone() comm = Ice.initialize(initData) comm.stringToProxy("test:default -p 12010").ice_pingAsync() comm.destroy() print("ok") sys.stdout.write("testing whether server is gone... ") sys.stdout.flush() try: obj.ice_ping() test(False) except Ice.LocalException: print("ok") return obj
def handle(self, *args, **options): username = raw_input('MotionDB Username: '******'MotionDB Password: '******'') # Configure Ice and Connect to database. properties = Ice.createProperties(sys.argv) properties.load(ICE_CLIENT_CONFIG_PATH) init_data = Ice.InitializationData() init_data.properties = properties ic = Ice.initialize(init_data) router = Glacier2.RouterPrx.checkedCast(ic.getDefaultRouter()) session = router.createSession(username, password) db = MotionDatabase.MotionDatabaseSessionPrx.checkedCast(session) # Collect all matching C3D and MMM files. self.stdout.write('Collecting data from motion database ...') q = MotionFile.objects.filter(is_hidden=False, is_broken_reported=False, is_broken_confirmed=False) motion_ids = list(set([m.motion_db_id for m in q.all()])) all_c3d_files = [] all_mmm_files = [] all_annotations = [] all_motion_ids = [] all_database_entries = [] for idx, motion_id in enumerate(motion_ids): self.stdout.write(' {}/{} ...'.format(idx + 1, len(motion_ids)), ending=' ') self.stdout.flush() files = db.listFiles(motion_id) c3d_files = [f for f in files if f.fileType == 'Vicon C3D File'] mmm_files = [ f for f in files if f.fileType == 'Converted MMM Motion' ] for c3d_file in c3d_files: # Ensure that only visible data is exported. assert c3d_file.visibility == MotionDatabase.VisibilityLevel.Public # Fetch motion file from database. try: motion_file = MotionFile.objects.get( motion_db_file_id=c3d_file.id) except MotionFile.DoesNotExist: continue assert motion_file.motion_db_id == motion_id # Skip broken motions. if motion_file.is_broken_reported or motion_file.is_broken_confirmed: continue # Find the matching MMM file for the given C3D file. mmm_file = None for f in mmm_files: if f.originatedFrom.id == c3d_file.id: mmm_file = f break assert mmm_file is not None # Get all annotations. We include data even if it isn't annotated yet. annotations = Annotation.objects.filter( motion_file=motion_file).all() all_c3d_files.append(c3d_file) all_mmm_files.append(mmm_file) all_annotations.append(annotations) all_motion_ids.append(motion_id) all_database_entries.append(motion_file) self.stdout.write('done') n_motions = len(all_c3d_files) assert n_motions == len(all_mmm_files) assert n_motions == len(all_annotations) assert n_motions == len(all_motion_ids) assert n_motions == len(all_database_entries) self.stdout.write( 'done, obtained {} motions and their annotations'.format( n_motions)) self.stdout.write('') # Create temporary directory. tmp_path = mkdtemp() self.stdout.write('Downloading data to "{}" ...'.format(tmp_path)) motion_entry_cache = {} nb_annotations = 0 nb_motions = 0 for idx, (database_entry, c3d_file, mmm_file, annotations, motion_id) in enumerate( zip(all_database_entries, all_c3d_files, all_mmm_files, all_annotations, all_motion_ids)): self.stdout.write(' {}/{}: ...'.format(idx + 1, n_motions), ending=' ') self.stdout.flush() filename_prefix = '{0:05d}'.format(database_entry.id) filename_mmm = filename_prefix + '_mmm.xml' filename_c3d = filename_prefix + '_raw.c3d' filename_meta = filename_prefix + '_meta.json' filename_annotation = filename_prefix + '_annotations.json' # Download MMM. r = db.getFileReader(mmm_file.id) d = read_file(r) r.destroy() if d is None: return -1 with open(os.path.join(tmp_path, filename_mmm), 'wb') as f: f.write(d) # Download C3D. r = db.getFileReader(c3d_file.id) d = read_file(r) r.destroy() if d is None: return -1 with open(os.path.join(tmp_path, filename_c3d), 'wb') as f: f.write(d) # Retrieve motion information. if c3d_file.attachedToId in motion_entry_cache: motion_entry = motion_entry_cache[c3d_file.attachedToId] else: motion_entry = db.getMotion(c3d_file.attachedToId) motion_entry_cache[c3d_file.attachedToId] = motion_entry # Save annotations and extract their IDs for metadata. with open(os.path.join(tmp_path, filename_annotation), 'w') as f: json.dump([a.description for a in annotations], f) mat_annotation_ids = [a.id for a in annotations] # Save metadata. annotation_perplexities = [a.perplexity for a in annotations] assert len(annotation_perplexities) == len(annotations) with open(os.path.join(tmp_path, filename_meta), 'w') as f: data = { 'motion_annotation_tool': { 'id': database_entry.id, 'annotation_ids': mat_annotation_ids, }, 'source': { 'institution': { 'name': motion_entry.associatedInstitution.name, 'identifier': motion_entry.associatedInstitution.acronym.lower(), }, 'database': { 'identifier': 'kit', 'motion_id': motion_id, 'motion_file_id': c3d_file.id, }, }, 'nb_annotations': len(annotations), 'annotation_perplexities': annotation_perplexities, } if motion_entry.associatedInstitution.acronym.lower() == 'cmu': # Reference actual CMU database first and provide KIT database as the mirror. data['source']['mirror_database'] = data['source'][ 'database'] motion_id, file_id = [ int(x) for x in os.path.splitext(c3d_file.fileName) [0].split('_') ] data['source']['database'] = { 'identifier': 'cmu', 'motion_id': motion_id, 'motion_file_id': file_id, } json.dump(data, f) # Book-keeping. nb_annotations += len(annotations) nb_motions += 1 self.stdout.write('done') self.stdout.write('done') self.stdout.write('') # Create ZIP archive. filename = time.strftime('%Y-%m-%d') + '.zip' self.stdout.write('Exporting ZIP archive "{}" ...'.format(filename), ending=' ') self.stdout.flush() def callback_before(file): self.stdout.write(' processing file "{}" ...'.format(file), ending=' ') self.stdout.flush() def callback_after(file): self.stdout.write('done') zipdir(tmp_path, os.path.join(DATA_PATH, filename), callback_before=callback_before, callback_after=callback_after) self.stdout.write('done') self.stdout.write('') # Create dataset entry in DB. dataset = Dataset() dataset.nb_annotations = nb_annotations dataset.nb_motions = nb_motions dataset.filename = filename dataset.filesize = os.path.getsize(os.path.join(DATA_PATH, filename)) dataset.save() # Clean up tmp directory. self.stdout.write( 'Cleaning up temp directory "{}" ...'.format(tmp_path), ending=' ') self.stdout.flush() shutil.rmtree(tmp_path) self.stdout.write('done') self.stdout.write('') self.stdout.write( 'All done, remember to collect the static files so that people can download the dataset!' )