<commit_before># -*- coding: utf-8 -*-
from ooni.utils import log
from twisted.python import usage
from twisted.internet import defer
from ooni.templates import dnst

class UsageOptions(usage.Options):
    optParameters = [
                     ['target', 't', None, 'Specify a single hostname to query.'],
                     ['expected', 'e', None, 'Speficy file containing expected lookup results'],
                     ]

class DNSLookup(dnst.DNSTest):    
    name = "DNSLookupTest"
    version = 0.1
    
    usageOptions = UsageOptions
   
    def setUp(self):
        self.expected_results = []
        self.dns_servers = []
        if self.input:
            self.hostname = self.input
        elif self.localOptions['target']:
            self.hostname = self.localOptions['target']
        else:
            self.hostname = "torproject.org"

        if self.localOptions['expected']:
            with open (self.localOptions['expected']) as file:
                for line in file:
                    self.expected_results.append(line.strip())
        else:
            self.expected_results = ['172.16.31.10', '172.16.31.10', '192.168.127.12', '192.168.127.12', '172.16.31.10'] 
            
        self.report['expected_results'] = self.expected_results
        
        with open('/etc/resolv.conf') as f:
            for line in f:
                if line.startswith('nameserver'):
                    self.dns_servers.append(line.split(' ')[1].strip())
            self.report['dns_servers'] = self.dns_servers
    
    def verify_results(self, results):
        for result in results:
            if result not in self.expected_results:
                return False
        return True
    
    @defer.inlineCallbacks
    def test_dns_comparison(self):
        """
        Performs A lookup on specified host and matches the results 
        against a set of expected results. When not specified, host and 
        expected results default to "torproject.org" and 
        ['172.16.31.10', '172.16.31.10', '192.168.127.12', '192.168.127.12', '172.16.31.10'].
        
        """
        for s in self.dns_servers:    
            dnsServer = (s, 53)
            results = yield self.performALookup(self.hostname, dnsServer)  
            
            if results:
                if self.verify_results(results):
                    self.report['dns_lookup_status'] = "success"
                    self.report['tampering_found'] = "false"
                    log.msg("TestStatus: [ OK ]")
                else:
                    self.report['dns_lookup_status'] = "warning"
                    self.report['tampering_found'] = "possibly"
                    log.msg("TestStatus: [ FAILED ]")
                    log.msg("TestException: [ unexpected results ]")
                     
        
    @defer.inlineCallbacks
    def test_control_results(self):
        """
        Googles 8.8.8.8 server is queried, in order to generate 
        control data. 
        """
        results = yield self.performALookup(self.hostname, ("8.8.8.8", 53) )
        
        if results:
            self.report['control_results'] = results
<commit_msg>Update expected results<commit_after># -*- coding: utf-8 -*-
from ooni.utils import log
from twisted.python import usage
from twisted.internet import defer
from ooni.templates import dnst

class UsageOptions(usage.Options):
    optParameters = [
                     ['target', 't', None, 'Specify a single hostname to query.'],
                     ['expected', 'e', None, 'Speficy file containing expected lookup results'],
                     ]

class DNSLookup(dnst.DNSTest):    
    name = "DNSLookupTest"
    version = 0.1
    
    usageOptions = UsageOptions
   
    def setUp(self):
        self.expected_results = []
        self.dns_servers = []
        if self.input:
            self.hostname = self.input
        elif self.localOptions['target']:
            self.hostname = self.localOptions['target']
        else:
            self.hostname = "torproject.org"

        if self.localOptions['expected']:
            with open (self.localOptions['expected']) as file:
                for line in file:
                    self.expected_results.append(line.strip())
        else:
            self.expected_results = [
                                    '192.168.127.12',
                                    '172.16.31.10',
                                    '172.16.31.10',
                                    '192.168.127.12',
                                    '192.168.127.12',
                                    '172.16.31.10'
                                    ]
            
        self.report['expected_results'] = self.expected_results
        
        with open('/etc/resolv.conf') as f:
            for line in f:
                if line.startswith('nameserver'):
                    self.dns_servers.append(line.split(' ')[1].strip())
            self.report['dns_servers'] = self.dns_servers
    
    def verify_results(self, results):
        for result in results:
            if result not in self.expected_results:
                return False
        return True
    
    @defer.inlineCallbacks
    def test_dns_comparison(self):
        """
        Performs A lookup on specified host and matches the results 
        against a set of expected results. When not specified, host and 
        expected results default to "torproject.org" and 
        ['172.16.31.10', '172.16.31.10', '192.168.127.12', '192.168.127.12', '172.16.31.10'].
        
        """
        for s in self.dns_servers:    
            dnsServer = (s, 53)
            results = yield self.performALookup(self.hostname, dnsServer)  
            
            if results:
                if self.verify_results(results):
                    self.report['dns_lookup_status'] = "success"
                    self.report['tampering_found'] = "false"
                    log.msg("TestStatus: [ OK ]")
                else:
                    self.report['dns_lookup_status'] = "warning"
                    self.report['tampering_found'] = "possibly"
                    log.msg("TestStatus: [ FAILED ]")
                    log.msg("TestException: [ unexpected results ]")
                     
        
    @defer.inlineCallbacks
    def test_control_results(self):
        """
        Googles 8.8.8.8 server is queried, in order to generate 
        control data. 
        """
        results = yield self.performALookup(self.hostname, ("8.8.8.8", 53) )
        
        if results:
            self.report['control_results'] = results

{'fraction_non_alphanumeric': 0.10937736706559611, 'fraction_numerical': 0.036206635358279046, 'mean_word_length': 1.560899922420481, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 25, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 7, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6907317', 'n_tokens_mistral': 1982, 'n_tokens_neox': 1788, 'n_words': 409}