Home | History | Annotate | Download | only in plan9obj
      1 // Copyright 2014 The Go 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 plan9obj
      6 
      7 import (
      8 	"reflect"
      9 	"testing"
     10 )
     11 
     12 type fileTest struct {
     13 	file     string
     14 	hdr      FileHeader
     15 	sections []*SectionHeader
     16 }
     17 
     18 var fileTests = []fileTest{
     19 	{
     20 		"testdata/386-plan9-exec",
     21 		FileHeader{Magic386, 0x324, 0x14, 4, 0x1000, 32},
     22 		[]*SectionHeader{
     23 			{"text", 0x4c5f, 0x20},
     24 			{"data", 0x94c, 0x4c7f},
     25 			{"syms", 0x2c2b, 0x55cb},
     26 			{"spsz", 0x0, 0x81f6},
     27 			{"pcsz", 0xf7a, 0x81f6},
     28 		},
     29 	},
     30 	{
     31 		"testdata/amd64-plan9-exec",
     32 		FileHeader{MagicAMD64, 0x618, 0x13, 8, 0x200000, 40},
     33 		[]*SectionHeader{
     34 			{"text", 0x4213, 0x28},
     35 			{"data", 0xa80, 0x423b},
     36 			{"syms", 0x2c8c, 0x4cbb},
     37 			{"spsz", 0x0, 0x7947},
     38 			{"pcsz", 0xca0, 0x7947},
     39 		},
     40 	},
     41 }
     42 
     43 func TestOpen(t *testing.T) {
     44 	for i := range fileTests {
     45 		tt := &fileTests[i]
     46 
     47 		f, err := Open(tt.file)
     48 		if err != nil {
     49 			t.Error(err)
     50 			continue
     51 		}
     52 		if !reflect.DeepEqual(f.FileHeader, tt.hdr) {
     53 			t.Errorf("open %s:\n\thave %#v\n\twant %#v\n", tt.file, f.FileHeader, tt.hdr)
     54 			continue
     55 		}
     56 
     57 		for i, sh := range f.Sections {
     58 			if i >= len(tt.sections) {
     59 				break
     60 			}
     61 			have := &sh.SectionHeader
     62 			want := tt.sections[i]
     63 			if !reflect.DeepEqual(have, want) {
     64 				t.Errorf("open %s, section %d:\n\thave %#v\n\twant %#v\n", tt.file, i, have, want)
     65 			}
     66 		}
     67 		tn := len(tt.sections)
     68 		fn := len(f.Sections)
     69 		if tn != fn {
     70 			t.Errorf("open %s: len(Sections) = %d, want %d", tt.file, fn, tn)
     71 		}
     72 	}
     73 }
     74 
     75 func TestOpenFailure(t *testing.T) {
     76 	filename := "file.go"    // not a Plan 9 a.out file
     77 	_, err := Open(filename) // don't crash
     78 	if err == nil {
     79 		t.Errorf("open %s: succeeded unexpectedly", filename)
     80 	}
     81 }
     82