Skip to content

Commit

Permalink
added tests for the walker
Browse files Browse the repository at this point in the history
  • Loading branch information
phith0n committed Mar 24, 2022
1 parent 6d42277 commit 99a83be
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 24 deletions.
19 changes: 15 additions & 4 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
package main

import (
"encoding/hex"
"fmt"
"github.com/phith0n/zkar/serz"
"io/ioutil"
"log"
)

func main() {
var s = "\x8c\xA3\x8B"
var bs = []byte(s)
fmt.Println(bs, s, hex.EncodeToString(bs))
data, _ := ioutil.ReadFile("./testcases/ysoserial/Jdk7u21.ser")
serialization, err := serz.FromBytes(data)
if err != nil {
log.Fatal("parse error")
}

desc := serz.FindClassDesc(serialization, "sun.reflect.annotation.AnnotationInvocationHandler")
if desc != nil {
fmt.Println(desc.ToString())
} else {
log.Fatal("class desc not found")
}
}
38 changes: 25 additions & 13 deletions serz/parser_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,14 @@ func extractPackage(name string) string {
}

func TestYsoserial(t *testing.T) {
files, err := filepath.Glob("../testcases/ysoserial/*.ser")
require.Nil(t, err)
require.NotZero(t, len(files))

for _, name := range files {
data, err := ioutil.ReadFile(name)
require.Nil(t, err)

ser, err := FromBytes(data)
require.Nilf(t, err, "an error is occurred in file %v", name)
require.Truef(t, bytes.Equal(data, ser.ToBytes()), "original serz data is different from generation data in file %v", name)
}
walkAndTest("../testcases/ysoserial/*.ser", t, func(filename string, data []byte, ser *Serialization) {
require.Truef(
t,
bytes.Equal(data, ser.ToBytes()),
"original data is different from generation data in file %v",
filename,
)
})
}

func TestJDK8u20(t *testing.T) {
Expand All @@ -49,7 +45,7 @@ func TestJDK8u20(t *testing.T) {

ser, err := FromJDK8u20Bytes(data)
require.Nilf(t, err, "an error is occurred in file %v", filename)
require.Truef(t, bytes.Equal(data, ser.ToJDK8u20Bytes()), "original serz data is different from generation data in file %v", filename)
require.Truef(t, bytes.Equal(data, ser.ToJDK8u20Bytes()), "original data is different from generation data in file %v", filename)
}

func TestMain(m *testing.M) {
Expand Down Expand Up @@ -113,3 +109,19 @@ func TestMain(m *testing.M) {
cleanup:
os.Exit(exitCode)
}

func walkAndTest(pathGlob string, t *testing.T, callback func(filename string, data []byte, ser *Serialization)) {
files, err := filepath.Glob(pathGlob)
require.Nil(t, err)
require.NotZero(t, len(files))

for _, name := range files {
data, err := ioutil.ReadFile(name)
require.Nil(t, err)

ser, err := FromBytes(data)
require.Nilf(t, err, "an error is occurred in file %v", name)

callback(name, data, ser)
}
}
10 changes: 3 additions & 7 deletions serz/walker.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,5 @@
package serz

import (
"fmt"
)

type WalkCallback func(object Object) error
type FindCallback func(object Object) bool
type StopWalkError struct{}
Expand Down Expand Up @@ -34,7 +30,7 @@ func FindObject(orig AllowWalked, callback FindCallback) Object {
}
}

func FindClassDesc(orig AllowWalked, name string) (*TCClassDesc, error) {
func FindClassDesc(orig AllowWalked, name string) *TCClassDesc {
var obj = FindObject(orig, func(object Object) bool {
var desc *TCClassDesc
var ok bool
Expand All @@ -46,8 +42,8 @@ func FindClassDesc(orig AllowWalked, name string) (*TCClassDesc, error) {
})

if obj != nil {
return obj.(*TCClassDesc), nil
return obj.(*TCClassDesc)
} else {
return nil, fmt.Errorf("class desc %v not found", name)
return nil
}
}
38 changes: 38 additions & 0 deletions serz/walker_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package serz

import (
"github.com/stretchr/testify/require"
"io/ioutil"
"testing"
)

func TestFindClassDesc(t *testing.T) {
data, err := ioutil.ReadFile("../testcases/ysoserial/Jdk7u21.ser")
require.Nil(t, err)

ser, err := FromBytes(data)
require.Nil(t, err)

desc := FindClassDesc(ser, "sun.reflect.annotation.AnnotationInvocationHandler")
require.NotNil(t, desc)
require.Equal(t, desc.SerialVersionUID, int64(6182022883658399397))

// check pointer
desc.SerialVersionUID = int64(1)
desc2 := FindClassDesc(ser, "sun.reflect.annotation.AnnotationInvocationHandler")
require.NotNil(t, desc2)
require.Equal(t, desc2.SerialVersionUID, int64(1))

desc3 := FindClassDesc(ser, "not.found.Class")
require.Nil(t, desc3)
}

func TestWalk(t *testing.T) {
walkAndTest("../testcases/ysoserial/*.ser", t, func(filename string, data []byte, ser *Serialization) {
err := ser.Walk(func(object Object) error {
require.NotNil(t, object)
return nil
})
require.Nil(t, err)
})
}

0 comments on commit 99a83be

Please sign in to comment.