Home | History | Annotate | Download | only in symbol_inject
      1 // Copyright 2018 Google Inc. All rights reserved.
      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 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     12 // See the License for the specific language governing permissions and
     13 // limitations under the License.
     14 
     15 package main
     16 
     17 import (
     18 	"bytes"
     19 	"strconv"
     20 	"testing"
     21 )
     22 
     23 func TestCopyAndInject(t *testing.T) {
     24 	s := "abcdefghijklmnopqrstuvwxyz"
     25 	testCases := []struct {
     26 		offset, size uint64
     27 		value        string
     28 		expected     string
     29 	}{
     30 		{
     31 			offset:   0,
     32 			size:     1,
     33 			value:    "A",
     34 			expected: "Abcdefghijklmnopqrstuvwxyz",
     35 		},
     36 		{
     37 			offset:   1,
     38 			size:     1,
     39 			value:    "B",
     40 			expected: "aBcdefghijklmnopqrstuvwxyz",
     41 		},
     42 		{
     43 			offset:   1,
     44 			size:     1,
     45 			value:    "BCD",
     46 			expected: "aBcdefghijklmnopqrstuvwxyz",
     47 		},
     48 		{
     49 			offset:   25,
     50 			size:     1,
     51 			value:    "Z",
     52 			expected: "abcdefghijklmnopqrstuvwxyZ",
     53 		},
     54 	}
     55 
     56 	for i, testCase := range testCases {
     57 		t.Run(strconv.Itoa(i), func(t *testing.T) {
     58 			in := bytes.NewReader([]byte(s))
     59 			out := &bytes.Buffer{}
     60 			copyAndInject(in, out, testCase.offset, testCase.size, testCase.value)
     61 
     62 			if out.String() != testCase.expected {
     63 				t.Errorf("expected %s, got %s", testCase.expected, out.String())
     64 			}
     65 		})
     66 	}
     67 }
     68