Skip to content

Commit 4fa2b67

Browse files
committed
feat(go): add comprehensive unit tests for command serialization and fix 4 bugs
1 parent 0102b50 commit 4fa2b67

11 files changed

Lines changed: 2425 additions & 9 deletions

File tree

foreign/go/contracts/users.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ func (p *Permissions) MarshalBinary() ([]byte, error) {
9999
}
100100
}
101101
} else {
102-
bytes[0] = byte(0)
102+
bytes[position] = byte(0)
103103
}
104104

105105
return bytes, nil
Lines changed: 225 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,225 @@
1+
// Licensed to the Apache Software Foundation (ASF) under one
2+
// or more contributor license agreements. See the NOTICE file
3+
// distributed with this work for additional information
4+
// regarding copyright ownership. The ASF licenses this file
5+
// to you under the Apache License, Version 2.0 (the
6+
// "License"); you may not use this file except in compliance
7+
// with the License. You may obtain a copy of the License at
8+
//
9+
// http://www.apache.org/licenses/LICENSE-2.0
10+
//
11+
// Unless required by applicable law or agreed to in writing,
12+
// software distributed under the License is distributed on an
13+
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
// KIND, either express or implied. See the License for the
15+
// specific language governing permissions and limitations
16+
// under the License.
17+
18+
package command
19+
20+
import (
21+
"bytes"
22+
"testing"
23+
)
24+
25+
// TestSerialize_GetPersonalAccessTokens tests serialization of GetPersonalAccessTokens command
26+
func TestSerialize_GetPersonalAccessTokens(t *testing.T) {
27+
cmd := GetPersonalAccessTokens{}
28+
29+
serialized, err := cmd.MarshalBinary()
30+
if err != nil {
31+
t.Fatalf("Failed to serialize GetPersonalAccessTokens: %v", err)
32+
}
33+
34+
expected := []byte{} // Empty byte array
35+
36+
if !bytes.Equal(serialized, expected) {
37+
t.Errorf("Serialized bytes are incorrect.\nExpected:\t%v\nGot:\t\t%v", expected, serialized)
38+
}
39+
}
40+
41+
// TestSerialize_DeletePersonalAccessToken tests serialization with normal token name
42+
func TestSerialize_DeletePersonalAccessToken(t *testing.T) {
43+
cmd := DeletePersonalAccessToken{
44+
Name: "test_token",
45+
}
46+
47+
serialized, err := cmd.MarshalBinary()
48+
if err != nil {
49+
t.Fatalf("Failed to serialize DeletePersonalAccessToken: %v", err)
50+
}
51+
52+
expected := []byte{
53+
0x0A, // Name length = 10
54+
0x74, 0x65, 0x73, 0x74, 0x5F, 0x74, 0x6F, 0x6B, 0x65, 0x6E, // "test_token"
55+
}
56+
57+
if !bytes.Equal(serialized, expected) {
58+
t.Errorf("Serialized bytes are incorrect.\nExpected:\t%v\nGot:\t\t%v", expected, serialized)
59+
}
60+
}
61+
62+
// TestSerialize_DeletePersonalAccessToken_SingleChar tests edge case with single character name
63+
func TestSerialize_DeletePersonalAccessToken_SingleChar(t *testing.T) {
64+
cmd := DeletePersonalAccessToken{
65+
Name: "a",
66+
}
67+
68+
serialized, err := cmd.MarshalBinary()
69+
if err != nil {
70+
t.Fatalf("Failed to serialize DeletePersonalAccessToken with single char: %v", err)
71+
}
72+
73+
expected := []byte{
74+
0x01, // Name length = 1
75+
0x61, // "a"
76+
}
77+
78+
if !bytes.Equal(serialized, expected) {
79+
t.Errorf("Serialized bytes are incorrect.\nExpected:\t%v\nGot:\t\t%v", expected, serialized)
80+
}
81+
}
82+
83+
// TestSerialize_DeletePersonalAccessToken_EmptyName tests edge case with empty name
84+
func TestSerialize_DeletePersonalAccessToken_EmptyName(t *testing.T) {
85+
cmd := DeletePersonalAccessToken{
86+
Name: "",
87+
}
88+
89+
serialized, err := cmd.MarshalBinary()
90+
if err != nil {
91+
t.Fatalf("Failed to serialize DeletePersonalAccessToken with empty name: %v", err)
92+
}
93+
94+
expected := []byte{
95+
0x00, // Name length = 0
96+
}
97+
98+
if !bytes.Equal(serialized, expected) {
99+
t.Errorf("Serialized bytes are incorrect.\nExpected:\t%v\nGot:\t\t%v", expected, serialized)
100+
}
101+
}
102+
103+
// TestSerialize_CreatePersonalAccessToken tests serialization with normal values
104+
func TestSerialize_CreatePersonalAccessToken(t *testing.T) {
105+
cmd := CreatePersonalAccessToken{
106+
Name: "test",
107+
Expiry: 3600, // 1 hour in seconds
108+
}
109+
110+
serialized, err := cmd.MarshalBinary()
111+
if err != nil {
112+
t.Fatalf("Failed to serialize CreatePersonalAccessToken: %v", err)
113+
}
114+
115+
expected := []byte{
116+
0x04, // Name length = 4
117+
0x74, 0x65, 0x73, 0x74, // "test"
118+
0x00, 0x00, 0x00, 0x00, // Reserved/padding (4 bytes)
119+
0x10, 0x0E, 0x00, 0x00, // Expiry = 3600 (little endian uint32)
120+
}
121+
122+
if !bytes.Equal(serialized, expected) {
123+
t.Errorf("Serialized bytes are incorrect.\nExpected:\t%v\nGot:\t\t%v", expected, serialized)
124+
}
125+
}
126+
127+
// TestSerialize_CreatePersonalAccessToken_ZeroExpiry tests edge case with zero expiry
128+
func TestSerialize_CreatePersonalAccessToken_ZeroExpiry(t *testing.T) {
129+
cmd := CreatePersonalAccessToken{
130+
Name: "token",
131+
Expiry: 0,
132+
}
133+
134+
serialized, err := cmd.MarshalBinary()
135+
if err != nil {
136+
t.Fatalf("Failed to serialize CreatePersonalAccessToken with zero expiry: %v", err)
137+
}
138+
139+
expected := []byte{
140+
0x05, // Name length = 5
141+
0x74, 0x6F, 0x6B, 0x65, 0x6E, // "token"
142+
0x00, 0x00, 0x00, 0x00, // Reserved/padding (4 bytes)
143+
0x00, 0x00, 0x00, 0x00, // Expiry = 0 (little endian uint32)
144+
}
145+
146+
if !bytes.Equal(serialized, expected) {
147+
t.Errorf("Serialized bytes are incorrect.\nExpected:\t%v\nGot:\t\t%v", expected, serialized)
148+
}
149+
}
150+
151+
// TestSerialize_CreatePersonalAccessToken_MaxExpiry tests edge case with maximum uint32 expiry
152+
func TestSerialize_CreatePersonalAccessToken_MaxExpiry(t *testing.T) {
153+
cmd := CreatePersonalAccessToken{
154+
Name: "long_token",
155+
Expiry: 4294967295, // Max uint32 value
156+
}
157+
158+
serialized, err := cmd.MarshalBinary()
159+
if err != nil {
160+
t.Fatalf("Failed to serialize CreatePersonalAccessToken with max expiry: %v", err)
161+
}
162+
163+
expected := []byte{
164+
0x0A, // Name length = 10
165+
0x6C, 0x6F, 0x6E, 0x67, 0x5F, 0x74, 0x6F, 0x6B, 0x65, 0x6E, // "long_token"
166+
0x00, 0x00, 0x00, 0x00, // Reserved/padding (4 bytes)
167+
0xFF, 0xFF, 0xFF, 0xFF, // Expiry = 4294967295 (little endian uint32)
168+
}
169+
170+
if !bytes.Equal(serialized, expected) {
171+
t.Errorf("Serialized bytes are incorrect.\nExpected:\t%v\nGot:\t\t%v", expected, serialized)
172+
}
173+
}
174+
175+
// TestSerialize_CreatePersonalAccessToken_EmptyName tests edge case with empty name
176+
func TestSerialize_CreatePersonalAccessToken_EmptyName(t *testing.T) {
177+
cmd := CreatePersonalAccessToken{
178+
Name: "",
179+
Expiry: 1000,
180+
}
181+
182+
serialized, err := cmd.MarshalBinary()
183+
if err != nil {
184+
t.Fatalf("Failed to serialize CreatePersonalAccessToken with empty name: %v", err)
185+
}
186+
187+
expected := []byte{
188+
0x00, // Name length = 0
189+
0x00, 0x00, 0x00, 0x00, // Reserved/padding (4 bytes)
190+
0xE8, 0x03, 0x00, 0x00, // Expiry = 1000 (little endian uint32)
191+
}
192+
193+
if !bytes.Equal(serialized, expected) {
194+
t.Errorf("Serialized bytes are incorrect.\nExpected:\t%v\nGot:\t\t%v", expected, serialized)
195+
}
196+
}
197+
198+
// TestSerialize_CreatePersonalAccessToken_LongName tests with a longer token name
199+
func TestSerialize_CreatePersonalAccessToken_LongName(t *testing.T) {
200+
cmd := CreatePersonalAccessToken{
201+
Name: "my_very_long_personal_access_token_name",
202+
Expiry: 86400, // 24 hours in seconds
203+
}
204+
205+
serialized, err := cmd.MarshalBinary()
206+
if err != nil {
207+
t.Fatalf("Failed to serialize CreatePersonalAccessToken with long name: %v", err)
208+
}
209+
210+
expected := []byte{
211+
0x27, // Name length = 39
212+
// "my_very_long_personal_access_token_name"
213+
0x6D, 0x79, 0x5F, 0x76, 0x65, 0x72, 0x79, 0x5F,
214+
0x6C, 0x6F, 0x6E, 0x67, 0x5F, 0x70, 0x65, 0x72,
215+
0x73, 0x6F, 0x6E, 0x61, 0x6C, 0x5F, 0x61, 0x63,
216+
0x63, 0x65, 0x73, 0x73, 0x5F, 0x74, 0x6F, 0x6B,
217+
0x65, 0x6E, 0x5F, 0x6E, 0x61, 0x6D, 0x65,
218+
0x00, 0x00, 0x00, 0x00, // Reserved/padding (4 bytes)
219+
0x80, 0x51, 0x01, 0x00, // Expiry = 86400 (little endian uint32)
220+
}
221+
222+
if !bytes.Equal(serialized, expected) {
223+
t.Errorf("Serialized bytes are incorrect.\nExpected:\t%v\nGot:\t\t%v", expected, serialized)
224+
}
225+
}

0 commit comments

Comments
 (0)