-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_system.rb
More file actions
384 lines (337 loc) · 11.2 KB
/
Copy pathtest_system.rb
File metadata and controls
384 lines (337 loc) · 11.2 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env ruby
require 'json'
require 'fileutils'
require 'optparse'
# Try to load the zerotrust_scope module, but handle the case where the C library is missing
begin
require_relative 'zerotrust_scope'
FFI_AVAILABLE = true
rescue LoadError => e
puts "Warning: Could not load zerotrust_scope module: #{e.message}"
puts "This is expected if the C library hasn't been compiled yet."
puts "Using Ruby stub implementation for testing..."
FFI_AVAILABLE = false
# Load the stub implementation
require_relative 'zerotrust_stub'
end
class ZeroTrustTester
def initialize
@test_results = []
@config = load_config
end
def run_all_tests
puts "=== ZeroTrustScope System Test ==="
puts
test_ffi_integration
test_logging_system
test_config_loading
test_ip_validation
test_file_permissions
display_results
end
private
def test_ffi_integration
puts "Testing FFI integration..."
begin
# Test if we can call C functions (or stub functions)
ZeroTrustScope.add_trusted_ip("192.168.1.100")
if FFI_AVAILABLE
@test_results << { test: "FFI Integration", status: "PASS", message: "Successfully called C functions via FFI" }
else
@test_results << { test: "FFI Integration", status: "PASS", message: "Successfully called stub functions (C library not compiled)" }
end
rescue => e
@test_results << { test: "FFI Integration", status: "FAIL", message: "FFI/Stub error: #{e.message}" }
end
end
def test_logging_system
puts "Testing logging system..."
begin
# Test log file creation
log_file = "zerotrust_log.json"
FileUtils.touch(log_file) unless File.exist?(log_file)
# Test JSON log format
test_log_entry = {
timestamp: Time.now.strftime("%Y-%m-%d %H:%M:%S"),
event_type: "TEST",
description: "Test log entry"
}
File.open(log_file, "a") do |f|
f.puts(test_log_entry.to_json)
end
# Verify log entry can be parsed
last_line = File.readlines(log_file).last
parsed = JSON.parse(last_line)
if parsed["event_type"] == "TEST"
@test_results << { test: "Logging System", status: "PASS", message: "Log file created and JSON format valid" }
else
@test_results << { test: "Logging System", status: "FAIL", message: "Log entry format invalid" }
end
rescue => e
@test_results << { test: "Logging System", status: "FAIL", message: "Logging error: #{e.message}" }
end
end
def test_config_loading
puts "Testing configuration loading..."
begin
if @config && @config["network"] && @config["security"]
@test_results << { test: "Configuration", status: "PASS", message: "Configuration file loaded successfully" }
else
@test_results << { test: "Configuration", status: "FAIL", message: "Invalid configuration format" }
end
rescue => e
@test_results << { test: "Configuration", status: "FAIL", message: "Config error: #{e.message}" }
end
end
def test_ip_validation
puts "Testing IP address validation..."
valid_ips = ["192.168.1.1", "10.0.0.1", "172.16.0.1", "127.0.0.1", "0.0.0.0", "255.255.255.255"]
invalid_ips = ["256.256.256.256", "192.168.1", "invalid", "192.168.1.256", "192.168.1.-1", "192.168.1.1000"]
def valid_ip?(ip)
# Check format first
return false unless ip.match(/\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/)
# Check each octet is in valid range (0-255)
octets = ip.split('.')
octets.all? { |octet| octet.to_i >= 0 && octet.to_i <= 255 }
end
all_valid = valid_ips.all? { |ip| valid_ip?(ip) }
all_invalid = invalid_ips.none? { |ip| valid_ip?(ip) }
if all_valid && all_invalid
@test_results << { test: "IP Validation", status: "PASS", message: "IP address validation working correctly" }
else
@test_results << { test: "IP Validation", status: "FAIL", message: "IP validation logic has issues" }
end
end
def test_file_permissions
puts "Testing file permissions..."
begin
# Test if we can write to log file
log_file = "zerotrust_log.json"
File.open(log_file, "a") { |f| f.puts("test") }
# Test if we can read config
File.read("config.json")
@test_results << { test: "File Permissions", status: "PASS", message: "File read/write permissions OK" }
rescue => e
@test_results << { test: "File Permissions", status: "FAIL", message: "Permission error: #{e.message}" }
end
end
def load_config
begin
JSON.parse(File.read("config.json"))
rescue => e
puts "Warning: Could not load config.json: #{e.message}"
nil
end
end
def display_results
puts
puts "=== Test Results ==="
puts
passed = 0
failed = 0
skipped = 0
@test_results.each do |result|
case result[:status]
when "PASS"
status_icon = "✓"
color = "\033[32m" # Green
when "FAIL"
status_icon = "✗"
color = "\033[31m" # Red
when "SKIP"
status_icon = "⚠"
color = "\033[33m" # Yellow
end
reset = "\033[0m"
puts "#{color}#{status_icon} #{result[:test]}#{reset}"
puts " #{result[:message]}"
puts
case result[:status]
when "PASS"
passed += 1
when "FAIL"
failed += 1
when "SKIP"
skipped += 1
end
end
puts "Summary: #{passed} passed, #{failed} failed, #{skipped} skipped"
if failed == 0
puts "\n All tests passed! ZeroTrustScope is ready to use."
else
puts "\n Some tests failed. Please check the issues above."
end
end
end
class ZeroTrustCLI
def self.valid_ip?(ip)
return false unless ip.match(/\A\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\z/)
octets = ip.split('.')
octets.all? { |octet| octet.to_i >= 0 && octet.to_i <= 255 }
end
def self.start_monitoring
puts "Starting ZeroTrustScope network monitoring..."
puts "Press Ctrl+C to stop monitoring"
begin
ZeroTrustScope.start_monitoring
# Keep the monitoring running
loop do
sleep 1
end
rescue Interrupt
puts "\nMonitoring stopped."
rescue => e
puts "Error during monitoring: #{e.message}"
end
end
def self.add_trusted_ip(ip_address)
unless valid_ip?(ip_address)
puts "Error: Invalid IP address format: #{ip_address}"
puts "Please use format: xxx.xxx.xxx.xxx (e.g., 192.168.1.100)"
return
end
puts "Adding #{ip_address} to trusted IP list..."
begin
ZeroTrustScope.add_trusted_ip(ip_address)
puts "✓ Successfully added #{ip_address} to trusted IPs"
rescue => e
puts "Error adding trusted IP: #{e.message}"
end
end
def self.block_ip(ip_address)
unless valid_ip?(ip_address)
puts "Error: Invalid IP address format: #{ip_address}"
puts "Please use format: xxx.xxx.xxx.xxx (e.g., 192.168.1.100)"
return
end
puts "Blocking IP address: #{ip_address}..."
begin
ZeroTrustScope.block_untrusted_ip(ip_address)
puts "✓ Successfully blocked #{ip_address}"
rescue => e
puts "Error blocking IP: #{e.message}"
end
end
def self.show_logs
puts "Displaying real-time security logs..."
puts "Press Ctrl+C to stop"
begin
loop do
if File.exist?("zerotrust_log.json")
File.open("zerotrust_log.json", "r") do |file|
file.each_line do |line|
begin
log_entry = JSON.parse(line.strip)
timestamp = log_entry['timestamp'] || 'Unknown'
event_type = log_entry['event_type'] || 'UNKNOWN'
description = log_entry['description'] || 'No description'
# Color coding for different event types
case event_type
when 'BLOCK_IP'
color = "\033[31m" # Red
when 'TRUST_IP'
color = "\033[32m" # Green
when 'MONITOR_START'
color = "\033[34m" # Blue
else
color = "\033[37m" # White
end
reset = "\033[0m"
puts "#{color}[#{timestamp}] [#{event_type}] #{description}#{reset}"
rescue JSON::ParserError => e
puts "Error parsing log entry: #{e.message}"
end
end
end
# Clear the log file after reading
File.truncate("zerotrust_log.json", 0)
end
sleep 1
end
rescue Interrupt
puts "\nLog display stopped."
rescue => e
puts "Error displaying logs: #{e.message}"
end
end
def self.show_help(command = nil)
case command
when "start"
puts "Usage: test_system.rb start"
puts ""
puts "Start network monitoring"
puts "This command will begin monitoring network traffic and applying"
puts "ZeroTrust security policies based on your configuration."
when "trust"
puts "Usage: test_system.rb trust IP_ADDRESS"
puts ""
puts "Add an IP address to the trusted list"
puts "Example: test_system.rb trust 192.168.1.100"
when "block"
puts "Usage: test_system.rb block IP_ADDRESS"
puts ""
puts "Block an IP address"
puts "Example: test_system.rb block 10.0.0.50"
when "logs"
puts "Usage: test_system.rb logs"
puts ""
puts "Display real-time security logs"
puts "Shows live security events and network activity."
else
puts "ZeroTrustScope Command Line Interface"
puts ""
puts "Commands:"
puts " test_system.rb start # Start network monitoring"
puts " test_system.rb trust IP_ADDRESS # Add an IP address to the trusted list"
puts " test_system.rb block IP_ADDRESS # Block an IP address"
puts " test_system.rb logs # Display real-time security logs"
puts " test_system.rb help [COMMAND] # Describe available commands or one specific command"
puts ""
puts "Examples:"
puts " test_system.rb start"
puts " test_system.rb trust 192.168.1.100"
puts " test_system.rb block 10.0.0.50"
puts " test_system.rb logs"
puts ""
puts "For more information about a command, use: test_system.rb help <command>"
end
end
def self.run(args)
command = args.first
case command
when "start"
start_monitoring
when "trust"
if args[1]
add_trusted_ip(args[1])
else
puts "Error: IP address required"
puts "Usage: test_system.rb trust IP_ADDRESS"
end
when "block"
if args[1]
block_ip(args[1])
else
puts "Error: IP address required"
puts "Usage: test_system.rb block IP_ADDRESS"
end
when "logs"
show_logs
when "help"
show_help(args[1])
when "test"
# Run the test suite
tester = ZeroTrustTester.new
tester.run_all_tests
when nil
# No command provided, show help
show_help
else
puts "Unknown command: #{command}"
puts "Use 'test_system.rb help' for available commands"
end
end
end
if __FILE__ == $0
ZeroTrustCLI.run(ARGV)
end