2021-03-12 12:57:23 +00:00
|
|
|
package sessiontest
|
|
|
|
|
|
|
|
import (
|
2024-08-26 11:13:20 +00:00
|
|
|
crand "crypto/rand"
|
2023-08-11 12:21:17 +00:00
|
|
|
"math/rand"
|
|
|
|
"time"
|
|
|
|
|
2023-03-07 10:38:56 +00:00
|
|
|
acltest "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/acl/test"
|
|
|
|
refstest "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/refs/test"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/session"
|
|
|
|
statustest "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/status/test"
|
2021-03-12 12:57:23 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func GenerateCreateRequestBody(empty bool) *session.CreateRequestBody {
|
|
|
|
m := new(session.CreateRequestBody)
|
|
|
|
|
|
|
|
if !empty {
|
|
|
|
m.SetExpiration(555)
|
2021-06-11 12:44:16 +00:00
|
|
|
m.SetOwnerID(refstest.GenerateOwnerID(false))
|
2021-03-12 12:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateCreateRequest(empty bool) *session.CreateRequest {
|
|
|
|
m := new(session.CreateRequest)
|
|
|
|
|
2021-06-11 12:44:16 +00:00
|
|
|
if !empty {
|
|
|
|
m.SetBody(GenerateCreateRequestBody(false))
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:57:23 +00:00
|
|
|
m.SetMetaHeader(GenerateRequestMetaHeader(empty))
|
|
|
|
m.SetVerificationHeader(GenerateRequestVerificationHeader(empty))
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateCreateResponseBody(empty bool) *session.CreateResponseBody {
|
|
|
|
m := new(session.CreateResponseBody)
|
|
|
|
|
|
|
|
if !empty {
|
2024-08-26 11:13:20 +00:00
|
|
|
id := make([]byte, 16)
|
|
|
|
_, _ = crand.Read(id)
|
|
|
|
|
|
|
|
m.SetID(id)
|
2021-03-12 12:57:23 +00:00
|
|
|
m.SetSessionKey([]byte{4, 5, 6})
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateCreateResponse(empty bool) *session.CreateResponse {
|
|
|
|
m := new(session.CreateResponse)
|
|
|
|
|
2021-06-11 12:44:16 +00:00
|
|
|
if !empty {
|
|
|
|
m.SetBody(GenerateCreateResponseBody(false))
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:57:23 +00:00
|
|
|
m.SetMetaHeader(GenerateResponseMetaHeader(empty))
|
|
|
|
m.SetVerificationHeader(GenerateResponseVerificationHeader(empty))
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateResponseVerificationHeader(empty bool) *session.ResponseVerificationHeader {
|
|
|
|
return generateResponseVerificationHeader(empty, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateResponseVerificationHeader(empty, withOrigin bool) *session.ResponseVerificationHeader {
|
|
|
|
m := new(session.ResponseVerificationHeader)
|
|
|
|
|
2021-06-11 12:44:16 +00:00
|
|
|
if !empty {
|
|
|
|
m.SetBodySignature(refstest.GenerateSignature(false))
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:57:23 +00:00
|
|
|
m.SetMetaSignature(refstest.GenerateSignature(empty))
|
|
|
|
m.SetOriginSignature(refstest.GenerateSignature(empty))
|
|
|
|
|
|
|
|
if withOrigin {
|
|
|
|
m.SetOrigin(generateResponseVerificationHeader(empty, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateResponseMetaHeader(empty bool) *session.ResponseMetaHeader {
|
|
|
|
return generateResponseMetaHeader(empty, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateResponseMetaHeader(empty, withOrigin bool) *session.ResponseMetaHeader {
|
|
|
|
m := new(session.ResponseMetaHeader)
|
|
|
|
|
|
|
|
if !empty {
|
|
|
|
m.SetEpoch(13)
|
|
|
|
m.SetTTL(100)
|
|
|
|
}
|
|
|
|
|
|
|
|
m.SetXHeaders(GenerateXHeaders(empty))
|
|
|
|
m.SetVersion(refstest.GenerateVersion(empty))
|
2021-11-06 10:40:15 +00:00
|
|
|
m.SetStatus(statustest.Status(empty))
|
2021-03-12 12:57:23 +00:00
|
|
|
|
|
|
|
if withOrigin {
|
|
|
|
m.SetOrigin(generateResponseMetaHeader(empty, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateRequestVerificationHeader(empty bool) *session.RequestVerificationHeader {
|
|
|
|
return generateRequestVerificationHeader(empty, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRequestVerificationHeader(empty, withOrigin bool) *session.RequestVerificationHeader {
|
|
|
|
m := new(session.RequestVerificationHeader)
|
|
|
|
|
2021-06-11 12:44:16 +00:00
|
|
|
if !empty {
|
|
|
|
m.SetBodySignature(refstest.GenerateSignature(false))
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:57:23 +00:00
|
|
|
m.SetMetaSignature(refstest.GenerateSignature(empty))
|
|
|
|
m.SetOriginSignature(refstest.GenerateSignature(empty))
|
|
|
|
|
|
|
|
if withOrigin {
|
|
|
|
m.SetOrigin(generateRequestVerificationHeader(empty, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateRequestMetaHeader(empty bool) *session.RequestMetaHeader {
|
|
|
|
return generateRequestMetaHeader(empty, true)
|
|
|
|
}
|
|
|
|
|
|
|
|
func generateRequestMetaHeader(empty, withOrigin bool) *session.RequestMetaHeader {
|
|
|
|
m := new(session.RequestMetaHeader)
|
|
|
|
|
|
|
|
if !empty {
|
|
|
|
m.SetEpoch(13)
|
|
|
|
m.SetTTL(100)
|
2022-01-13 12:25:03 +00:00
|
|
|
m.SetNetworkMagic(1337)
|
2021-03-12 12:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
m.SetXHeaders(GenerateXHeaders(empty))
|
|
|
|
m.SetVersion(refstest.GenerateVersion(empty))
|
|
|
|
m.SetSessionToken(GenerateSessionToken(empty))
|
|
|
|
m.SetBearerToken(acltest.GenerateBearerToken(empty))
|
|
|
|
|
|
|
|
if withOrigin {
|
|
|
|
m.SetOrigin(generateRequestMetaHeader(empty, false))
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-02-25 07:36:46 +00:00
|
|
|
func GenerateSessionToken(empty bool) *session.Token {
|
|
|
|
m := new(session.Token)
|
2021-03-12 12:57:23 +00:00
|
|
|
|
2021-06-11 12:44:16 +00:00
|
|
|
if !empty {
|
|
|
|
m.SetBody(GenerateSessionTokenBody(false))
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:57:23 +00:00
|
|
|
m.SetSignature(refstest.GenerateSignature(empty))
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-02-25 07:36:46 +00:00
|
|
|
func GenerateSessionTokenBody(empty bool) *session.TokenBody {
|
|
|
|
m := new(session.TokenBody)
|
2021-03-12 12:57:23 +00:00
|
|
|
|
|
|
|
if !empty {
|
2024-08-26 11:13:20 +00:00
|
|
|
id := make([]byte, 16)
|
|
|
|
_, _ = crand.Read(id)
|
|
|
|
|
|
|
|
m.SetID(id)
|
2021-03-12 12:57:23 +00:00
|
|
|
m.SetSessionKey([]byte{2})
|
2021-06-11 12:44:16 +00:00
|
|
|
m.SetOwnerID(refstest.GenerateOwnerID(false))
|
|
|
|
m.SetLifetime(GenerateTokenLifetime(false))
|
2022-05-30 18:13:35 +00:00
|
|
|
|
2023-08-11 12:21:17 +00:00
|
|
|
switch randomInt(2) {
|
2022-05-30 18:13:35 +00:00
|
|
|
case 0:
|
|
|
|
m.SetContext(GenerateObjectSessionContext(false))
|
|
|
|
case 1:
|
|
|
|
m.SetContext(GenerateContainerSessionContext(false))
|
|
|
|
}
|
2021-03-12 12:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateTokenLifetime(empty bool) *session.TokenLifetime {
|
|
|
|
m := new(session.TokenLifetime)
|
|
|
|
|
|
|
|
if !empty {
|
|
|
|
m.SetExp(1)
|
|
|
|
m.SetIat(2)
|
|
|
|
m.SetExp(3)
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
|
|
|
func GenerateObjectSessionContext(empty bool) *session.ObjectSessionContext {
|
|
|
|
m := new(session.ObjectSessionContext)
|
|
|
|
|
|
|
|
if !empty {
|
|
|
|
m.SetVerb(session.ObjectVerbHead)
|
2022-09-16 15:09:25 +00:00
|
|
|
m.SetTarget(refstest.GenerateContainerID(false), refstest.GenerateObjectIDs(false)...)
|
2021-03-12 12:57:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2021-05-24 14:46:39 +00:00
|
|
|
func GenerateContainerSessionContext(empty bool) *session.ContainerSessionContext {
|
|
|
|
m := new(session.ContainerSessionContext)
|
|
|
|
|
|
|
|
if !empty {
|
|
|
|
m.SetVerb(session.ContainerVerbDelete)
|
|
|
|
m.SetWildcard(true)
|
2021-06-11 12:44:16 +00:00
|
|
|
m.SetContainerID(refstest.GenerateContainerID(false))
|
2021-05-24 14:46:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2021-03-12 12:57:23 +00:00
|
|
|
func GenerateXHeader(empty bool) *session.XHeader {
|
|
|
|
m := new(session.XHeader)
|
|
|
|
|
|
|
|
if !empty {
|
|
|
|
m.SetKey("key")
|
|
|
|
m.SetValue("val")
|
|
|
|
}
|
|
|
|
|
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2022-03-02 08:01:03 +00:00
|
|
|
func GenerateXHeaders(empty bool) []session.XHeader {
|
|
|
|
var xs []session.XHeader
|
2021-03-12 12:57:23 +00:00
|
|
|
|
|
|
|
if !empty {
|
|
|
|
xs = append(xs,
|
2022-03-02 08:01:03 +00:00
|
|
|
*GenerateXHeader(false),
|
|
|
|
*GenerateXHeader(false),
|
2021-03-12 12:57:23 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
return xs
|
|
|
|
}
|
2023-08-11 12:21:17 +00:00
|
|
|
|
|
|
|
func randomInt(n int) int {
|
|
|
|
return rand.New(rand.NewSource(time.Now().UnixNano())).Intn(n)
|
|
|
|
}
|