-
Notifications
You must be signed in to change notification settings - Fork 303
fix(go): correct Permissions serialization and add unit tests #3015
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
Merged
+767
−4
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
26730f0
fix(go): fix command serialization bugs
saie-ch 57d356f
fix(go): remove redundant nil checks for lint
saie-ch cf9f508
scope PR to Permissions fix only, add tests
saie-ch 1764aa2
fix(go): format test file
saie-ch 231c531
fix(go): address staticcheck lint issue
saie-ch 343bff9
test(go): enhance Permissions test to verify all encoded values
saie-ch e6add02
test(go): add individual field tests and use asymmetric values for Pe…
saie-ch 0fe9765
fix(go): correct gofmt alignment in test file
saie-ch d6e2146
Merge branch 'master' into go_issues
hubcio 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
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 |
|---|---|---|
| @@ -0,0 +1,232 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package iggcon | ||
|
|
||
| import ( | ||
| "encoding/binary" | ||
| "testing" | ||
| ) | ||
|
|
||
| func TestPermissions_MarshalBinary_WithStreamsAndTopics(t *testing.T) { | ||
| // Test case: Permissions with 2 streams, first stream has 2 topics, second has none | ||
| permissions := &Permissions{ | ||
| Global: GlobalPermissions{ | ||
| ManageServers: true, | ||
| ReadServers: false, | ||
| ManageUsers: true, | ||
| ReadUsers: false, | ||
| ManageStreams: true, | ||
| ReadStreams: false, | ||
| ManageTopics: true, | ||
| ReadTopics: false, | ||
| PollMessages: true, | ||
| SendMessages: false, | ||
| }, | ||
| Streams: map[int]*StreamPermissions{ | ||
| 1: { | ||
| ManageStream: true, | ||
| ReadStream: false, | ||
| ManageTopics: true, | ||
| ReadTopics: false, | ||
| PollMessages: true, | ||
| SendMessages: false, | ||
| Topics: map[int]*TopicPermissions{ | ||
| 10: { | ||
| ManageTopic: true, | ||
| ReadTopic: false, | ||
| PollMessages: true, | ||
| SendMessages: false, | ||
| }, | ||
| 20: { | ||
| ManageTopic: false, | ||
| ReadTopic: true, | ||
| PollMessages: false, | ||
| SendMessages: true, | ||
| }, | ||
| }, | ||
| }, | ||
hubcio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| 2: { | ||
| ManageStream: false, | ||
| ReadStream: true, | ||
| ManageTopics: false, | ||
| ReadTopics: true, | ||
| PollMessages: false, | ||
| SendMessages: true, | ||
| Topics: nil, | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| bytes, err := permissions.MarshalBinary() | ||
| if err != nil { | ||
| t.Fatalf("MarshalBinary failed: %v", err) | ||
| } | ||
|
|
||
| // Verify structure | ||
| position := 0 | ||
|
|
||
| // Global permissions (10 bytes) | ||
| if bytes[position] != 1 { | ||
| t.Errorf("Expected ManageServers=1, got %d", bytes[position]) | ||
| } | ||
| position += 10 | ||
|
|
||
| // Has streams flag | ||
| if bytes[position] != 1 { | ||
| t.Errorf("Expected has_streams=1, got %d", bytes[position]) | ||
| } | ||
| position++ | ||
|
|
||
| // Verify continuation flags are present for streams | ||
| // We should have 2 streams, so we need to find stream continuation flags | ||
| streamsFound := 0 | ||
| for position < len(bytes) { | ||
| // Each stream has: 4 bytes (ID) + 6 bytes (perms) + 1 byte (has_topics) + topics + 1 byte (has_next_stream) | ||
| if position+4 > len(bytes) { | ||
| break | ||
| } | ||
| streamID := binary.LittleEndian.Uint32(bytes[position : position+4]) | ||
| if streamID == 0 { | ||
| break | ||
| } | ||
| position += 4 // stream ID | ||
| position += 6 // stream permissions | ||
| position += 1 // has_topics flag | ||
|
|
||
| // Skip topics if present | ||
| if bytes[position-1] == 1 { | ||
| // Topics exist, need to skip them | ||
| for position+4 <= len(bytes) { | ||
| position += 4 // topic ID | ||
| position += 4 // topic permissions | ||
| if position >= len(bytes) { | ||
| t.Fatalf("Unexpected end of bytes while reading topic continuation flag") | ||
| } | ||
| hasNextTopic := bytes[position] | ||
| position++ | ||
| if hasNextTopic == 0 { | ||
| break | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Check stream continuation flag | ||
| if position >= len(bytes) { | ||
| t.Fatalf("Unexpected end of bytes while reading stream continuation flag") | ||
| } | ||
| hasNextStream := bytes[position] | ||
| position++ | ||
| streamsFound++ | ||
|
|
||
| if streamsFound == 1 && hasNextStream != 1 { | ||
| t.Errorf("Expected has_next_stream=1 for first stream, got %d", hasNextStream) | ||
| } | ||
| if streamsFound == 2 && hasNextStream != 0 { | ||
| t.Errorf("Expected has_next_stream=0 for second stream, got %d", hasNextStream) | ||
| } | ||
|
|
||
| if hasNextStream == 0 { | ||
| break | ||
| } | ||
| } | ||
|
|
||
| if streamsFound != 2 { | ||
| t.Errorf("Expected 2 streams, found %d", streamsFound) | ||
| } | ||
| } | ||
hubcio marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| func TestPermissions_MarshalBinary_WithEmptyStreamsMap(t *testing.T) { | ||
| // Test case: Permissions with empty streams map should be treated as nil | ||
| permissions := &Permissions{ | ||
| Global: GlobalPermissions{ | ||
| ManageServers: true, | ||
| }, | ||
| Streams: map[int]*StreamPermissions{}, // Empty map | ||
| } | ||
|
|
||
| bytes, err := permissions.MarshalBinary() | ||
| if err != nil { | ||
| t.Fatalf("MarshalBinary failed: %v", err) | ||
| } | ||
|
|
||
| // Should be 10 bytes (global) + 1 byte (has_streams=0) | ||
| if len(bytes) != 11 { | ||
| t.Errorf("Expected 11 bytes for empty streams, got %d", len(bytes)) | ||
| } | ||
|
|
||
| // Check has_streams flag is 0 | ||
| if bytes[10] != 0 { | ||
| t.Errorf("Expected has_streams=0 for empty map, got %d", bytes[10]) | ||
| } | ||
| } | ||
|
|
||
| func TestPermissions_MarshalBinary_WithNilStreams(t *testing.T) { | ||
| // Test case: Permissions with nil streams | ||
| permissions := &Permissions{ | ||
| Global: GlobalPermissions{ | ||
| ManageServers: true, | ||
| }, | ||
| Streams: nil, | ||
| } | ||
|
|
||
| bytes, err := permissions.MarshalBinary() | ||
| if err != nil { | ||
| t.Fatalf("MarshalBinary failed: %v", err) | ||
| } | ||
|
|
||
| // Should be 10 bytes (global) + 1 byte (has_streams=0) | ||
| if len(bytes) != 11 { | ||
| t.Errorf("Expected 11 bytes for nil streams, got %d", len(bytes)) | ||
| } | ||
|
|
||
| // Check has_streams flag is 0 | ||
| if bytes[10] != 0 { | ||
| t.Errorf("Expected has_streams=0 for nil, got %d", bytes[10]) | ||
| } | ||
| } | ||
|
|
||
| func TestPermissions_MarshalBinary_WithEmptyTopicsMap(t *testing.T) { | ||
| // Test case: Stream with empty topics map should be treated as nil | ||
| permissions := &Permissions{ | ||
| Global: GlobalPermissions{}, | ||
| Streams: map[int]*StreamPermissions{ | ||
| 1: { | ||
| ManageStream: true, | ||
| Topics: map[int]*TopicPermissions{}, // Empty topics map | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| bytes, err := permissions.MarshalBinary() | ||
| if err != nil { | ||
| t.Fatalf("MarshalBinary failed: %v", err) | ||
| } | ||
|
|
||
| // Verify structure | ||
| position := 10 // Skip global permissions | ||
| position++ // Skip has_streams flag | ||
|
|
||
| // Skip stream ID and permissions | ||
| position += 4 // stream ID | ||
| position += 6 // stream permissions | ||
|
|
||
| // Check has_topics flag is 0 for empty map | ||
| if bytes[position] != 0 { | ||
| t.Errorf("Expected has_topics=0 for empty topics map, got %d", bytes[position]) | ||
| } | ||
| } | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.