Improve osusers.models coverage

This commit adds tests for more corner cases of
SshPublicKeyManager.parse_keytext to raise the test coverage to 100%.
The method now handles invalid keys more thoroughly.
This commit is contained in:
Jan Dittberner 2015-12-06 17:47:27 +01:00
parent 8616b2d6c9
commit 28ff099df9
2 changed files with 46 additions and 1 deletions

View file

@ -468,6 +468,8 @@ class SshPublicKeyManager(models.Manager):
except TypeError: except TypeError:
raise ValueError('invalid SSH public key') raise ValueError('invalid SSH public key')
parts = keybytes.split(b'\x00' * 3) parts = keybytes.split(b'\x00' * 3)
if len(parts) < 2:
raise ValueError('invalid SSH public key')
alglength = six.byte2int(parts[1]) alglength = six.byte2int(parts[1])
algname = parts[1][1:1+alglength] algname = parts[1][1:1+alglength]
return algname, data, comment return algname, data, comment

View file

@ -1,3 +1,5 @@
# -*- coding: utf8 -*-
from __future__ import unicode_literals
from datetime import date from datetime import date
from django.conf import settings from django.conf import settings
@ -91,6 +93,19 @@ n24VYtYtsMu74qXviYjziVucWKjjKEb11juqnF0GDlB3VVmxHLmxnAz643WK42Z7dLM5
sY29ouezv4Xz2PuMch5VGPP+CDqzCM4loWgV sY29ouezv4Xz2PuMch5VGPP+CDqzCM4loWgV
---- END SSH2 PUBLIC KEY ----""" ---- END SSH2 PUBLIC KEY ----"""
EXAMPLE_KEY_7_NO_COMMENT = """---- BEGIN SSH2 PUBLIC KEY ----
AAAAB3NzaC1yc2EAAAABIwAAAIEA1on8gxCGJJWSRT4uOrR13mUaUk0hRf4RzxSZ1zRb
YYFw8pfGesIFoEuVth4HKyF8k1y4mRUnYHP1XNMNMJl1JcEArC2asV8sHf6zSPVffozZ
5TT4SfsUu/iKy9lUcCfXzwre4WWZSXXcPff+EHtWshahu3WzBdnGxm5Xoi89zcE=
---- END SSH2 PUBLIC KEY ----"""
EXAMPLE_KEY_8_OPENSSH_BROKEN = "".join((
"ssh-rsa ",
"AschrÖdderöd"
))
EXAMPLE_KEY_9_RFC4716_ONLY_HEADER = "---- BEGIN SSH2 PUBLIC KEY ----"
Customer = get_user_model() Customer = get_user_model()
@ -135,6 +150,13 @@ class AdditionalGroupTest(TestCaseWithCeleryTasks):
(1, 'handle_user_added_to_group')]: (1, 'handle_user_added_to_group')]:
self.assertEqual(creators.count(tcreator), tcount) self.assertEqual(creators.count(tcreator), tcount)
def test_save_again(self):
group2 = Group.objects.create(groupname='test2', gid=1001)
TaskResult.objects.all().delete()
group2.save()
taskres = TaskResult.objects.all()
self.assertEqual(len(taskres), 0)
def test_delete(self): def test_delete(self):
group2 = Group.objects.create(groupname='test2', gid=1001) group2 = Group.objects.create(groupname='test2', gid=1001)
addgroup = AdditionalGroup.objects.create(user=self.user, group=group2) addgroup = AdditionalGroup.objects.create(user=self.user, group=group2)
@ -442,7 +464,7 @@ class SshPublicKeyManagerTest(TestCaseWithCeleryTasks):
self.assertGreater(len(res[1]), 40) self.assertGreater(len(res[1]), 40)
self.assertEqual(res[2], '') self.assertEqual(res[2], '')
def test_parse_keytext_invalid(self): def test_parse_keytext_invalid_multiline(self):
with self.assertRaises(ValueError): with self.assertRaises(ValueError):
SshPublicKey.objects.parse_keytext("\r\n".join(["xx"]*10)) SshPublicKey.objects.parse_keytext("\r\n".join(["xx"]*10))
@ -454,6 +476,19 @@ class SshPublicKeyManagerTest(TestCaseWithCeleryTasks):
self.assertGreater(len(res[1]), 40) self.assertGreater(len(res[1]), 40)
self.assertEqual(res[2], "DSA Public Key for use with MyIsp") self.assertEqual(res[2], "DSA Public Key for use with MyIsp")
def test_parse_keytext_invalid_empty_rfc4716_header(self):
with self.assertRaises(ValueError):
SshPublicKey.objects.parse_keytext(
EXAMPLE_KEY_9_RFC4716_ONLY_HEADER)
def test_parse_keytext_no_comment(self):
res = SshPublicKey.objects.parse_keytext(
EXAMPLE_KEY_7_NO_COMMENT)
self.assertEqual(len(res), 3)
self.assertEqual(res[0], 'ssh-rsa')
self.assertGreater(len(res[1]), 40)
self.assertEqual(res[2], '')
def test_parse_keytext_multiline_comment(self): def test_parse_keytext_multiline_comment(self):
res = SshPublicKey.objects.parse_keytext( res = SshPublicKey.objects.parse_keytext(
EXAMPLE_KEY_5_RFC4716_MULTILINE) EXAMPLE_KEY_5_RFC4716_MULTILINE)
@ -462,6 +497,14 @@ class SshPublicKeyManagerTest(TestCaseWithCeleryTasks):
self.assertGreater(len(res[1]), 40) self.assertGreater(len(res[1]), 40)
self.assertEqual(res[2], "DSA Public Key for use with MyIsp") self.assertEqual(res[2], "DSA Public Key for use with MyIsp")
def test_parse_keytext_invalid(self):
with self.assertRaises(ValueError):
SshPublicKey.objects.parse_keytext('invalid')
def test_parse_keytext_invalid_openssh(self):
with self.assertRaises(ValueError):
SshPublicKey.objects.parse_keytext(EXAMPLE_KEY_8_OPENSSH_BROKEN)
def test_create_ssh_public_key(self): def test_create_ssh_public_key(self):
customer = Customer.objects.create_user('test') customer = Customer.objects.create_user('test')
user = User.objects.create_user(customer) user = User.objects.create_user(customer)