added release.txt blurb. Fixed spelling typo in Defaults.xml
[scons.git] / test / option / hash-format.py
blob663ad2ee8be0f2ac38f307affaf13829feedfe87
1 #!/usr/bin/env python
3 # __COPYRIGHT__
5 # Permission is hereby granted, free of charge, to any person obtaining
6 # a copy of this software and associated documentation files (the
7 # "Software"), to deal in the Software without restriction, including
8 # without limitation the rights to use, copy, modify, merge, publish,
9 # distribute, sublicense, and/or sell copies of the Software, and to
10 # permit persons to whom the Software is furnished to do so, subject to
11 # the following conditions:
13 # The above copyright notice and this permission notice shall be included
14 # in all copies or substantial portions of the Software.
16 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY
17 # KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
18 # WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20 # LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21 # OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22 # WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 __revision__ = "__FILE__ __REVISION__ __DATE__ __DEVELOPER__"
27 import hashlib
28 import os
29 import TestSCons
30 from SCons.Util import ALLOWED_HASH_FORMATS, DEFAULT_HASH_FORMATS
32 # Test passing the hash format by command-line.
33 INVALID_ALGORITHM = 'testfailure'
35 for algorithm in [*DEFAULT_HASH_FORMATS, INVALID_ALGORITHM, None]:
36 test = TestSCons.TestSCons()
37 test.dir_fixture('hash-format')
39 # Expect failure due to an unsupported/invalid algorithm.
40 # The error message however changes if SCons detects that the host system doesn't support one or more algorithms
41 # Primary reason the message changes is so user doesn't have to start with unsupported algorithm A and then attempt
42 # to switch to unsupported algorithm B.
43 # On normal systems (allowed=default) this will output a fixed message, but on FIPS-enabled or other weird systems
44 # that don't have default allowed algorithms, it informs the user of the mismatch _and_ the currently supported
45 # algorithms on the system they're using.
46 # In Python 3.9 this becomes somewhat obselete as the hashlib is informed we don't use hashing for security but
47 # for loose integrity.
48 if algorithm == INVALID_ALGORITHM:
49 if ALLOWED_HASH_FORMATS == DEFAULT_HASH_FORMATS:
50 test.run('--hash-format=%s .' % algorithm, stderr=r"""
51 scons: \*\*\* Hash format "{}" is not supported by SCons. Only the following hash formats are supported: {}
52 File "[^"]+", line \d+, in \S+
53 """.format(algorithm, ', '.join(DEFAULT_HASH_FORMATS)), status=2, match=TestSCons.match_re)
54 else:
55 test.run('--hash-format=%s .' % algorithm, stderr=r"""
56 scons: \*\*\* Hash format "{}" is not supported by SCons. SCons supports more hash formats than your local system is reporting; SCons supports: {}. Your local system only supports: {}
57 File "[^"]+", line \d+, in \S+
58 """.format(algorithm, ', '.join(DEFAULT_HASH_FORMATS), ', '.join(ALLOWED_HASH_FORMATS)), status=2, match=TestSCons.match_re)
59 continue
60 elif algorithm is not None:
61 if algorithm in ALLOWED_HASH_FORMATS:
62 expected_dblite = test.workpath('.sconsign_%s.dblite' % algorithm)
63 test.run('--hash-format=%s .' % algorithm)
64 else:
65 test.run('--hash-format=%s' % algorithm, stderr=r"""
66 scons: \*\*\* While hash format "{}" is supported by SCons, the local system indicates only the following hash formats are supported by the hashlib library: {}
67 File "[^"]+", line \d+, in \S+
68 Error in atexit._run_exitfuncs:
69 Traceback \(most recent call last\):
70 File "[^"]+", line \d+, in \S+
71 assert csig == '[a-z0-9]+', csig
72 AssertionError: [a-z0-9]+
73 """.format(algorithm, ', '.join(ALLOWED_HASH_FORMATS)), status=2, match=TestSCons.match_re)
74 continue
75 else:
76 # The SConsign file in the hash-format folder has logic to call
77 # SCons.Util.set_hash_format('sha256') if the default algorithm is
78 # being used. So this test of algorithm==None effectively validates
79 # that the sconsign db includes the algorithm name if that function is
80 # used instead of --hash-format. This is useful because consumers are
81 # expected to call that function if they want to opt into stronger
82 # hashes.
83 expected_dblite = test.workpath('.sconsign_sha256.dblite')
84 test.run('.')
86 assert os.path.isfile(expected_dblite), \
87 "%s does not exist when running algorithm %s" % (expected_dblite,
88 algorithm)
90 test.run('-C .')
91 os.unlink(expected_dblite)
93 # In this case, the SConstruct file will use SetOption to override the hash
94 # format.
95 test.run()
97 test.pass_test()
99 # Local Variables:
100 # tab-width:4
101 # indent-tabs-mode:nil
102 # End:
103 # vim: set expandtab tabstop=4 shiftwidth=4: