tor

The Tor anonymity network
git clone https://git.dasho.dev/tor.git
Log | Files | Refs | README | LICENSE

practracker_tests.py (1709B)


      1 #!/usr/bin/env python
      2 
      3 """Some simple tests for practracker metrics"""
      4 
      5 # Future imports for Python 2.7, mandatory in 3.0
      6 from __future__ import division
      7 from __future__ import print_function
      8 from __future__ import unicode_literals
      9 
     10 import unittest
     11 
     12 try:
     13    # python 2 names the module this way...
     14    from StringIO import StringIO
     15 except ImportError:
     16    # python 3 names the module this way.
     17    from io import StringIO
     18 
     19 import metrics
     20 
     21 function_file = """static void
     22 fun(directory_request_t *req, const char *resource)
     23 {
     24  time_t if_modified_since = 0;
     25  uint8_t or_diff_from[DIGEST256_LEN];
     26 }
     27 
     28 static void
     29 fun(directory_request_t *req,
     30      const char *resource)
     31 {
     32  time_t if_modified_since = 0;
     33  uint8_t or_diff_from[DIGEST256_LEN];
     34 }
     35 
     36 MOCK_IMPL(void,
     37 fun,(
     38       uint8_t dir_purpose,
     39       uint8_t router_purpose,
     40       const char *resource,
     41       int pds_flags,
     42       download_want_authority_t want_authority))
     43 {
     44  const routerstatus_t *rs = NULL;
     45  const or_options_t *options = get_options();
     46 }
     47 """
     48 
     49 class TestFunctionLength(unittest.TestCase):
     50    def test_function_length(self):
     51        funcs = StringIO(function_file)
     52        # All functions should have length 2
     53        for name, lines in metrics.get_function_lines(funcs):
     54            self.assertEqual(name, "fun")
     55 
     56        funcs.seek(0)
     57 
     58        for name, lines in metrics.get_function_lines(funcs):
     59            self.assertEqual(lines, 4)
     60 
     61 class TestIncludeCount(unittest.TestCase):
     62    def test_include_count(self):
     63        f = StringIO("""
     64  #   include <abc.h>
     65  #   include "def.h"
     66 #include "ghi.h"
     67 \t#\t include "jkl.h"
     68 """)
     69        self.assertEqual(metrics.get_include_count(f),4)
     70 
     71 if __name__ == '__main__':
     72    unittest.main()