-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Python version GenCrc32 code and unit test cases #101
Open
YuweiChen1110
wants to merge
1
commit into
master
Choose a base branch
from
77-feature-python-gencrc32-tool-development
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
## @file | ||
# Calculate Crc32 value and Verify Crc32 value for input data. | ||
# | ||
# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR> | ||
# SPDX-License-Identifier: BSD-2-Clause-Patent | ||
# | ||
# | ||
|
||
## | ||
# Import Modules | ||
# | ||
import logging | ||
import argparse | ||
import sys | ||
from binascii import crc32 | ||
|
||
|
||
parser = argparse.ArgumentParser(description=''' | ||
Calculate Crc32 value and Verify Crc32 value for input data. | ||
''') | ||
parser.add_argument("-e", "--encode", dest="EncodeInputFile", | ||
help="Calculate andverify CRC32 value for the input file.") | ||
parser.add_argument("-d", "--decode", dest="DecodeInputFile", | ||
help="Verify CRC32 value for the input file.") | ||
parser.add_argument("-o", "--output", dest="OutputFile", | ||
help="Output file name.") | ||
parser.add_argument("-s", "--silent", help="Returns only the exit code;\ | ||
informational and error messages are not displayed.") | ||
parser.add_argument("--version", action="version", version='%(prog)s Version 2.0', | ||
help="Show program's version number and exit.") | ||
|
||
group = parser.add_mutually_exclusive_group() | ||
group.add_argument("-v", "--verbose", action="store_true", help="Print information statements") | ||
group.add_argument("-q", "--quiet", action="store_true", help="Disable all messages except fatal errors") | ||
|
||
|
||
## Calculate the Crc32 and store it in the file | ||
def CalculateCrc32(inputfile: str, outputfile: str, filebytes=b''): | ||
logger = logging.getLogger('GenCrC32') | ||
try: | ||
if filebytes != b'': | ||
InputData = filebytes | ||
CrcCheckSum = crc32(InputData).to_bytes(4, byteorder="little") | ||
else: | ||
with open(inputfile, 'rb') as fin: | ||
InputData = fin.read() | ||
CrcCheckSum = crc32(InputData).to_bytes(4, byteorder="little") | ||
with open(outputfile, 'wb') as fout: | ||
fout.write(CrcCheckSum) | ||
fout.write(InputData) | ||
except Exception as err: | ||
logger.error("Calculation failed!") | ||
raise (err) | ||
return CrcCheckSum | ||
|
||
|
||
## Verify the CRC and checkout if the file is correct | ||
def VerifyCrc32(inputfile: str, outputfile=""): | ||
logger = logging.getLogger('GenCrC32') | ||
try: | ||
with open(inputfile, 'rb') as fin: | ||
InputData = fin.read() | ||
CurCrcCheckSum = InputData[0:4] | ||
CalCrcCheckSum = CalculateCrc32('', '', InputData[4:]) | ||
if CurCrcCheckSum != CalCrcCheckSum: | ||
logger.error("Invalid file!") | ||
elif outputfile != "": | ||
with open(outputfile, 'wb') as fout: | ||
fout.write(InputData[4:]) | ||
except Exception as err: | ||
logger.error("Verification failed!") | ||
raise (err) | ||
|
||
|
||
def main(): | ||
args = parser.parse_args() | ||
|
||
logger = logging.getLogger('GenCrc32') | ||
if args.quiet: | ||
logger.setLevel(logging.CRITICAL) | ||
if args.verbose: | ||
logger.setLevel(logging.DEBUG) | ||
lh = logging.StreamHandler(sys.stdout) | ||
lf = logging.Formatter("%(levelname)-8s: %(message)s") | ||
lh.setFormatter(lf) | ||
logger.addHandler(lh) | ||
|
||
try: | ||
if not args.OutputFile: | ||
parser.print_help() | ||
logger.error("Missing options - outputfile!") | ||
assert () | ||
if args.EncodeInputFile: | ||
CalculateCrc32(args.EncodeInputFile, args.OutputFile) | ||
elif args.DecodeInputFile: | ||
VerifyCrc32(args.DecodeInputFile, args.OutputFile) | ||
except Exception: | ||
return 1 | ||
return 0 | ||
|
||
|
||
if __name__ == "__main__": | ||
exit(main()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
# @file | ||
# Calculate the crc32 value of file. | ||
# | ||
# Copyright (c) 2021, Intel Corporation. All rights reserved.<BR> | ||
# | ||
# SPDX-License-Identifier: BSD-2-Clause-Patent | ||
# | ||
## | ||
|
||
# Import Modules |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a test file for the GenCrc32 python tool~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
U@"�This is a test file for the GenCrc32 python tool~ |
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
This is a test file for the GenCrc32 python tool~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
U@"�This is a test file for the GenCrc32 python tool~ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,117 @@ | ||
## @file | ||
# Calculate Crc32 value and Verify Crc32 value for input data. | ||
# | ||
# Copyright (c) 2007 - 2018, Intel Corporation. All rights reserved.<BR> | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same command about the copyright line as above. |
||
# SPDX-License-Identifier: BSD-2-Clause-Patent | ||
# | ||
# | ||
|
||
## Import Modules | ||
import shutil | ||
import unittest | ||
import tempfile | ||
import os | ||
import edk2basetools.GenCrc32.GenCrc32 as Gen | ||
import filecmp | ||
import struct as st | ||
|
||
|
||
class TestGenCrc32(unittest.TestCase): | ||
def setUp(self): | ||
self.tmpdir = tempfile.mkdtemp() | ||
self.binary_file = os.path.join(self.tmpdir, "Binary.bin") | ||
self.create_inputfile() | ||
self.decode_output_folder = os.path.join(os.path.dirname(__file__), r"decode\decode_result") | ||
if not os.path.exists(self.decode_output_folder): | ||
os.mkdir(self.decode_output_folder) | ||
self.encode_output_folder = os.path.join(os.path.dirname(__file__), r"encode\encode_result") | ||
if not os.path.exists(self.encode_output_folder): | ||
os.mkdir(self.encode_output_folder) | ||
|
||
def tearDown(self): | ||
if os.path.exists(self.tmpdir): | ||
shutil.rmtree(self.tmpdir) | ||
# if os.path.exists(self.decode_output_folder): | ||
# shutil.rmtree(self.decode_output_folder) | ||
# if os.path.exists(self.encode_output_folder): | ||
# shutil.rmtree(self.encode_output_folder) | ||
|
||
def create_inputfile(self): | ||
with open(self.binary_file, "wb") as fout: | ||
for i in range(512): | ||
fout.write(st.pack("<H", i)) | ||
|
||
def test_crc32encode(self): | ||
inputfile = [ | ||
os.path.join(os.path.dirname(__file__), "encode", "demo.bin"), | ||
os.path.join(os.path.dirname(__file__), "encode", "PcdPeim.efi"), | ||
os.path.join(os.path.dirname(__file__), "encode", "S3Resume2Pei.efi") | ||
] | ||
|
||
outputfile = [ | ||
os.path.join(os.path.dirname(__file__), r"encode\encode_result", "demo_crc_py"), | ||
os.path.join(os.path.dirname(__file__), r"encode\encode_result", "PcdPeim_crc32_py"), | ||
os.path.join(os.path.dirname(__file__), r"encode\encode_result", "S3Resume2Pei_crc32_py") | ||
] | ||
|
||
expected_output = [ | ||
os.path.join(os.path.dirname(__file__), "encode", "demo_crc"), | ||
os.path.join(os.path.dirname(__file__), "encode", "PcdPeim_crc32"), | ||
os.path.join(os.path.dirname(__file__), "encode", "S3Resume2Pei_crc32") | ||
] | ||
for index, o in enumerate(inputfile): | ||
output = outputfile[index] | ||
try: | ||
Gen.CalculateCrc32(o, output) | ||
status = filecmp.cmp(output, expected_output[index]) | ||
except Exception: | ||
self.assertTrue(False, msg="GenCrc32 encode function error") | ||
self.assertEqual(status, 1) | ||
|
||
def test_crc32decode(self): | ||
inputfile = [ | ||
os.path.join(os.path.dirname(__file__), "decode", "demo_crc"), | ||
os.path.join(os.path.dirname(__file__), "decode", "PcdPeim_crc32"), | ||
os.path.join(os.path.dirname(__file__), "decode", "S3Resume2Pei_crc32") | ||
] | ||
|
||
outputfile = [ | ||
os.path.join(os.path.dirname(__file__), r"decode\decode_result", "demo_decode"), | ||
os.path.join(os.path.dirname(__file__), r"decode\decode_result", "PcdPeim_decode"), | ||
os.path.join(os.path.dirname(__file__), r"decode\decode_result", "S3Resume2Pei_decode") | ||
] | ||
|
||
expected_output = [ | ||
os.path.join(os.path.dirname(__file__), "decode", "demo"), | ||
os.path.join(os.path.dirname(__file__), "decode", "PcdPeim"), | ||
os.path.join(os.path.dirname(__file__), "decode", "S3Resume2Pei") | ||
] | ||
|
||
for index, o in enumerate(inputfile): | ||
output = outputfile[index] | ||
try: | ||
Gen.VerifyCrc32(o, output) | ||
status = filecmp.cmp(output, expected_output[index]) | ||
except Exception: | ||
self.assertTrue(False, msg="GenCrc32 decode function error") | ||
self.assertEqual(status, 1) | ||
|
||
def test_CalculateCrc32_outputfile(self): | ||
output = [ | ||
os.path.join(self.tmpdir, "Binary1.bin") | ||
] | ||
|
||
expected_output = [ | ||
os.path.join(self.tmpdir, "Binary1.bin") | ||
] | ||
|
||
for index, o in enumerate(output): | ||
try: | ||
Gen.CalculateCrc32(self.binary_file, o) | ||
except Exception: | ||
self.assertTrue(False, msg="GenCrc32 output file directory error") | ||
self.assertTrue(os.path.exists(expected_output[index])) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this is new code, it should probably be "Copyright (c) 2024...." since it wasn't written in 2007.