save_restore_test.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. /*
  2. Copyright 2018 The Kubernetes Authors.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. */
  13. package iptables
  14. import (
  15. "testing"
  16. )
  17. func TestReadLinesFromByteBuffer(t *testing.T) {
  18. testFn := func(byteArray []byte, expected []string) {
  19. index := 0
  20. readIndex := 0
  21. for ; readIndex < len(byteArray); index++ {
  22. line, n := readLine(readIndex, byteArray)
  23. readIndex = n
  24. if expected[index] != string(line) {
  25. t.Errorf("expected:%q, actual:%q", expected[index], line)
  26. }
  27. } // for
  28. if readIndex < len(byteArray) {
  29. t.Errorf("Byte buffer was only partially read. Buffer length is:%d, readIndex is:%d", len(byteArray), readIndex)
  30. }
  31. if index < len(expected) {
  32. t.Errorf("All expected strings were not compared. expected arr length:%d, matched count:%d", len(expected), index-1)
  33. }
  34. }
  35. byteArray1 := []byte("\n Line 1 \n\n\n L ine4 \nLine 5 \n \n")
  36. expected1 := []string{"", "Line 1", "", "", "L ine4", "Line 5", ""}
  37. testFn(byteArray1, expected1)
  38. byteArray1 = []byte("")
  39. expected1 = []string{}
  40. testFn(byteArray1, expected1)
  41. byteArray1 = []byte("\n\n")
  42. expected1 = []string{"", ""}
  43. testFn(byteArray1, expected1)
  44. }