binman: Complete test coverage of comp_util
Drop the unused gzip code, update comments and add a test for an invalid algorithm. The temporary file is not needed now, so drop that also. Signed-off-by: Simon Glass <sjg@chromium.org>
This commit is contained in:
		
							parent
							
								
									4f9ee83ba9
								
							
						
					
					
						commit
						a00d9713e4
					
				| 
						 | 
					@ -25,28 +25,20 @@ def compress(indata, algo, with_header=True):
 | 
				
			||||||
    This requires 'lz4' and 'lzma_alone' tools. It also requires an output
 | 
					    This requires 'lz4' and 'lzma_alone' tools. It also requires an output
 | 
				
			||||||
    directory to be previously set up, by calling PrepareOutputDir().
 | 
					    directory to be previously set up, by calling PrepareOutputDir().
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Care is taken to use unique temporary files so that this function can be
 | 
					 | 
				
			||||||
    called from multiple threads.
 | 
					 | 
				
			||||||
 | 
					 | 
				
			||||||
    Args:
 | 
					    Args:
 | 
				
			||||||
        indata (bytes): Input data to compress
 | 
					        indata (bytes): Input data to compress
 | 
				
			||||||
        algo (str): Algorithm to use ('none', 'gzip', 'lz4' or 'lzma')
 | 
					        algo (str): Algorithm to use ('none', 'lz4' or 'lzma')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Returns:
 | 
					    Returns:
 | 
				
			||||||
        bytes: Compressed data
 | 
					        bytes: Compressed data
 | 
				
			||||||
    """
 | 
					    """
 | 
				
			||||||
    if algo == 'none':
 | 
					    if algo == 'none':
 | 
				
			||||||
        return indata
 | 
					        return indata
 | 
				
			||||||
    fname = tempfile.NamedTemporaryFile(prefix='%s.comp.tmp' % algo,
 | 
					 | 
				
			||||||
                                        dir=tools.GetOutputDir()).name
 | 
					 | 
				
			||||||
    tools.WriteFile(fname, indata)
 | 
					 | 
				
			||||||
    if algo == 'lz4':
 | 
					    if algo == 'lz4':
 | 
				
			||||||
        data = LZ4.compress(indata)
 | 
					        data = LZ4.compress(indata)
 | 
				
			||||||
    # cbfstool uses a very old version of lzma
 | 
					    # cbfstool uses a very old version of lzma
 | 
				
			||||||
    elif algo == 'lzma':
 | 
					    elif algo == 'lzma':
 | 
				
			||||||
        data = LZMA_ALONE.compress(indata)
 | 
					        data = LZMA_ALONE.compress(indata)
 | 
				
			||||||
    elif algo == 'gzip':
 | 
					 | 
				
			||||||
        data = tools.Run('gzip', '-c', fname, binary=True)
 | 
					 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        raise ValueError("Unknown algorithm '%s'" % algo)
 | 
					        raise ValueError("Unknown algorithm '%s'" % algo)
 | 
				
			||||||
    if with_header:
 | 
					    if with_header:
 | 
				
			||||||
| 
						 | 
					@ -65,7 +57,7 @@ def decompress(indata, algo, with_header=True):
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Args:
 | 
					    Args:
 | 
				
			||||||
        indata (bytes): Input data to decompress
 | 
					        indata (bytes): Input data to decompress
 | 
				
			||||||
        algo (str): Algorithm to use ('none', 'gzip', 'lz4' or 'lzma')
 | 
					        algo (str): Algorithm to use ('none', 'lz4' or 'lzma')
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    Returns:
 | 
					    Returns:
 | 
				
			||||||
        (bytes) Compressed data
 | 
					        (bytes) Compressed data
 | 
				
			||||||
| 
						 | 
					@ -75,14 +67,10 @@ def decompress(indata, algo, with_header=True):
 | 
				
			||||||
    if with_header:
 | 
					    if with_header:
 | 
				
			||||||
        data_len = struct.unpack('<I', indata[:4])[0]
 | 
					        data_len = struct.unpack('<I', indata[:4])[0]
 | 
				
			||||||
        indata = indata[4:4 + data_len]
 | 
					        indata = indata[4:4 + data_len]
 | 
				
			||||||
    fname = tools.GetOutputFilename('%s.decomp.tmp' % algo)
 | 
					 | 
				
			||||||
    tools.WriteFile(fname, indata)
 | 
					 | 
				
			||||||
    if algo == 'lz4':
 | 
					    if algo == 'lz4':
 | 
				
			||||||
        data = LZ4.decompress(indata)
 | 
					        data = LZ4.decompress(indata)
 | 
				
			||||||
    elif algo == 'lzma':
 | 
					    elif algo == 'lzma':
 | 
				
			||||||
        data = LZMA_ALONE.decompress(indata)
 | 
					        data = LZMA_ALONE.decompress(indata)
 | 
				
			||||||
    elif algo == 'gzip':
 | 
					 | 
				
			||||||
        data = tools.Run('gzip', '-cd', fname, binary=True)
 | 
					 | 
				
			||||||
    else:
 | 
					    else:
 | 
				
			||||||
        raise ValueError("Unknown algorithm '%s'" % algo)
 | 
					        raise ValueError("Unknown algorithm '%s'" % algo)
 | 
				
			||||||
    return data
 | 
					    return data
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
| 
						 | 
					@ -5076,6 +5076,15 @@ fdt         fdtmap                Extract the devicetree blob from the fdtmap
 | 
				
			||||||
                self._DoBinman(*args)
 | 
					                self._DoBinman(*args)
 | 
				
			||||||
        self.assertIn('failed to fetch with all methods', stdout.getvalue())
 | 
					        self.assertIn('failed to fetch with all methods', stdout.getvalue())
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					    def testInvalidCompress(self):
 | 
				
			||||||
 | 
					        with self.assertRaises(ValueError) as e:
 | 
				
			||||||
 | 
					            comp_util.compress(b'', 'invalid')
 | 
				
			||||||
 | 
					        self.assertIn("Unknown algorithm 'invalid'", str(e.exception))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					        with self.assertRaises(ValueError) as e:
 | 
				
			||||||
 | 
					            comp_util.decompress(b'1234', 'invalid')
 | 
				
			||||||
 | 
					        self.assertIn("Unknown algorithm 'invalid'", str(e.exception))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
 | 
					
 | 
				
			||||||
if __name__ == "__main__":
 | 
					if __name__ == "__main__":
 | 
				
			||||||
    unittest.main()
 | 
					    unittest.main()
 | 
				
			||||||
| 
						 | 
					
 | 
				
			||||||
		Loading…
	
		Reference in New Issue