--- /dev/null
+# @file\r
+# Split a file into two pieces at the request offset.\r
+#\r
+# Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>\r
+#\r
+# SPDX-License-Identifier: BSD-2-Clause-Patent\r
+#\r
+##\r
+\r
+# Import Modules\r
+import unittest\r
+import tempfile\r
+import os\r
+import shutil\r
+import Split.Split as sp\r
+import struct as st\r
+\r
+\r
+class TestSplit(unittest.TestCase):\r
+ def setUp(self):\r
+ self.WORKSPACE = tempfile.mkdtemp()\r
+ self.binary_file = os.path.join(self.WORKSPACE, "Binary.bin")\r
+ self.create_inputfile()\r
+\r
+ def tearDown(self):\r
+ if os.path.exists(self.WORKSPACE):\r
+ shutil.rmtree(self.WORKSPACE)\r
+\r
+ def test_splitFile_position(self):\r
+ position = [-1, 0, 256, 512, 700, 1024, 2048]\r
+ result = [(0, 1024), (0, 1024), (256, 768),\r
+ (512, 512), (700, 324), (1024, 0), (1024, 0)]\r
+ for index, po in enumerate(position):\r
+ try:\r
+ sp.splitFile(self.binary_file, po)\r
+ except Exception as e:\r
+ self.assertTrue(False, msg="splitFile function error")\r
+\r
+ output1 = os.path.join(self.WORKSPACE, "Binary.bin1")\r
+ output2 = os.path.join(self.WORKSPACE, "Binary.bin2")\r
+ with open(output1, "rb") as f1:\r
+ size1 = len(f1.read())\r
+ with open(output2, "rb") as f2:\r
+ size2 = len(f2.read())\r
+\r
+ ex_result = result[index]\r
+ self.assertEqual(size1, ex_result[0])\r
+ self.assertEqual(size2, ex_result[1])\r
+\r
+ def create_inputfile(self):\r
+ with open(self.binary_file, "wb") as fout:\r
+ for i in range(512):\r
+ fout.write(st.pack("<H", i))\r
+\r
+ def test_splitFile_outputfile(self):\r
+ output = [None, "Binary.bin", "Binary1.bin", r"output/Binary1.bin",\r
+ os.path.join(self.WORKSPACE, r"output/Binary1.bin")]\r
+ for o in output:\r
+ try:\r
+ sp.splitFile(self.binary_file, 123, outputfile1=o)\r
+ except Exception as e:\r
+ self.assertTrue(False, msg="splitFile function error")\r
+ if o is None:\r
+ self.assertTrue(os.path.exists(\r
+ os.path.join(self.WORKSPACE, "Binary.bin1")))\r
+ else:\r
+ if os.path.isabs(o):\r
+ self.assertTrue(os.path.exists(o))\r
+ else:\r
+ self.assertTrue(os.path.exists(\r
+ os.path.join(self.WORKSPACE, o)))\r
+ self.create_inputfile()\r
+\r
+ try:\r
+ sp.splitFile(self.binary_file, 123, outputfile2=o)\r
+ except Exception as e:\r
+ self.assertTrue(False, msg="splitFile function error")\r
+ if o is None:\r
+ self.assertTrue(os.path.exists(\r
+ os.path.join(self.WORKSPACE, "Binary.bin2")))\r
+ else:\r
+ if os.path.isabs(o):\r
+ self.assertTrue(os.path.exists(o))\r
+ else:\r
+ self.assertTrue(os.path.exists(\r
+ os.path.join(self.WORKSPACE, o)))\r
+ self.create_inputfile()\r
+\r
+ def test_splitFile_outputfolder(self):\r
+ outputfolder = [None, "output", r"output1/output2",\r
+ os.path.join(self.WORKSPACE, "output")]\r
+ for o in outputfolder:\r
+ try:\r
+ sp.splitFile(self.binary_file, 123, outputdir=o)\r
+ except Exception as e:\r
+ self.assertTrue(False, msg="splitFile function error")\r
+\r
+ if o is None:\r
+ self.assertTrue(os.path.exists(\r
+ os.path.join(self.WORKSPACE, "Binary.bin1")))\r
+ else:\r
+ if os.path.isabs(o):\r
+ self.assertTrue(os.path.exists(\r
+ os.path.join(o, "Binary.bin1")))\r
+ else:\r
+ self.assertTrue(os.path.exists(\r
+ os.path.join(self.WORKSPACE, o, "Binary.bin1")))\r
+\r
+\r
+if __name__ == '__main__':\r
+ unittest.main()\r