github.com/linuxboot/fiano@v1.2.0/pkg/compression/compression_test.go (about)

     1  // Copyright 2018 the LinuxBoot Authors. All rights reserved
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  package compression
     6  
     7  import (
     8  	"os"
     9  	"reflect"
    10  	"testing"
    11  
    12  	"github.com/linuxboot/fiano/pkg/guid"
    13  )
    14  
    15  var tests = []struct {
    16  	name            string
    17  	encodedFilename string
    18  	decodedFilename string
    19  	compressor      Compressor
    20  }{
    21  	{
    22  		name:            "random data LZMA",
    23  		encodedFilename: "testdata/random.bin.lzma",
    24  		decodedFilename: "testdata/random.bin",
    25  		compressor:      &LZMA{},
    26  	},
    27  	{
    28  		name:            "random data LZ4",
    29  		encodedFilename: "testdata/random.bin.lz4",
    30  		decodedFilename: "testdata/random.bin",
    31  		compressor:      &LZ4{},
    32  	},
    33  	{
    34  		name:            "random data SystemLZMA",
    35  		encodedFilename: "testdata/random.bin.lzma",
    36  		decodedFilename: "testdata/random.bin",
    37  		compressor:      &SystemLZMA{"xz"},
    38  	},
    39  	{
    40  		name:            "random data LZMAX86",
    41  		encodedFilename: "testdata/random.bin.lzma86",
    42  		decodedFilename: "testdata/random.bin",
    43  		compressor:      &LZMAX86{&LZMA{}},
    44  	},
    45  	{
    46  		name:            "random data SystemLZMAX86",
    47  		encodedFilename: "testdata/random.bin.lzma86",
    48  		decodedFilename: "testdata/random.bin",
    49  		compressor:      &LZMAX86{&SystemLZMA{"xz"}},
    50  	},
    51  }
    52  
    53  func TestEncodeDecode(t *testing.T) {
    54  	for _, tt := range tests {
    55  		t.Run(tt.name, func(t *testing.T) {
    56  			// Read test data.
    57  			want, err := os.ReadFile(tt.decodedFilename)
    58  			if err != nil {
    59  				t.Fatal(err)
    60  			}
    61  
    62  			// Encoded and decode
    63  			encoded, err := tt.compressor.Encode(want)
    64  			if err != nil {
    65  				t.Fatal(err)
    66  			}
    67  			got, err := tt.compressor.Decode(encoded)
    68  			if err != nil {
    69  				t.Fatal(err)
    70  			}
    71  			if !reflect.DeepEqual(got, want) {
    72  				t.Fatalf("decompressed image did not match, (got: %d bytes, want: %d bytes)", len(got), len(want))
    73  			}
    74  		})
    75  	}
    76  }
    77  
    78  func TestDecode(t *testing.T) {
    79  	for _, tt := range tests {
    80  		t.Run(tt.name, func(t *testing.T) {
    81  			// Read test data.
    82  			want, err := os.ReadFile(tt.decodedFilename)
    83  			if err != nil {
    84  				t.Fatal(err)
    85  			}
    86  			encoded, err := os.ReadFile(tt.encodedFilename)
    87  			if err != nil {
    88  				t.Fatal(err)
    89  			}
    90  
    91  			// Decode
    92  			got, err := tt.compressor.Decode(encoded)
    93  			if err != nil {
    94  				t.Fatal(err)
    95  			}
    96  			if !reflect.DeepEqual(got, want) {
    97  				t.Fatalf("decompressed image did not match, (got: %d bytes, want: %d bytes)", len(got), len(want))
    98  			}
    99  		})
   100  	}
   101  }
   102  
   103  func TestCompressorFromGUID(t *testing.T) {
   104  	var compressors = []struct {
   105  		name            string
   106  		guid            *guid.GUID
   107  		expected        Compressor
   108  		encodedFilename string
   109  		decodedFilename string
   110  	}{
   111  		{
   112  			name:            "system xz",
   113  			guid:            &LZMAGUID,
   114  			expected:        &SystemLZMA{"xz"},
   115  			encodedFilename: "testdata/random.bin.lzma",
   116  			decodedFilename: "testdata/random.bin",
   117  		},
   118  		{
   119  			name:            "lzma",
   120  			guid:            &LZMAX86GUID,
   121  			expected:        &LZMAX86{&SystemLZMA{"xz"}},
   122  			encodedFilename: "testdata/random.bin.lzma86",
   123  			decodedFilename: "testdata/random.bin",
   124  		},
   125  	}
   126  	for _, tt := range compressors {
   127  		t.Run(tt.name, func(t *testing.T) {
   128  			compressor := CompressorFromGUID(tt.guid)
   129  			if compressor.Name() != tt.expected.Name() {
   130  				t.Fatalf("compressor from guid %v did not match (got: %s, want: %s)", tt.guid, compressor.Name(), tt.expected.Name())
   131  			}
   132  			// Read test data.
   133  			want, err := os.ReadFile(tt.decodedFilename)
   134  			if err != nil {
   135  				t.Fatal(err)
   136  			}
   137  			// Compare encodings
   138  			encoded, err := compressor.Encode(want)
   139  			if err != nil {
   140  				t.Fatal(err)
   141  			}
   142  			expectedEncoded, terr := tt.expected.Encode(want)
   143  			if terr != nil {
   144  				t.Fatal(terr)
   145  			}
   146  			if !reflect.DeepEqual(encoded, expectedEncoded) {
   147  				t.Fatalf("compressor from guid %v encoding did not match (got: %s, want: %s)", tt.guid, encoded, expectedEncoded)
   148  			}
   149  			// Compare decodings
   150  			got, err := compressor.Decode(encoded)
   151  			if err != nil {
   152  				t.Fatal(err)
   153  			}
   154  			expectedGot, terr := tt.expected.Decode(encoded)
   155  			if terr != nil {
   156  				t.Fatal(terr)
   157  			}
   158  			if !reflect.DeepEqual(got, expectedGot) {
   159  				t.Fatalf("compressor from guid %v decoding did not match (got: %s, want: %s)", tt.guid, got, expectedGot)
   160  			}
   161  			if !reflect.DeepEqual(got, want) {
   162  				t.Fatalf("decompressed image did not match, (got: %d bytes, want: %d bytes)", len(got), len(want))
   163  			}
   164  		})
   165  
   166  	}
   167  }