示例#1
0
 def test_process_once(self, p, q):
     main.main(
         ("--cwd", cwd(), "process", "-r", "1234"),
         standalone_mode=False
     )
     p.assert_called_once_with("1234")
     q.assert_called_once()
示例#2
0
 def test_submit_abort(self, p, capsys):
     p.side_effect = KeyboardInterrupt
     main.main((
         "--cwd", cwd(), "submit", Files.create(cwd(), "a.txt", "hello")
     ), standalone_mode=False)
     out, _ = capsys.readouterr()
     assert "Aborting submission of" in out
示例#3
0
 def test_community(self):
     with mock.patch("cuckoo.main.fetch_community") as p:
         p.return_value = None
         main.main(("--cwd", cwd(), "community"), standalone_mode=False)
         p.assert_called_once_with(
             force=False, branch="master", filepath=None
         )
示例#4
0
 def test_process_many(self, p, q):
     main.main(
         ("--cwd", cwd(), "process", "instance"),
         standalone_mode=False
     )
     p.assert_called_once_with("instance", 0)
     q.assert_called_once()
示例#5
0
    def test_import_noconfirm(self, p):
        set_cwd(tempfile.mkdtemp())
        p.side_effect = True, False

        dirpath = init_legacy_analyses()
        os.makedirs(os.path.join(dirpath, "lib", "cuckoo", "common"))
        open(os.path.join(
            dirpath, "lib", "cuckoo", "common", "constants.py"
        ), "wb").write(constants_11_py)

        shutil.copytree(
            "tests/files/conf/110_plain", os.path.join(dirpath, "conf")
        )

        filepath = os.path.join(dirpath, "conf", "cuckoo.conf")
        buf = open(filepath, "rb").read()
        open(filepath, "wb").write(buf.replace(
            "connection =", "connection = %s" % self.URI
        ))

        main.main(
            ("--cwd", cwd(), "import", dirpath), standalone_mode=False
        )

        db = Database()
        db.connect()
        assert db.engine.name == self.ENGINE
        assert open(cwd("logs", "a.txt", analysis=1), "rb").read() == "a"
        assert config("cuckoo:database:connection") == self.URI
        assert db.count_tasks() == 2
示例#6
0
    def test_import_confirm(self, p):
        set_cwd(tempfile.mkdtemp())
        p.return_value = True

        dirpath = init_legacy_analyses()
        os.makedirs(os.path.join(dirpath, "lib", "cuckoo", "common"))
        open(os.path.join(
            dirpath, "lib", "cuckoo", "common", "constants.py"
        ), "wb").write(constants_11_py)

        shutil.copytree(
            "tests/files/conf/110_plain", os.path.join(dirpath, "conf")
        )

        filepath = os.path.join(dirpath, "conf", "cuckoo.conf")
        buf = open(filepath, "rb").read()
        open(filepath, "wb").write(buf.replace(
            "connection =", "connection = %s" % self.URI
        ))

        try:
            main.main(
                ("--cwd", cwd(), "import", dirpath), standalone_mode=False
            )
        except CuckooOperationalError as e:
            assert "SQL database dump as the command" in e.message
            assert not is_linux()
            return

        db = Database()
        db.connect()
        assert db.engine.name == self.ENGINE
        assert open(cwd("logs", "a.txt", analysis=1), "rb").read() == "a"
        assert config("cuckoo:database:connection") == self.URI
        assert db.count_tasks() == 2
示例#7
0
def test_process_json_logging():
    set_cwd(tempfile.mkdtemp())
    cuckoo_create()
    init_yara()
    init_logfile("process-p0.json")

    def process_tasks(instance, maxcount):
        logger("foo bar", action="hello.world", status="success")

    with mock.patch("cuckoo.main.Database"):
        with mock.patch("cuckoo.main.process_tasks") as p1:
            with mock.patch("time.time") as p2:
                p1.side_effect = process_tasks
                p2.return_value = 1484232003
                main.main(
                    ("--cwd", cwd(), "process", "p0"), standalone_mode=False
                )

    assert json.load(open(cwd("log", "process-p0.json"), "rb")) == {
        "asctime": mock.ANY,
        "action": "hello.world",
        "level": "info",
        "message": "foo bar",
        "status": "success",
        "task_id": None,
        "time": 1484232003,
    }
示例#8
0
 def test_cuckoo_init_main(self):
     """Tests that 'cuckoo' works with a new CWD."""
     main.main(
         ("--cwd", cwd(), "--nolog"),
         standalone_mode=False
     )
     assert os.path.exists(os.path.join(cwd(), "mitm.py"))
示例#9
0
    def test_cuckoo_init_no_resultserver(self):
        """Tests that 'cuckoo init' doesn't launch the ResultServer."""
        with pytest.raises(SystemExit):
            main.main(
                ("--cwd", cwd(), "--nolog", "init"),
                standalone_mode=False
            )

        # We copy the monitor binary directory over from user-CWD (which is
        # also present in the Travis CI environment, etc) as otherwise the
        # following call will raise an exception about not having found the
        # monitoring binaries.
        shutil.rmtree(os.path.join(cwd(), "monitor"))
        shutil.copytree(
            os.path.expanduser("~/.cuckoo/monitor"),
            os.path.join(cwd(), "monitor")
        )

        # Raises CuckooCriticalError if ResultServer can't bind (which no
        # longer happens now, naturally).
        main.main(
            ("--cwd", cwd(), "--nolog", "init"),
            standalone_mode=False
        )

        assert ResultServer not in Singleton._instances
示例#10
0
 def test_main_exception(self, p, q):
     q.side_effect = Exception("this is a test")
     with pytest.raises(SystemExit):
         main.main(
             ("--cwd", cwd(), "-d", "--nolog"), standalone_mode=False
         )
     p.exception.assert_called_once()
示例#11
0
    def migrate(cls):
        tasks = cls.d.engine.execute(
            "SELECT status FROM tasks ORDER BY id"
        ).fetchall()
        assert tasks[0][0] == "success"
        assert tasks[1][0] == "processing"
        assert tasks[2][0] == "pending"

        main.main(
            ("--cwd", cwd(), "migrate", "--revision", "263a45963c72"),
            standalone_mode=False
        )

        tasks = cls.d.engine.execute(
            "SELECT status FROM tasks ORDER BY id"
        ).fetchall()
        assert tasks[0][0] == "completed"
        assert tasks[1][0] == "running"
        assert tasks[2][0] == "pending"

        main.main(
            ("--cwd", cwd(), "migrate"),
            standalone_mode=False
        )

        tasks = cls.d.engine.execute(
            "SELECT status, owner FROM tasks ORDER BY id"
        ).fetchall()
        assert tasks[0][0] == "completed"
        assert tasks[0][1] is None
        assert tasks[1][0] == "running"
        assert tasks[2][0] == "pending"
示例#12
0
 def test_dist_server(self):
     with mock.patch("cuckoo.main.cuckoo_distributed") as p:
         p.return_value = None
         main.main(
             ("--cwd", cwd(), "distributed", "server"),
             standalone_mode=False
         )
         p.assert_called_once_with("localhost", 9003, False)
示例#13
0
    def test_main(self, p, q):
        p.side_effect = SystemExit(0)

        # Ensure that the "latest" binary value makes sense so that the
        # "run community command" exception is not thrown.
        mkdir(cwd("monitor", open(cwd("monitor", "latest")).read().strip()))
        main.main(("--cwd", cwd(), "-d", "--nolog"), standalone_mode=False)
        q.assert_called_once()
示例#14
0
 def test_dist_instance(self):
     with mock.patch("cuckoo.main.cuckoo_distributed_instance") as p:
         p.return_value = None
         main.main(
             ("--cwd", cwd(), "distributed", "instance", "name"),
             standalone_mode=False
         )
         p.assert_called_once_with("name")
示例#15
0
 def test_import_noconfirm(self, p, capsys):
     p.return_value = False
     with pytest.raises(SystemExit) as e:
         main.main(
             ("--cwd", cwd(), "import", tempfile.mkdtemp()),
             standalone_mode=False
         )
     e.match("Aborting operation")
示例#16
0
 def test_cuckoo_init_main_nosigs(self, p):
     """Ensure load_signatures() isn't called for 'cuckoo' with new CWD."""
     main.main(
         ("--cwd", cwd(), "--nolog"),
         standalone_mode=False
     )
     assert os.path.exists(os.path.join(cwd(), "mitm.py"))
     p.assert_not_called()
示例#17
0
 def test_process_abort(self, p, q, capsys):
     p.side_effect = KeyboardInterrupt
     main.main(
         ("--cwd", cwd(), "process", "-r", "1234"),
         standalone_mode=False
     )
     out, _ = capsys.readouterr()
     assert "Aborting (re-)processing of your analyses" in out
示例#18
0
def test_init(p):
    set_cwd(tempfile.mkdtemp())
    with pytest.raises(SystemExit):
        main.main(
            ("--cwd", cwd(), "--nolog", "init"),
            standalone_mode=False
        )
    p.assert_not_called()
示例#19
0
 def test_import_abort(self, p, q, capsys):
     p.side_effect = KeyboardInterrupt
     q.return_value = True
     main.main(
         ("--cwd", cwd(), "import", tempfile.mkdtemp()),
         standalone_mode=False
     )
     out, _ = capsys.readouterr()
     assert "Aborting import of Cuckoo instance" in out
示例#20
0
    def test_web_noargs(self, p):
        curdir = os.getcwd()

        main.main(("--cwd", cwd(), "web"), standalone_mode=False)
        p.assert_called_once_with(
            ("cuckoo", "runserver", "localhost:8000")
        )

        os.chdir(curdir)
示例#21
0
    def test_web_args(self, p):
        curdir = os.getcwd()

        main.main(
            ("--cwd", cwd(), "web", "foo", "bar"),
            standalone_mode=False
        )
        p.assert_called_once_with(("cuckoo", "foo", "bar"))

        os.chdir(curdir)
示例#22
0
 def test_dist_migrate(self):
     with mock.patch("cuckoo.main.subprocess.check_call") as p:
         p.return_value = None
         main.main(
             ("--cwd", cwd(), "distributed", "migrate"),
             standalone_mode=False
         )
         p.assert_called_once_with(
             ["alembic", "-x", "cwd=%s" % cwd(), "upgrade", "head"],
             cwd=cwd("distributed", "migration", private=True)
         )
示例#23
0
 def test_import_confirm(self, p, q, capsys):
     p.return_value = None
     q.return_value = True
     dirpath = tempfile.mkdtemp()
     main.main(
         ("--cwd", cwd(), "import", dirpath),
         standalone_mode=False
     )
     p.assert_called_once_with(None, "copy", dirpath)
     out, err = capsys.readouterr()
     assert "understand that, depending on the mode" in out
示例#24
0
    def test_machine(self):
        with mock.patch("cuckoo.main.cuckoo_machine") as p:
            p.return_value = None
            main.main((
                "--cwd", cwd(), "machine", "cuckoo2", "1.2.3.4", "--add"
            ), standalone_mode=False)

            p.assert_called_once_with(
                "cuckoo2", "add", "1.2.3.4", "windows",
                None, None, None, None, None
            )
示例#25
0
    def test_process_task(self, q, p1, p2):
        mkdir(cwd(analysis=1))
        p1.return_value.view_task.return_value = {}
        main.main(
            ("--cwd", cwd(), "process", "-r", "1"),
            standalone_mode=False
        )

        q.assert_called_once()
        p2.return_value.connect.assert_called_once()
        p1.return_value.connect.assert_not_called()
示例#26
0
def test_import_cuckoo_cwd(capsys):
    set_cwd(tempfile.mkdtemp())
    cuckoo_create()

    with pytest.raises(SystemExit):
        main.main(
            ("--cwd", cwd(), "import", cwd()), standalone_mode=False
        )

    out, _ = capsys.readouterr()
    assert "import a legacy Cuckoo" in out
示例#27
0
    def test_cuckoo_init_kv_conf(self):
        filepath = Files.temp_put(
            "cuckoo.cuckoo.version_check = no"
        )

        # Create a new CWD as Files.temp_put() indexes - or tries to - the
        # original cuckoo.conf (even though it doesn't exist yet).
        set_cwd(tempfile.mkdtemp())
        with pytest.raises(SystemExit):
            main.main(
                ("--cwd", cwd(), "init", "--conf", filepath),
                standalone_mode=False
            )

        assert config("cuckoo:cuckoo:version_check") is False
示例#28
0
    def test_cuckoo_init(self):
        """Tests that 'cuckoo init' works with a new CWD."""
        with pytest.raises(SystemExit):
            main.main(
                ("--cwd", cwd(), "--nolog", "init"),
                standalone_mode=False
            )

        assert os.path.exists(os.path.join(cwd(), "mitm.py"))
        assert os.path.exists(os.path.join(cwd(), "conf"))
        assert os.path.exists(os.path.join(cwd(), "storage"))
        assert os.path.exists(os.path.join(cwd(), "storage", "binaries"))
        assert os.path.exists(os.path.join(cwd(), "storage", "analyses"))
        assert os.path.exists(os.path.join(cwd(), "storage", "baseline"))
        assert os.path.exists(os.path.join(cwd(), "log"))
示例#29
0
    def test_process_tasks(self, q, p1, p2):
        p1.return_value.processing_get_task.side_effect = 1, 2
        p1.return_value.view_task.side_effect = [
            Task(id=1, category="url", target="http://google.com/"),
            Task(id=2, category="url", target="http://google.nl/"),
        ]

        main.main(
            ("--cwd", cwd(), "process", "p0"),
            standalone_mode=False
        )

        assert q.call_count == 2
        p2.return_value.connect.assert_called_once()
        p1.return_value.connect.assert_not_called()
示例#30
0
def test_submit_unique_with_duplicates(p, q, capsys):
    set_cwd(tempfile.mkdtemp())
    cuckoo_create()

    p.return_value = [
        ("category", "target", 1),
        ("category", "target", None),
    ]

    main.main(
        ("--cwd", cwd(), "submit", "--unique", "a", "b"),
        standalone_mode=False
    )
    out, err = capsys.readouterr()
    assert "added as task with" in out
    assert "Skipped" in out
示例#31
0
 def test_dnsserve_abort(self, p, capsys):
     p.side_effect = KeyboardInterrupt
     main.main(("--cwd", cwd(), "dnsserve"), standalone_mode=False)
     out, _ = capsys.readouterr()
     assert "Aborting Cuckoo DNS Serve" in out
示例#32
0
 def test_cuckoo_init_main(self):
     """Tests that 'cuckoo' works with a new CWD."""
     main.main(("--cwd", cwd(), "--nolog"), standalone_mode=False)
     assert os.path.exists(os.path.join(cwd(), "stuff", "mitm.py"))
示例#33
0
 def test_process_init_modules(self, p, q):
     main.main(
         ("--cwd", cwd(), "process", "-r", "1"),
         standalone_mode=False
     )
     p.assert_called_once()
示例#34
0
 def test_api(self):
     with mock.patch("cuckoo.main.cuckoo_api") as p:
         p.return_value = None
         main.main(("--cwd", cwd(), "api"), standalone_mode=False)
         p.assert_called_once_with("localhost", 8090, False)
示例#35
0
 def test_rooter_abort(self, p, capsys):
     p.side_effect = KeyboardInterrupt
     main.main(("--cwd", cwd(), "rooter"), standalone_mode=False)
     out, _ = capsys.readouterr()
     assert "Aborting the Cuckoo Rooter" in out
示例#36
0
 def test_community_abort(self, p, capsys):
     p.side_effect = KeyboardInterrupt
     main.main(("--cwd", cwd(), "community"), standalone_mode=False)
     out, _ = capsys.readouterr()
     assert "Aborting fetching of" in out
示例#37
0
 def test_clean(self):
     with mock.patch("cuckoo.main.cuckoo_clean") as p:
         p.return_value = None
         main.main(("--cwd", cwd(), "clean"), standalone_mode=False)
         p.assert_called_once_with()
示例#38
0
 def test_dnsserve(self):
     with mock.patch("cuckoo.main.cuckoo_dnsserve") as p:
         p.return_value = None
         main.main(("--cwd", cwd(), "dnsserve"), standalone_mode=False)
         p.assert_called_once_with("0.0.0.0", 53, None, None)
示例#39
0
 def test_cuckoo_init_main_nosigs(self, p):
     """Ensure load_signatures() isn't called for 'cuckoo' with new CWD."""
     main.main(("--cwd", cwd(), "--nolog"), standalone_mode=False)
     assert os.path.exists(os.path.join(cwd(), "stuff", "mitm.py"))
     p.assert_not_called()
示例#40
0
 def migrate(cls):
     main.main(("--cwd", cwd(), "migrate"), standalone_mode=False)
示例#41
0
 def test_submit(self):
     with mock.patch("cuckoo.main.submit_tasks") as p:
         p.return_value = []
         main.main((
             "--cwd", cwd(), "submit", Files.create(cwd(), "a.txt", "hello")
         ), standalone_mode=False)
示例#42
0
 def test_clean_abort(self, p, capsys):
     p.side_effect = KeyboardInterrupt
     main.main(("--cwd", cwd(), "clean"), standalone_mode=False)
     out, _ = capsys.readouterr()
     assert "Aborting cleaning up of" in out