File: shell_test.go

package info (click to toggle)
mender-connect 2.3.0%2Bds1-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 700 kB
  • sloc: makefile: 123; ansic: 66; sh: 21
file content (110 lines) | stat: -rw-r--r-- 3,177 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright 2021 Northern.tech AS
//
//    Licensed under the Apache License, Version 2.0 (the "License");
//    you may not use this file except in compliance with the License.
//    You may obtain a copy of the License at
//
//        http://www.apache.org/licenses/LICENSE-2.0
//
//    Unless required by applicable law or agreed to in writing, software
//    distributed under the License is distributed on an "AS IS" BASIS,
//    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//    See the License for the specific language governing permissions and
//    limitations under the License.
package shell

import (
	"os"
	"os/user"
	"strconv"
	"syscall"
	"testing"
	"time"

	"github.com/stretchr/testify/assert"

	"github.com/mendersoftware/mender-connect/procps"
)

func TestMenderShellExecShell(t *testing.T) {
	currentUser, err := user.Current()
	if err != nil {
		t.Errorf("cant get current user: %s", err.Error())
		return
	}
	uid, err := strconv.ParseUint(currentUser.Uid, 10, 32)
	if err != nil {
		t.Errorf("cant get current uid: %s", err.Error())
		return
	}

	gid, err := strconv.ParseUint(currentUser.Gid, 10, 32)
	if err != nil {
		t.Errorf("cant get current gid: %s", err.Error())
		return
	}

	//command does not exist
	pid, pseudoTTY, cmd, err := ExecuteShell(uint32(uid), uint32(gid), "/", "thatissomethingthatdoesnotexecute", "xterm-256color", 24, 80, []string{"--login"})
	assert.Error(t, err)
	assert.Equal(t, pid, -1)
	assert.Nil(t, pseudoTTY)
	assert.Nil(t, cmd)

	//home directory doesn't exist
	pid, pseudoTTY, cmd, err = ExecuteShell(uint32(uid), uint32(gid), "/does-not-exist", "true", "xterm-256color", 24, 80, []string{"--login"})
	assert.Nil(t, err)
	assert.NotZero(t, pid)
	assert.NotNil(t, pseudoTTY)
	assert.Equal(t, "/", cmd.Dir)

	// Empty ShellArguments
	pid, pseudoTTY, cmd, err = ExecuteShell(uint32(uid), uint32(gid), "/", "thatissomethingthatdoesnotexecute", "xterm-256color", 24, 80, []string{""})
	assert.Error(t, err)
	assert.Equal(t, pid, -1)
	assert.Nil(t, pseudoTTY)
	assert.Nil(t, cmd)

	// Bogus ShellArguments
	pid, pseudoTTY, cmd, err = ExecuteShell(uint32(uid), uint32(gid), "/", "thatissomethingthatdoesnotexecute", "xterm-256color", 24, 80, []string{"--i-do-not-exist-flag"})
	assert.Error(t, err)
	assert.Equal(t, pid, -1)
	assert.Nil(t, pseudoTTY)
	assert.Nil(t, cmd)

	//shell
	pid, pseudoTTY, cmd, err = ExecuteShell(uint32(uid), uint32(gid), "/tmp", "/bin/sh", "xterm-256color", 24, 80, []string{"--login"})
	assert.Nil(t, err)
	assert.NotZero(t, pid)
	assert.NotNil(t, pseudoTTY)
	assert.Equal(t, "/tmp", cmd.Dir)

	t.Logf("started shell, pid: %d", pid)

	p, err := os.FindProcess(pid)
	t.Logf("FindProcess p: %v err: %v", p, err)
	assert.Nil(t, err)
	assert.NotNil(t, p)
	p.Signal(syscall.SIGHUP)
	time.Sleep(time.Second)
	pseudoTTY.Close()
	p.Signal(syscall.SIGTERM)
	time.Sleep(time.Second)
	err = p.Signal(syscall.SIGKILL)
	time.Sleep(time.Second)

	done := make(chan error, 1)
	go func() {
		done <- cmd.Wait()
	}()
	select {
	case err := <-done:
		if err != nil {
		}
	}
	time.Sleep(time.Second)

	if procps.ProcessExists(pid) {
		t.Logf("process is still running after kill -9")
	}
}