Repository navigation
test(go): cover access token command serialization #4640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
p4rtridge
wants to merge
7
commits into
apache:master
Choose a base branch
from
p4rtridge:go-access-token-serialization-2883
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+72
−14
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
e1b8ad2
test(go): cover access token command serialization
p4rtridge c883415
test(go): change test failure messages to use hex format
p4rtridge 0877902
Merge branch 'master' into go-access-token-serialization-2883
p4rtridge 3322fda
Merge branch 'master' into go-access-token-serialization-2883
p4rtridge 5784311
Merge branch 'master' into go-access-token-serialization-2883
p4rtridge 82fa460
test(go): simplify access token tests
p4rtridge e72bd78
Merge branch 'master' into go-access-token-serialization-2883
p4rtridge File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -24,26 +24,84 @@ import ( | |
| iggcon "github.com/apache/iggy/foreign/go/contracts" | ||
| ) | ||
|
|
||
| func TestCreatePersonalAccessTokenMarshalBinary(t *testing.T) { | ||
| tests := []struct { | ||
| name string | ||
| expiry iggcon.Duration | ||
| want []byte | ||
| func TestPersonalAccessTokenCommandsMarshalBinary(t *testing.T) { | ||
| for _, test := range []struct { | ||
| name string | ||
| request Command | ||
| code Code | ||
| want []byte | ||
| }{ | ||
| {"server default", iggcon.IggyExpiryServerDefault, []byte{5, 't', 'o', 'k', 'e', 'n', 0, 0, 0, 0, 0, 0, 0, 0}}, | ||
| {"little endian", 0x01020304, []byte{5, 't', 'o', 'k', 'e', 'n', 4, 3, 2, 1, 0, 0, 0, 0}}, | ||
| {"past 32 bits", 0x0102030405060708, []byte{5, 't', 'o', 'k', 'e', 'n', 8, 7, 6, 5, 4, 3, 2, 1}}, | ||
| {"never expire", iggcon.IggyExpiryNeverExpire, []byte{5, 't', 'o', 'k', 'e', 'n', 255, 255, 255, 255, 255, 255, 255, 255}}, | ||
| } | ||
|
|
||
| for _, test := range tests { | ||
| { | ||
| name: "server default expiry", | ||
| request: &CreatePersonalAccessToken{Name: "token", Expiry: iggcon.IggyExpiryServerDefault}, | ||
| code: 42, | ||
| want: []byte{5, 't', 'o', 'k', 'e', 'n', 0, 0, 0, 0, 0, 0, 0, 0}, | ||
| }, | ||
| { | ||
| name: "little endian expiry", | ||
| request: &CreatePersonalAccessToken{Name: "token", Expiry: 0x01020304}, | ||
| code: 42, | ||
| want: []byte{5, 't', 'o', 'k', 'e', 'n', 4, 3, 2, 1, 0, 0, 0, 0}, | ||
| }, | ||
| { | ||
| name: "expiry past 32 bits", | ||
| request: &CreatePersonalAccessToken{Name: "token", Expiry: 0x0102030405060708}, | ||
| code: 42, | ||
| want: []byte{5, 't', 'o', 'k', 'e', 'n', 8, 7, 6, 5, 4, 3, 2, 1}, | ||
| }, | ||
| { | ||
| name: "never expire", | ||
| request: &CreatePersonalAccessToken{Name: "token", Expiry: iggcon.IggyExpiryNeverExpire}, | ||
| code: 42, | ||
| want: []byte{5, 't', 'o', 'k', 'e', 'n', 255, 255, 255, 255, 255, 255, 255, 255}, | ||
| }, | ||
| { | ||
| name: "empty name", | ||
| request: &CreatePersonalAccessToken{Name: "", Expiry: 0x01020304}, | ||
| code: 42, | ||
| want: []byte{0, 4, 3, 2, 1, 0, 0, 0, 0}, | ||
| }, | ||
| { | ||
| name: "longer name", | ||
| request: &CreatePersonalAccessToken{Name: "my-access-token", Expiry: iggcon.IggyExpiryServerDefault}, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: The name rows stop at 15 bytes, so the 255-byte wire limit and the |
||
| code: 42, | ||
| want: []byte{15, 'm', 'y', '-', 'a', 'c', 'c', 'e', 's', 's', '-', 't', 'o', 'k', 'e', 'n', 0, 0, 0, 0, 0, 0, 0, 0}, | ||
| }, | ||
| { | ||
| name: "get tokens with no request payload", | ||
| request: &GetPersonalAccessTokens{}, | ||
| code: 41, | ||
| want: []byte{}, | ||
| }, | ||
| { | ||
| name: "short name", | ||
| request: &DeletePersonalAccessToken{Name: "token"}, | ||
| code: 43, | ||
| want: []byte{5, 't', 'o', 'k', 'e', 'n'}, | ||
| }, | ||
| { | ||
| name: "empty name", | ||
| request: &DeletePersonalAccessToken{Name: ""}, | ||
| code: 43, | ||
| want: []byte{0}, | ||
| }, | ||
| { | ||
| name: "longer name", | ||
| request: &DeletePersonalAccessToken{Name: "my-access-token"}, | ||
| code: 43, | ||
| want: []byte{15, 'm', 'y', '-', 'a', 'c', 'c', 'e', 's', 's', '-', 't', 'o', 'k', 'e', 'n'}, | ||
| }, | ||
| } { | ||
| t.Run(test.name, func(t *testing.T) { | ||
| got, err := (&CreatePersonalAccessToken{Name: "token", Expiry: test.expiry}).MarshalBinary() | ||
| if got := test.request.Code(); got != test.code { | ||
| t.Fatalf("command code = %d, want %d", got, test.code) | ||
| } | ||
| got, err := test.request.MarshalBinary() | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
| if !bytes.Equal(got, test.want) { | ||
| t.Fatalf("CreatePersonalAccessToken body = % x, want % x", got, test.want) | ||
| t.Fatalf("command body = %x, want %x", got, test.want) | ||
| } | ||
| }) | ||
| } | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: The
empty namerows assert a body the server rejects, becauseWireNamerequires 1 to 255 bytes (core/binary_protocol/src/primitives/identifier.rs:105). Replace them with a one-character name, the shortest nameWireNameaccepts. Also at line 130.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @justinmclean, after reviewing
core/binary_protocol/src/primitives/identifier.rs, I noticed thatCreatePersonalAccessTokendoes not validate the token name’s length. As a result, submitting an empty name causes the server to reject the request.I suggest adding token name validation in a separate PR to keep this PR focused on its current scope. What do you think?