Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

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

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

# Authors: 

#   Rob Crittenden <rcritten@redhat.com> 

# 

# Copyright (C) 2012  Red Hat 

# see file 'COPYING' for use and warranty information 

# 

# This program is free software; you can redistribute it and/or modify 

# it under the terms of the GNU General Public License as published by 

# the Free Software Foundation, either version 3 of the License, or 

# (at your option) any later version. 

# 

# This program is distributed in the hope that it will be useful, 

# but WITHOUT ANY WARRANTY; without even the implied warranty of 

# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 

# GNU General Public License for more details. 

# 

# You should have received a copy of the GNU General Public License 

# along with this program.  If not, see <http://www.gnu.org/licenses/>. 

""" 

Test the `kernel_keyring.py` module. 

""" 

 

from nose.tools import raises, assert_raises  # pylint: disable=E0611 

from ipapython import kernel_keyring 

 

TEST_KEY = 'ipa_test' 

TEST_VALUE = 'abc123' 

UPDATE_VALUE = '123abc' 

 

SIZE_256 = 'abcdefgh' * 32 

SIZE_512 = 'abcdefgh' * 64 

SIZE_1024 = 'abcdefgh' * 128 

 

class test_keyring(object): 

    """ 

    Test the kernel keyring interface 

    """ 

 

    def setUp(self): 

        try: 

            kernel_keyring.del_key(TEST_KEY) 

        except ValueError: 

            pass 

        try: 

            kernel_keyring.del_key(SIZE_256) 

        except ValueError: 

            pass 

 

    def test_01(self): 

        """ 

        Add a new key and value, then remove it 

        """ 

        kernel_keyring.add_key(TEST_KEY, TEST_VALUE) 

        result = kernel_keyring.read_key(TEST_KEY) 

        assert(result == TEST_VALUE) 

 

        kernel_keyring.del_key(TEST_KEY) 

 

        # Make sure it is gone 

        try: 

            result = kernel_keyring.read_key(TEST_KEY) 

        except ValueError, e: 

            assert e.message == 'key %s not found' % TEST_KEY 

 

    def test_02(self): 

        """ 

        Delete a non_existent key 

        """ 

        try: 

            kernel_keyring.del_key(TEST_KEY) 

            raise AssertionError('key should not have been deleted') 

        except ValueError: 

            pass 

 

    @raises(ValueError) 

    def test_03(self): 

        """ 

        Add a duplicate key 

        """ 

        kernel_keyring.add_key(TEST_KEY, TEST_VALUE) 

        kernel_keyring.add_key(TEST_KEY, TEST_VALUE) 

 

    def test_04(self): 

        """ 

        Update the value in a key 

        """ 

        kernel_keyring.update_key(TEST_KEY, UPDATE_VALUE) 

        result = kernel_keyring.read_key(TEST_KEY) 

        assert(result == UPDATE_VALUE) 

 

        # Now update it 10 times 

        for i in xrange(10): 

            kernel_keyring.update_key(TEST_KEY, 'test %d' %  i) 

            result = kernel_keyring.read_key(TEST_KEY) 

            assert(result == 'test %d' % i) 

 

        kernel_keyring.del_key(TEST_KEY) 

 

    @raises(ValueError) 

    def test_05(self): 

        """ 

        Read a non-existent key 

        """ 

        result = kernel_keyring.read_key(TEST_KEY) 

 

    def test_06(self): 

        """ 

        See if a key is available 

        """ 

        kernel_keyring.add_key(TEST_KEY, TEST_VALUE) 

 

        result = kernel_keyring.has_key(TEST_KEY) 

        assert(result == True) 

        kernel_keyring.del_key(TEST_KEY) 

 

        result = kernel_keyring.has_key(TEST_KEY) 

        assert(result == False) 

 

    def test_07(self): 

        """ 

        Test a 256-byte key 

        """ 

        kernel_keyring.add_key(SIZE_256, TEST_VALUE) 

        result = kernel_keyring.read_key(SIZE_256) 

        assert(result == TEST_VALUE) 

 

        kernel_keyring.del_key(SIZE_256) 

 

    def test_08(self): 

        """ 

        Test 512-bytes of data 

        """ 

        kernel_keyring.add_key(TEST_KEY, SIZE_512) 

        result = kernel_keyring.read_key(TEST_KEY) 

        assert(result == SIZE_512) 

 

        kernel_keyring.del_key(TEST_KEY) 

 

    def test_09(self): 

        """ 

        Test 1k bytes of data 

        """ 

        kernel_keyring.add_key(TEST_KEY, SIZE_1024) 

        result = kernel_keyring.read_key(TEST_KEY) 

        assert(result == SIZE_1024) 

 

        kernel_keyring.del_key(TEST_KEY)