Skip to content
Projeler
Gruplar
Parçacıklar
Yardım
Yükleniyor...
Oturum aç / Kaydol
Gezinmeyi değiştir
C
cpython
Proje
Proje
Ayrıntılar
Etkinlik
Cycle Analytics
Depo (repository)
Depo (repository)
Dosyalar
Kayıtlar (commit)
Dallar (branch)
Etiketler
Katkıda bulunanlar
Grafik
Karşılaştır
Grafikler
Konular (issue)
0
Konular (issue)
0
Liste
Pano
Etiketler
Kilometre Taşları
Birleştirme (merge) Talepleri
0
Birleştirme (merge) Talepleri
0
CI / CD
CI / CD
İş akışları (pipeline)
İşler
Zamanlamalar
Grafikler
Paketler
Paketler
Wiki
Wiki
Parçacıklar
Parçacıklar
Üyeler
Üyeler
Collapse sidebar
Close sidebar
Etkinlik
Grafik
Grafikler
Yeni bir konu (issue) oluştur
İşler
Kayıtlar (commit)
Konu (issue) Panoları
Kenar çubuğunu aç
Batuhan Osman TASKAYA
cpython
Commits
65aa5730
Kaydet (Commit)
65aa5730
authored
Tem 30, 2013
tarafından
Christian Heimes
Dosyalara gözat
Seçenekler
Dosyalara Gözat
İndir
Eposta Yamaları
Sade Fark
Add more tests for hashlib and hash object attributes
üst
4fec4314
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
62 additions
and
13 deletions
+62
-13
test_hashlib.py
Lib/test/test_hashlib.py
+62
-13
No files found.
Lib/test/test_hashlib.py
Dosyayı görüntüle @
65aa5730
...
...
@@ -96,10 +96,14 @@ class HashLibTestCase(unittest.TestCase):
super
(
HashLibTestCase
,
self
)
.
__init__
(
*
args
,
**
kwargs
)
@property
def
hash_constructors
(
self
):
constructors
=
self
.
constructors_to_test
.
values
()
return
itertools
.
chain
.
from_iterable
(
constructors
)
def
test_hash_array
(
self
):
a
=
array
.
array
(
"b"
,
range
(
10
))
constructors
=
self
.
constructors_to_test
.
values
()
for
cons
in
itertools
.
chain
.
from_iterable
(
constructors
):
for
cons
in
self
.
hash_constructors
:
c
=
cons
(
a
)
c
.
hexdigest
()
...
...
@@ -136,39 +140,57 @@ class HashLibTestCase(unittest.TestCase):
self
.
assertRaises
(
TypeError
,
get_builtin_constructor
,
3
)
def
test_hexdigest
(
self
):
for
name
in
self
.
supported_hash_name
s
:
h
=
hashlib
.
new
(
name
)
for
cons
in
self
.
hash_constructor
s
:
h
=
cons
(
)
assert
isinstance
(
h
.
digest
(),
bytes
),
name
self
.
assertEqual
(
hexstr
(
h
.
digest
()),
h
.
hexdigest
())
def
test_large_update
(
self
):
aas
=
b
'a'
*
128
bees
=
b
'b'
*
127
cees
=
b
'c'
*
126
dees
=
b
'd'
*
2048
# HASHLIB_GIL_MINSIZE
for
name
in
self
.
supported_hash_name
s
:
m1
=
hashlib
.
new
(
name
)
for
cons
in
self
.
hash_constructor
s
:
m1
=
cons
(
)
m1
.
update
(
aas
)
m1
.
update
(
bees
)
m1
.
update
(
cees
)
m1
.
update
(
dees
)
m2
=
hashlib
.
new
(
name
)
m2
.
update
(
aas
+
bees
+
cees
)
m2
=
cons
(
)
m2
.
update
(
aas
+
bees
+
cees
+
dees
)
self
.
assertEqual
(
m1
.
digest
(),
m2
.
digest
())
def
check
(
self
,
name
,
data
,
digest
):
m3
=
cons
(
aas
+
bees
+
cees
+
dees
)
self
.
assertEqual
(
m1
.
digest
(),
m3
.
digest
())
# verify copy() doesn't touch original
m4
=
cons
(
aas
+
bees
+
cees
)
m4_digest
=
m4
.
digest
()
m4_copy
=
m4
.
copy
()
m4_copy
.
update
(
dees
)
self
.
assertEqual
(
m1
.
digest
(),
m4_copy
.
digest
())
self
.
assertEqual
(
m4
.
digest
(),
m4_digest
)
def
check
(
self
,
name
,
data
,
hexdigest
):
hexdigest
=
hexdigest
.
lower
()
constructors
=
self
.
constructors_to_test
[
name
]
# 2 is for hashlib.name(...) and hashlib.new(name, ...)
self
.
assertGreaterEqual
(
len
(
constructors
),
2
)
for
hash_object_constructor
in
constructors
:
computed
=
hash_object_constructor
(
data
)
.
hexdigest
()
m
=
hash_object_constructor
(
data
)
computed
=
m
.
hexdigest
()
self
.
assertEqual
(
computed
,
digest
,
computed
,
hex
digest
,
"Hash algorithm
%
s constructed using
%
s returned hexdigest"
"
%
r for
%
d byte input data that should have hashed to
%
r."
%
(
name
,
hash_object_constructor
,
computed
,
len
(
data
),
digest
))
computed
,
len
(
data
),
hexdigest
))
computed
=
m
.
digest
()
digest
=
bytes
.
fromhex
(
hexdigest
)
self
.
assertEqual
(
computed
,
digest
)
self
.
assertEqual
(
len
(
digest
),
m
.
digest_size
)
def
check_no_unicode
(
self
,
algorithm_name
):
# Unicode objects are not allowed as input.
...
...
@@ -184,6 +206,24 @@ class HashLibTestCase(unittest.TestCase):
self
.
check_no_unicode
(
'sha384'
)
self
.
check_no_unicode
(
'sha512'
)
def
check_blocksize_name
(
self
,
name
,
block_size
=
0
,
digest_size
=
0
):
constructors
=
self
.
constructors_to_test
[
name
]
for
hash_object_constructor
in
constructors
:
m
=
hash_object_constructor
()
self
.
assertEqual
(
m
.
block_size
,
block_size
)
self
.
assertEqual
(
m
.
digest_size
,
digest_size
)
self
.
assertEqual
(
len
(
m
.
digest
()),
digest_size
)
self
.
assertEqual
(
m
.
name
.
lower
(),
name
.
lower
())
self
.
assertIn
(
name
.
split
(
"_"
)[
0
],
repr
(
m
)
.
lower
())
def
test_blocksize_name
(
self
):
self
.
check_blocksize_name
(
'md5'
,
64
,
16
)
self
.
check_blocksize_name
(
'sha1'
,
64
,
20
)
self
.
check_blocksize_name
(
'sha224'
,
64
,
28
)
self
.
check_blocksize_name
(
'sha256'
,
64
,
32
)
self
.
check_blocksize_name
(
'sha384'
,
128
,
48
)
self
.
check_blocksize_name
(
'sha512'
,
128
,
64
)
def
test_case_md5_0
(
self
):
self
.
check
(
'md5'
,
b
''
,
'd41d8cd98f00b204e9800998ecf8427e'
)
...
...
@@ -323,6 +363,15 @@ class HashLibTestCase(unittest.TestCase):
# for multithreaded operation (which is hardwired to 2048).
gil_minsize
=
2048
for
cons
in
self
.
hash_constructors
:
m
=
cons
()
m
.
update
(
b
'1'
)
m
.
update
(
b
'#'
*
gil_minsize
)
m
.
update
(
b
'1'
)
m
=
cons
(
b
'x'
*
gil_minsize
)
m
.
update
(
b
'1'
)
m
=
hashlib
.
md5
()
m
.
update
(
b
'1'
)
m
.
update
(
b
'#'
*
gil_minsize
)
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment