Skip to content

Commit

Permalink
test(rootkeys): add unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Gyuho Lee <[email protected]>
  • Loading branch information
gyuho committed Feb 5, 2025
1 parent d96906e commit 8fd4771
Showing 1 changed file with 73 additions and 0 deletions.
73 changes: 73 additions & 0 deletions rootkeys/fs_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package rootkeys

import (
"testing"
)

func TestRootsFS(t *testing.T) {
// Test that we can list the embedded files
entries, err := RootsFS.ReadDir("keys")
if err != nil {
t.Fatalf("failed to read keys directory: %v", err)
}

// Verify we have exactly 2 files
if len(entries) != 2 {
t.Errorf("expected 2 files, got %d", len(entries))
}

// Expected files
expectedFiles := map[string]bool{
"gpud-root-1.pem": false,
"gpud-root-2.pem": false,
}

// Verify each file exists and can be read
for _, entry := range entries {
name := entry.Name()
if _, ok := expectedFiles[name]; !ok {
t.Errorf("unexpected file: %s", name)
continue
}
expectedFiles[name] = true

// Try to read the file
content, err := RootsFS.ReadFile("keys/" + name)
if err != nil {
t.Errorf("failed to read file %s: %v", name, err)
continue
}

// Verify the file is not empty
if len(content) == 0 {
t.Errorf("file %s is empty", name)
}
}

// Verify all expected files were found
for name, found := range expectedFiles {
if !found {
t.Errorf("expected file %s was not found", name)
}
}
}

func TestRootsFSErrors(t *testing.T) {
// Test reading non-existent directory
_, err := RootsFS.ReadDir("nonexistent")
if err == nil {
t.Error("expected error when reading non-existent directory")
}

// Test reading non-existent file
_, err = RootsFS.ReadFile("keys/nonexistent.pem")
if err == nil {
t.Error("expected error when reading non-existent file")
}

// Test reading a directory as a file
_, err = RootsFS.ReadFile("keys")
if err == nil {
t.Error("expected error when reading directory as file")
}
}

0 comments on commit 8fd4771

Please sign in to comment.