...

Source file src/github.com/chaos-mesh/chaos-mesh/pkg/ptrace/ptrace_linux_test.go

Documentation: github.com/chaos-mesh/chaos-mesh/pkg/ptrace

     1  // Copyright 2020 Chaos Mesh Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  // +build cgo
    15  
    16  package ptrace
    17  
    18  import (
    19  	"encoding/binary"
    20  	"math/rand"
    21  	"os"
    22  	"os/exec"
    23  	"testing"
    24  	"time"
    25  	"unsafe"
    26  
    27  	"github.com/go-logr/zapr"
    28  	. "github.com/onsi/ginkgo"
    29  	. "github.com/onsi/gomega"
    30  	"go.uber.org/zap"
    31  	"sigs.k8s.io/controller-runtime/pkg/envtest/printer"
    32  
    33  	"github.com/chaos-mesh/chaos-mesh/test/pkg/timer"
    34  )
    35  
    36  func TestPTrace(t *testing.T) {
    37  	RegisterFailHandler(Fail)
    38  
    39  	RunSpecsWithDefaultAndCustomReporters(t,
    40  		"PTrace Suit",
    41  		[]Reporter{printer.NewlineReporter{}})
    42  }
    43  
    44  var _ = BeforeSuite(func(done Done) {
    45  	rand.Seed(GinkgoRandomSeed())
    46  
    47  	By("change working directory")
    48  
    49  	err := os.Chdir("../../")
    50  	Expect(err).NotTo(HaveOccurred())
    51  
    52  	By("register logger")
    53  	zapLog, err := zap.NewDevelopment()
    54  	Expect(err).NotTo(HaveOccurred())
    55  	log := zapr.NewLogger(zapLog)
    56  	RegisterLogger(log)
    57  
    58  	close(done)
    59  })
    60  
    61  // These tests are written in BDD-style using Ginkgo framework. Refer to
    62  // http://onsi.github.io/ginkgo to learn more.
    63  
    64  var _ = Describe("PTrace", func() {
    65  
    66  	var t *timer.Timer
    67  	var program *TracedProgram
    68  
    69  	BeforeEach(func() {
    70  		var err error
    71  
    72  		t, err = timer.StartTimer()
    73  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    74  
    75  		time.Sleep(time.Millisecond)
    76  
    77  		program, err = Trace(t.Pid())
    78  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    79  	})
    80  
    81  	AfterEach(func() {
    82  		err := program.Detach()
    83  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    84  
    85  		err = t.Stop()
    86  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    87  	})
    88  
    89  	It("should mmap slice successfully", func() {
    90  		Expect(program.Pid()).Should(Equal(t.Pid()))
    91  
    92  		helloWorld := []byte("Hello World")
    93  		entry, err := program.MmapSlice(helloWorld)
    94  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    95  
    96  		readBuf, err := program.ReadSlice(entry.StartAddress, uint64(len(helloWorld)))
    97  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
    98  
    99  		Expect(*readBuf).Should(Equal(helloWorld))
   100  	})
   101  
   102  	It("double trace should get error", func() {
   103  		_, err := Trace(t.Pid())
   104  		Expect(err).Should(HaveOccurred())
   105  	})
   106  
   107  	It("should ptrace write slice successfully", func() {
   108  		helloWorld := []byte("Hello World")
   109  		addr, err := program.Mmap(uint64(len(helloWorld)), 0)
   110  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   111  
   112  		err = program.PtraceWriteSlice(addr, helloWorld)
   113  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v, addr: %d", err, addr)
   114  
   115  		readBuf, err := program.ReadSlice(addr, uint64(len(helloWorld)))
   116  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v, addr: %d", err, addr)
   117  
   118  		Expect(*readBuf).Should(Equal(helloWorld))
   119  	})
   120  
   121  	It("should write uint64 successfully", func() {
   122  		number := rand.Uint64()
   123  		size := uint64(unsafe.Sizeof(number))
   124  		expectBuf := make([]byte, size)
   125  		binary.LittleEndian.PutUint64(expectBuf, number)
   126  
   127  		addr, err := program.Mmap(size, 0)
   128  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   129  
   130  		err = program.WriteUint64ToAddr(addr, number)
   131  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v, addr: %d", err, addr)
   132  
   133  		readBuf, err := program.ReadSlice(addr, size)
   134  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v, addr: %d", err, addr)
   135  
   136  		Expect(*readBuf).Should(Equal(expectBuf))
   137  	})
   138  
   139  	It("should be able to detach and reattach", func() {
   140  		err := program.Detach()
   141  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   142  
   143  		program, err = Trace(t.Pid())
   144  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   145  	})
   146  
   147  	It("should be able to attach and detach multithread program", func() {
   148  		p := exec.Command("./bin/test/multithread_tracee")
   149  		err := p.Start()
   150  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   151  
   152  		time.Sleep(time.Millisecond)
   153  
   154  		pid := p.Process.Pid
   155  		program, err := Trace(pid)
   156  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   157  
   158  		err = program.Detach()
   159  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   160  
   161  		err = p.Process.Kill()
   162  		Expect(err).ShouldNot(HaveOccurred(), "error: %+v", err)
   163  	})
   164  })
   165