|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "os" |
| 5 | + "reflect" |
| 6 | + "strings" |
| 7 | + "syscall" |
| 8 | + "testing" |
| 9 | +) |
| 10 | + |
| 11 | +func TestSetRLimitNoFile(t *testing.T) { |
| 12 | + // Save original rlimit |
| 13 | + var originalLimit syscall.Rlimit |
| 14 | + err := syscall.Getrlimit(syscall.RLIMIT_NOFILE, &originalLimit) |
| 15 | + if err != nil { |
| 16 | + t.Fatalf("Failed to get original rlimit: %v", err) |
| 17 | + } |
| 18 | + |
| 19 | + // Test SetRLimitNoFile function |
| 20 | + err = SetRLimitNoFile() |
| 21 | + if err != nil { |
| 22 | + t.Errorf("SetRLimitNoFile failed: %v", err) |
| 23 | + } |
| 24 | + |
| 25 | + // Verify that the limit was set correctly |
| 26 | + var newLimit syscall.Rlimit |
| 27 | + err = syscall.Getrlimit(syscall.RLIMIT_NOFILE, &newLimit) |
| 28 | + if err != nil { |
| 29 | + t.Fatalf("Failed to get new rlimit: %v", err) |
| 30 | + } |
| 31 | + |
| 32 | + // The current limit should now equal the max limit |
| 33 | + if newLimit.Cur != newLimit.Max { |
| 34 | + t.Errorf("Expected current limit to equal max limit after SetRLimitNoFile, got cur=%d, max=%d", newLimit.Cur, newLimit.Max) |
| 35 | + } |
| 36 | + |
| 37 | + // Restore original limit |
| 38 | + err = syscall.Setrlimit(syscall.RLIMIT_NOFILE, &originalLimit) |
| 39 | + if err != nil { |
| 40 | + t.Logf("Warning: Failed to restore original rlimit: %v", err) |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +// Test helper functions for flag validation |
| 45 | +func TestValidateClientFlags(t *testing.T) { |
| 46 | + tests := []struct { |
| 47 | + name string |
| 48 | + connectFlavor string |
| 49 | + protocol string |
| 50 | + expectError bool |
| 51 | + errorSubstring string |
| 52 | + }{ |
| 53 | + { |
| 54 | + name: "valid persistent tcp", |
| 55 | + connectFlavor: flavorPersistent, |
| 56 | + protocol: "tcp", |
| 57 | + expectError: false, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "valid ephemeral udp", |
| 61 | + connectFlavor: flavorEphemeral, |
| 62 | + protocol: "udp", |
| 63 | + expectError: false, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: "invalid connect flavor", |
| 67 | + connectFlavor: "invalid", |
| 68 | + protocol: "tcp", |
| 69 | + expectError: true, |
| 70 | + errorSubstring: "unexpected connect flavor", |
| 71 | + }, |
| 72 | + { |
| 73 | + name: "invalid protocol", |
| 74 | + connectFlavor: flavorPersistent, |
| 75 | + protocol: "invalid", |
| 76 | + expectError: true, |
| 77 | + errorSubstring: "unexpected protocol", |
| 78 | + }, |
| 79 | + } |
| 80 | + |
| 81 | + for _, tt := range tests { |
| 82 | + t.Run(tt.name, func(t *testing.T) { |
| 83 | + // Save original values |
| 84 | + originalConnectFlavor := connectFlavor |
| 85 | + originalProtocol := protocol |
| 86 | + |
| 87 | + // Set test values |
| 88 | + connectFlavor = tt.connectFlavor |
| 89 | + protocol = tt.protocol |
| 90 | + |
| 91 | + // Test validation logic (simulate what's in runClient) |
| 92 | + var err error |
| 93 | + switch connectFlavor { |
| 94 | + case flavorPersistent, flavorEphemeral: |
| 95 | + default: |
| 96 | + err = &ValidationError{Field: "connectFlavor", Value: connectFlavor} |
| 97 | + } |
| 98 | + |
| 99 | + if err == nil { |
| 100 | + switch protocol { |
| 101 | + case "tcp", "udp": |
| 102 | + default: |
| 103 | + err = &ValidationError{Field: "protocol", Value: protocol} |
| 104 | + } |
| 105 | + } |
| 106 | + |
| 107 | + // Restore original values |
| 108 | + connectFlavor = originalConnectFlavor |
| 109 | + protocol = originalProtocol |
| 110 | + |
| 111 | + if tt.expectError { |
| 112 | + if err == nil { |
| 113 | + t.Errorf("Expected error but got none") |
| 114 | + } else if tt.errorSubstring != "" && !strings.Contains(err.Error(), tt.errorSubstring) { |
| 115 | + t.Errorf("Expected error to contain '%s', got: %v", tt.errorSubstring, err) |
| 116 | + } |
| 117 | + } else { |
| 118 | + if err != nil { |
| 119 | + t.Errorf("Unexpected error: %v", err) |
| 120 | + } |
| 121 | + } |
| 122 | + }) |
| 123 | + } |
| 124 | +} |
| 125 | + |
| 126 | +// ValidationError represents a validation error for testing |
| 127 | +type ValidationError struct { |
| 128 | + Field string |
| 129 | + Value string |
| 130 | +} |
| 131 | + |
| 132 | +func (e *ValidationError) Error() string { |
| 133 | + switch e.Field { |
| 134 | + case "connectFlavor": |
| 135 | + return "unexpected connect flavor \"" + e.Value + "\"" |
| 136 | + case "protocol": |
| 137 | + return "unexpected protocol \"" + e.Value + "\"" |
| 138 | + default: |
| 139 | + return "validation error" |
| 140 | + } |
| 141 | +} |
| 142 | + |
| 143 | +func TestGetAddrsFromFile(t *testing.T) { |
| 144 | + tests := []struct { |
| 145 | + name string |
| 146 | + content string |
| 147 | + expected []string |
| 148 | + wantErr bool |
| 149 | + }{ |
| 150 | + { |
| 151 | + name: "single address", |
| 152 | + content: "127.0.0.1:8080", |
| 153 | + expected: []string{"127.0.0.1:8080"}, |
| 154 | + wantErr: false, |
| 155 | + }, |
| 156 | + { |
| 157 | + name: "multiple addresses", |
| 158 | + content: "127.0.0.1:8080 192.168.1.1:9090 example.com:3000", |
| 159 | + expected: []string{"127.0.0.1:8080", "192.168.1.1:9090", "example.com:3000"}, |
| 160 | + wantErr: false, |
| 161 | + }, |
| 162 | + { |
| 163 | + name: "addresses with newlines", |
| 164 | + content: "127.0.0.1:8080\n192.168.1.1:9090\n", |
| 165 | + expected: []string{"127.0.0.1:8080", "192.168.1.1:9090"}, |
| 166 | + wantErr: false, |
| 167 | + }, |
| 168 | + { |
| 169 | + name: "empty file", |
| 170 | + content: "", |
| 171 | + expected: []string{}, |
| 172 | + wantErr: false, |
| 173 | + }, |
| 174 | + { |
| 175 | + name: "whitespace only", |
| 176 | + content: " \n\t \n", |
| 177 | + expected: []string{}, |
| 178 | + wantErr: false, |
| 179 | + }, |
| 180 | + } |
| 181 | + |
| 182 | + for _, tt := range tests { |
| 183 | + t.Run(tt.name, func(t *testing.T) { |
| 184 | + tmpfile, err := os.CreateTemp("", "addrs_test") |
| 185 | + if err != nil { |
| 186 | + t.Fatalf("Failed to create temp file: %v", err) |
| 187 | + } |
| 188 | + defer os.Remove(tmpfile.Name()) |
| 189 | + |
| 190 | + if _, err := tmpfile.WriteString(tt.content); err != nil { |
| 191 | + t.Fatalf("Failed to write to temp file: %v", err) |
| 192 | + } |
| 193 | + if err := tmpfile.Close(); err != nil { |
| 194 | + t.Fatalf("Failed to close temp file: %v", err) |
| 195 | + } |
| 196 | + |
| 197 | + got, err := getAddrsFromFile(tmpfile.Name()) |
| 198 | + if (err != nil) != tt.wantErr { |
| 199 | + t.Errorf("getAddrsFromFile() error = %v, wantErr %v", err, tt.wantErr) |
| 200 | + return |
| 201 | + } |
| 202 | + if !reflect.DeepEqual(got, tt.expected) { |
| 203 | + t.Errorf("getAddrsFromFile() = %v, want %v", got, tt.expected) |
| 204 | + } |
| 205 | + }) |
| 206 | + } |
| 207 | +} |
| 208 | + |
| 209 | +func TestGetAddrsFromFileNotFound(t *testing.T) { |
| 210 | + _, err := getAddrsFromFile("/nonexistent/file") |
| 211 | + if err == nil { |
| 212 | + t.Error("Expected error for non-existent file, got nil") |
| 213 | + } |
| 214 | +} |
0 commit comments