github.com/pawelgaczynski/giouring@v0.0.0-20230826085535-69588b89acb9/prepare_test.go (about) 1 // MIT License 2 // 3 // Copyright (c) 2023 Paweł Gaczyński 4 // 5 // Permission is hereby granted, free of charge, to any person obtaining a 6 // copy of this software and associated documentation files (the 7 // "Software"), to deal in the Software without restriction, including 8 // without limitation the rights to use, copy, modify, merge, publish, 9 // distribute, sublicense, and/or sell copies of the Software, and to 10 // permit persons to whom the Software is furnished to do so, subject to 11 // the following conditions: 12 // 13 // The above copyright notice and this permission notice shall be included 14 // in all copies or substantial portions of the Software. 15 // 16 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 17 // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 18 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 19 // IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY 20 // CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, 21 // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE 22 // SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 23 24 package giouring 25 26 import ( 27 "syscall" 28 "testing" 29 "time" 30 "unsafe" 31 32 . "github.com/stretchr/testify/require" 33 ) 34 35 func TestPrepareMsgRing(t *testing.T) { 36 entry := &SubmissionQueueEntry{} 37 entry.PrepareMsgRing(10, 100, 200, 60) 38 39 Equal(t, uint8(40), entry.OpCode) 40 Equal(t, uint8(0), entry.Flags) 41 Equal(t, uint16(0), entry.IoPrio) 42 Equal(t, int32(10), entry.Fd) 43 Equal(t, uint64(200), entry.Off) 44 Equal(t, uint64(0), entry.Addr) 45 Equal(t, uint32(100), entry.Len) 46 Equal(t, uint32(60), entry.OpcodeFlags) 47 Equal(t, uint64(0), entry.UserData) 48 Equal(t, uint16(0), entry.BufIG) 49 Equal(t, uint16(0), entry.Personality) 50 Equal(t, int32(0), entry.SpliceFdIn) 51 } 52 53 func TestPrepareAccept(t *testing.T) { 54 entry := &SubmissionQueueEntry{} 55 entry.PrepareAccept(10, 100, 200, 60) 56 57 Equal(t, uint8(13), entry.OpCode) 58 Equal(t, uint8(0), entry.Flags) 59 Equal(t, uint16(0), entry.IoPrio) 60 Equal(t, int32(10), entry.Fd) 61 Equal(t, uint64(200), entry.Off) 62 Equal(t, uint64(100), entry.Addr) 63 Equal(t, uint32(0), entry.Len) 64 Equal(t, uint32(60), entry.OpcodeFlags) 65 Equal(t, uint64(0), entry.UserData) 66 Equal(t, uint16(0), entry.BufIG) 67 Equal(t, uint16(0), entry.Personality) 68 Equal(t, int32(0), entry.SpliceFdIn) 69 } 70 71 func TestPrepareClose(t *testing.T) { 72 entry := &SubmissionQueueEntry{} 73 entry.PrepareClose(10) 74 75 Equal(t, uint8(19), entry.OpCode) 76 Equal(t, uint8(0), entry.Flags) 77 Equal(t, uint16(0), entry.IoPrio) 78 Equal(t, int32(10), entry.Fd) 79 Equal(t, uint64(0), entry.Off) 80 Equal(t, uint64(0), entry.Addr) 81 Equal(t, uint32(0), entry.Len) 82 Equal(t, uint32(0), entry.OpcodeFlags) 83 Equal(t, uint64(0), entry.UserData) 84 Equal(t, uint16(0), entry.BufIG) 85 Equal(t, uint16(0), entry.Personality) 86 Equal(t, int32(0), entry.SpliceFdIn) 87 } 88 89 func TestPrepareCloseDirect(t *testing.T) { 90 entry := &SubmissionQueueEntry{} 91 entry.PrepareCloseDirect(10) 92 93 Equal(t, uint8(19), entry.OpCode) 94 Equal(t, uint8(0), entry.Flags) 95 Equal(t, uint16(0), entry.IoPrio) 96 Equal(t, int32(0), entry.Fd) 97 Equal(t, uint64(0), entry.Off) 98 Equal(t, uint64(0), entry.Addr) 99 Equal(t, uint32(0), entry.Len) 100 Equal(t, uint32(0), entry.OpcodeFlags) 101 Equal(t, uint64(0), entry.UserData) 102 Equal(t, uint16(0), entry.BufIG) 103 Equal(t, uint16(0), entry.Personality) 104 Equal(t, int32(11), entry.SpliceFdIn) 105 } 106 107 func TestPrepareReadv(t *testing.T) { 108 entry := &SubmissionQueueEntry{} 109 entry.PrepareReadv(10, uintptr(12345), 60, 10) 110 111 Equal(t, uint8(1), entry.OpCode) 112 Equal(t, uint8(0), entry.Flags) 113 Equal(t, uint16(0), entry.IoPrio) 114 Equal(t, int32(10), entry.Fd) 115 Equal(t, uint64(10), entry.Off) 116 Equal(t, uint64(12345), entry.Addr) 117 Equal(t, uint32(60), entry.Len) 118 Equal(t, uint32(0), entry.OpcodeFlags) 119 Equal(t, uint64(0), entry.UserData) 120 Equal(t, uint16(0), entry.BufIG) 121 Equal(t, uint16(0), entry.Personality) 122 Equal(t, int32(0), entry.SpliceFdIn) 123 } 124 125 func TestPrepareReadv2(t *testing.T) { 126 entry := &SubmissionQueueEntry{} 127 entry.PrepareReadv2(10, uintptr(12345), 60, 10, 15) 128 129 Equal(t, uint8(1), entry.OpCode) 130 Equal(t, uint8(0), entry.Flags) 131 Equal(t, uint16(0), entry.IoPrio) 132 Equal(t, int32(10), entry.Fd) 133 Equal(t, uint64(10), entry.Off) 134 Equal(t, uint64(12345), entry.Addr) 135 Equal(t, uint32(60), entry.Len) 136 Equal(t, uint32(15), entry.OpcodeFlags) 137 Equal(t, uint64(0), entry.UserData) 138 Equal(t, uint16(0), entry.BufIG) 139 Equal(t, uint16(0), entry.Personality) 140 Equal(t, int32(0), entry.SpliceFdIn) 141 } 142 143 func TestPrepareReadFixed(t *testing.T) { 144 entry := &SubmissionQueueEntry{} 145 entry.PrepareReadFixed(10, uintptr(12345), 60, 10, 15) 146 147 Equal(t, uint8(4), entry.OpCode) 148 Equal(t, uint8(0), entry.Flags) 149 Equal(t, uint16(0), entry.IoPrio) 150 Equal(t, int32(10), entry.Fd) 151 Equal(t, uint64(10), entry.Off) 152 Equal(t, uint64(12345), entry.Addr) 153 Equal(t, uint32(60), entry.Len) 154 Equal(t, uint32(0), entry.OpcodeFlags) 155 Equal(t, uint64(0), entry.UserData) 156 Equal(t, uint16(15), entry.BufIG) 157 Equal(t, uint16(0), entry.Personality) 158 Equal(t, int32(0), entry.SpliceFdIn) 159 } 160 161 func TestPrepareWritev(t *testing.T) { 162 entry := &SubmissionQueueEntry{} 163 entry.PrepareWritev(10, uintptr(12345), 60, 10) 164 165 Equal(t, uint8(2), entry.OpCode) 166 Equal(t, uint8(0), entry.Flags) 167 Equal(t, uint16(0), entry.IoPrio) 168 Equal(t, int32(10), entry.Fd) 169 Equal(t, uint64(10), entry.Off) 170 Equal(t, uint64(12345), entry.Addr) 171 Equal(t, uint32(60), entry.Len) 172 Equal(t, uint32(0), entry.OpcodeFlags) 173 Equal(t, uint64(0), entry.UserData) 174 Equal(t, uint16(0), entry.BufIG) 175 Equal(t, uint16(0), entry.Personality) 176 Equal(t, int32(0), entry.SpliceFdIn) 177 } 178 179 func TestPrepareWritev2(t *testing.T) { 180 entry := &SubmissionQueueEntry{} 181 entry.PrepareWritev2(10, uintptr(12345), 60, 10, 15) 182 183 Equal(t, uint8(2), entry.OpCode) 184 Equal(t, uint8(0), entry.Flags) 185 Equal(t, uint16(0), entry.IoPrio) 186 Equal(t, int32(10), entry.Fd) 187 Equal(t, uint64(10), entry.Off) 188 Equal(t, uint64(12345), entry.Addr) 189 Equal(t, uint32(60), entry.Len) 190 Equal(t, uint32(15), entry.OpcodeFlags) 191 Equal(t, uint64(0), entry.UserData) 192 Equal(t, uint16(0), entry.BufIG) 193 Equal(t, uint16(0), entry.Personality) 194 Equal(t, int32(0), entry.SpliceFdIn) 195 } 196 197 func TestPrepareWriteFixed(t *testing.T) { 198 entry := &SubmissionQueueEntry{} 199 entry.PrepareWriteFixed(10, uintptr(12345), 60, 10, 15) 200 201 Equal(t, uint8(5), entry.OpCode) 202 Equal(t, uint8(0), entry.Flags) 203 Equal(t, uint16(0), entry.IoPrio) 204 Equal(t, int32(10), entry.Fd) 205 Equal(t, uint64(10), entry.Off) 206 Equal(t, uint64(12345), entry.Addr) 207 Equal(t, uint32(60), entry.Len) 208 Equal(t, uint32(0), entry.OpcodeFlags) 209 Equal(t, uint64(0), entry.UserData) 210 Equal(t, uint16(15), entry.BufIG) 211 Equal(t, uint16(0), entry.Personality) 212 Equal(t, int32(0), entry.SpliceFdIn) 213 } 214 215 func TestPrepareSendMsg(t *testing.T) { 216 entry := &SubmissionQueueEntry{} 217 entry.PrepareSend(10, uintptr(12345), 60, 10) 218 219 Equal(t, uint8(26), entry.OpCode) 220 Equal(t, uint8(0), entry.Flags) 221 Equal(t, uint16(0), entry.IoPrio) 222 Equal(t, int32(10), entry.Fd) 223 Equal(t, uint64(0), entry.Off) 224 Equal(t, uint64(12345), entry.Addr) 225 Equal(t, uint32(60), entry.Len) 226 Equal(t, uint32(10), entry.OpcodeFlags) 227 Equal(t, uint64(0), entry.UserData) 228 Equal(t, uint16(0), entry.BufIG) 229 Equal(t, uint16(0), entry.Personality) 230 Equal(t, int32(0), entry.SpliceFdIn) 231 } 232 233 func TestPrepareNop(t *testing.T) { 234 entry := &SubmissionQueueEntry{} 235 entry.PrepareNop() 236 237 Equal(t, uint8(0), entry.OpCode) 238 Equal(t, uint8(0), entry.Flags) 239 Equal(t, uint16(0), entry.IoPrio) 240 Equal(t, int32(-1), entry.Fd) 241 Equal(t, uint64(0), entry.Off) 242 Equal(t, uint64(0), entry.Addr) 243 Equal(t, uint32(0), entry.Len) 244 Equal(t, uint32(0), entry.OpcodeFlags) 245 Equal(t, uint64(0), entry.UserData) 246 Equal(t, uint16(0), entry.BufIG) 247 Equal(t, uint16(0), entry.Personality) 248 Equal(t, int32(0), entry.SpliceFdIn) 249 } 250 251 func TestPrepareTimeout(t *testing.T) { 252 entry := &SubmissionQueueEntry{} 253 duration := time.Second 254 spec := syscall.NsecToTimespec(duration.Nanoseconds()) 255 entry.PrepareTimeout(&spec, 10, 15) 256 257 Equal(t, uint8(11), entry.OpCode) 258 Equal(t, uint8(0), entry.Flags) 259 Equal(t, uint16(0), entry.IoPrio) 260 Equal(t, int32(-1), entry.Fd) 261 Equal(t, uint64(10), entry.Off) 262 NotZero(t, entry.Addr) 263 Equal(t, uint32(1), entry.Len) 264 Equal(t, uint32(15), entry.OpcodeFlags) 265 Equal(t, uint64(0), entry.UserData) 266 Equal(t, uint16(0), entry.BufIG) 267 Equal(t, uint16(0), entry.Personality) 268 Equal(t, int32(0), entry.SpliceFdIn) 269 } 270 271 func TestPrepareTimeoutRemove(t *testing.T) { 272 entry := &SubmissionQueueEntry{} 273 duration := time.Second 274 entry.PrepareTimeoutRemove(duration, 10, 15) 275 276 Equal(t, uint8(12), entry.OpCode) 277 Equal(t, uint8(0), entry.Flags) 278 Equal(t, uint16(0), entry.IoPrio) 279 Equal(t, int32(-1), entry.Fd) 280 Equal(t, uint64(10), entry.Off) 281 NotZero(t, entry.Addr) 282 Equal(t, uint32(1), entry.Len) 283 Equal(t, uint32(15), entry.OpcodeFlags) 284 Equal(t, uint64(0), entry.UserData) 285 Equal(t, uint16(0), entry.BufIG) 286 Equal(t, uint16(0), entry.Personality) 287 Equal(t, int32(0), entry.SpliceFdIn) 288 } 289 290 func TestPrepareTimeoutUpdate(t *testing.T) { 291 entry := &SubmissionQueueEntry{} 292 duration := time.Second 293 entry.PrepareTimeoutUpdate(duration, 10, 15) 294 295 Equal(t, uint8(12), entry.OpCode) 296 Equal(t, uint8(0), entry.Flags) 297 Equal(t, uint16(0), entry.IoPrio) 298 Equal(t, int32(-1), entry.Fd) 299 Equal(t, uint64(10), entry.Off) 300 NotZero(t, entry.Addr) 301 Equal(t, uint32(1), entry.Len) 302 Equal(t, uint32(15), entry.OpcodeFlags) 303 Equal(t, uint64(0), entry.UserData) 304 Equal(t, uint16(0), entry.BufIG) 305 Equal(t, uint16(0), entry.Personality) 306 Equal(t, int32(0), entry.SpliceFdIn) 307 } 308 309 func TestPrepareAcceptDirect(t *testing.T) { 310 entry := &SubmissionQueueEntry{} 311 entry.PrepareAcceptDirect(10, uintptr(12345), 10, 15, 7) 312 313 Equal(t, uint8(13), entry.OpCode) 314 Equal(t, uint8(0), entry.Flags) 315 Equal(t, uint16(0), entry.IoPrio) 316 Equal(t, int32(10), entry.Fd) 317 Equal(t, uint64(10), entry.Off) 318 Equal(t, uint64(12345), entry.Addr) 319 Equal(t, uint32(0), entry.Len) 320 Equal(t, uint32(15), entry.OpcodeFlags) 321 Equal(t, uint64(0), entry.UserData) 322 Equal(t, uint16(0), entry.BufIG) 323 Equal(t, uint16(0), entry.Personality) 324 Equal(t, int32(8), entry.SpliceFdIn) 325 } 326 327 func TestPrepareSend(t *testing.T) { 328 entry := &SubmissionQueueEntry{} 329 entry.PrepareSend(10, uintptr(12345), 10, 15) 330 331 Equal(t, uint8(26), entry.OpCode) 332 Equal(t, uint8(0), entry.Flags) 333 Equal(t, uint16(0), entry.IoPrio) 334 Equal(t, int32(10), entry.Fd) 335 Equal(t, uint64(0), entry.Off) 336 Equal(t, uint64(12345), entry.Addr) 337 Equal(t, uint32(10), entry.Len) 338 Equal(t, uint32(15), entry.OpcodeFlags) 339 Equal(t, uint64(0), entry.UserData) 340 Equal(t, uint16(0), entry.BufIG) 341 Equal(t, uint16(0), entry.Personality) 342 Equal(t, int32(0), entry.SpliceFdIn) 343 } 344 345 func TestPrepareRecv(t *testing.T) { 346 entry := &SubmissionQueueEntry{} 347 entry.PrepareRecv(10, uintptr(12345), 10, 15) 348 349 Equal(t, uint8(27), entry.OpCode) 350 Equal(t, uint8(0), entry.Flags) 351 Equal(t, uint16(0), entry.IoPrio) 352 Equal(t, int32(10), entry.Fd) 353 Equal(t, uint64(0), entry.Off) 354 Equal(t, uint64(12345), entry.Addr) 355 Equal(t, uint32(10), entry.Len) 356 Equal(t, uint32(15), entry.OpcodeFlags) 357 Equal(t, uint64(0), entry.UserData) 358 Equal(t, uint16(0), entry.BufIG) 359 Equal(t, uint16(0), entry.Personality) 360 Equal(t, int32(0), entry.SpliceFdIn) 361 } 362 363 func TestPrepareRecvMsg(t *testing.T) { 364 var ( 365 msg syscall.Msghdr 366 entry = &SubmissionQueueEntry{} 367 ) 368 msgAddr := uint64(uintptr(unsafe.Pointer(&msg))) 369 370 entry.PrepareRecvMsg(10, &msg, 15) 371 372 Equal(t, uint8(10), entry.OpCode) 373 Equal(t, uint8(0), entry.Flags) 374 Equal(t, uint16(0), entry.IoPrio) 375 Equal(t, int32(10), entry.Fd) 376 Equal(t, uint64(0), entry.Off) 377 Equal(t, msgAddr, entry.Addr) 378 Equal(t, uint32(1), entry.Len) 379 Equal(t, uint32(15), entry.OpcodeFlags) 380 Equal(t, uint64(0), entry.UserData) 381 Equal(t, uint16(0), entry.BufIG) 382 Equal(t, uint16(0), entry.Personality) 383 Equal(t, int32(0), entry.SpliceFdIn) 384 } 385 386 func TestPrepareRecvMultishot(t *testing.T) { 387 entry := &SubmissionQueueEntry{} 388 entry.PrepareRecvMultishot(10, uintptr(12345), 10, 15) 389 390 Equal(t, uint8(27), entry.OpCode) 391 Equal(t, uint8(0), entry.Flags) 392 Equal(t, uint16(2), entry.IoPrio) 393 Equal(t, int32(10), entry.Fd) 394 Equal(t, uint64(0), entry.Off) 395 Equal(t, uint64(12345), entry.Addr) 396 Equal(t, uint32(10), entry.Len) 397 Equal(t, uint32(15), entry.OpcodeFlags) 398 Equal(t, uint64(0), entry.UserData) 399 Equal(t, uint16(0), entry.BufIG) 400 Equal(t, uint16(0), entry.Personality) 401 Equal(t, int32(0), entry.SpliceFdIn) 402 } 403 404 func TestPrepareProvideBuffers(t *testing.T) { 405 entry := &SubmissionQueueEntry{} 406 entry.PrepareProvideBuffers(uintptr(12345), 16, 10, 3, 10) 407 408 Equal(t, uint8(31), entry.OpCode) 409 Equal(t, uint8(0), entry.Flags) 410 Equal(t, uint16(0), entry.IoPrio) 411 Equal(t, int32(10), entry.Fd) 412 Equal(t, uint64(10), entry.Off) 413 Equal(t, uint64(12345), entry.Addr) 414 Equal(t, uint32(16), entry.Len) 415 Equal(t, uint32(0), entry.OpcodeFlags) 416 Equal(t, uint64(0), entry.UserData) 417 Equal(t, uint16(3), entry.BufIG) 418 Equal(t, uint16(0), entry.Personality) 419 Equal(t, int32(0), entry.SpliceFdIn) 420 }