def _test_EnvFilter_as_DnsMasq(self, config_file_arg): usercmd = [ 'env', config_file_arg + '=A', 'NETWORK_ID=foobar', 'dnsmasq', 'foo' ] f = filters.EnvFilter("env", "root", config_file_arg + '=A', 'NETWORK_ID=', "/usr/bin/dnsmasq") self.assertTrue(f.match(usercmd)) self.assertEqual(f.get_command(usercmd), ['/usr/bin/dnsmasq', 'foo']) env = f.get_environment(usercmd) self.assertEqual(env.get(config_file_arg), 'A') self.assertEqual(env.get('NETWORK_ID'), 'foobar')
def test_EnvFilter_without_leading_env(self): envset = ['A=/some/thing', 'B=somethingelse'] envcmd = ['env'] + envset realcmd = ['sleep', '10'] f = filters.EnvFilter("sleep", "root", "A=", "B=ignored") # accept without leading env self.assertTrue(f.match(envset + ["sleep"])) self.assertEqual(f.get_command(envcmd + realcmd), realcmd) self.assertEqual(f.get_command(envset + realcmd), realcmd) env = f.get_environment(envset + realcmd) # check that environment variables are set self.assertEqual(env.get('A'), '/some/thing') self.assertEqual(env.get('B'), 'somethingelse') self.assertFalse('sleep' in env.keys())
def test_EnvFilter(self): envset = ['A=/some/thing', 'B=somethingelse'] envcmd = ['env'] + envset realcmd = ['sleep', '10'] usercmd = envcmd + realcmd f = filters.EnvFilter("env", "root", "A=", "B=ignored", "sleep") # accept with leading env self.assertTrue(f.match(envcmd + ["sleep"])) # accept without leading env self.assertTrue(f.match(envset + ["sleep"])) # any other command does not match self.assertFalse(f.match(envcmd + ["sleep2"])) self.assertFalse(f.match(envset + ["sleep2"])) # accept any trailing arguments self.assertTrue(f.match(usercmd)) # require given environment variables to match self.assertFalse(f.match([envcmd, 'C=ELSE'])) self.assertFalse(f.match(['env', 'C=xx'])) self.assertFalse(f.match(['env', 'A=xx'])) # require env command to be given # (otherwise CommandFilters should match self.assertFalse(f.match(realcmd)) # require command to match self.assertFalse(f.match(envcmd)) self.assertFalse(f.match(envcmd[1:])) # ensure that the env command is stripped when executing self.assertEqual(f.exec_args(usercmd), realcmd) env = f.get_environment(usercmd) # check that environment variables are set self.assertEqual(env.get('A'), '/some/thing') self.assertEqual(env.get('B'), 'somethingelse') self.assertFalse('sleep' in env.keys())