github.com/Schaudge/grailbase@v0.0.0-20240223061707-44c758a471c0/embedbin/embedbin_test.go (about)

     1  // Copyright 2019 GRAIL, Inc. All rights reserved.
     2  // Use of this source code is governed by the Apache 2.0
     3  // license that can be found in the LICENSE file.
     4  
     5  package embedbin
     6  
     7  import (
     8  	"bytes"
     9  	"io"
    10  	"io/ioutil"
    11  	"os"
    12  	"testing"
    13  )
    14  
    15  func TestEmbedbin(t *testing.T) {
    16  	filename, err := os.Executable()
    17  	if err != nil {
    18  		t.Fatal(err)
    19  	}
    20  	body, err := ioutil.ReadFile(filename)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  
    25  	self, err := Self()
    26  	if err != nil {
    27  		t.Fatal(err)
    28  	}
    29  	r, err := self.OpenBase()
    30  	if err != nil {
    31  		t.Fatal(err)
    32  	}
    33  	defer r.Close()
    34  	embedded, err := ioutil.ReadAll(r)
    35  	if err != nil {
    36  		t.Fatal(err)
    37  	}
    38  	if !bytes.Equal(body, embedded) {
    39  		t.Error("content mismatch")
    40  	}
    41  }
    42  
    43  func TestEmbedbinNonExist(t *testing.T) {
    44  	self, err := Self()
    45  	if err != nil {
    46  		t.Fatal(err)
    47  	}
    48  	_, err = self.Open("nonexistent")
    49  	if got, want := err, ErrNoSuchFile; got != want {
    50  		t.Errorf("got %v, want %v", got, want)
    51  	}
    52  }
    53  
    54  func TestSniff(t *testing.T) {
    55  	filename, err := os.Executable()
    56  	if err != nil {
    57  		t.Fatal(err)
    58  	}
    59  	f, err := os.Open(filename)
    60  	if err != nil {
    61  		t.Fatal(err)
    62  	}
    63  	defer f.Close()
    64  	info, err := f.Stat()
    65  	if err != nil {
    66  		t.Fatal(err)
    67  	}
    68  
    69  	size, err := Sniff(f, info.Size())
    70  	if err != nil {
    71  		t.Fatal(err)
    72  	}
    73  	if got, want := size, info.Size(); got != want {
    74  		t.Errorf("got %v, want %v", got, want)
    75  	}
    76  }
    77  
    78  func TestCreate(t *testing.T) {
    79  	f, err := ioutil.TempFile("", "")
    80  	must(t, err)
    81  	_, err = f.Write(svelteLinuxElfBinary)
    82  	must(t, err)
    83  	w, err := NewFileWriter(f)
    84  	must(t, err)
    85  	dw, err := w.Create("darwin/amd64")
    86  	must(t, err)
    87  	_, err = dw.Write([]byte("darwin/amd64"))
    88  	must(t, err)
    89  	dw, err = w.Create("darwin/386")
    90  	must(t, err)
    91  	_, err = dw.Write([]byte("darwin/386"))
    92  	must(t, err)
    93  	must(t, w.Close())
    94  	info, err := f.Stat()
    95  	must(t, err)
    96  	r, err := OpenFile(f, info.Size())
    97  	must(t, err)
    98  
    99  	cases := []struct {
   100  		base bool
   101  		name string
   102  		body []byte
   103  	}{
   104  		{base: true, body: svelteLinuxElfBinary},
   105  		{name: "darwin/amd64", body: []byte("darwin/amd64")},
   106  		{name: "darwin/386", body: []byte("darwin/386")},
   107  	}
   108  	for _, c := range cases {
   109  		t.Run(c.name, func(t *testing.T) {
   110  			var rc io.ReadCloser
   111  			if c.base {
   112  				rc, err = r.OpenBase()
   113  			} else {
   114  				rc, err = r.Open(c.name)
   115  			}
   116  			if err != nil {
   117  				t.Fatal(err)
   118  			}
   119  			mustBytes(t, rc, c.body)
   120  			must(t, rc.Close())
   121  			if c.base {
   122  				return
   123  			}
   124  			info, ok := r.Stat(c.name)
   125  			if !ok {
   126  				t.Errorf("%s/%t: not found", c.name, c.base)
   127  				return
   128  			}
   129  			if got, want := info.Size, int64(len(c.body)); got != want {
   130  				t.Errorf("%s: got %v, want %v", c.name, got, want)
   131  			}
   132  		})
   133  	}
   134  
   135  	_, err = r.Open("nonexistent")
   136  	if got, want := err, ErrNoSuchFile; got != want {
   137  		t.Errorf("got %v, want %v", got, want)
   138  	}
   139  }
   140  
   141  func must(t *testing.T, err error) {
   142  	t.Helper()
   143  	if err != nil {
   144  		t.Fatal(err)
   145  	}
   146  }
   147  
   148  func mustBytes(t *testing.T, r io.Reader, want []byte) {
   149  	t.Helper()
   150  	got, err := ioutil.ReadAll(r)
   151  	if err != nil {
   152  		t.Fatal(err)
   153  	}
   154  	if !bytes.Equal(got, want) {
   155  		t.Errorf("got %s, want %s", got, want)
   156  	}
   157  }