-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_test.go
More file actions
145 lines (121 loc) · 4.03 KB
/
main_test.go
File metadata and controls
145 lines (121 loc) · 4.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright (C) 2026, Zoo Labs Foundation. All rights reserved.
// See the file LICENSE for licensing terms.
package main
import (
"os"
"path/filepath"
"testing"
"github.com/zooai/node/vm"
)
func TestInstallPlugin_CreatesSymlink(t *testing.T) {
tmpDir := t.TempDir()
pluginDir := filepath.Join(tmpDir, "plugins")
err := installPlugin(pluginDir, vm.EVMID.String())
if err != nil {
t.Fatalf("installPlugin failed: %v", err)
}
pluginPath := filepath.Join(pluginDir, vm.EVMID.String())
// Verify the symlink exists.
info, err := os.Lstat(pluginPath)
if err != nil {
t.Fatalf("plugin file does not exist: %v", err)
}
// Should be a symlink (or hard link).
if info.Mode()&os.ModeSymlink != 0 {
target, err := os.Readlink(pluginPath)
if err != nil {
t.Fatalf("readlink failed: %v", err)
}
if target == "" {
t.Fatal("symlink target is empty")
}
t.Logf("symlink -> %s", target)
} else {
// Hard link -- just verify it's a regular file.
if !info.Mode().IsRegular() {
t.Fatalf("expected regular file or symlink, got %s", info.Mode())
}
t.Log("installed as hard link")
}
}
func TestInstallPlugin_EmptyPluginDir_UsesDefault(t *testing.T) {
// When pluginDir is empty, installPlugin uses ~/.lux/plugins.
// We cannot easily test the actual default path without side effects,
// but we can verify that passing a non-empty dir works correctly and
// the function doesn't error with an explicit directory.
tmpDir := t.TempDir()
err := installPlugin(tmpDir, vm.EVMID.String())
if err != nil {
t.Fatalf("installPlugin with explicit dir failed: %v", err)
}
pluginPath := filepath.Join(tmpDir, vm.EVMID.String())
if _, err := os.Lstat(pluginPath); err != nil {
t.Fatalf("plugin not found at %s: %v", pluginPath, err)
}
}
func TestInstallPlugin_AtomicReplacement(t *testing.T) {
tmpDir := t.TempDir()
pluginDir := filepath.Join(tmpDir, "plugins")
// First install.
if err := installPlugin(pluginDir, vm.EVMID.String()); err != nil {
t.Fatalf("first installPlugin failed: %v", err)
}
pluginPath := filepath.Join(pluginDir, vm.EVMID.String())
info1, err := os.Lstat(pluginPath)
if err != nil {
t.Fatalf("plugin missing after first install: %v", err)
}
// Second install -- should atomically replace.
if err := installPlugin(pluginDir, vm.EVMID.String()); err != nil {
t.Fatalf("second installPlugin failed: %v", err)
}
info2, err := os.Lstat(pluginPath)
if err != nil {
t.Fatalf("plugin missing after second install: %v", err)
}
// Both installs should succeed and produce a valid file.
if info1.Name() != info2.Name() {
t.Fatalf("plugin filename changed: %s -> %s", info1.Name(), info2.Name())
}
// Verify no temp files left behind.
entries, err := os.ReadDir(pluginDir)
if err != nil {
t.Fatalf("readdir failed: %v", err)
}
for _, e := range entries {
if filepath.Ext(e.Name()) == ".tmp" || len(e.Name()) > len(vm.EVMID.String())+5 {
// Temp files have format: {vmid}.tmp.{pid}
if e.Name() != vm.EVMID.String() {
t.Errorf("stale temp file left behind: %s", e.Name())
}
}
}
}
func TestInstallPlugin_DirectoryPermissions(t *testing.T) {
tmpDir := t.TempDir()
pluginDir := filepath.Join(tmpDir, "restricted-plugins")
if err := installPlugin(pluginDir, vm.EVMID.String()); err != nil {
t.Fatalf("installPlugin failed: %v", err)
}
// Verify the plugin directory was created with 0700 permissions.
info, err := os.Stat(pluginDir)
if err != nil {
t.Fatalf("stat plugin dir failed: %v", err)
}
perm := info.Mode().Perm()
if perm != 0700 {
t.Errorf("plugin dir permissions: got %o, want %o", perm, 0700)
}
}
func TestInstallPlugin_NestedPluginDir(t *testing.T) {
tmpDir := t.TempDir()
pluginDir := filepath.Join(tmpDir, "a", "b", "c", "plugins")
// Should create all intermediate directories.
if err := installPlugin(pluginDir, vm.EVMID.String()); err != nil {
t.Fatalf("installPlugin with nested dir failed: %v", err)
}
pluginPath := filepath.Join(pluginDir, vm.EVMID.String())
if _, err := os.Lstat(pluginPath); err != nil {
t.Fatalf("plugin not found at nested path: %v", err)
}
}