|
| 1 | +#include "scitokens.h" |
| 2 | +#include <iostream> |
| 3 | +#include <string> |
| 4 | +#include <cstring> |
| 5 | + |
| 6 | +// Helper function to print monitoring JSON |
| 7 | +void print_monitoring_stats(const std::string &label) { |
| 8 | + char *json_out = nullptr; |
| 9 | + char *err_msg = nullptr; |
| 10 | + |
| 11 | + int result = scitoken_get_monitoring_json(&json_out, &err_msg); |
| 12 | + if (result != 0) { |
| 13 | + std::cerr << "Error getting monitoring JSON: " |
| 14 | + << (err_msg ? err_msg : "unknown error") << std::endl; |
| 15 | + if (err_msg) |
| 16 | + free(err_msg); |
| 17 | + return; |
| 18 | + } |
| 19 | + |
| 20 | + std::cout << "\n=== " << label << " ===" << std::endl; |
| 21 | + std::cout << json_out << std::endl; |
| 22 | + free(json_out); |
| 23 | +} |
| 24 | + |
| 25 | +int main() { |
| 26 | + char *err_msg = nullptr; |
| 27 | + |
| 28 | + // Reset monitoring stats at start |
| 29 | + scitoken_reset_monitoring_stats(&err_msg); |
| 30 | + |
| 31 | + std::cout << "Testing Monitoring API with Token Validation\n"; |
| 32 | + std::cout << "=============================================\n"; |
| 33 | + |
| 34 | + // Test 1: Initial state |
| 35 | + print_monitoring_stats("Initial State (should be empty)"); |
| 36 | + |
| 37 | + // Test 2: Failed validation - expired token from demo.scitokens.org |
| 38 | + // This token is from the test.cpp and is expired |
| 39 | + std::cout << "\n--- Test 1: Validating an expired token ---\n"; |
| 40 | + std::string expired_token = |
| 41 | + "eyJ0eXAiOiJKV1QiLCJhbGciOiJSUzI1NiIsImtpZCI6ImtleS1yczI1NiJ9." |
| 42 | + "eyJpc3MiOiJodHRwczovL2RlbW8uc2NpdG9rZW5zLm9yZyIsImV4cCI6MTU0NjM5MjAwOS" |
| 43 | + "wiaWF0IjoxNTQ2MzkxNDA5LCJuYmYiOjE1NDYzOTE0MDksImp0aSI6ImFkYTk2MjdiLWEx" |
| 44 | + "MGYtNGMyYS05Nzc2LTE4ZThkN2JmN2M4NSJ9.cNMG5zI2-JHh7l_" |
| 45 | + "PUPUAxom5Vi6Q3akKmv6q57CoVKHtxZAZRc47Uoix_" |
| 46 | + "AH3Xzr42qohr2FPamRTxUMsfZjrAFDJ_4JhJ-kKjJ3cRXXF-" |
| 47 | + "gj7lbniCDGOBuPXeMsVmeED15nauZ3XKXUHTGLEsg5O6RjS7sGKM_" |
| 48 | + "e9YiYvcTvWXcdkrkxZ2dPPU-R3IxdK6PtE9OB2XOk85H670OAJT3qimKm8Dk_" |
| 49 | + "Ri6DEEty1Su_" |
| 50 | + "1Tov3ac5B19iZkbhhVPMVP0cRolR9UNLhMxQAsbgEmArQOcs046AOzqQz6osOkdYOrVVO7" |
| 51 | + "lO2owUyMol94mB_39y1M8jcf5WNq3ukMMIzMCAPwA"; |
| 52 | + |
| 53 | + SciToken token = nullptr; |
| 54 | + int result = scitoken_deserialize(expired_token.c_str(), &token, nullptr, &err_msg); |
| 55 | + if (result != 0) { |
| 56 | + std::cout << "Token deserialization/validation failed (expected): " |
| 57 | + << (err_msg ? err_msg : "unknown error") << std::endl; |
| 58 | + if (err_msg) { |
| 59 | + free(err_msg); |
| 60 | + err_msg = nullptr; |
| 61 | + } |
| 62 | + } else { |
| 63 | + std::cout << "Token was valid (unexpected)" << std::endl; |
| 64 | + scitoken_destroy(token); |
| 65 | + } |
| 66 | + |
| 67 | + print_monitoring_stats("After Expired Token Validation"); |
| 68 | + |
| 69 | + // Test 3: Invalid issuer (should not create unbounded entries) |
| 70 | + std::cout << "\n--- Test 2: Testing DDoS protection with multiple invalid issuers ---\n"; |
| 71 | + |
| 72 | + // Try to create many tokens with different invalid issuers |
| 73 | + // The monitoring system should limit tracking to MAX_FAILED_ISSUERS (100) |
| 74 | + for (int i = 0; i < 150; i++) { |
| 75 | + // These are malformed tokens that will fail early |
| 76 | + std::string fake_token = "invalid.token." + std::to_string(i); |
| 77 | + SciToken temp_token = nullptr; |
| 78 | + scitoken_deserialize(fake_token.c_str(), &temp_token, nullptr, &err_msg); |
| 79 | + if (err_msg) { |
| 80 | + free(err_msg); |
| 81 | + err_msg = nullptr; |
| 82 | + } |
| 83 | + } |
| 84 | + |
| 85 | + print_monitoring_stats("After Multiple Invalid Token Attempts"); |
| 86 | + |
| 87 | + // Test 4: Reset stats |
| 88 | + std::cout << "\n--- Test 3: Testing reset functionality ---\n"; |
| 89 | + result = scitoken_reset_monitoring_stats(&err_msg); |
| 90 | + if (result != 0) { |
| 91 | + std::cerr << "Error resetting stats: " |
| 92 | + << (err_msg ? err_msg : "unknown error") << std::endl; |
| 93 | + if (err_msg) |
| 94 | + free(err_msg); |
| 95 | + return 1; |
| 96 | + } |
| 97 | + |
| 98 | + print_monitoring_stats("After Reset"); |
| 99 | + |
| 100 | + std::cout << "\n=== All monitoring API tests completed successfully ===\n"; |
| 101 | + return 0; |
| 102 | +} |
0 commit comments