github.com/jerryclinesmith/packer@v0.3.7/common/download_test.go (about)

     1  package common
     2  
     3  import (
     4  	"crypto/md5"
     5  	"encoding/hex"
     6  	"io/ioutil"
     7  	"os"
     8  	"testing"
     9  )
    10  
    11  func TestDownloadClient_VerifyChecksum(t *testing.T) {
    12  	tf, err := ioutil.TempFile("", "packer")
    13  	if err != nil {
    14  		t.Fatalf("tempfile error: %s", err)
    15  	}
    16  	defer os.Remove(tf.Name())
    17  
    18  	// "foo"
    19  	checksum, err := hex.DecodeString("acbd18db4cc2f85cedef654fccc4a4d8")
    20  	if err != nil {
    21  		t.Fatalf("decode err: %s", err)
    22  	}
    23  
    24  	// Write the file
    25  	tf.Write([]byte("foo"))
    26  	tf.Close()
    27  
    28  	config := &DownloadConfig{
    29  		Hash:     md5.New(),
    30  		Checksum: checksum,
    31  	}
    32  
    33  	d := NewDownloadClient(config)
    34  	result, err := d.VerifyChecksum(tf.Name())
    35  	if err != nil {
    36  		t.Fatalf("Verify err: %s", err)
    37  	}
    38  
    39  	if !result {
    40  		t.Fatal("didn't verify")
    41  	}
    42  }
    43  
    44  func TestHashForType(t *testing.T) {
    45  	if h := HashForType("md5"); h == nil {
    46  		t.Fatalf("md5 hash is nil")
    47  	} else {
    48  		h.Write([]byte("foo"))
    49  		result := h.Sum(nil)
    50  
    51  		expected := "acbd18db4cc2f85cedef654fccc4a4d8"
    52  		actual := hex.EncodeToString(result)
    53  		if actual != expected {
    54  			t.Fatalf("bad hash: %s", actual)
    55  		}
    56  	}
    57  
    58  	if h := HashForType("sha1"); h == nil {
    59  		t.Fatalf("sha1 hash is nil")
    60  	} else {
    61  		h.Write([]byte("foo"))
    62  		result := h.Sum(nil)
    63  
    64  		expected := "0beec7b5ea3f0fdbc95d0dd47f3c5bc275da8a33"
    65  		actual := hex.EncodeToString(result)
    66  		if actual != expected {
    67  			t.Fatalf("bad hash: %s", actual)
    68  		}
    69  	}
    70  
    71  	if h := HashForType("sha256"); h == nil {
    72  		t.Fatalf("sha256 hash is nil")
    73  	} else {
    74  		h.Write([]byte("foo"))
    75  		result := h.Sum(nil)
    76  
    77  		expected := "2c26b46b68ffc68ff99b453c1d30413413422d706483bfa0f98a5e886266e7ae"
    78  		actual := hex.EncodeToString(result)
    79  		if actual != expected {
    80  			t.Fatalf("bad hash: %s", actual)
    81  		}
    82  	}
    83  
    84  	if h := HashForType("sha512"); h == nil {
    85  		t.Fatalf("sha512 hash is nil")
    86  	} else {
    87  		h.Write([]byte("foo"))
    88  		result := h.Sum(nil)
    89  
    90  		expected := "f7fbba6e0636f890e56fbbf3283e524c6fa3204ae298382d624741d0dc6638326e282c41be5e4254d8820772c5518a2c5a8c0c7f7eda19594a7eb539453e1ed7"
    91  		actual := hex.EncodeToString(result)
    92  		if actual != expected {
    93  			t.Fatalf("bad hash: %s", actual)
    94  		}
    95  	}
    96  
    97  	if HashForType("fake") != nil {
    98  		t.Fatalf("fake hash is not nil")
    99  	}
   100  }