| 79 | } |
| 80 | |
| 81 | func TestCancelRequestEncode(t *testing.T) { |
| 82 | secretKey32 := []byte{ |
| 83 | 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, |
| 84 | 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, |
| 85 | 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17, 0x18, |
| 86 | 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F, 0x20, |
| 87 | } |
| 88 | |
| 89 | tests := []struct { |
| 90 | name string |
| 91 | msg CancelRequest |
| 92 | expected []byte |
| 93 | expectError bool |
| 94 | }{ |
| 95 | { |
| 96 | name: "Protocol 3.0 (4-byte key)", |
| 97 | msg: CancelRequest{ |
| 98 | ProcessID: 8864, |
| 99 | SecretKey: []byte{0xD9, 0x0C, 0xAE, 0xDB}, |
| 100 | }, |
| 101 | expected: []byte{ |
| 102 | 0x00, 0x00, 0x00, 0x10, // length: 16 |
| 103 | 0x04, 0xD2, 0x16, 0x2E, // cancelRequestCode: 80877102 |
| 104 | 0x00, 0x00, 0x22, 0xA0, // ProcessID: 8864 |
| 105 | 0xD9, 0x0C, 0xAE, 0xDB, // SecretKey |
| 106 | }, |
| 107 | }, |
| 108 | { |
| 109 | name: "Protocol 3.2 (32-byte key)", |
| 110 | msg: CancelRequest{ |
| 111 | ProcessID: 8864, |
| 112 | SecretKey: secretKey32, |
| 113 | }, |
| 114 | // 4 byte length + 4 byte code + 4 byte ProcessID + 32 byte SecretKey = 44 bytes total |
| 115 | expected: append([]byte{ |
| 116 | 0x00, 0x00, 0x00, 0x2C, // length: 44 (12 + 32) |
| 117 | 0x04, 0xD2, 0x16, 0x2E, // cancelRequestCode: 80877102 |
| 118 | 0x00, 0x00, 0x22, 0xA0, // ProcessID: 8864 |
| 119 | }, secretKey32...), |
| 120 | }, |
| 121 | { |
| 122 | name: "Too long secret key", |
| 123 | msg: CancelRequest{ |
| 124 | ProcessID: 8864, |
| 125 | SecretKey: make([]byte, 257), // 1 byte too many |
| 126 | }, |
| 127 | expectError: true, |
| 128 | }, |
| 129 | } |
| 130 | |
| 131 | for _, tt := range tests { |
| 132 | t.Run(tt.name, func(t *testing.T) { |
| 133 | buf, err := tt.msg.Encode(nil) |
| 134 | if tt.expectError { |
| 135 | require.Error(t, err) |
| 136 | return |
| 137 | } |
| 138 | require.NoError(t, err) |