def testMkReportObfuscSimple(self): argv = ["-o", "simple"] options = Carl.parse_cmdline(argv)[0] ret = Carl.mkreport(options, self.stats) for (key, value) in self.stats["ip2hname"].items(): self.assertNotIn(key, ret) self.assertNotIn(value, ret)
def testGarbage(): mocked_sys_stderr = mock.MagicMock() mocked_sys_stderr.write = mock.MagicMock() saved_sys_exit = Carl.sys.exit saved_stderr = Carl.sys.stderr Carl.sys.stderr = mocked_sys_stderr Carl.parsedata("I have no log and I must scream.") Carl.sys.exit = saved_sys_exit # We don't really care about _what_ was written. Or anything at all. Carl.sys.stderr = saved_stderr
def testParseCmdlineVerboseWithStdin(self): argv = ["-o", "fancy", "-r"] (options, _, errmsgs) = Carl.parse_cmdline(argv) self.assertEqual(options.shortoutput, False) self.assertEqual(options.ostyle, "fancy") self.assertEqual(options.reverse, True) self.assertEqual(errmsgs, [])
def testParseCmdline(self): argv = ["-o", "fancy", "-r", "-s"] (options, msgs, errmsgs) = Carl.parse_cmdline(argv) self.assertEqual(options.shortoutput, True) self.assertEqual(options.ostyle, "fancy") self.assertEqual(options.reverse, True) self.assertEqual(msgs, []) self.assertEqual(errmsgs, [])
def testObFancy(self): golden = [ ("eenie", "b7259125...fd872fa5"), ("meenie", "9d9b3703...5ae754db"), ("miney", "3751a98b...89aad47b"), ("moe", "1b9fd716...9196f82f"), ] for (inp, out) in golden: self.assertEqual(out, Carl.obfuscate(inp, "fancy"))
def testMkReport(self): argv = [] options = Carl.parse_cmdline(argv)[0] ret = Carl.mkreport(options, self.stats) for (key, value) in self.stats["ip2hname"].items(): self.assertIn(key, ret) self.assertIn(value, ret) self.assertIn("Total traffic: 72.00 GBytes", ret) self.assertIn("Total number of sessions: 1000", ret) self.assertIn( "Total number of unique IPs: %i" % len(self.stats["ip2hname"]), ret) self.assertIn("Log seems to span 86400.00 days.", ret) self.assertIn("account for 60 sessions", ret) self.assertIn( "1000000000 lines in 1.00 seconds, " "1000000000.00 lines per second", ret)
def testObNone(self): golden = [ "127.0.0.1", "192.168.65.3", "172.19.22.4", "2001::a:b:c:d", "2001:db8:a:b:c::", ] for inp in golden: self.assertEqual(inp, Carl.obfuscate(inp, ""))
def testObSimple(self): golden = [ ("127.0.0.1", "127.0..."), ("192.168.65.3", "192.168..."), ("172.19.22.4", "172.19..."), ("10.4.2.65", "10.4..."), ("2001::a:b:c:d", "2001:..."), ("2001:db8:a:b:c::", "2001:db8..."), ("::1", "::1"), ] for (inp, out) in golden: self.assertEqual(out, Carl.obfuscate(inp, "simple"))
def testCrunchSIDev(self): golden = [ (1, (1, 0)), (1000, (1, 1)), (1001, (1.001, 1)), (4000, (4, 1)), (8000, (8, 1)), (10000, (10, 1)), (894352, (894.352, 1)), (3465298345823, (3.465298345823, 4)), ] for (inp, out) in golden: (res, mag) = Carl.crunch(inp, 1000) self.assertAlmostEqual(res, out[0]) self.assertEqual(mag, out[1])
def testCrunchDefDiv(self): golden = [ (1, (1, 0)), (512, (512, 0)), (1024, (1, 1)), (1025, (1.0009765625, 1)), (4096, (4, 1)), (8192, (8, 1)), (10240, (10, 1)), (894352, (873.390625, 1)), (3465298345823, (3.1516704855, 4)), ] for (inp, out) in golden: (res, mag) = Carl.crunch(inp) self.assertAlmostEqual(res, out[0]) self.assertEqual(mag, out[1])
def testNumbersWithCommas(): inp = open("testdata/test_commas_in_numbers.log").read() Carl.parsedata(inp)
def testSession(): inp = open("testdata/test_snippet1.log").read() Carl.parsedata(inp)
def testGarbageMulti(): Carl.parsedata("Demogorgon")
def testNoLines(): Carl.parsedata("")