Example #1
0
        public void ProcessManager_Kill_By_Process_Name()
        {
            {
                var process = StartProcess();

                var uid = Syscall.GetEffectiveUserId();
                Assert.NotNull(process);
                Linux.ProcessManager.Instance.Kill("sleep", (uint)uid, ProcessSignal.SIGKILL, e => _testOutput.WriteLine(e.Message));

                Waiter.Wait(() => Linux.ProcessManager.GetProcessInfoById(process.Id) == null, TimeSpan.FromSeconds(2));
                var actual = Linux.ProcessManager.GetProcessInfoById(process.Id);
                Assert.Null(actual);
            }

            {
                var process = StartProcess();
                var name    = Syscall.GetPasswdByUserId(Syscall.GetEffectiveUserId()).pw_name;
                Assert.NotNull(process);
                Linux.ProcessManager.Instance.Kill("sleep", name, ProcessSignal.SIGKILL, e => _testOutput.WriteLine(e.Message));
                Waiter.Wait(() => Linux.ProcessManager.GetProcessInfoById(process.Id) == null, TimeSpan.FromSeconds(2));
                var actual = Linux.ProcessManager.GetProcessInfoById(process.Id);
                Assert.Null(actual);
            }

            {
                var process = StartProcess();
                Assert.NotNull(process);
                Linux.ProcessManager.Instance.Kill(_ => _.ProcessName == "sleep", ProcessSignal.SIGKILL, e => _testOutput.WriteLine(e.Message));
                Waiter.Wait(() => Linux.ProcessManager.GetProcessInfoById(process.Id) == null, TimeSpan.FromSeconds(2));
                var actual = Linux.ProcessManager.GetProcessInfoById(process.Id);
                Assert.Null(actual);
            }
        }
        public void GetPasswdByUserName_Should_Retrieve_Passwd_Thread_Safe()
        {
            var simpleUserId = GetSimpleUserId();
            var rootId       = 0;
            var expected     = Syscall.GetPasswdByUserId(simpleUserId);
            var rootExpected = Syscall.GetPasswdByUserId(rootId);

            var tasksCount = 1024;
            var tasks      = new Task <KeyValuePair <string, Syscall.Passwd> > [tasksCount];

            for (var i = 0; i < tasksCount; i++)
            {
                tasks[i] = i % 2 == 0
                    ? new Task <KeyValuePair <string, Syscall.Passwd> >(() => new KeyValuePair <string, Syscall.Passwd>(expected.pw_name, Syscall.GetPasswdByUserName(expected.pw_name)))
                    :new Task <KeyValuePair <string, Syscall.Passwd> >(() => new KeyValuePair <string, Syscall.Passwd>(rootExpected.pw_name, Syscall.GetPasswdByUserName(rootExpected.pw_name)));
            }
            tasks.ForEach(_ => _.Start());
            var actual = Task.WhenAll(tasks).Result;

            var root   = actual.Where(_ => _.Key == rootExpected.pw_name);
            var simple = actual.Where(_ => _.Key == expected.pw_name);

            Assert.All(root, _ => AssertEqual(rootExpected, _.Value));
            Assert.All(simple, _ => AssertEqual(expected, _.Value));
        }
        public void GetPasswdByUserId_Should_Return_Passwd_Info()
        {
            var actual = Syscall.GetPasswdByUserId(0);

            Assert.Equal("root", actual.pw_name);
            Assert.Equal("root", actual.pw_gecos);
            Assert.Equal("/root", actual.pw_dir);
            Assert.Equal("x", actual.pw_passwd);
            Assert.Equal("/bin/bash", actual.pw_shell);
            Assert.Equal(0u, actual.pw_uid);
            Assert.Equal(0u, actual.pw_gid);
        }
        public void GetPasswdByUserId_Should_Return_Empty_If_Passwd_Not_Found()
        {
            var expected = new Syscall.Passwd();
            var actual   = Syscall.GetPasswdByUserId(-1);

            Assert.Equal(expected.pw_uid, actual.pw_uid);
            Assert.Equal(expected.pw_gid, actual.pw_gid);
            Assert.Equal(expected.pw_gecos, actual.pw_gecos);
            Assert.Equal(expected.pw_dir, actual.pw_dir);
            Assert.Equal(expected.pw_name, actual.pw_name);
            Assert.Equal(expected.pw_passwd, actual.pw_passwd);
            Assert.Equal(expected.pw_shell, actual.pw_shell);
        }