summaryrefslogtreecommitdiffstats
path: root/test/puppet/tc_suidmanager.rb
blob: b157080ca295777f87a9c44bd31d74c34cecc93b (plain)
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
#!/usr/bin/env ruby -I../lib -I../../lib

require 'puppet'
require 'puppettest'
require 'test/unit'

class TestSUIDManager < Test::Unit::TestCase
    include PuppetTest

    def setup
        if Process.uid != 0
            warn "Process tests must be run as root"
            @run = false
        else 
            @run = true
        end
        super
    end

    def test_metaprogramming_function_additions
        # NOTE: the way that we are dynamically generating the methods in SUIDManager for
        # the UID/GID calls was causing problems due to the modification
        # of a closure. Should the bug rear itself again, this test
        # will fail.
        assert_nothing_raised do
            Puppet::SUIDManager.uid
            Puppet::SUIDManager.uid
        end
    end

    def test_id_set
        if @run
            user = nonrootuser
            assert_nothing_raised do
                Puppet::SUIDManager.egid = user.gid
                Puppet::SUIDManager.euid = user.uid
            end
            
            assert_equal(Puppet::SUIDManager.euid, Process.euid)
            assert_equal(Puppet::SUIDManager.egid, Process.egid)

            assert_nothing_raised do
                Puppet::SUIDManager.euid = 0
                Puppet::SUIDManager.egid = 0
            end

            assert_uid_gid(user.uid, user.gid, tempfile)
        end
    end
    def test_utiluid
        user = nonrootuser.name
        if @run
            assert_not_equal(nil, Puppet::Util.uid(user))
        end
    end
    def test_asuser
        if @run
            user = nonrootuser
            uid, gid = [nil, nil]

            assert_nothing_raised do
                Puppet::SUIDManager.asuser(user.uid, user.gid) do 
                    uid = Puppet::SUIDManager.euid
                    gid = Puppet::SUIDManager.egid
                end
            end
            assert_equal(user.uid, uid)
            assert_equal(user.gid, gid)
        end
    end
    def test_system
        # NOTE: not sure what shells this will work on..
        if @run 
            user = nonrootuser
            status = Puppet::SUIDManager.system("exit $EUID", user.uid, user.gid)
            assert_equal(status.exitstatus, user.uid)
        end
    end

    def test_run_and_capture
        if (RUBY_VERSION <=> "1.8.4") < 0
            warn "Cannot run this test on ruby < 1.8.4"
        else
            # NOTE: because of the way that run_and_capture currently 
            # works, we cannot just blindly echo to stderr. This little
            # hack gets around our problem, but the real problem is the
            # way that run_and_capture works.
            output = Puppet::SUIDManager.run_and_capture("ruby -e '$stderr.puts \"foo\"'")[0].chomp
            assert_equal(output, 'foo')
        end
    end
end

# $Id:$