1"""
2Test target commands: target.auto-install-main-executable.
3"""
4
5import socket
6import time
7import lldbgdbserverutils
8
9from lldbsuite.test.decorators import *
10from lldbsuite.test.lldbtest import *
11from lldbsuite.test import lldbutil
12
13
14class TestAutoInstallMainExecutable(TestBase):
15    mydir = TestBase.compute_mydir(__file__)
16    NO_DEBUG_INFO_TESTCASE = True
17
18    @skipIfRemote
19    def test_target_auto_install_main_executable(self):
20        if lldbgdbserverutils.get_lldb_server_exe() is None:
21          self.skipTest("lldb-server not found")
22        self.build()
23
24        hostname = socket.getaddrinfo("localhost", 0, proto=socket.IPPROTO_TCP)[0][4][0]
25        listen_url = "[%s]:0"%hostname
26
27        port_file = self.getBuildArtifact("port")
28        commandline_args = [
29            "platform",
30            "--listen",
31            listen_url,
32            "--socket-file",
33            port_file]
34        self.spawnSubprocess(
35            lldbgdbserverutils.get_lldb_server_exe(),
36            commandline_args)
37
38        socket_id = lldbutil.wait_for_file_on_target(self, port_file)
39
40        new_platform = lldb.SBPlatform("remote-" + self.getPlatform())
41        self.dbg.SetSelectedPlatform(new_platform)
42
43        connect_url = "connect://[%s]:%s" % (hostname, socket_id)
44        connect_opts = lldb.SBPlatformConnectOptions(connect_url)
45        self.assertSuccess(new_platform.ConnectRemote(connect_opts))
46
47        wd = self.getBuildArtifact("wd")
48        os.mkdir(wd)
49        new_platform.SetWorkingDirectory(wd)
50
51
52        # Manually install the modified binary.
53        src_device = lldb.SBFileSpec(self.getBuildArtifact("a.device.out"))
54        dest = lldb.SBFileSpec(os.path.join(wd, "a.out"))
55        self.assertSuccess(new_platform.Put(src_device, dest))
56
57        # Test the default setting.
58        self.expect("settings show target.auto-install-main-executable",
59                substrs=["target.auto-install-main-executable (boolean) = true"],
60                msg="Default settings for target.auto-install-main-executable failed.")
61
62        # Disable the auto install.
63        self.runCmd("settings set target.auto-install-main-executable false")
64        self.expect("settings show target.auto-install-main-executable",
65            substrs=["target.auto-install-main-executable (boolean) = false"])
66
67        # Create the target with the original file.
68        self.runCmd("target create --remote-file %s %s "%
69                                        (dest.fullpath,
70                                            self.getBuildArtifact("a.out")))
71
72        self.expect("process launch", substrs=["exited with status = 74"])
73