root / test / py / ganeti.utils.hash_unittest.py @ 90066780
History | View | Annotate | Download (4.4 kB)
1 |
#!/usr/bin/python
|
---|---|
2 |
#
|
3 |
|
4 |
# Copyright (C) 2006, 2007, 2010, 2011 Google Inc.
|
5 |
#
|
6 |
# This program is free software; you can redistribute it and/or modify
|
7 |
# it under the terms of the GNU General Public License as published by
|
8 |
# the Free Software Foundation; either version 2 of the License, or
|
9 |
# (at your option) any later version.
|
10 |
#
|
11 |
# This program is distributed in the hope that it will be useful, but
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 |
# General Public License for more details.
|
15 |
#
|
16 |
# You should have received a copy of the GNU General Public License
|
17 |
# along with this program; if not, write to the Free Software
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 |
# 02110-1301, USA.
|
20 |
|
21 |
|
22 |
"""Script for testing ganeti.utils.hash"""
|
23 |
|
24 |
import unittest |
25 |
import random |
26 |
import operator |
27 |
import tempfile |
28 |
|
29 |
from ganeti import constants |
30 |
from ganeti import utils |
31 |
|
32 |
import testutils |
33 |
|
34 |
|
35 |
class TestHmacFunctions(unittest.TestCase): |
36 |
# Digests can be checked with "openssl sha1 -hmac $key"
|
37 |
def testSha1Hmac(self): |
38 |
self.assertEqual(utils.Sha1Hmac("", ""), |
39 |
"fbdb1d1b18aa6c08324b7d64b71fb76370690e1d")
|
40 |
self.assertEqual(utils.Sha1Hmac("3YzMxZWE", "Hello World"), |
41 |
"ef4f3bda82212ecb2f7ce868888a19092481f1fd")
|
42 |
self.assertEqual(utils.Sha1Hmac("TguMTA2K", ""), |
43 |
"f904c2476527c6d3e6609ab683c66fa0652cb1dc")
|
44 |
|
45 |
longtext = 1500 * "The quick brown fox jumps over the lazy dog\n" |
46 |
self.assertEqual(utils.Sha1Hmac("3YzMxZWE", longtext), |
47 |
"35901b9a3001a7cdcf8e0e9d7c2e79df2223af54")
|
48 |
|
49 |
def testSha1HmacSalt(self): |
50 |
self.assertEqual(utils.Sha1Hmac("TguMTA2K", "", salt="abc0"), |
51 |
"4999bf342470eadb11dfcd24ca5680cf9fd7cdce")
|
52 |
self.assertEqual(utils.Sha1Hmac("TguMTA2K", "", salt="abc9"), |
53 |
"17a4adc34d69c0d367d4ffbef96fd41d4df7a6e8")
|
54 |
self.assertEqual(utils.Sha1Hmac("3YzMxZWE", "Hello World", salt="xyz0"), |
55 |
"7f264f8114c9066afc9bb7636e1786d996d3cc0d")
|
56 |
|
57 |
def testVerifySha1Hmac(self): |
58 |
self.assert_(utils.VerifySha1Hmac("", "", ("fbdb1d1b18aa6c08324b" |
59 |
"7d64b71fb76370690e1d")))
|
60 |
self.assert_(utils.VerifySha1Hmac("TguMTA2K", "", |
61 |
("f904c2476527c6d3e660"
|
62 |
"9ab683c66fa0652cb1dc")))
|
63 |
|
64 |
digest = "ef4f3bda82212ecb2f7ce868888a19092481f1fd"
|
65 |
self.assert_(utils.VerifySha1Hmac("3YzMxZWE", "Hello World", digest)) |
66 |
self.assert_(utils.VerifySha1Hmac("3YzMxZWE", "Hello World", |
67 |
digest.lower())) |
68 |
self.assert_(utils.VerifySha1Hmac("3YzMxZWE", "Hello World", |
69 |
digest.upper())) |
70 |
self.assert_(utils.VerifySha1Hmac("3YzMxZWE", "Hello World", |
71 |
digest.title())) |
72 |
|
73 |
def testVerifySha1HmacSalt(self): |
74 |
self.assert_(utils.VerifySha1Hmac("TguMTA2K", "", |
75 |
("17a4adc34d69c0d367d4"
|
76 |
"ffbef96fd41d4df7a6e8"),
|
77 |
salt="abc9"))
|
78 |
self.assert_(utils.VerifySha1Hmac("3YzMxZWE", "Hello World", |
79 |
("7f264f8114c9066afc9b"
|
80 |
"b7636e1786d996d3cc0d"),
|
81 |
salt="xyz0"))
|
82 |
|
83 |
|
84 |
class TestFingerprintFiles(unittest.TestCase): |
85 |
def setUp(self): |
86 |
self.tmpfile = tempfile.NamedTemporaryFile()
|
87 |
self.tmpfile2 = tempfile.NamedTemporaryFile()
|
88 |
utils.WriteFile(self.tmpfile2.name, data="Hello World\n") |
89 |
self.results = {
|
90 |
self.tmpfile.name: "da39a3ee5e6b4b0d3255bfef95601890afd80709", |
91 |
self.tmpfile2.name: "648a6a6ffffdaa0badb23b8baf90b6168dd16b3a", |
92 |
} |
93 |
|
94 |
def testSingleFile(self): |
95 |
self.assertEqual(utils.hash._FingerprintFile(self.tmpfile.name), |
96 |
self.results[self.tmpfile.name]) |
97 |
|
98 |
self.assertEqual(utils.hash._FingerprintFile("/no/such/file"), None) |
99 |
|
100 |
def testBigFile(self): |
101 |
self.tmpfile.write("A" * 8192) |
102 |
self.tmpfile.flush()
|
103 |
self.assertEqual(utils.hash._FingerprintFile(self.tmpfile.name), |
104 |
"35b6795ca20d6dc0aff8c7c110c96cd1070b8c38")
|
105 |
|
106 |
def testMultiple(self): |
107 |
all_files = self.results.keys()
|
108 |
all_files.append("/no/such/file")
|
109 |
self.assertEqual(utils.FingerprintFiles(self.results.keys()), self.results) |
110 |
|
111 |
|
112 |
if __name__ == "__main__": |
113 |
testutils.GanetiTestProgram() |