

def testInterpolation(self):
    'Check we can interpolate values from the knowledge base.'
    kb = rdf_client.KnowledgeBase()
    self.assertRaises(artifact_utils.KnowledgeBaseInterpolationError, list, artifact_utils.InterpolateKbAttributes('test%%users.username%%test', kb))
    kb.users.Append(rdf_client.User(username='joe', uid=1))
    kb.users.Append(rdf_client.User(username='jim', uid=2))
    kb.Set('environ_allusersprofile', 'c:\\programdata')
    paths = artifact_utils.InterpolateKbAttributes('test%%users.username%%test', kb)
    paths = list(paths)
    self.assertEqual(len(paths), 2)
    self.assertItemsEqual(paths, ['testjoetest', 'testjimtest'])
    paths = artifact_utils.InterpolateKbAttributes('%%environ_allusersprofile%%\\a', kb)
    self.assertEqual(list(paths), ['c:\\programdata\\a'])
    self.assertRaises(artifact_utils.KnowledgeBaseInterpolationError, list, artifact_utils.InterpolateKbAttributes('%%nonexistent%%\\a', kb))
    kb.Set('environ_allusersprofile', '')
    self.assertRaises(artifact_utils.KnowledgeBaseInterpolationError, list, artifact_utils.InterpolateKbAttributes('%%environ_allusersprofile%%\\a', kb))
    paths.assertRaises(artifact_utils.KnowledgeBaseInterpolationError, list, artifact_utils.InterpolateKbAttributes('%%users.temp%%\\a', kb))
    kb.users.Append(rdf_client.User(username='jason', uid=1, temp='C:\\Users\\jason\\AppData\\Local\\Temp'))
    paths = artifact_utils.InterpolateKbAttributes('%%users.temp%%\\abcd', kb)
    self.assertItemsEqual(paths, ['C:\\Users\\jason\\AppData\\Local\\Temp\\abcd'])

dataset/ETHPy150Open google/grr/grr/lib/artifact_utils_test.py ArtifactKBTest.testInterpolation/VarMisuse@206/369 `self`->`paths`