summaryrefslogtreecommitdiffstats
path: root/Monitoring/MonitoringService/Credential/test.py
blob: 04a59f3339092d56db9151419a0ec451ab50dcb2 (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
'''
Created on Aug 10, 2011

@author: steger
'''
import unittest
from os import close, unlink
from tempfile import mkstemp
from subprocess import Popen, PIPE
from SshKeygen import SshKeygen

class Test(unittest.TestCase):
    sshkeygen = '/usr/bin/ssh-keygen'

    def test_sshkeygen(self):
        # generate a pair of RSA keys
        self.key = SshKeygen()
        
        # save keys in a file 
        fid, fn = mkstemp(suffix = "_rsa")
        close(fid)
        unlink(fn)
        self.fn_private = fn
        self.fn_public = "%s.pub" % fn 
        self.key.save_private_key(self.fn_private)
        self.key.save_public_key(self.fn_public)

        # Test the base64 format of the public key.
        # convert and compare private key using ssh-keygen
        proc = Popen(args = [self.sshkeygen, '-y', '-f', self.fn_private], stdout = PIPE)
        converted = str(proc.communicate(input = None)[0])
        expected = "ssh-rsa %s\n" % self.key.public
        self.assertEqual(expected, converted, "Base64 encoded public RSA key differs from the one generated by %s" % self.sshkeygen)

        # Test SshKeygen objects convert_key_from_file method.
        expected = self.key.public
        converted = SshKeygen.convert_key_from_file(self.fn_private)
        self.assertEqual(expected, converted, "Base64 encoded public RSA key generated from file %s differs from expected" % self.fn_private)

        # remove generated files
        unlink(self.fn_private)
        unlink(self.fn_public)

if __name__ == "__main__":
    #import sys;sys.argv = ['', 'Test.test_sshkeygen']
    unittest.main()