Skip to content

Commit a411084

Browse files
grokifyclaude
andcommitted
test(sarif): add tests for gzip compression and upload types
Tests gzipCompress/gzipDecompress with empty, large, binary, and invalid data. Tests UploadOptions, UploadResult, UploadStatus structs and ProcessingStatus comparison. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent b1ea423 commit a411084

1 file changed

Lines changed: 151 additions & 0 deletions

File tree

sarif/sarif_test.go

Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,3 +95,154 @@ func TestProcessingStatusConstants(t *testing.T) {
9595
}
9696
}
9797
}
98+
99+
func TestGzipCompressEmpty(t *testing.T) {
100+
original := []byte{}
101+
102+
compressed, err := gzipCompress(original)
103+
if err != nil {
104+
t.Fatalf("gzipCompress failed on empty input: %v", err)
105+
}
106+
107+
// Even empty data produces some gzip header bytes
108+
if len(compressed) == 0 {
109+
t.Error("compressed data should not be empty (gzip has headers)")
110+
}
111+
112+
decompressed, err := gzipDecompress(compressed)
113+
if err != nil {
114+
t.Fatalf("gzipDecompress failed: %v", err)
115+
}
116+
117+
if len(decompressed) != 0 {
118+
t.Errorf("decompressed empty data should be empty, got %d bytes", len(decompressed))
119+
}
120+
}
121+
122+
func TestGzipCompressLargeData(t *testing.T) {
123+
// Create large repetitive data (compresses well)
124+
original := bytes.Repeat([]byte("SARIF test data with repetitive content. "), 1000)
125+
126+
compressed, err := gzipCompress(original)
127+
if err != nil {
128+
t.Fatalf("gzipCompress failed: %v", err)
129+
}
130+
131+
// Compressed should be smaller for repetitive data
132+
if len(compressed) >= len(original) {
133+
t.Logf("Warning: compressed size (%d) >= original size (%d)", len(compressed), len(original))
134+
}
135+
136+
decompressed, err := gzipDecompress(compressed)
137+
if err != nil {
138+
t.Fatalf("gzipDecompress failed: %v", err)
139+
}
140+
141+
if !bytes.Equal(decompressed, original) {
142+
t.Error("decompressed data should match original")
143+
}
144+
}
145+
146+
func TestGzipDecompressInvalidData(t *testing.T) {
147+
invalidData := []byte("this is not gzip data")
148+
149+
_, err := gzipDecompress(invalidData)
150+
if err == nil {
151+
t.Error("gzipDecompress should fail on invalid gzip data")
152+
}
153+
}
154+
155+
func TestUploadOptionsStruct(t *testing.T) {
156+
opts := UploadOptions{
157+
CommitSHA: "abc123def456",
158+
Ref: "refs/heads/main",
159+
CheckoutURI: "file:///github/workspace/",
160+
ToolName: "my-scanner",
161+
}
162+
163+
if opts.CommitSHA != "abc123def456" {
164+
t.Errorf("CommitSHA = %q, want %q", opts.CommitSHA, "abc123def456")
165+
}
166+
if opts.Ref != "refs/heads/main" {
167+
t.Errorf("Ref = %q, want %q", opts.Ref, "refs/heads/main")
168+
}
169+
if opts.CheckoutURI != "file:///github/workspace/" {
170+
t.Errorf("CheckoutURI = %q, want %q", opts.CheckoutURI, "file:///github/workspace/")
171+
}
172+
if opts.ToolName != "my-scanner" {
173+
t.Errorf("ToolName = %q, want %q", opts.ToolName, "my-scanner")
174+
}
175+
}
176+
177+
func TestUploadResultStruct(t *testing.T) {
178+
result := UploadResult{
179+
SarifID: "sarif-12345",
180+
URL: "https://api.github.com/repos/owner/repo/code-scanning/sarifs/sarif-12345",
181+
}
182+
183+
if result.SarifID != "sarif-12345" {
184+
t.Errorf("SarifID = %q, want %q", result.SarifID, "sarif-12345")
185+
}
186+
if result.URL != "https://api.github.com/repos/owner/repo/code-scanning/sarifs/sarif-12345" {
187+
t.Errorf("URL = %q, want expected URL", result.URL)
188+
}
189+
}
190+
191+
func TestUploadStatusStruct(t *testing.T) {
192+
status := UploadStatus{
193+
Status: StatusComplete,
194+
AnalysesURL: "https://api.github.com/repos/owner/repo/code-scanning/analyses",
195+
}
196+
197+
if status.Status != StatusComplete {
198+
t.Errorf("Status = %q, want %q", status.Status, StatusComplete)
199+
}
200+
if status.AnalysesURL != "https://api.github.com/repos/owner/repo/code-scanning/analyses" {
201+
t.Errorf("AnalysesURL = %q, want expected URL", status.AnalysesURL)
202+
}
203+
}
204+
205+
func TestProcessingStatusComparison(t *testing.T) {
206+
// Test that status values can be compared
207+
tests := []struct {
208+
name string
209+
status ProcessingStatus
210+
check ProcessingStatus
211+
equal bool
212+
}{
213+
{"pending equals pending", StatusPending, StatusPending, true},
214+
{"complete equals complete", StatusComplete, StatusComplete, true},
215+
{"failed equals failed", StatusFailed, StatusFailed, true},
216+
{"pending not equals complete", StatusPending, StatusComplete, false},
217+
{"complete not equals failed", StatusComplete, StatusFailed, false},
218+
{"pending not equals failed", StatusPending, StatusFailed, false},
219+
}
220+
221+
for _, tt := range tests {
222+
t.Run(tt.name, func(t *testing.T) {
223+
got := tt.status == tt.check
224+
if got != tt.equal {
225+
t.Errorf("(%q == %q) = %v, want %v", tt.status, tt.check, got, tt.equal)
226+
}
227+
})
228+
}
229+
}
230+
231+
func TestGzipCompressDecompressBinary(t *testing.T) {
232+
// Test with binary data including null bytes
233+
original := []byte{0x00, 0x01, 0x02, 0xFF, 0xFE, 0xFD, 0x00, 0x00}
234+
235+
compressed, err := gzipCompress(original)
236+
if err != nil {
237+
t.Fatalf("gzipCompress failed on binary data: %v", err)
238+
}
239+
240+
decompressed, err := gzipDecompress(compressed)
241+
if err != nil {
242+
t.Fatalf("gzipDecompress failed: %v", err)
243+
}
244+
245+
if !bytes.Equal(decompressed, original) {
246+
t.Error("decompressed binary data should match original")
247+
}
248+
}

0 commit comments

Comments
 (0)