github.com/btwiuse/jiri@v0.0.0-20191125065820-53353bcfef54/metadata/metadata_test.go (about)

     1  // Copyright 2015 The Vanadium 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 metadata
     6  
     7  import (
     8  	"os/exec"
     9  	"reflect"
    10  	"runtime"
    11  	"testing"
    12  )
    13  
    14  var allTests = []struct {
    15  	MD  *T
    16  	XML string
    17  	B64 []string
    18  }{
    19  	{
    20  		MD:  FromMap(nil),
    21  		XML: `<metadata></metadata>`,
    22  		B64: []string{`eJyyyU0tSUxJLEm0s9GHMwEBAAD//1RmB6Y=`},
    23  	},
    24  	{
    25  		MD: FromMap(map[string]string{
    26  			"A": `a value`,
    27  			"B": `b value`,
    28  			"C": `c value`,
    29  		}),
    30  		XML: `<metadata>
    31    <md id="A">a value</md>
    32    <md id="B">b value</md>
    33    <md id="C">c value</md>
    34  </metadata>`,
    35  		B64: []string{`eJyyyU0tSUxJLEm0s8lNUchMsVVyVLJLVChLzClNtdHPTYELOynZJWERdlayS0YW1oebBwgAAP//H2Qc4g==`},
    36  	},
    37  	{
    38  		MD: FromMap(map[string]string{
    39  			"A": `a value`,
    40  			"B": `
    41  b value
    42     has newlines
    43   galore
    44  `,
    45  			"C": `c value`,
    46  		}),
    47  		XML: `<metadata>
    48    <md id="A">a value</md>
    49    <md id="B"><![CDATA[
    50  b value
    51     has newlines
    52   galore
    53    ]]></md>
    54    <md id="C">c value</md>
    55  </metadata>`,
    56  		B64: []string{`eJyyyU0tSUxJLEm0s8lNUchMsVVyVLJLVChLzClNtdHPTYELOynZ2ShGO7s4hjhGcyVBFHApKChkJBYr5KWW52TmpRZzKaQn5uQXgcRjY+1QtDsr2SUjm6oPtxcQAAD//0qVKG0=`},
    57  	},
    58  	{
    59  		MD: FromMap(map[string]string{
    60  			"BuildPlatform": `amd64unknown-linux-unknown`,
    61  			"BuildTime":     `2015-05-01T01:33:35Z`,
    62  			"Manifest": `
    63  <manifest label="">
    64    <projects>
    65      <project exclude="false" name="release.go.v23" path="release/go/src/v.io/v23" protocol="git" remote="https://vanadium.googlesource.com/release.go.v23" revision="16889ec00eb4008849057a5e9014a025a231f836"></project>
    66      <project exclude="false" name="release.go.x.devtools" path="release/go/src/v.io/x/devtools" protocol="git" remote="https://vanadium.googlesource.com/release.go.x.devtools" revision="de0bd3a5f0f9b30532c41bdb01661cfe8c24df76"></project>
    67    </projects>
    68  </manifest>
    69  `,
    70  			"ZZZ": `zzz`,
    71  		}),
    72  		XML: `<metadata>
    73    <md id="BuildPlatform">amd64unknown-linux-unknown</md>
    74    <md id="BuildTime">2015-05-01T01:33:35Z</md>
    75    <md id="Manifest"><![CDATA[
    76  <manifest label="">
    77    <projects>
    78      <project exclude="false" name="release.go.v23" path="release/go/src/v.io/v23" protocol="git" remote="https://vanadium.googlesource.com/release.go.v23" revision="16889ec00eb4008849057a5e9014a025a231f836"></project>
    79      <project exclude="false" name="release.go.x.devtools" path="release/go/src/v.io/x/devtools" protocol="git" remote="https://vanadium.googlesource.com/release.go.x.devtools" revision="de0bd3a5f0f9b30532c41bdb01661cfe8c24df76"></project>
    80    </projects>
    81  </manifest>
    82    ]]></md>
    83    <md id="ZZZ">zzz</md>
    84  </metadata>`,
    85  		B64: []string{
    86  			`eJyskrFu2zAQhvc8hcrdIilKimxIBNJ2LdDBk4MMJ/KksCV1hkiphp++COI6cYYCBTp+/x1x+MC/DZjAQgLdBps527HPi/P2u4c00ByYhmDrcpl+TvRr2ng3LafNhVoe7O2rvQvIdCFktRHVRsi9kDuldqo63Ox+g8kNGBPT7afHL18f9g+Pd224hJmHHn3HmL7LsvY40w80Kb7AG2Z4Mn6x2LEBfESWTRCwYzN6hIj5SPlaKJYdIT1fUz4Sj7Pha+6Iv45nSmTId2x0iWUzBkrYseeUjnHH+QoTWLeEfCQaPUZaZoO5ocA/3plxddHR1DFZN80WjRDYl0I0TbkV1T1UuBWyBFFUUCg5NKpmuuUXl381O+UW10Tk498ET/zd1n/wfH/1Tdei6K2CahDDtleiUoUpZW97IetamgEbU5R2uP+oe4Wo71r+5+NfJk9P+qYph8OB6fP5/Brya1d/BwAA//8zuuV+`,
    87  			`eJyskjFv2zAQhff8CpW7RVKUFNmQBaTtWqCDJwcZTuTJYUvqDJFSDf/6MojhxBkKFAjA5d0d7+HDvdZjBAMRutabzJot+zpbZ346iANNnnXgTV3O4++R/owrZ8f5tLqolntz+2tnPbKuELJaifTkTsiNUhtV7W9mf8BoBwyRde2Xx2/fH3YPj3etvxQzBz26LWPdXZa1x4l+oY7hRbzJDE/azQa3bAAXkGUj+CQmdAgB8wPlS6FYdoT4fK3yA/Ewab7klvhre6JImpLXwUaWTegppi3PMR7DhvMFRjB29mkdHRwGmieNuSbPP/pMuNhgadwyWTfNGrUQ2JdCNE25FtU9VLgWsgRRVFAoOTSqTuT8wvK/ZKfc4BKJXPgX4Im/m/oEzveub7gGRW8UVIMY1r0SlSp0KXvTC1nXUg/Y6KI0w/1H3KtIZ03BuBz+pfP01N0kZb/fs+58Pr8W+TWrfwMAAP//M7rlfg==`,
    88  		},
    89  	},
    90  }
    91  
    92  func TestToMap(t *testing.T) {
    93  	for _, test := range allTests {
    94  		if got, want := test.MD.ToMap(), test.MD.entries; !reflect.DeepEqual(got, want) {
    95  			t.Errorf("got %v, want %v", got, want)
    96  		}
    97  	}
    98  }
    99  
   100  func TestFromMap(t *testing.T) {
   101  	for _, test := range allTests {
   102  		if got, want := FromMap(test.MD.entries), test.MD; !reflect.DeepEqual(got, want) {
   103  			t.Errorf("got %#v, want %#v", got, want)
   104  		}
   105  	}
   106  }
   107  
   108  func TestToXML(t *testing.T) {
   109  	for _, test := range allTests {
   110  		if got, want := test.MD.ToXML(), test.XML; got != want {
   111  			t.Errorf("got %v, want %v", got, want)
   112  		}
   113  	}
   114  }
   115  
   116  func TestFromXML(t *testing.T) {
   117  	for _, test := range allTests {
   118  		got, err := FromXML([]byte(test.XML))
   119  		if err != nil {
   120  			t.Errorf("%v FromXML failed: %v", test.XML, err)
   121  		}
   122  		if want := test.MD; !reflect.DeepEqual(got, want) {
   123  			t.Errorf("got %#v, want %#v", got, want)
   124  		}
   125  		// Add some garbage and make sure it fails.
   126  		bad := "<notclosed" + test.XML
   127  		if got, err := FromXML([]byte(bad)); got != nil || err == nil {
   128  			t.Errorf("%v FromXML should have failed: (%#v, %v)", bad, got, err)
   129  		}
   130  	}
   131  }
   132  
   133  func TestToBase64(t *testing.T) {
   134  	for _, test := range allTests {
   135  		got := test.MD.ToBase64()
   136  		found := false
   137  		for _, want := range test.B64 {
   138  			if got == want {
   139  				found = true
   140  				break
   141  			}
   142  		}
   143  		if !found {
   144  			t.Errorf("got %q, want one of %q", got, test.B64)
   145  		}
   146  	}
   147  }
   148  
   149  func TestFromBase64(t *testing.T) {
   150  	for _, test := range allTests {
   151  		for _, b64 := range test.B64 {
   152  			got, err := FromBase64([]byte(b64))
   153  			if err != nil {
   154  				t.Errorf("%v FromBase64 failed: %v", b64, err)
   155  			}
   156  			if want := test.MD; !reflect.DeepEqual(got, want) {
   157  				t.Errorf("got %#v, want %#v", got, want)
   158  			}
   159  			// Add some garbage and make sure it fails.
   160  			bad := "!@#$%^&*()_+" + b64
   161  			if got, err := FromBase64([]byte(bad)); got != nil || err == nil {
   162  				t.Errorf("%v FromBase64 should have failed: (%#v, %v)", bad, got, err)
   163  			}
   164  		}
   165  	}
   166  }
   167  
   168  func TestInsertLookup(t *testing.T) {
   169  	tests := []struct {
   170  		ID, Value, Old string
   171  		Map            map[string]string
   172  	}{
   173  		{"A", "abc", "", map[string]string{"A": "abc"}},
   174  		{"B", "123", "", map[string]string{"A": "abc", "B": "123"}},
   175  		{"A", "xyz", "abc", map[string]string{"A": "xyz", "B": "123"}},
   176  		{"C", "s p a c e s", "", map[string]string{"A": "xyz", "B": "123", "C": "s p a c e s"}},
   177  	}
   178  	var x T
   179  	for _, test := range tests {
   180  		if got, want := x.Lookup(test.ID), test.Old; got != want {
   181  			t.Errorf("(%q, %q) Lookup got %q, want %q", test.ID, test.Value, got, want)
   182  		}
   183  		if got, want := x.Insert(test.ID, test.Value), test.Old; got != want {
   184  			t.Errorf("(%q, %q) Insert got %q, want %q", test.ID, test.Value, got, want)
   185  		}
   186  		if got, want := x.Lookup(test.ID), test.Value; got != want {
   187  			t.Errorf("(%q, %q) Lookup got %q, want %q", test.ID, test.Value, got, want)
   188  		}
   189  		// Add some leading and trailing spaces, which will be stripped.
   190  		value2 := "ZZ" + test.Value + "ZZ"
   191  		if got, want := x.Insert(test.ID, " \n\t"+value2+"\n\t "), test.Value; got != want {
   192  			t.Errorf("(%q, %q) Insert got %q, want %q", test.ID, value2, got, want)
   193  		}
   194  		if got, want := x.Lookup(test.ID), value2; got != want {
   195  			t.Errorf("(%q, %q) Lookup got %q, want %q", test.ID, value2, got, want)
   196  		}
   197  		// Set the value back.
   198  		if got, want := x.Insert(test.ID, test.Value), value2; got != want {
   199  			t.Errorf("(%q, %q) Insert got %q, want %q", test.ID, test.Value, got, want)
   200  		}
   201  		// Check the map form.
   202  		if got, want := x.ToMap(), test.Map; !reflect.DeepEqual(got, want) {
   203  			t.Errorf("(%q, %q) ToMap got %q, want %q", test.ID, test.Value, got, want)
   204  		}
   205  	}
   206  }
   207  
   208  func TestLDFlag(t *testing.T) {
   209  	for _, test := range allTests {
   210  		got := LDFlag(test.MD)
   211  		found := false
   212  		for _, b64 := range test.B64 {
   213  			want := "-X " + thisPkgPath + ".initBuiltIn=" + b64
   214  			if got == want {
   215  				found = true
   216  				break
   217  			}
   218  		}
   219  		if !found {
   220  			t.Errorf("got %q, want one of %v", got, test.B64)
   221  		}
   222  	}
   223  }
   224  
   225  // TestBuiltIn tests the package-level functions that operate on BuiltIn.
   226  func TestBuiltIn(t *testing.T) {
   227  	const id, value1, value2 = "TestID", "testvalue1", "testvalue2"
   228  	if got, want := Lookup(id), ""; got != want {
   229  		t.Errorf("Lookup %s got %q, want %q", id, got, want)
   230  	}
   231  	if got, want := Insert(id, value1), ""; got != want {
   232  		t.Errorf("Insert %s got %q, want %q", id, got, want)
   233  	}
   234  	if got, want := Lookup(id), value1; got != want {
   235  		t.Errorf("Lookup %s got %q, want %q", id, got, want)
   236  	}
   237  	if got, want := Insert(id, value2), value1; got != want {
   238  		t.Errorf("Insert %s got %q, want %q", id, got, want)
   239  	}
   240  	wantMap := map[string]string{
   241  		id:           value2,
   242  		"go.Arch":    runtime.GOARCH,
   243  		"go.OS":      runtime.GOOS,
   244  		"go.Version": runtime.Version(),
   245  	}
   246  	if got, want := ToMap(), wantMap; !reflect.DeepEqual(got, want) {
   247  		t.Errorf("got map %q, want %q", got, want)
   248  	}
   249  	if got, want := ToXML(), FromMap(wantMap).ToXML(); got != want {
   250  		t.Errorf("got xml %q, want %q", got, want)
   251  	}
   252  	if got, want := ToBase64(), FromMap(wantMap).ToBase64(); got != want {
   253  		t.Errorf("got base64 %q, want %q", got, want)
   254  	}
   255  }
   256  
   257  // TestInitAndFlag builds a test binary with some metadata, and invokes the
   258  // -metadata flag to make sure it dumps the expected metadata.
   259  func TestInitAndFlag(t *testing.T) {
   260  	// Run the test binary.
   261  	const id, value = "zzzTestID", "abcdefg"
   262  	x := FromMap(map[string]string{id: value})
   263  	cmdRun := exec.Command("go", "run", "-ldflags="+LDFlag(x), "./testdata/testbin.go", "-metadata")
   264  	outXML, err := cmdRun.CombinedOutput()
   265  	if err != nil {
   266  		t.Errorf("%v failed: %v\n%s", cmdRun.Args, err, outXML)
   267  	}
   268  	wantXML := `<metadata>
   269    <md id="go.Arch">` + runtime.GOARCH + `</md>
   270    <md id="go.OS">` + runtime.GOOS + `</md>
   271    <md id="go.Version">` + runtime.Version() + `</md>
   272    <md id="` + id + `">` + value + `</md>
   273  </metadata>
   274  `
   275  	if got, want := string(outXML), wantXML; got != want {
   276  		t.Errorf("got %q, want %q", got, want)
   277  	}
   278  }