gitee.com/ks-custle/core-gm@v0.0.0-20230922171213-b83bdd97b62c/grpc/credentials/alts/internal/conn/counter_test.go (about) 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 19 package conn 20 21 import ( 22 "bytes" 23 "testing" 24 25 core "gitee.com/ks-custle/core-gm/grpc/credentials/alts/internal" 26 ) 27 28 const ( 29 testOverflowLen = 5 30 ) 31 32 func (s) TestCounterSides(t *testing.T) { 33 for _, side := range []core.Side{core.ClientSide, core.ServerSide} { 34 outCounter := NewOutCounter(side, testOverflowLen) 35 inCounter := NewInCounter(side, testOverflowLen) 36 for i := 0; i < 1024; i++ { 37 value, _ := outCounter.Value() 38 if g, w := CounterSide(value), side; g != w { 39 t.Errorf("after %d iterations, CounterSide(outCounter.Value()) = %v, want %v", i, g, w) 40 break 41 } 42 value, _ = inCounter.Value() 43 if g, w := CounterSide(value), side; g == w { 44 t.Errorf("after %d iterations, CounterSide(inCounter.Value()) = %v, want %v", i, g, w) 45 break 46 } 47 outCounter.Inc() 48 inCounter.Inc() 49 } 50 } 51 } 52 53 func (s) TestCounterInc(t *testing.T) { 54 for _, test := range []struct { 55 counter []byte 56 want []byte 57 }{ 58 { 59 counter: []byte{0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 60 want: []byte{0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 61 }, 62 { 63 counter: []byte{0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80}, 64 want: []byte{0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x80}, 65 }, 66 { 67 counter: []byte{0xff, 0x00, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 68 want: []byte{0x00, 0x01, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 69 }, 70 { 71 counter: []byte{0x42, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 72 want: []byte{0x43, 0xff, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, 73 }, 74 { 75 counter: []byte{0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 76 want: []byte{0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 77 }, 78 { 79 counter: []byte{0xff, 0xff, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}, 80 want: []byte{0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}, 81 }, 82 } { 83 c := CounterFromValue(test.counter, overflowLenAES128GCM) 84 c.Inc() 85 value, _ := c.Value() 86 if g, w := value, test.want; !bytes.Equal(g, w) || c.invalid { 87 t.Errorf("counter(%v).Inc() =\n%v, want\n%v", test.counter, g, w) 88 } 89 } 90 } 91 92 func (s) TestRolloverCounter(t *testing.T) { 93 for _, test := range []struct { 94 desc string 95 value []byte 96 overflowLen int 97 }{ 98 { 99 desc: "testing overflow without rekeying 1", 100 value: []byte{0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x80}, 101 overflowLen: 5, 102 }, 103 { 104 desc: "testing overflow without rekeying 2", 105 value: []byte{0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 106 overflowLen: 5, 107 }, 108 { 109 desc: "testing overflow for rekeying mode 1", 110 value: []byte{0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x80}, 111 overflowLen: 8, 112 }, 113 { 114 desc: "testing overflow for rekeying mode 2", 115 value: []byte{0xFE, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00}, 116 overflowLen: 8, 117 }, 118 } { 119 c := CounterFromValue(test.value, overflowLenAES128GCM) 120 121 // First Inc() + Value() should work. 122 c.Inc() 123 _, err := c.Value() 124 if err != nil { 125 t.Errorf("%v: first Inc() + Value() unexpectedly failed: %v, want <nil> error", test.desc, err) 126 } 127 // Second Inc() + Value() should fail. 128 c.Inc() 129 _, err = c.Value() 130 if err != errInvalidCounter { 131 t.Errorf("%v: second Inc() + Value() unexpectedly succeeded: want %v", test.desc, errInvalidCounter) 132 } 133 // Third Inc() + Value() should also fail because the counter is 134 // already in an invalid state. 135 c.Inc() 136 _, err = c.Value() 137 if err != errInvalidCounter { 138 t.Errorf("%v: Third Inc() + Value() unexpectedly succeeded: want %v", test.desc, errInvalidCounter) 139 } 140 } 141 }