You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

142 lines
4.2 KiB

  1. /*
  2. *
  3. * Copyright 2018 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package conn
  19. import (
  20. "bytes"
  21. "testing"
  22. core "google.golang.org/grpc/credentials/alts/internal"
  23. )
  24. const (
  25. testOverflowLen = 5
  26. )
  27. func TestCounterSides(t *testing.T) {
  28. for _, side := range []core.Side{core.ClientSide, core.ServerSide} {
  29. outCounter := NewOutCounter(side, testOverflowLen)
  30. inCounter := NewInCounter(side, testOverflowLen)
  31. for i := 0; i < 1024; i++ {
  32. value, _ := outCounter.Value()
  33. if g, w := CounterSide(value), side; g != w {
  34. t.Errorf("after %d iterations, CounterSide(outCounter.Value()) = %v, want %v", i, g, w)
  35. break
  36. }
  37. value, _ = inCounter.Value()
  38. if g, w := CounterSide(value), side; g == w {
  39. t.Errorf("after %d iterations, CounterSide(inCounter.Value()) = %v, want %v", i, g, w)
  40. break
  41. }
  42. outCounter.Inc()
  43. inCounter.Inc()
  44. }
  45. }
  46. }
  47. func TestCounterInc(t *testing.T) {
  48. for _, test := range []struct {
  49. counter []byte
  50. want []byte
  51. }{
  52. {
  53. counter: []byte{0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  54. want: []byte{0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  55. },
  56. {
  57. counter: []byte{0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80},
  58. want: []byte{0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80},
  59. },
  60. {
  61. counter: []byte{0xff, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  62. want: []byte{0x00, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  63. },
  64. {
  65. counter: []byte{0x42, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  66. want: []byte{0x43, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
  67. },
  68. {
  69. counter: []byte{0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  70. want: []byte{0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  71. },
  72. {
  73. counter: []byte{0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80},
  74. want: []byte{0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80},
  75. },
  76. } {
  77. c := CounterFromValue(test.counter, overflowLenAES128GCM)
  78. c.Inc()
  79. value, _ := c.Value()
  80. if g, w := value, test.want; !bytes.Equal(g, w) || c.invalid {
  81. t.Errorf("counter(%v).Inc() =\n%v, want\n%v", test.counter, g, w)
  82. }
  83. }
  84. }
  85. func TestRolloverCounter(t *testing.T) {
  86. for _, test := range []struct {
  87. desc string
  88. value []byte
  89. overflowLen int
  90. }{
  91. {
  92. desc: "testing overflow without rekeying 1",
  93. value: []byte{0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80},
  94. overflowLen: 5,
  95. },
  96. {
  97. desc: "testing overflow without rekeying 2",
  98. value: []byte{0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
  99. overflowLen: 5,
  100. },
  101. {
  102. desc: "testing overflow for rekeying mode 1",
  103. value: []byte{0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x80},
  104. overflowLen: 8,
  105. },
  106. {
  107. desc: "testing overflow for rekeying mode 2",
  108. value: []byte{0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00},
  109. overflowLen: 8,
  110. },
  111. } {
  112. c := CounterFromValue(test.value, overflowLenAES128GCM)
  113. // First Inc() + Value() should work.
  114. c.Inc()
  115. _, err := c.Value()
  116. if err != nil {
  117. t.Errorf("%v: first Inc() + Value() unexpectedly failed: %v, want <nil> error", test.desc, err)
  118. }
  119. // Second Inc() + Value() should fail.
  120. c.Inc()
  121. _, err = c.Value()
  122. if err != errInvalidCounter {
  123. t.Errorf("%v: second Inc() + Value() unexpectedly succeeded: want %v", test.desc, errInvalidCounter)
  124. }
  125. // Third Inc() + Value() should also fail because the counter is
  126. // already in an invalid state.
  127. c.Inc()
  128. _, err = c.Value()
  129. if err != errInvalidCounter {
  130. t.Errorf("%v: Third Inc() + Value() unexpectedly succeeded: want %v", test.desc, errInvalidCounter)
  131. }
  132. }
  133. }