1# @file
2#  Split a file into two pieces at the request offset.
3#
4#  Copyright (c) 2021, Intel Corporation. All rights reserved.<BR>
5#
6#  SPDX-License-Identifier: BSD-2-Clause-Patent
7#
8##
9
10# Import Modules
11import unittest
12import tempfile
13import os
14import shutil
15import Split.Split as sp
16import struct as st
17
18
19class TestSplit(unittest.TestCase):
20    def setUp(self):
21        self.tmpdir = tempfile.mkdtemp()
22        self.binary_file = os.path.join(self.tmpdir, "Binary.bin")
23        self.create_inputfile()
24
25    def tearDown(self):
26        if os.path.exists(self.tmpdir):
27            shutil.rmtree(self.tmpdir)
28
29    def test_splitFile_position(self):
30        position = [-1, 0, 256, 512, 700, 1024, 2048]
31        result = [(0, 1024), (0, 1024), (256, 768),
32                  (512, 512), (700, 324), (1024, 0), (1024, 0)]
33        outputfolder = self.tmpdir
34        for index, po in enumerate(position):
35            try:
36                sp.splitFile(self.binary_file, po)
37            except Exception as e:
38                self.assertTrue(False, msg="splitFile function error")
39
40            output1 = os.path.join(outputfolder, "Binary.bin1")
41            output2 = os.path.join(outputfolder, "Binary.bin2")
42            with open(output1, "rb") as f1:
43                size1 = len(f1.read())
44            with open(output2, "rb") as f2:
45                size2 = len(f2.read())
46
47            ex_result = result[index]
48            self.assertEqual(size1, ex_result[0])
49            self.assertEqual(size2, ex_result[1])
50
51    def create_inputfile(self):
52        with open(self.binary_file, "wb") as fout:
53            for i in range(512):
54                fout.write(st.pack("<H", i))
55
56    def test_splitFile_outputfile(self):
57        output = [
58            None,
59            "Binary.bin",
60            "Binary1.bin",
61            r"output/Binary1.bin",
62            os.path.abspath( r"output/Binary1.bin")
63            ]
64        expected_output = [
65            os.path.join(os.path.dirname(self.binary_file),"Binary.bin1" ),
66            os.path.join(os.getcwd(),"Binary.bin"),
67            os.path.join(os.getcwd(),"Binary1.bin"),
68            os.path.join(os.getcwd(),r"output/Binary1.bin"),
69            os.path.join(os.path.abspath( r"output/Binary1.bin"))
70            ]
71        for index, o in enumerate(output):
72            try:
73                sp.splitFile(self.binary_file, 123, outputfile1=o)
74            except Exception as e:
75                self.assertTrue(False, msg="splitFile function error")
76
77            self.assertTrue(os.path.exists(expected_output[index]))
78            self.create_inputfile()
79
80    def test_splitFile_outputfolder(self):
81        outputfolder = [
82            None,
83            "output",
84            r"output1/output2",
85            os.path.abspath("output"),
86            "output"
87            ]
88        output = [
89            None,
90            None,
91            "Binary1.bin",
92            r"output/Binary1.bin",
93            os.path.abspath( r"output_1/Binary1.bin")
94            ]
95
96        expected_output = [
97            os.path.join(os.path.dirname(self.binary_file),"Binary.bin1" ),
98            os.path.join(os.getcwd(),"output", "Binary.bin1"),
99            os.path.join(os.getcwd(), r"output1/output2" , "Binary1.bin"),
100            os.path.join(os.getcwd(),r"output", "output/Binary1.bin"),
101            os.path.join(os.path.abspath( r"output/Binary1.bin"))
102            ]
103
104        for index, o in enumerate(outputfolder):
105            try:
106                sp.splitFile(self.binary_file, 123, outputdir=o,outputfile1=output[index])
107            except Exception as e:
108                self.assertTrue(False, msg="splitFile function error")
109
110            self.assertTrue(os.path.exists(expected_output[index]))
111            self.create_inputfile()
112
113
114if __name__ == '__main__':
115    unittest.main()
116