def testRsubAOR(self):
   mutation_analyser = MutationAnalyser()
   key = {'line':'rsub-int v8, v8, -0x2', 'operator':'rsub-'}
   result = mutation_analyser.processAOR(key)
   self.assertEqual(4, len(result))
   with_lit16 = False
   for m in result:
     with_lit16 = 'lit16' in m['mutant']
     self.assertEqual(with_lit16, True)
Exemple #2
0
 def testRsubAOR(self):
     mutation_analyser = MutationAnalyser()
     key = {'line': 'rsub-int v8, v8, -0x2', 'operator': 'rsub-'}
     result = mutation_analyser.processAOR(key)
     self.assertEqual(4, len(result))
     with_lit16 = False
     for m in result:
         with_lit16 = 'lit16' in m['mutant']
         self.assertEqual(with_lit16, True)
Exemple #3
0
def generateMutants(file):
  if not file.endswith('.apk'):
    print 'Input must be an Android Apk file!'
    sys.exit(2)
  
  source_directory = decompress(file, True)

  config_file = os.path.join(file[:-4], 'config')

  if os.path.exists(config_file):
    with open(config_file, 'rb') as handle:
      config = json.load(handle)
  else:
    config = {}
    config['file'] = file[:-4]
    config['package'], config['start_activity'] = readAndroidManifest(source_directory)
    config['instrument_package'] = config['package']
    with open(config_file, 'wb') as handle:
      json.dump(config, handle)

  # path = os.path.join(source_directory, 'smali')
  path = os.path.join(source_directory, 'smali', *config['instrument_package'].split('.')) #TODO: Take paramater or read from file

  mutation_analyser = MutationAnalyser()
  mutants = mutation_analyser.checkMutations(path)

  print "\nNumber of mutants generated: %d\n" % len(mutants)

  selector = MutantsSelector()
  # mutants = selector.randomSampling(mutants)
  # mutants = selector.inlineRandomSmali(mutants)
  # mutants = selector.inlineRandomSource(mutants)
  mutants = selector.equalization(mutants)
  # mutants = selector.patternSelection(mutants)

  print "\nNumber of mutants selected: %d\n" % len(mutants)

  mutants_list = os.path.join(config['file'], 'mutants')
  with open(mutants_list, 'wb') as handle:
    json.dump(mutants, handle)

  for m in mutants:
    # time1 = time.time()
    if 'label' in m:
      file_original = instrument(m['file'], m['line_num'], m['mutant'], m['label_line'], m['label'])
    else:
      file_original = instrument(m['file'], m['line_num'], m['mutant'])
    new_apk_path = compress(file.split('.')[0], m['id'])
    with open(m['file'], 'w') as f:
      f.writelines(file_original)
    signApk(new_apk_path)
    # time_spend = time.time() - time1
    # print 'Spend: %s seconds' % time_spend

  return config['file']
 def testNormalLit16AOR(self):
   mutation_analyser = MutationAnalyser()
   key = {'line':'add-int/lit16 v8, v8, -0x2', 'operator':'add-'}
   result = mutation_analyser.processAOR(key)
   self.assertEqual(4, len(result))
   no_add = False
   for m in result:
     no_add = not 'add-' in m['mutant']
     self.assertEqual(no_add, True)
     if 'rsub-' in m['mutant']:
       self.assertEqual('lit16' not in m['mutant'], True)
 def testNormalAOR(self):
   mutation_analyser = MutationAnalyser()
   key = {'line':'add-int/lit8 v8, v8, -0x2', 'operator':'add-'}
   result = mutation_analyser.processAOR(key)
   self.assertEqual(4, len(result))
   no_add = False
   for m in result:
     no_add = not 'add-' in m['mutant']
     if not no_add:
       break
   self.assertEqual(no_add, True)    
Exemple #6
0
 def testNormalLit16AOR(self):
     mutation_analyser = MutationAnalyser()
     key = {'line': 'add-int/lit16 v8, v8, -0x2', 'operator': 'add-'}
     result = mutation_analyser.processAOR(key)
     self.assertEqual(4, len(result))
     no_add = False
     for m in result:
         no_add = not 'add-' in m['mutant']
         self.assertEqual(no_add, True)
         if 'rsub-' in m['mutant']:
             self.assertEqual('lit16' not in m['mutant'], True)
Exemple #7
0
 def testNormalAOR(self):
     mutation_analyser = MutationAnalyser()
     key = {'line': 'add-int/lit8 v8, v8, -0x2', 'operator': 'add-'}
     result = mutation_analyser.processAOR(key)
     self.assertEqual(4, len(result))
     no_add = False
     for m in result:
         no_add = not 'add-' in m['mutant']
         if not no_add:
             break
     self.assertEqual(no_add, True)
 def testFloatConst(self):
   mutation_analyser = MutationAnalyser()
   test_line = 'const/high16 v2, 0x40e00000    # 7.0f'
   mutation = mutation_analyser.processConst(test_line)
   result = 'const/high16 v2, 0x41000000    # 7.0f'
   self.assertEqual(mutation, result)
 def testDoubleConst(self):
   mutation_analyser = MutationAnalyser()
   test_line = 'const-wide/high16 v4, 0x4020000000000000L    # 8.0'
   mutation = mutation_analyser.processConst(test_line)
   result = 'const-wide/high16 v4, 0x4022000000000000L    # 8.0'
   self.assertEqual(mutation, result)
 def testShortIntegerConst(self):
   mutation_analyser = MutationAnalyser()
   test_line = 'const/4 v0, 0x7'
   mutation = mutation_analyser.processConst(test_line)
   result = 'const/4 v0, -0x8'
   self.assertEqual(mutation, result)
Exemple #11
0
 def testFloatConst(self):
     mutation_analyser = MutationAnalyser()
     test_line = 'const/high16 v2, 0x40e00000    # 7.0f'
     mutation = mutation_analyser.processConst(test_line)
     result = 'const/high16 v2, 0x41000000    # 7.0f'
     self.assertEqual(mutation, result)
Exemple #12
0
 def testDoubleConst(self):
     mutation_analyser = MutationAnalyser()
     test_line = 'const-wide/high16 v4, 0x4020000000000000L    # 8.0'
     mutation = mutation_analyser.processConst(test_line)
     result = 'const-wide/high16 v4, 0x4022000000000000L    # 8.0'
     self.assertEqual(mutation, result)
Exemple #13
0
 def testShortIntegerConst(self):
     mutation_analyser = MutationAnalyser()
     test_line = 'const/4 v0, 0x7'
     mutation = mutation_analyser.processConst(test_line)
     result = 'const/4 v0, -0x8'
     self.assertEqual(mutation, result)