diff --git a/src/classloader/cpUtils_test.go b/src/classloader/cpUtils_test.go index 6f031b24..7cc35f4c 100644 --- a/src/classloader/cpUtils_test.go +++ b/src/classloader/cpUtils_test.go @@ -7,8 +7,11 @@ package classloader import ( + "jacobin/frames" "jacobin/globals" "jacobin/log" + "jacobin/types" + "os" "testing" ) @@ -42,3 +45,59 @@ func TestMeInfoFromMethRefInvalid(t *testing.T) { t.Errorf("Did not get expected result for pointing to CPentry that's not a MethodRef") } } + +func TestMeInfoFromMethRefValid(t *testing.T) { + globals.InitGlobals("test") + + // redirect stderr so as not to pollute the test output with the expected error message + normalStderr := os.Stderr + _, w, _ := os.Pipe() + os.Stderr = w + + // Initialize classloaders and method area + err := Init() + if err != nil { + t.Errorf("Failure to load classes in TestMeInfoFromMethRefValid") + } + LoadBaseClasses() // must follow classloader.Init() + + f := frames.CreateFrame(4) + f.Meth = append(f.Meth, 0x00) + f.Meth = append(f.Meth, 0x01) // Go to slot 0x0001 in the CP + + CP := CPool{} + CP.CpIndex = make([]CpEntry, 10) + CP.CpIndex[0] = CpEntry{Type: 0, Slot: 0} + CP.CpIndex[1] = CpEntry{Type: MethodRef, Slot: 0} + + CP.MethodRefs = make([]MethodRefEntry, 1) + CP.MethodRefs[0] = MethodRefEntry{ClassIndex: 2, NameAndType: 3} + + CP.CpIndex[2] = CpEntry{Type: ClassRef, Slot: 0} + CP.ClassRefs = make([]uint32, 4) + CP.ClassRefs[0] = types.ObjectPoolStringIndex + + CP.CpIndex[3] = CpEntry{Type: NameAndType, Slot: 0} + CP.NameAndTypes = make([]NameAndTypeEntry, 4) + CP.NameAndTypes[0] = NameAndTypeEntry{ + NameIndex: 4, + DescIndex: 5, + } + CP.CpIndex[4] = CpEntry{Type: UTF8, Slot: 0} // method name + CP.Utf8Refs = make([]string, 4) + CP.Utf8Refs[0] = "" + + CP.CpIndex[5] = CpEntry{Type: UTF8, Slot: 1} // method name + CP.Utf8Refs[1] = "()V" + + f.CP = &CP + + _, s2, s3 := GetMethInfoFromCPmethref(&CP, 1) + if s2 != "" && s3 != "()V" { + t.Errorf("Expect to get a method: ()V, got %s%s", s2, s2) + } + + // restore stderr + _ = w.Close() + os.Stderr = normalStderr +}