Statistics
| Branch: | Tag: | Revision:

root / test / ganeti.utils.hash_unittest.py @ 3b877f08

History | View | Annotate | Download (4.4 kB)

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