From a217f88ea106c173e1afe5fb034c0598da41c049 Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Fri, 8 Sep 2023 09:40:41 +0200 Subject: [PATCH 01/19] Many unittests for internal folder --- internal/certs/certs_test.go | 28 ++++ internal/certs/data/testclient.crt | 27 ++++ internal/certs/data/testclientkey.pem | 182 +++++++++++++++++++++++++ internal/filter/filter_test.go | 39 ++++++ internal/misc/doc_test.go | 10 ++ internal/misc/mime_test.go | 28 ++++ internal/models/models_test.go | 89 ++++++++++++ internal/options/data/config.toml | 1 + internal/options/data/config_plus.toml | 2 + internal/options/options_test.go | 100 ++++++++++++++ 10 files changed, 506 insertions(+) create mode 100644 internal/certs/certs_test.go create mode 100644 internal/certs/data/testclient.crt create mode 100644 internal/certs/data/testclientkey.pem create mode 100644 internal/filter/filter_test.go create mode 100644 internal/misc/doc_test.go create mode 100644 internal/misc/mime_test.go create mode 100644 internal/models/models_test.go create mode 100644 internal/options/data/config.toml create mode 100644 internal/options/data/config_plus.toml create mode 100644 internal/options/options_test.go diff --git a/internal/certs/certs_test.go b/internal/certs/certs_test.go new file mode 100644 index 00000000..3e49e346 --- /dev/null +++ b/internal/certs/certs_test.go @@ -0,0 +1,28 @@ +package certs + +import "testing" + +func TestLoadCertificates(t *testing.T) { + goodTestCert := "data/testclient.crt" + goodTestkey := "data/testclientkey.pem" + goodPassphrase := "security123" +// missingTestCert := "data/testclient_missing.crt" + missingTestkey := "data/testclientkey_missing.pem" +// missingPassphrase := "" + + if certificate, err := LoadCertificate(&goodTestCert, &goodTestkey, nil); certificate == nil || err != nil { + t.Errorf("Failure: Couldn't load supposedly valid certificate.") + } + if certificate, err := LoadCertificate(nil, &goodTestkey, nil); certificate != nil || err == nil { + t.Errorf("Failure: No error despite missing certificate") + } + if certificate, err := LoadCertificate(&goodTestCert, &missingTestkey, nil); certificate != nil || err == nil { + t.Errorf("Failure: No Failure while loading certificate using missing key.") + } + if certificate, err := LoadCertificate(&goodTestCert, &goodTestkey, &goodPassphrase); certificate == nil || err != nil { + t.Errorf("Failure: Couldn't load supposedly valid certificate with passphrase.") + } + if certificate, err := LoadCertificate(&goodTestCert, &missingTestkey, &goodPassphrase); certificate != nil || err == nil { + t.Errorf("Failure: No Failure while loading certificate using missing key with passphrase.") + } +} diff --git a/internal/certs/data/testclient.crt b/internal/certs/data/testclient.crt new file mode 100644 index 00000000..f46f386a --- /dev/null +++ b/internal/certs/data/testclient.crt @@ -0,0 +1,27 @@ +-----BEGIN CERTIFICATE----- +MIIEkDCCAvigAwIBAgIBFDANBgkqhkiG9w0BAQsFADBKMQ8wDQYDVQQDEwZUZXN0 +ZXIxKjAoBgNVBAoTIUNTQUYgVG9vbHMgRGV2ZWxvcG1lbnQgKGludGVybmFsKTEL +MAkGA1UEBhMCREUwHhcNMjMwOTA0MDcyMjAzWhcNMjMxMDI0MDcyMjAzWjBVMRow +GAYDVQQDExFUTFMgVGVzdCBDbGllbnQgMTEqMCgGA1UEChMhQ1NBRiBUb29scyBE +ZXZlbG9wbWVudCAoaW50ZXJuYWwpMQswCQYDVQQGEwJERTCCAaIwDQYJKoZIhvcN +AQEBBQADggGPADCCAYoCggGBAN0vZbLXtRzd61rR8Hos0BGnqCaJXIwGARwxJojM +yxASFT+KeC4QDRkgRrK6OY4k/i7TEHuUGk/Bm754++554wmmhDqv1Q4+VhhR1K/J +Az/HVZNTAR1rPKwG82lyEpPxlRNZg/QtF9DqQSoSkL/fJLs+rq4zlKozXzREauZ5 +Be8So1dXRZfMVUMDgtk+IX8+iCeZisiWfv62ttQ0EiuiXLagd6ruEuoCSVi2tVsw +sC/Hp8AI2Ro56mmHiWthuae1H8yDWUFLSe9AQW65qC/xVUgo/nMpK2BYVFKb70TM +jl/dZM0Qn1tdiNyqCkbIhXjklZvZYhO+15TPkgDXDsqRUjpTrLZXLGrD6XIxCRLZ +GY6YrUfsFTjUC6JrUrAR8zY7SLsYN5sUmFUSMpJnI+T/SD4p/0CXrKrbMOjWQqz6 +FX/WHPxvswGKHk5zHYGHrzx7OKmfVa6gzUgZSfOHj2xOOR2Un9DwNavIrmSCWYXK +Zqig5qDyfzBvlXWEio/5GrDwgQIDAQABo3YwdDAMBgNVHRMBAf8EAjAAMBMGA1Ud +JQQMMAoGCCsGAQUFBwMCMA8GA1UdDwEB/wQFAwMHoAAwHQYDVR0OBBYEFKrFhODj +TKCopb+WQa29PsHR4HXgMB8GA1UdIwQYMBaAFI6GhktAq9L2uRChC9LcXeedKiUg +MA0GCSqGSIb3DQEBCwUAA4IBgQAbUDaIkmubooDde7BpZQx742BsPg4IN68bIg9A +3jI9codx9c8l9ROvZ/7FeRNXzhYrQUwzcKpwtQ1mB7kM85oXaTLxrtnkZAO2fFSb +8RA6QjOrnOvewWaO3moCZaPnN1wWtlnUev2tD7D2Tz/f20dE2wbDV0BGb8bU4eGI +UVgzYrMh0MHaC8LKoXUWP97jp/p+9CG4D2S1CmpzP2Nm1dS03oj4UHIUtamjivYY +vOeoKATXmj59lgYqqoAVbTH6f4mZlZGmzUhRxK6hck7xBdiXAwfta72m4WzE7HRh +nHAgO5aVWb6zltvVDJhYumB9Itv+LI7uU8fF9Uyc65SZ2BevxgikoDNxTx0oNr+4 +hExQhJfKuPFF2NI1N2tPYJT53Cek/ZJfjX3TyBneqehthtRqoAIIEaF/QlXqzJIi +G66YFC3xFlLmaQh52DJkF2+hzcPhFTVQv3yCirGLUSS9Nm7vTO2wnnW5arZazSV+ +enRZb3oiVYFVDh0Hymz9g5VraMw= +-----END CERTIFICATE----- diff --git a/internal/certs/data/testclientkey.pem b/internal/certs/data/testclientkey.pem new file mode 100644 index 00000000..ce2726ea --- /dev/null +++ b/internal/certs/data/testclientkey.pem @@ -0,0 +1,182 @@ +Public Key Info: + Public Key Algorithm: RSA + Key Security Level: High (3072 bits) + +modulus: + 00:dd:2f:65:b2:d7:b5:1c:dd:eb:5a:d1:f0:7a:2c:d0 + 11:a7:a8:26:89:5c:8c:06:01:1c:31:26:88:cc:cb:10 + 12:15:3f:8a:78:2e:10:0d:19:20:46:b2:ba:39:8e:24 + fe:2e:d3:10:7b:94:1a:4f:c1:9b:be:78:fb:ee:79:e3 + 09:a6:84:3a:af:d5:0e:3e:56:18:51:d4:af:c9:03:3f + c7:55:93:53:01:1d:6b:3c:ac:06:f3:69:72:12:93:f1 + 95:13:59:83:f4:2d:17:d0:ea:41:2a:12:90:bf:df:24 + bb:3e:ae:ae:33:94:aa:33:5f:34:44:6a:e6:79:05:ef + 12:a3:57:57:45:97:cc:55:43:03:82:d9:3e:21:7f:3e + 88:27:99:8a:c8:96:7e:fe:b6:b6:d4:34:12:2b:a2:5c + b6:a0:77:aa:ee:12:ea:02:49:58:b6:b5:5b:30:b0:2f + c7:a7:c0:08:d9:1a:39:ea:69:87:89:6b:61:b9:a7:b5 + 1f:cc:83:59:41:4b:49:ef:40:41:6e:b9:a8:2f:f1:55 + 48:28:fe:73:29:2b:60:58:54:52:9b:ef:44:cc:8e:5f + dd:64:cd:10:9f:5b:5d:88:dc:aa:0a:46:c8:85:78:e4 + 95:9b:d9:62:13:be:d7:94:cf:92:00:d7:0e:ca:91:52 + 3a:53:ac:b6:57:2c:6a:c3:e9:72:31:09:12:d9:19:8e + 98:ad:47:ec:15:38:d4:0b:a2:6b:52:b0:11:f3:36:3b + 48:bb:18:37:9b:14:98:55:12:32:92:67:23:e4:ff:48 + 3e:29:ff:40:97:ac:aa:db:30:e8:d6:42:ac:fa:15:7f + d6:1c:fc:6f:b3:01:8a:1e:4e:73:1d:81:87:af:3c:7b + 38:a9:9f:55:ae:a0:cd:48:19:49:f3:87:8f:6c:4e:39 + 1d:94:9f:d0:f0:35:ab:c8:ae:64:82:59:85:ca:66:a8 + a0:e6:a0:f2:7f:30:6f:95:75:84:8a:8f:f9:1a:b0:f0 + 81: + +public exponent: + 01:00:01: + +private exponent: + 14:ff:c0:f9:ff:bc:b4:26:e5:87:53:d3:2e:e6:3e:42 + ce:d6:0a:02:94:84:be:b5:30:46:02:50:8e:90:e0:cf + b6:b0:b7:a6:bd:48:cc:d5:8b:d8:ea:72:ff:af:dd:17 + 3c:be:d1:1b:ca:6d:cd:10:a6:86:a8:d9:d2:44:44:27 + d0:65:51:65:0c:27:34:07:dc:7b:38:64:10:03:7c:f4 + a1:cd:40:de:24:3a:e0:21:bc:ef:33:1d:9f:61:e8:57 + ac:e4:9c:c0:7b:df:7c:f8:20:83:ac:0b:8e:0b:d3:62 + eb:8a:8e:03:5b:a3:e5:08:ae:df:a7:fe:85:92:e8:a5 + ae:58:46:72:d6:fc:91:43:b1:7b:a4:c0:5f:51:c3:50 + 0d:e2:67:e8:af:51:13:41:a9:8d:ef:fb:a1:a4:e2:84 + 7c:2b:a0:50:c5:fe:ed:84:a5:25:83:86:4a:d3:0f:56 + 37:38:e6:1e:26:7d:45:22:0b:ba:22:35:be:f8:8b:1b + 72:90:13:c4:1f:c5:d1:34:b5:0e:b2:ee:f7:e1:b9:5e + a2:29:8d:f9:6e:23:4b:50:8f:35:c8:a9:f3:d2:1f:dd + ce:a0:96:50:2d:2e:af:cf:b5:e1:20:e7:e9:d2:49:ed + b5:0e:5b:3e:d1:4b:f1:fa:c2:73:3a:1b:51:34:7e:75 + 30:06:d2:47:d2:a8:2a:45:be:16:fb:8f:63:84:85:b7 + bf:f7:c4:c5:3d:95:56:8c:d1:02:7f:58:ac:4d:11:7b + c5:55:f3:c8:4e:d7:d9:aa:62:b0:e3:1e:04:5c:97:d1 + ca:e2:71:aa:8b:33:b4:34:e9:04:d4:70:7c:f4:cb:57 + 19:c1:03:23:f4:bc:4d:91:8f:b2:9a:99:1c:6c:81:2d + 4d:2d:e9:a1:e3:ce:e3:c9:62:52:89:1f:47:86:61:f1 + dd:bc:46:8d:79:0a:99:9d:aa:4b:a9:0a:72:54:db:dc + ae:48:be:60:4a:73:99:d8:3c:9e:07:78:05:df:87:39 + + +prime1: + 00:e9:63:0f:d7:49:31:27:a8:36:fe:95:bd:8d:05:c1 + 35:48:2e:03:4f:a6:57:54:3a:a4:95:3f:8e:9f:28:7c + d2:df:af:54:36:9e:7c:9f:c3:b9:64:8f:c0:b0:96:3c + aa:01:f6:9a:be:83:e2:85:20:0d:33:de:88:97:af:6f + be:3f:53:5a:a3:77:02:fd:81:17:91:3b:b2:2d:ab:78 + db:d9:43:db:04:69:82:61:30:e4:96:ac:88:8b:f6:3f + 56:c4:49:fd:d5:e5:8c:9d:30:ad:cf:d9:8d:5c:87:b5 + 27:4b:09:8e:19:ed:e2:11:3f:69:b2:47:be:70:39:11 + 41:a3:db:bb:b9:0e:e4:7b:50:d0:d2:c2:89:81:36:b9 + 6b:a6:fe:94:5b:06:66:e6:ed:86:52:42:5e:a9:0e:18 + db:18:f9:14:21:3d:e0:3c:8d:79:c3:f5:d2:cc:51:65 + fb:1c:49:ed:0a:d5:33:99:34:16:f9:1d:68:4a:78:da + 5f: + +prime2: + 00:f2:9d:ae:5f:bd:b7:a3:87:a7:8d:30:46:06:8b:15 + a9:e5:a9:58:1c:2b:3a:7e:78:35:36:56:31:42:df:46 + 87:e8:57:0d:6e:99:de:cf:fb:a8:72:16:71:4b:b3:ad + ed:74:07:cb:cf:7d:2b:12:89:66:c4:0f:8a:ea:e3:37 + 17:2c:75:92:11:7a:a6:da:29:24:33:9b:69:c2:64:68 + 03:db:31:de:fe:1d:a2:4d:9d:91:9f:f0:50:b8:8f:d0 + 22:11:b9:b0:95:98:5e:65:bf:45:97:9b:35:f2:98:27 + 46:7c:b2:86:eb:7b:8b:57:f2:c3:49:47:7d:01:4a:9a + b0:e6:67:05:e5:61:7a:ab:63:c8:cb:d8:44:69:88:72 + a5:a9:60:89:60:df:e6:d9:4d:16:2b:35:7b:20:00:f3 + 3c:d1:78:f9:22:eb:48:c3:7f:78:63:e6:34:60:48:30 + 66:02:bb:38:c2:94:2e:b9:86:b2:2f:9a:4f:17:7f:e1 + 1f: + +coefficient: + 00:93:3e:7c:b9:ea:87:52:37:fa:d5:0a:36:fb:e1:d0 + fc:62:4d:00:0b:ad:a8:fb:bd:34:53:96:c2:6c:a1:6a + 49:b7:a0:24:33:16:95:79:14:ac:bb:75:8d:78:e9:10 + fa:be:44:60:58:94:4a:9c:ba:64:1d:86:27:8b:7f:51 + 4d:80:b0:ff:7a:91:c0:4d:a4:aa:d1:f1:79:7d:8f:71 + 49:12:73:d4:44:5f:0c:2e:55:a6:d9:13:b8:3b:e5:dc + e1:14:98:7e:eb:5b:60:ad:d7:4b:da:c0:d8:3f:bf:70 + 92:53:8c:31:6a:8b:61:5e:a3:7d:ff:84:2c:7d:ed:9f + 74:29:9a:e7:14:fb:c3:ab:8e:9f:60:6a:98:ab:86:0b + ea:fb:ff:20:2f:3b:a7:76:03:3a:55:bb:b2:c6:9c:b5 + 66:36:b8:1c:7f:9b:b6:62:89:ff:6a:d6:35:58:0b:f0 + 55:27:01:f0:67:8d:88:3f:74:48:3d:bf:8c:fc:05:62 + 47: + +exp1: + 00:99:16:2d:91:dd:a4:ac:8a:9e:68:27:f8:89:c4:38 + 93:a6:a0:e7:f3:1a:fd:35:76:b1:f6:64:16:3d:37:e5 + 88:bc:c8:d8:c8:6a:f4:fc:26:fa:38:88:42:b0:92:1b + 80:b8:80:f5:c7:f9:e2:5f:c8:42:60:bf:9b:81:43:c6 + 5c:58:55:68:a2:c8:b1:e1:6f:07:f2:6f:e1:d4:2b:21 + bf:b3:a7:da:c5:ee:1f:63:79:1a:b7:ea:bc:36:72:73 + e1:8a:27:ae:a4:db:49:7c:e2:2d:60:a5:27:20:86:b3 + c0:ee:6b:7a:16:6f:ff:55:a8:ee:bf:ce:67:90:5d:1e + 80:9b:e6:ca:1f:fd:30:c9:e2:9c:d7:62:5b:a7:b2:29 + b5:ff:78:06:00:1f:16:e8:6a:ed:2c:8f:f4:5f:97:ab + 9e:2b:a7:56:18:e7:e9:6a:4e:b2:8c:63:76:be:26:b6 + 6a:1c:88:31:40:65:d0:ce:b1:68:50:47:85:dd:33:a0 + a9: + +exp2: + 00:8d:b1:5f:7c:94:ed:62:39:40:b6:a9:a1:cc:02:80 + c5:77:d6:9e:19:dd:79:4d:11:61:6a:79:8e:4d:92:de + bb:53:0b:3c:52:02:d5:69:3c:7d:95:1b:dc:51:2d:00 + 00:35:0a:b4:92:5a:74:c4:5f:b0:c0:02:9f:cc:2c:a5 + 29:08:93:25:9a:c5:ba:1a:a1:7a:7e:15:5e:ff:e3:ea + 07:8e:85:a2:c9:60:7f:40:bb:2c:a8:6f:0e:85:ab:a0 + 0f:b5:b0:70:1b:fe:1f:eb:66:78:fb:60:ef:71:de:40 + d9:de:cb:d9:16:40:52:12:2c:3a:b7:5a:63:fc:54:18 + e2:05:bd:d7:68:ae:b4:98:d2:2f:1c:36:13:46:5b:25 + 31:f1:28:eb:32:c3:b1:2b:e9:e4:6f:99:cd:6d:d4:80 + 3a:5d:d0:3c:18:93:b7:2c:4e:0e:fe:b1:1c:97:ba:b1 + 61:72:68:eb:6e:60:62:a5:81:b0:21:33:0a:cc:1b:a8 + 5b: + + +Public Key PIN: + pin-sha256:iFdBnKP/7hZCLdj7qqTtdNPFjpZGka259fSYvv3X02U= +Public Key ID: + sha256:8857419ca3ffee16422dd8fbaaa4ed74d3c58e964691adb9f5f498befdd7d365 + sha1:aac584e0e34ca0a8a5bf9641adbd3ec1d1e075e0 + +-----BEGIN RSA PRIVATE KEY----- +MIIG5QIBAAKCAYEA3S9lste1HN3rWtHweizQEaeoJolcjAYBHDEmiMzLEBIVP4p4 +LhANGSBGsro5jiT+LtMQe5QaT8Gbvnj77nnjCaaEOq/VDj5WGFHUr8kDP8dVk1MB +HWs8rAbzaXISk/GVE1mD9C0X0OpBKhKQv98kuz6urjOUqjNfNERq5nkF7xKjV1dF +l8xVQwOC2T4hfz6IJ5mKyJZ+/ra21DQSK6JctqB3qu4S6gJJWLa1WzCwL8enwAjZ +GjnqaYeJa2G5p7UfzINZQUtJ70BBbrmoL/FVSCj+cykrYFhUUpvvRMyOX91kzRCf +W12I3KoKRsiFeOSVm9liE77XlM+SANcOypFSOlOstlcsasPpcjEJEtkZjpitR+wV +ONQ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wULiP0CIRubCVmF5lv0WXmzXymCdGfLKG +63uLV/LDSUd9AUqasOZnBeVheqtjyMvYRGmIcqWpYIlg3+bZTRYrNXsgAPM80Xj5 +IutIw394Y+Y0YEgwZgK7OMKULrmGsi+aTxd/4R8CgcEAmRYtkd2krIqeaCf4icQ4 +k6ag5/Ma/TV2sfZkFj035Yi8yNjIavT8Jvo4iEKwkhuAuID1x/niX8hCYL+bgUPG +XFhVaKLIseFvB/Jv4dQrIb+zp9rF7h9jeRq36rw2cnPhiieupNtJfOItYKUnIIaz +wO5rehZv/1Wo7r/OZ5BdHoCb5sof/TDJ4pzXYlunsim1/3gGAB8W6GrtLI/0X5er +niunVhjn6WpOsoxjdr4mtmociDFAZdDOsWhQR4XdM6CpAoHBAI2xX3yU7WI5QLap +ocwCgMV31p4Z3XlNEWFqeY5Nkt67Uws8UgLVaTx9lRvcUS0AADUKtJJadMRfsMAC +n8wspSkIkyWaxboaoXp+FV7/4+oHjoWiyWB/QLssqG8OhaugD7WwcBv+H+tmePtg +73HeQNney9kWQFISLDq3WmP8VBjiBb3XaK60mNIvHDYTRlslMfEo6zLDsSvp5G+Z +zW3UgDpd0DwYk7csTg7+sRyXurFhcmjrbmBipYGwITMKzBuoWwKBwQCTPny56odS +N/rVCjb74dD8Yk0AC62o+700U5bCbKFqSbegJDMWlXkUrLt1jXjpEPq+RGBYlEqc +umQdhieLf1FNgLD/epHATaSq0fF5fY9xSRJz1ERfDC5VptkTuDvl3OEUmH7rW2Ct +10vawNg/v3CSU4wxaothXqN9/4Qsfe2fdCma5xT7w6uOn2BqmKuGC+r7/yAvO6d2 +AzpVu7LGnLVmNrgcf5u2Yon/atY1WAvwVScB8GeNiD90SD2/jPwFYkc= +-----END RSA PRIVATE KEY----- diff --git a/internal/filter/filter_test.go b/internal/filter/filter_test.go new file mode 100644 index 00000000..7b20767c --- /dev/null +++ b/internal/filter/filter_test.go @@ -0,0 +1,39 @@ +// This file is Free Software under the MIT License +// without warranty, see README.md and LICENSES/MIT.txt for details. +// +// SPDX-License-Identifier: MIT +// +// SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) +// Software-Engineering: 2023 Intevation GmbH + +// Package filter implements helps to filter advisories. +package filter + +import ( + "testing" +) + +// NewPatternMatcher compiles a new list of regular expression from +// a given list of strings. +func TestNewPatternMatcher(t *testing.T) { + var regex []string + if pm, err := NewPatternMatcher(regex); pm == nil || err != nil { + t.Errorf("Failure: Did not compile valid regex pattern") + } + regex = append(regex, "++") + if pm, err := NewPatternMatcher(regex); pm != nil || err == nil { + t.Errorf("Failure: No error thrown at invalid compile pattern") + } +} + +// Matches returns true if the given string matches any of the expressions. +func TestMatches(t *testing.T) { + regex := []string{"a"} + pm, _ := NewPatternMatcher(regex) + if !pm.Matches("a") { + t.Errorf("Failure: Did not match two identical strings") + } + if pm.Matches("b") { + t.Errorf("Failure: Matched two non-matching strings") + } +} diff --git a/internal/misc/doc_test.go b/internal/misc/doc_test.go new file mode 100644 index 00000000..1fec00ae --- /dev/null +++ b/internal/misc/doc_test.go @@ -0,0 +1,10 @@ +// This file is Free Software under the MIT License +// without warranty, see README.md and LICENSES/MIT.txt for details. +// +// SPDX-License-Identifier: MIT +// +// SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) +// Software-Engineering: 2023 Intevation GmbH + +// Package misc implements miscellaneous helper functions. +package misc diff --git a/internal/misc/mime_test.go b/internal/misc/mime_test.go new file mode 100644 index 00000000..58c11398 --- /dev/null +++ b/internal/misc/mime_test.go @@ -0,0 +1,28 @@ +// This file is Free Software under the MIT License +// without warranty, see README.md and LICENSES/MIT.txt for details. +// +// SPDX-License-Identifier: MIT +// +// SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) +// Software-Engineering: 2023 Intevation GmbH + +package misc + +import ( + "testing" + "bytes" + "mime/multipart" +) + + +// CreateFormFile creates an [io.Writer] like [mime/multipart.Writer.CreateFromFile]. +// This version allows to set the mime type, too. +func TestCreateFormFile( t *testing.T) { + body := new(bytes.Buffer) + writer := multipart.NewWriter(body) + + _, err := CreateFormFile(writer, "csaf", "data", "application/json") + if err != nil { + t.Errorf("failed to create an io.Writer via CreateFormFile") + } +} diff --git a/internal/models/models_test.go b/internal/models/models_test.go new file mode 100644 index 00000000..c9dd5b5d --- /dev/null +++ b/internal/models/models_test.go @@ -0,0 +1,89 @@ +// This file is Free Software under the MIT License +// without warranty, see README.md and LICENSES/MIT.txt for details. +// +// SPDX-License-Identifier: MIT +// +// SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) +// Software-Engineering: 2023 Intevation GmbH + +// Package models contains helper models used in the tools internally. +package models + +import ( + "time" + "testing" +) + + + +func TestNewTimeInterval(t *testing.T) { + var before time.Time + before = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) + var after time.Time + after = time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC) + NewTimeInterval(after, before) +} + + +func TestGuessDate(t *testing.T) { + if _, guess := guessDate("2006-01-02T15:04:05"); !guess { + t.Errorf("Failure: Could not guess valid Date from valid string") + } + if _, guess := guessDate(""); guess { + t.Errorf("Failure: Guessed Date from invalid string") + } +} + + +func TestUnmarshalText(t *testing.T) { + testTimeRange := NewTimeInterval( + time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), + time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)) + byteSlice := []byte{'3', 'h'} + var emptySlice []byte + if testTimeRange.UnmarshalText(byteSlice) != nil { + t.Errorf(testTimeRange.UnmarshalText(byteSlice).Error()) + } + if testTimeRange.UnmarshalText(emptySlice) == nil { + t.Errorf("Failure: UnmarshalText succeeded on invalid slice of bytes.") + } +} + +func TestMarshalJSON(t *testing.T) { + testTimeRange := NewTimeInterval( + time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), + time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)) + testTimeRange.MarshalJSON() +} + +func TestUnmarshalFlag(t *testing.T){ + testTimeRange := NewTimeInterval( + time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), + time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)) + if err := testTimeRange.UnmarshalFlag("3h"); err != nil { + t.Errorf(err.Error()) + } + if err := testTimeRange.UnmarshalFlag("2006-01-02T15:04:05"); err != nil { + t.Errorf(err.Error()) + } + if err := testTimeRange.UnmarshalFlag("2006-01-02T15:04:05a"); err == nil { + t.Errorf("Failure: Extracted time from invalid string") + } + if err := testTimeRange.UnmarshalFlag("2006-01-02T15:04:05a, 2007-01-02T15:04:05"); err == nil { + t.Errorf("Failure: Extracted time from invalid string") + } + if err := testTimeRange.UnmarshalFlag("2006-01-02T15:04:05, 2007-01-02T15:04:05a"); err == nil { + t.Errorf("Failure: Extracted time from invalid string") + } + if err := testTimeRange.UnmarshalFlag("2006-01-02T15:04:05, 2007-01-02T15:04:05"); err != nil { + t.Errorf(err.Error()) + } +} + +func TestContains(t *testing.T) { + testTimeRange := NewTimeInterval( + time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), + time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)) + testPointInTime := time.Date(2010, time.March, 10, 23, 0, 0, 0, time.UTC) + testTimeRange.Contains(testPointInTime) +} \ No newline at end of file diff --git a/internal/options/data/config.toml b/internal/options/data/config.toml new file mode 100644 index 00000000..49972250 --- /dev/null +++ b/internal/options/data/config.toml @@ -0,0 +1 @@ +Test = "data/config.toml" diff --git a/internal/options/data/config_plus.toml b/internal/options/data/config_plus.toml new file mode 100644 index 00000000..a7c600c0 --- /dev/null +++ b/internal/options/data/config_plus.toml @@ -0,0 +1,2 @@ +Test = "data/config_plus.toml" +surplus = true diff --git a/internal/options/options_test.go b/internal/options/options_test.go new file mode 100644 index 00000000..c62c4dbd --- /dev/null +++ b/internal/options/options_test.go @@ -0,0 +1,100 @@ +// Package options contains helpers to handle command line options and config files. +package options + +import ( + "fmt" + "os" + "testing" +) + +type config struct { + Test string `long:"Test" description:"Test config struct"` + Version bool `long:"version" description:"test print version"` + ConfigLocation string `long:"configlocation" description:"test location"` +} + +// Parser helps parsing command line arguments and loading +// stored configurations from file. +func TestParse(t *testing.T) { + os.Args = []string{"cmd"} + defaultConfigLocation := []string{"data/config.toml"} + p := Parser[config]{ + DefaultConfigLocations: defaultConfigLocation, + ConfigLocation: func(cfg *config) string { + return cfg.ConfigLocation + }, + Usage: "[OPTIONS] domain...", + HasVersion: func(cfg *config) bool { return cfg.Version }, + SetDefaults: func(cfg *config) { + }, + // Re-establish default values if not set. + EnsureDefaults: func(cfg *config) { + }, + } + + // Test searching for data/config.toml as config file + if _, _, err := p.Parse(); err != nil { + t.Errorf("Failure: Valid Parser using config location failed.") + } + + // Test invalid flag + os.Args = []string{"cmd", "--invalid"} + fmt.Println("The following test should produce a warning.") + if _, _, err := p.Parse(); err == nil { + t.Errorf("Failure: Parsed invalid flag 'invalid'") + } + + // Test with no default location; no config is loaded + var emptyLocation []string + p.DefaultConfigLocations = emptyLocation + os.Args = []string{"cmd"} + if _, _, err := p.Parse(); err != nil { + t.Errorf("Failure: Valid Parser without config location failed: %s", err.Error()) + } + + // Test failing to load TOML file + os.Args = []string{"cmd", "--configlocation=data/config_surplus.toml"} + if _, _, err := p.Parse(); err == nil { + t.Errorf("Failure: Parsed invalid toml file.") + } + + // Test failing to expand Path + os.Args = []string{"cmd", "--configlocation=~~"} + if _, _, err := p.Parse(); err == nil { + t.Errorf("Failure: Invalid path expanded.") + } + // fmt.Println(p.Parse()) +} + +func TestFindConfigFile(t *testing.T) { + locations := []string{"data/config.toml"} + notLocation := []string{"notomllocation"} + errorExpandLocation := []string{"~~"} + + if findConfigFile(locations) != "data/config.toml" { + t.Errorf("Failure: Couldn't find existing toml file in specified location") + } + if !(findConfigFile(notLocation) == "") { + t.Errorf("Failure: Supposedly found configuration file in nonexistant location") + } + fmt.Println("The following test should produce a warning.") + if !(findConfigFile(errorExpandLocation) == "") { + t.Errorf("Failure: Supposedly found configuration file in nonexistant location") + } +} + +func TestLoadToml(t *testing.T) { + var cfg config + if err := loadTOML(&cfg, "data/nonexistant.toml"); err.Error() != "open "+ + "data/nonexistant.toml: no such file or directory" { + t.Errorf("Failure: Didn't throw the correct " + + "error on trying to load nonexistant file") + } + if err := loadTOML(&cfg, "data/config_plus.toml"); err.Error() != "could not parse [\"surplus\"] "+ + "from \"data/config_plus.toml\"" { + t.Errorf("Failure: Succeeded in parsing nonexistant parameter") + } + if err := loadTOML(&cfg, "data/config.toml"); err != nil { + t.Errorf(err.Error()) + } +} From 2e129b9dc84087c1e5f24218f93c2772c1d8144b Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Fri, 8 Sep 2023 11:00:50 +0200 Subject: [PATCH 02/19] Clean up certs_test.go a bit --- internal/certs/certs_test.go | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/internal/certs/certs_test.go b/internal/certs/certs_test.go index 3e49e346..ed5d639f 100644 --- a/internal/certs/certs_test.go +++ b/internal/certs/certs_test.go @@ -3,26 +3,24 @@ package certs import "testing" func TestLoadCertificates(t *testing.T) { - goodTestCert := "data/testclient.crt" - goodTestkey := "data/testclientkey.pem" - goodPassphrase := "security123" -// missingTestCert := "data/testclient_missing.crt" + TestCert := "data/testclient.crt" + Testkey := "data/testclientkey.pem" + Passphrase := "security123" missingTestkey := "data/testclientkey_missing.pem" -// missingPassphrase := "" - if certificate, err := LoadCertificate(&goodTestCert, &goodTestkey, nil); certificate == nil || err != nil { + if certificate, err := LoadCertificate(&TestCert, &Testkey, nil); certificate == nil || err != nil { t.Errorf("Failure: Couldn't load supposedly valid certificate.") } - if certificate, err := LoadCertificate(nil, &goodTestkey, nil); certificate != nil || err == nil { + if certificate, err := LoadCertificate(nil, &Testkey, nil); certificate != nil || err == nil { t.Errorf("Failure: No error despite missing certificate") } - if certificate, err := LoadCertificate(&goodTestCert, &missingTestkey, nil); certificate != nil || err == nil { + if certificate, err := LoadCertificate(&TestCert, &missingTestkey, nil); certificate != nil || err == nil { t.Errorf("Failure: No Failure while loading certificate using missing key.") } - if certificate, err := LoadCertificate(&goodTestCert, &goodTestkey, &goodPassphrase); certificate == nil || err != nil { + if certificate, err := LoadCertificate(&TestCert, &Testkey, &Passphrase); certificate == nil || err != nil { t.Errorf("Failure: Couldn't load supposedly valid certificate with passphrase.") } - if certificate, err := LoadCertificate(&goodTestCert, &missingTestkey, &goodPassphrase); certificate != nil || err == nil { + if certificate, err := LoadCertificate(&TestCert, &missingTestkey, &Passphrase); certificate != nil || err == nil { t.Errorf("Failure: No Failure while loading certificate using missing key with passphrase.") } } From daab24eb2ff04d4b8a6f65e6077dbd3115dc93e8 Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Fri, 8 Sep 2023 13:26:38 +0200 Subject: [PATCH 03/19] Corrected wrong error requirement --- internal/certs/certs_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/certs/certs_test.go b/internal/certs/certs_test.go index ed5d639f..eb123d1c 100644 --- a/internal/certs/certs_test.go +++ b/internal/certs/certs_test.go @@ -17,8 +17,8 @@ func TestLoadCertificates(t *testing.T) { if certificate, err := LoadCertificate(&TestCert, &missingTestkey, nil); certificate != nil || err == nil { t.Errorf("Failure: No Failure while loading certificate using missing key.") } - if certificate, err := LoadCertificate(&TestCert, &Testkey, &Passphrase); certificate == nil || err != nil { - t.Errorf("Failure: Couldn't load supposedly valid certificate with passphrase.") + if certificate, err := LoadCertificate(&TestCert, &Testkey, &Passphrase); certificate != nil || err == nil { + t.Errorf("Failure: Could load unprotected valid certificate with passphrase.") } if certificate, err := LoadCertificate(&TestCert, &missingTestkey, &Passphrase); certificate != nil || err == nil { t.Errorf("Failure: No Failure while loading certificate using missing key with passphrase.") From 3a7b4117896ff2b507ba5df370f27c6a84494885 Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Fri, 8 Sep 2023 13:34:45 +0200 Subject: [PATCH 04/19] formatting --- internal/filter/filter_test.go | 8 ++++---- internal/misc/mime_test.go | 7 +++---- internal/models/models_test.go | 24 ++++++++++-------------- 3 files changed, 17 insertions(+), 22 deletions(-) diff --git a/internal/filter/filter_test.go b/internal/filter/filter_test.go index 7b20767c..fdbe3bc2 100644 --- a/internal/filter/filter_test.go +++ b/internal/filter/filter_test.go @@ -16,19 +16,19 @@ import ( // NewPatternMatcher compiles a new list of regular expression from // a given list of strings. func TestNewPatternMatcher(t *testing.T) { - var regex []string + var regex []string if pm, err := NewPatternMatcher(regex); pm == nil || err != nil { - t.Errorf("Failure: Did not compile valid regex pattern") + t.Errorf("Failure: Did not compile valid regex pattern") } regex = append(regex, "++") if pm, err := NewPatternMatcher(regex); pm != nil || err == nil { - t.Errorf("Failure: No error thrown at invalid compile pattern") + t.Errorf("Failure: No error thrown at invalid compile pattern") } } // Matches returns true if the given string matches any of the expressions. func TestMatches(t *testing.T) { - regex := []string{"a"} + regex := []string{"a"} pm, _ := NewPatternMatcher(regex) if !pm.Matches("a") { t.Errorf("Failure: Did not match two identical strings") diff --git a/internal/misc/mime_test.go b/internal/misc/mime_test.go index 58c11398..4744af1d 100644 --- a/internal/misc/mime_test.go +++ b/internal/misc/mime_test.go @@ -9,18 +9,17 @@ package misc import ( - "testing" "bytes" "mime/multipart" + "testing" ) - // CreateFormFile creates an [io.Writer] like [mime/multipart.Writer.CreateFromFile]. // This version allows to set the mime type, too. -func TestCreateFormFile( t *testing.T) { +func TestCreateFormFile(t *testing.T) { body := new(bytes.Buffer) writer := multipart.NewWriter(body) - + _, err := CreateFormFile(writer, "csaf", "data", "application/json") if err != nil { t.Errorf("failed to create an io.Writer via CreateFormFile") diff --git a/internal/models/models_test.go b/internal/models/models_test.go index c9dd5b5d..f9173eb9 100644 --- a/internal/models/models_test.go +++ b/internal/models/models_test.go @@ -10,12 +10,10 @@ package models import ( - "time" "testing" + "time" ) - - func TestNewTimeInterval(t *testing.T) { var before time.Time before = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) @@ -24,7 +22,6 @@ func TestNewTimeInterval(t *testing.T) { NewTimeInterval(after, before) } - func TestGuessDate(t *testing.T) { if _, guess := guessDate("2006-01-02T15:04:05"); !guess { t.Errorf("Failure: Could not guess valid Date from valid string") @@ -34,12 +31,11 @@ func TestGuessDate(t *testing.T) { } } - func TestUnmarshalText(t *testing.T) { testTimeRange := NewTimeInterval( time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)) - byteSlice := []byte{'3', 'h'} + byteSlice := []byte{'3', 'h'} var emptySlice []byte if testTimeRange.UnmarshalText(byteSlice) != nil { t.Errorf(testTimeRange.UnmarshalText(byteSlice).Error()) @@ -51,17 +47,17 @@ func TestUnmarshalText(t *testing.T) { func TestMarshalJSON(t *testing.T) { testTimeRange := NewTimeInterval( - time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), - time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)) + time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), + time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)) testTimeRange.MarshalJSON() } -func TestUnmarshalFlag(t *testing.T){ +func TestUnmarshalFlag(t *testing.T) { testTimeRange := NewTimeInterval( - time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), - time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)) + time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), + time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)) if err := testTimeRange.UnmarshalFlag("3h"); err != nil { - t.Errorf(err.Error()) + t.Errorf(err.Error()) } if err := testTimeRange.UnmarshalFlag("2006-01-02T15:04:05"); err != nil { t.Errorf(err.Error()) @@ -84,6 +80,6 @@ func TestContains(t *testing.T) { testTimeRange := NewTimeInterval( time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)) - testPointInTime := time.Date(2010, time.March, 10, 23, 0, 0, 0, time.UTC) + testPointInTime := time.Date(2010, time.March, 10, 23, 0, 0, 0, time.UTC) testTimeRange.Contains(testPointInTime) -} \ No newline at end of file +} From e4c636fe418408eb90aa900d88b055f758062d09 Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Tue, 19 Sep 2023 16:11:23 +0200 Subject: [PATCH 05/19] Complete Certs test --- internal/certs/certs_test.go | 40 ++++++++++++++++++++++++------ internal/certs/data/cert.crt | 37 ++++++++++++++++++++++++++++ internal/certs/data/privated.pem | 42 ++++++++++++++++++++++++++++++++ 3 files changed, 111 insertions(+), 8 deletions(-) create mode 100644 internal/certs/data/cert.crt create mode 100644 internal/certs/data/privated.pem diff --git a/internal/certs/certs_test.go b/internal/certs/certs_test.go index eb123d1c..fc761ce6 100644 --- a/internal/certs/certs_test.go +++ b/internal/certs/certs_test.go @@ -3,24 +3,48 @@ package certs import "testing" func TestLoadCertificates(t *testing.T) { - TestCert := "data/testclient.crt" - Testkey := "data/testclientkey.pem" - Passphrase := "security123" + testCert := "data/testclient.crt" + testKey := "data/testclientkey.pem" + passphrase := "qwer" + missingCert := "data/testclientcert_missing.crt" missingTestkey := "data/testclientkey_missing.pem" + privateKey := "data/privated.pem" + privateCert := "data/cert.crt" - if certificate, err := LoadCertificate(&TestCert, &Testkey, nil); certificate == nil || err != nil { + // Try to load certificate that is not protected, expect success. + if certificate, err := LoadCertificate(&testCert, &testKey, nil); certificate == nil || err != nil { t.Errorf("Failure: Couldn't load supposedly valid certificate.") } - if certificate, err := LoadCertificate(nil, &Testkey, nil); certificate != nil || err == nil { + // Try to load no certificate, expect error. + if certificate, err := LoadCertificate(nil, &testKey, nil); certificate != nil || err == nil { t.Errorf("Failure: No error despite missing certificate") } - if certificate, err := LoadCertificate(&TestCert, &missingTestkey, nil); certificate != nil || err == nil { + // Try to load certificate using a nonexistent key, expect error. + if certificate, err := LoadCertificate(&testCert, &missingTestkey, nil); certificate != nil || err == nil { t.Errorf("Failure: No Failure while loading certificate using missing key.") } - if certificate, err := LoadCertificate(&TestCert, &Testkey, &Passphrase); certificate != nil || err == nil { + // Try to decrypt not encrypted certificate, expect error + if certificate, err := LoadCertificate(&testCert, &testKey, &passphrase); certificate != nil || err == nil { t.Errorf("Failure: Could load unprotected valid certificate with passphrase.") } - if certificate, err := LoadCertificate(&TestCert, &missingTestkey, &Passphrase); certificate != nil || err == nil { + // Try to load encrypted certificate using a nonexistent key, but valid passphrase. Expect error. + if certificate, err := LoadCertificate(&testCert, &missingTestkey, &passphrase); certificate != nil || err == nil { t.Errorf("Failure: No Failure while loading certificate using missing key with passphrase.") } + // Try to load encrypted certificate, expecting success. + if certificate, err := LoadCertificate(&privateCert, &privateKey, &passphrase); certificate == nil || err != nil { + t.Errorf("Failure: Couldn't load supposedly valid encrypted certificate.") + } + // Try to load wrong encrypted certificate, expecting error. + if certificate, err := LoadCertificate(&testKey, &privateKey, &passphrase); certificate != nil || err == nil { + t.Errorf("Failure: No Failure while loading certificate using wrong encrypted key.") + } + // Try to load nonexistent encrypted certificate, expecting error. + if certificate, err := LoadCertificate(&missingCert, &privateKey, &passphrase); certificate != nil || err == nil { + t.Errorf("Failure: No Failure while loading nonexistens certificate.") + } + // Try to load nonexistent encrypted certificate, expecting error. + if certificate, err := LoadCertificate(nil, nil, nil); certificate != nil || err != nil { + t.Errorf("Failure: Expected nil return.") + } } diff --git a/internal/certs/data/cert.crt b/internal/certs/data/cert.crt new file mode 100644 index 00000000..f80d61c2 --- /dev/null +++ b/internal/certs/data/cert.crt @@ -0,0 +1,37 @@ +-----BEGIN CERTIFICATE----- +MIIGajCCBNKgAwIBAgIUGNi4GgCUssOOe3k0VuHf3R0+d54wDQYJKoZIhvcNAQEL +BQAwgY0xFDASBgNVBAMTC0NvbW1vbiBuYW1lMRMwEQYDVQQLEwppbnRldmF0aW9u +MRMwEQYDVQQKEwppbnRldmF0aW9uMRMwEQYDVQQHEwppbnRldmF0aW9uMRUwEwYD +VQQIEwxMb3dlciBTYXhvbnkxCzAJBgNVBAYTAkdFMR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RkaXRpb25hbCBkbnNOYW1lIG9mIHRoZSBzdWJqZWN0IG9mIHRoZSBjZXJ0 +aWZpY2F0ZYIBLoIBLoIBLoIBLoIBLoIBLoIBLoIBLoIBLoIBLoIBLoIBLoIBLoIP +c2Vjb25kIGFkZGl0aW9ugg50aGlyZCBhZGRpdGlvboIHZG5zTmFtZTAMBgNVHRMB +Af8EAjAAMDEGA1UdJQQqMCgGCCsGAQUFBwMJBggrBgEFBQcDAgYIKwYBBQUHAwEG +CCsGAQUFBwMCMA8GA1UdDwEB/wQFAwMHsAAwHQYDVR0OBBYEFKrFhODjTKCopb+W +Qa29PsHR4HXgMB8GA1UdIwQYMBaAFCyZxCa1ZUHVy8LjikE8zumAiEgfMA0GCSqG +SIb3DQEBCwUAA4IBgQBTrAgh6d+qiLumEfmkNCmhewxKxDZp+Ni2nz9XRzNO2cQE +U0n8MdbnQInW3xJXng2sAcl1fQz0RN1hkyjDwi69mbbPgcTYmxJFvyt+zRYBe/Sq +4CGGkxEdPW94tMpQ6SrCn2mAMnvcq9A1pYBVYyPeUsaRHC5OUBFOMCevNy8JwNyY +MJ0H5HQCyCysbzA1d521pogGUs/tmbE+ym9zpV8vG0b6De1PexjVeGkTNYz6NCR2 +VZTQ+OJ5iE5pHPEC1Qif44LrR9Kdn/wu3RjTYyHeBOJFjK+DKgleNF4QVTcZQIPE +snN4H+/VSgTZQ3kgWbtpd1m5oRBJovEc2Qe+l+iDFCk8OA4z/x+fkvOeD3NUAl7D +9Pt3cP3UtWUJp4NJn2dvUljmQhB02HSqdNBhqKSg4/cf7l8Zo1ejvBUosrlgw3C3 +apDaC4/xk7woFKVYW25teH2ze+Gpz/YsLDtmL7Bri8CGVsqsN9yqO8SstwKBa3Rt +xQ2em6XnnanApT4iFX4= +-----END CERTIFICATE----- diff --git a/internal/certs/data/privated.pem b/internal/certs/data/privated.pem new file mode 100644 index 00000000..354598af --- /dev/null +++ b/internal/certs/data/privated.pem @@ -0,0 +1,42 @@ +-----BEGIN RS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LHaxtw/P1eBlL44QcfqdqL67ROES+fB27d8vbajm1EQraw +QUoY+NM5KeQyKeRPWxDVQwAv02Lof/FSiB01yNqrzmRojtTykKB5VrnIA1DDP2vI +SoZjPZOSIJHh3qlDaKxlGOQD9Wp4OtIPLqxpBmRgGcq2AVtm57jRAF634nTGvB+N +7fvMpBay3EZy3sauM4MZk7bytJKK6huQjmER+GM/F/Wyw28L7rewK8ukPKx8Wybc +ljVLrduRPt97JH4WWejy+k5vv4LHWJLsGGU474YHGMXF2VE3kJ3JKj8Wm5gS6p/p +-----END RSA PRIVATE KEY----- From be50b7fc3a92cd0ac0f7e3b0044d69dd5283bec4 Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Mon, 25 Sep 2023 11:03:51 +0200 Subject: [PATCH 06/19] Introduce actual tests where missing --- internal/models/models_test.go | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/internal/models/models_test.go b/internal/models/models_test.go index f9173eb9..0452d7b1 100644 --- a/internal/models/models_test.go +++ b/internal/models/models_test.go @@ -19,7 +19,10 @@ func TestNewTimeInterval(t *testing.T) { before = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) var after time.Time after = time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC) - NewTimeInterval(after, before) + pseudoTimeRange := TimeRange{before, after} + if NewTimeInterval(after, before) != pseudoTimeRange { + t.Errorf("Failure: Couldn't generate timerange.") + } } func TestGuessDate(t *testing.T) { @@ -49,7 +52,9 @@ func TestMarshalJSON(t *testing.T) { testTimeRange := NewTimeInterval( time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)) - testTimeRange.MarshalJSON() + if _, err := testTimeRange.MarshalJSON(); err != nil { + t.Errorf("Failure: %v", err) + } } func TestUnmarshalFlag(t *testing.T) { @@ -81,5 +86,15 @@ func TestContains(t *testing.T) { time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC)) testPointInTime := time.Date(2010, time.March, 10, 23, 0, 0, 0, time.UTC) - testTimeRange.Contains(testPointInTime) + testPointAfterTime := time.Date(2022, time.March, 10, 23, 0, 0, 0, time.UTC) + testPointBeforeTime := time.Date(2002, time.March, 10, 23, 0, 0, 0, time.UTC) + if !testTimeRange.Contains(testPointInTime) { + t.Errorf("Failure: Did not recognize point within timerange correctly.") + } + if testTimeRange.Contains(testPointAfterTime) { + t.Errorf("Failure: Did not recognize that a point in time was after a timerange correctly.") + } + if testTimeRange.Contains(testPointBeforeTime) { + t.Errorf("Failure: Did not recognize that a point in time was before a timerange correctly.") + } } From a4954168822690b39906297a216dff0b6df0de24 Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Mon, 25 Sep 2023 11:05:59 +0200 Subject: [PATCH 07/19] Expanded tests where missing (models) --- internal/models/models_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/models/models_test.go b/internal/models/models_test.go index 0452d7b1..32920e8a 100644 --- a/internal/models/models_test.go +++ b/internal/models/models_test.go @@ -29,6 +29,13 @@ func TestGuessDate(t *testing.T) { if _, guess := guessDate("2006-01-02T15:04:05"); !guess { t.Errorf("Failure: Could not guess valid Date from valid string") } + if _, guess := guessDate("2006"); !guess { + t.Errorf("Failure: Could not guess valid Date from valid string") + } + if _, guess := guessDate("2006-01-02"); !guess { + t.Errorf("Failure: Could not guess valid Date from valid string") + } + if _, guess := guessDate(""); guess { t.Errorf("Failure: Guessed Date from invalid string") } From b14d7754221ac748c805b2953f892d17bf88c926 Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Mon, 25 Sep 2023 11:13:13 +0200 Subject: [PATCH 08/19] Add a minimum of documentation to test functions --- internal/models/models_test.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/internal/models/models_test.go b/internal/models/models_test.go index 32920e8a..a01157f1 100644 --- a/internal/models/models_test.go +++ b/internal/models/models_test.go @@ -14,6 +14,7 @@ import ( "time" ) +// TestNewTimeInterval tests the creation of time intervals via NewTimeInterval() func TestNewTimeInterval(t *testing.T) { var before time.Time before = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) @@ -25,6 +26,7 @@ func TestNewTimeInterval(t *testing.T) { } } +// TestGuessDate tests whether a sample of strings are correctly parsed into Dates by guessDate() func TestGuessDate(t *testing.T) { if _, guess := guessDate("2006-01-02T15:04:05"); !guess { t.Errorf("Failure: Could not guess valid Date from valid string") @@ -41,6 +43,7 @@ func TestGuessDate(t *testing.T) { } } +// TestUnmarshalText tests whether UnmarshalText() correctly unmarshals a sample of byteSlices func TestUnmarshalText(t *testing.T) { testTimeRange := NewTimeInterval( time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), @@ -55,6 +58,7 @@ func TestUnmarshalText(t *testing.T) { } } +// TestMarshalJSON tests whether MarshalJSON() correctly marshals a sample TimeRange func TestMarshalJSON(t *testing.T) { testTimeRange := NewTimeInterval( time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), @@ -64,6 +68,7 @@ func TestMarshalJSON(t *testing.T) { } } +// TestUnmarshalFlag tests whether UnmarshalFlag() correctly extracts time from a given timeRange string. func TestUnmarshalFlag(t *testing.T) { testTimeRange := NewTimeInterval( time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), @@ -88,6 +93,8 @@ func TestUnmarshalFlag(t *testing.T) { } } +// TestContains tests whether Contains() correctly identifies a sample of points in time to be within +// a timerange or not. func TestContains(t *testing.T) { testTimeRange := NewTimeInterval( time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC), From b06c316ee058ef9fdcb0011b989f4a35310416c4 Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Mon, 25 Sep 2023 11:15:01 +0200 Subject: [PATCH 09/19] Add minimum documentation to some functions --- internal/options/options_test.go | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/internal/options/options_test.go b/internal/options/options_test.go index c62c4dbd..0758b771 100644 --- a/internal/options/options_test.go +++ b/internal/options/options_test.go @@ -63,9 +63,10 @@ func TestParse(t *testing.T) { if _, _, err := p.Parse(); err == nil { t.Errorf("Failure: Invalid path expanded.") } - // fmt.Println(p.Parse()) } +// TestFindConfigFile tests if findConfigFile() correctly finds existing and +// doesn't find nonexisting config files func TestFindConfigFile(t *testing.T) { locations := []string{"data/config.toml"} notLocation := []string{"notomllocation"} @@ -83,6 +84,7 @@ func TestFindConfigFile(t *testing.T) { } } +// TestLoadToml tests if loadTOML() can correctly load TOML files func TestLoadToml(t *testing.T) { var cfg config if err := loadTOML(&cfg, "data/nonexistant.toml"); err.Error() != "open "+ From 22e6d49f3f743d92b5b603355d5e7766519d1759 Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Mon, 25 Sep 2023 11:16:47 +0200 Subject: [PATCH 10/19] Updated mime_test.go's function documentation --- internal/misc/mime_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/internal/misc/mime_test.go b/internal/misc/mime_test.go index 4744af1d..9d5676cf 100644 --- a/internal/misc/mime_test.go +++ b/internal/misc/mime_test.go @@ -14,8 +14,8 @@ import ( "testing" ) -// CreateFormFile creates an [io.Writer] like [mime/multipart.Writer.CreateFromFile]. -// This version allows to set the mime type, too. +// TestCreateFormFile tests if CreateFormFile throws an error when creating +// a FormFile func TestCreateFormFile(t *testing.T) { body := new(bytes.Buffer) writer := multipart.NewWriter(body) From 17945d67aca5d1282cda67da70f3d4d14790c301 Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Mon, 25 Sep 2023 11:19:32 +0200 Subject: [PATCH 11/19] Update filter_test.go's function documentation --- internal/filter/filter_test.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/internal/filter/filter_test.go b/internal/filter/filter_test.go index fdbe3bc2..775a14b0 100644 --- a/internal/filter/filter_test.go +++ b/internal/filter/filter_test.go @@ -13,8 +13,8 @@ import ( "testing" ) -// NewPatternMatcher compiles a new list of regular expression from -// a given list of strings. +// TestNewPatternMatcher tests if NewPatternMatcher recognizes +// whether a set of sample regular expressions is valid func TestNewPatternMatcher(t *testing.T) { var regex []string if pm, err := NewPatternMatcher(regex); pm == nil || err != nil { @@ -26,7 +26,8 @@ func TestNewPatternMatcher(t *testing.T) { } } -// Matches returns true if the given string matches any of the expressions. +// TestMatches tests if Matches returns whether a given string +// matches a sample of the expressions correctly. func TestMatches(t *testing.T) { regex := []string{"a"} pm, _ := NewPatternMatcher(regex) From 3de42e746dda25494feb27ba34159547930003e2 Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Mon, 25 Sep 2023 11:20:58 +0200 Subject: [PATCH 12/19] Add function documentation to certs_test.go --- internal/certs/certs_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/certs/certs_test.go b/internal/certs/certs_test.go index fc761ce6..eb4d4d13 100644 --- a/internal/certs/certs_test.go +++ b/internal/certs/certs_test.go @@ -2,6 +2,9 @@ package certs import "testing" +// TestLoadCertificates tests if LoadCertificate correctly loads +// valid certificates and throws an error at invalid certificates, +// keys or passphrases func TestLoadCertificates(t *testing.T) { testCert := "data/testclient.crt" testKey := "data/testclientkey.pem" From 1f7d5ada1433344de5d6a6606482e0bb028832ab Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Mon, 25 Sep 2023 11:31:22 +0200 Subject: [PATCH 13/19] Formatted TestCreateFormFile() --- internal/misc/mime_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/internal/misc/mime_test.go b/internal/misc/mime_test.go index 9d5676cf..043e5453 100644 --- a/internal/misc/mime_test.go +++ b/internal/misc/mime_test.go @@ -20,8 +20,7 @@ func TestCreateFormFile(t *testing.T) { body := new(bytes.Buffer) writer := multipart.NewWriter(body) - _, err := CreateFormFile(writer, "csaf", "data", "application/json") - if err != nil { - t.Errorf("failed to create an io.Writer via CreateFormFile") + if _, err := CreateFormFile(writer, "csaf", "data", "application/json"); err != nil { + t.Errorf("Failure: failed to create an io.Writer via CreateFormFile") } } From 21fbd401b79562cb68c409058f3e1428b5c36dc6 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Mon, 25 Sep 2023 13:05:53 +0200 Subject: [PATCH 14/19] Cosmetics --- internal/certs/certs_test.go | 60 +++++++++++++++++--------------- internal/filter/filter_test.go | 2 +- internal/misc/doc_test.go | 10 ------ internal/misc/mime_test.go | 5 ++- internal/options/options_test.go | 4 +-- 5 files changed, 36 insertions(+), 45 deletions(-) delete mode 100644 internal/misc/doc_test.go diff --git a/internal/certs/certs_test.go b/internal/certs/certs_test.go index eb4d4d13..46323d03 100644 --- a/internal/certs/certs_test.go +++ b/internal/certs/certs_test.go @@ -6,48 +6,50 @@ import "testing" // valid certificates and throws an error at invalid certificates, // keys or passphrases func TestLoadCertificates(t *testing.T) { - testCert := "data/testclient.crt" - testKey := "data/testclientkey.pem" - passphrase := "qwer" - missingCert := "data/testclientcert_missing.crt" - missingTestkey := "data/testclientkey_missing.pem" - privateKey := "data/privated.pem" - privateCert := "data/cert.crt" + var ( + testCert = "data/testclient.crt" + testKey = "data/testclientkey.pem" + passphrase = "qwer" + missingCert = "data/testclientcert_missing.crt" + missingTestkey = "data/testclientkey_missing.pem" + privateKey = "data/privated.pem" + privateCert = "data/cert.crt" + ) - // Try to load certificate that is not protected, expect success. - if certificate, err := LoadCertificate(&testCert, &testKey, nil); certificate == nil || err != nil { + // Try to load cert that is not protected, expect success. + if cert, err := LoadCertificate(&testCert, &testKey, nil); cert == nil || err != nil { t.Errorf("Failure: Couldn't load supposedly valid certificate.") } - // Try to load no certificate, expect error. - if certificate, err := LoadCertificate(nil, &testKey, nil); certificate != nil || err == nil { + // Try to load no cert, expect error. + if cert, err := LoadCertificate(nil, &testKey, nil); cert != nil || err == nil { t.Errorf("Failure: No error despite missing certificate") } - // Try to load certificate using a nonexistent key, expect error. - if certificate, err := LoadCertificate(&testCert, &missingTestkey, nil); certificate != nil || err == nil { + // Try to load cert using a nonexistent key, expect error. + if cert, err := LoadCertificate(&testCert, &missingTestkey, nil); cert != nil || err == nil { t.Errorf("Failure: No Failure while loading certificate using missing key.") } - // Try to decrypt not encrypted certificate, expect error - if certificate, err := LoadCertificate(&testCert, &testKey, &passphrase); certificate != nil || err == nil { + // Try to decrypt not encrypted cert, expect error + if cert, err := LoadCertificate(&testCert, &testKey, &passphrase); cert != nil || err == nil { t.Errorf("Failure: Could load unprotected valid certificate with passphrase.") } - // Try to load encrypted certificate using a nonexistent key, but valid passphrase. Expect error. - if certificate, err := LoadCertificate(&testCert, &missingTestkey, &passphrase); certificate != nil || err == nil { - t.Errorf("Failure: No Failure while loading certificate using missing key with passphrase.") + // Try to load encrypted cert using a nonexistent key, but valid passphrase. Expect error. + if cert, err := LoadCertificate(&testCert, &missingTestkey, &passphrase); cert != nil || err == nil { + t.Errorf("Failure: No Failure while loading certificate using missing key with passphrase.") } - // Try to load encrypted certificate, expecting success. - if certificate, err := LoadCertificate(&privateCert, &privateKey, &passphrase); certificate == nil || err != nil { + // Try to load encrypted cert, expecting success. + if cert, err := LoadCertificate(&privateCert, &privateKey, &passphrase); cert == nil || err != nil { t.Errorf("Failure: Couldn't load supposedly valid encrypted certificate.") } - // Try to load wrong encrypted certificate, expecting error. - if certificate, err := LoadCertificate(&testKey, &privateKey, &passphrase); certificate != nil || err == nil { - t.Errorf("Failure: No Failure while loading certificate using wrong encrypted key.") + // Try to load wrong encrypted cert, expecting error. + if cert, err := LoadCertificate(&testKey, &privateKey, &passphrase); cert != nil || err == nil { + t.Errorf("Failure: No Failure while loading certificate using wrong encrypted key.") } - // Try to load nonexistent encrypted certificate, expecting error. - if certificate, err := LoadCertificate(&missingCert, &privateKey, &passphrase); certificate != nil || err == nil { - t.Errorf("Failure: No Failure while loading nonexistens certificate.") + // Try to load nonexistent encrypted cert, expecting error. + if cert, err := LoadCertificate(&missingCert, &privateKey, &passphrase); cert != nil || err == nil { + t.Errorf("Failure: No Failure while loading nonexistens certificate.") } - // Try to load nonexistent encrypted certificate, expecting error. - if certificate, err := LoadCertificate(nil, nil, nil); certificate != nil || err != nil { - t.Errorf("Failure: Expected nil return.") + // Try to load nonexistent encrypted cert, expecting error. + if cert, err := LoadCertificate(nil, nil, nil); cert != nil || err != nil { + t.Errorf("Failure: Expected nil return.") } } diff --git a/internal/filter/filter_test.go b/internal/filter/filter_test.go index 775a14b0..0ac3438c 100644 --- a/internal/filter/filter_test.go +++ b/internal/filter/filter_test.go @@ -22,7 +22,7 @@ func TestNewPatternMatcher(t *testing.T) { } regex = append(regex, "++") if pm, err := NewPatternMatcher(regex); pm != nil || err == nil { - t.Errorf("Failure: No error thrown at invalid compile pattern") + t.Errorf("Failure: No error returned at invalid compile pattern") } } diff --git a/internal/misc/doc_test.go b/internal/misc/doc_test.go deleted file mode 100644 index 1fec00ae..00000000 --- a/internal/misc/doc_test.go +++ /dev/null @@ -1,10 +0,0 @@ -// This file is Free Software under the MIT License -// without warranty, see README.md and LICENSES/MIT.txt for details. -// -// SPDX-License-Identifier: MIT -// -// SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) -// Software-Engineering: 2023 Intevation GmbH - -// Package misc implements miscellaneous helper functions. -package misc diff --git a/internal/misc/mime_test.go b/internal/misc/mime_test.go index 043e5453..bd5a31c2 100644 --- a/internal/misc/mime_test.go +++ b/internal/misc/mime_test.go @@ -9,7 +9,7 @@ package misc import ( - "bytes" + "io" "mime/multipart" "testing" ) @@ -17,8 +17,7 @@ import ( // TestCreateFormFile tests if CreateFormFile throws an error when creating // a FormFile func TestCreateFormFile(t *testing.T) { - body := new(bytes.Buffer) - writer := multipart.NewWriter(body) + writer := multipart.NewWriter(io.Discard) if _, err := CreateFormFile(writer, "csaf", "data", "application/json"); err != nil { t.Errorf("Failure: failed to create an io.Writer via CreateFormFile") diff --git a/internal/options/options_test.go b/internal/options/options_test.go index 0758b771..06ff93f0 100644 --- a/internal/options/options_test.go +++ b/internal/options/options_test.go @@ -92,8 +92,8 @@ func TestLoadToml(t *testing.T) { t.Errorf("Failure: Didn't throw the correct " + "error on trying to load nonexistant file") } - if err := loadTOML(&cfg, "data/config_plus.toml"); err.Error() != "could not parse [\"surplus\"] "+ - "from \"data/config_plus.toml\"" { + const errMsg = `could not parse ["surplus"] from "data/config_plus.toml"` + if err := loadTOML(&cfg, "data/config_plus.toml"); err.Error() != errMsg { t.Errorf("Failure: Succeeded in parsing nonexistant parameter") } if err := loadTOML(&cfg, "data/config.toml"); err != nil { From 62290215ec0d655bd02a25d745dca9aed1fe16c5 Mon Sep 17 00:00:00 2001 From: JanHoefelmeyer Date: Mon, 25 Sep 2023 14:17:22 +0200 Subject: [PATCH 15/19] Test os.exit and log.Fatalf in options_test.go --- internal/options/options_test.go | 52 ++++++++++++++++++++++++++++++++ 1 file changed, 52 insertions(+) diff --git a/internal/options/options_test.go b/internal/options/options_test.go index 06ff93f0..200ffa6d 100644 --- a/internal/options/options_test.go +++ b/internal/options/options_test.go @@ -4,6 +4,7 @@ package options import ( "fmt" "os" + "os/exec" "testing" ) @@ -16,6 +17,7 @@ type config struct { // Parser helps parsing command line arguments and loading // stored configurations from file. func TestParse(t *testing.T) { + originalArgs := os.Args os.Args = []string{"cmd"} defaultConfigLocation := []string{"data/config.toml"} p := Parser[config]{ @@ -63,6 +65,39 @@ func TestParse(t *testing.T) { if _, _, err := p.Parse(); err == nil { t.Errorf("Failure: Invalid path expanded.") } + + // os.exit tests start here + // test the help flag + // if TEST_HELP was set, try parsing the help flag + if os.Getenv("TEST_HELP") == "1" { + os.Args = []string{"cmd", "--help"} + p.Parse() + return + } + + // Build subprocess that can be exited + cmd := exec.Command(originalArgs[0], "-test.run=TestParse") + cmd.Env = append(os.Environ(), "TEST_HELP=1") + err := cmd.Run() + if err != nil { + t.Fatalf(err.Error()) + } + + // test the version flag + if os.Getenv("TEST_VERSION") == "1" { + os.Args = []string{"cmd", "--version"} + p.Parse() + return + } + + cmd = exec.Command(originalArgs[0], "-test.run=TestParse") + cmd.Env = append(os.Environ(), "TEST_VERSION=1") + err = cmd.Run() + if err != nil { + t.Fatalf(err.Error()) + } + // Reset os.Args + os.Args = originalArgs } // TestFindConfigFile tests if findConfigFile() correctly finds existing and @@ -100,3 +135,20 @@ func TestLoadToml(t *testing.T) { t.Errorf(err.Error()) } } + +// TestErrorCheck checks whether the ErrorChecker correctly logs a fatal error +func TestErrorCheck(t *testing.T) { + if os.Getenv("TEST_ERROR") == "1" { + testError := fmt.Errorf("Succesful") + ErrorCheck(testError) + return + } + cmd := exec.Command(os.Args[0], "-test.run=TestErrorCheck") + cmd.Env = append(os.Environ(), "TEST_ERROR=1") + err := cmd.Run() + if e, ok := err.(*exec.ExitError); ok && !e.Success() { + return + } + t.Fatalf("process ran with err %v, want exit status 1", err) + +} From abce4e7f78b6191641fdc5306deabe357cda2fe1 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Mon, 25 Sep 2023 17:54:35 +0200 Subject: [PATCH 16/19] Add test for second pass command line passing. --- internal/options/data/empty.toml | 1 + internal/options/options_test.go | 21 ++++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) create mode 100644 internal/options/data/empty.toml diff --git a/internal/options/data/empty.toml b/internal/options/data/empty.toml new file mode 100644 index 00000000..b852bc01 --- /dev/null +++ b/internal/options/data/empty.toml @@ -0,0 +1 @@ +# Empty but valid config file diff --git a/internal/options/options_test.go b/internal/options/options_test.go index 200ffa6d..93b7b3f8 100644 --- a/internal/options/options_test.go +++ b/internal/options/options_test.go @@ -14,7 +14,7 @@ type config struct { ConfigLocation string `long:"configlocation" description:"test location"` } -// Parser helps parsing command line arguments and loading +// TestParse helps parsing command line arguments and loading // stored configurations from file. func TestParse(t *testing.T) { originalArgs := os.Args @@ -152,3 +152,22 @@ func TestErrorCheck(t *testing.T) { t.Fatalf("process ran with err %v, want exit status 1", err) } + +// TestSecondPassCommandlineParsing checks if the second pass +// of the command line passing is error checked. +func TestSecondPassCommandlineParsing(t *testing.T) { + orig := os.Args + defer func() { os.Args = orig }() + + os.Args = []string{"cmd"} + p := Parser[config]{ + ConfigLocation: func(cfg *config) string { + // This is a bit stupid. + os.Args = []string{"cmd", "--invalid"} + return "data/empty.toml" + }, + } + if _, _, err := p.Parse(); err == nil { + t.Fatalf("Second command line parsing pass did not fail.\n") + } +} From 1854cc4c584f37b7cdabb205bae41cc5d9c04482 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Mon, 25 Sep 2023 18:15:09 +0200 Subject: [PATCH 17/19] Add test to trigger help output in second pass in command line parsing. --- internal/options/options_test.go | 30 ++++++++++++++++++++++++++++-- 1 file changed, 28 insertions(+), 2 deletions(-) diff --git a/internal/options/options_test.go b/internal/options/options_test.go index 93b7b3f8..4716acb6 100644 --- a/internal/options/options_test.go +++ b/internal/options/options_test.go @@ -2,6 +2,7 @@ package options import ( + "errors" "fmt" "os" "os/exec" @@ -139,8 +140,7 @@ func TestLoadToml(t *testing.T) { // TestErrorCheck checks whether the ErrorChecker correctly logs a fatal error func TestErrorCheck(t *testing.T) { if os.Getenv("TEST_ERROR") == "1" { - testError := fmt.Errorf("Succesful") - ErrorCheck(testError) + ErrorCheck(errors.New("succesful")) return } cmd := exec.Command(os.Args[0], "-test.run=TestErrorCheck") @@ -171,3 +171,29 @@ func TestSecondPassCommandlineParsing(t *testing.T) { t.Fatalf("Second command line parsing pass did not fail.\n") } } + +// TestSecondPassCommandlineHelp triggers the help output in the +// second pass of the command line parsing. +func TestSecondPassCommandlineHelp(t *testing.T) { + if os.Getenv("TEST_ERROR") == "1" { + orig := os.Args + defer func() { os.Args = orig }() + + os.Args = []string{"cmd"} + p := Parser[config]{ + ConfigLocation: func(cfg *config) string { + // This is a bit stupid. + os.Args = []string{"cmd", "--help"} + return "data/empty.toml" + }, + } + p.Parse() + t.Fatalf("Second command line parsing pass help should not reach this.\n") + return + } + cmd := exec.Command(os.Args[0], "-test.run=TestSecondPassCommandlineHelp") + cmd.Env = append(os.Environ(), "TEST_ERROR=1") + if err := cmd.Run(); err != nil { + t.Fatalf("process ran with err %v", err) + } +} From 52476f85603894a4cc157fc7bf0f7a5704d3a394 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Mon, 25 Sep 2023 20:12:17 +0200 Subject: [PATCH 18/19] use defer to restore original args. --- internal/options/options_test.go | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/internal/options/options_test.go b/internal/options/options_test.go index 4716acb6..7e59a487 100644 --- a/internal/options/options_test.go +++ b/internal/options/options_test.go @@ -18,7 +18,9 @@ type config struct { // TestParse helps parsing command line arguments and loading // stored configurations from file. func TestParse(t *testing.T) { - originalArgs := os.Args + orig := os.Args + defer func() { os.Args = orig }() + os.Args = []string{"cmd"} defaultConfigLocation := []string{"data/config.toml"} p := Parser[config]{ @@ -77,7 +79,7 @@ func TestParse(t *testing.T) { } // Build subprocess that can be exited - cmd := exec.Command(originalArgs[0], "-test.run=TestParse") + cmd := exec.Command(orig[0], "-test.run=TestParse") cmd.Env = append(os.Environ(), "TEST_HELP=1") err := cmd.Run() if err != nil { @@ -91,14 +93,12 @@ func TestParse(t *testing.T) { return } - cmd = exec.Command(originalArgs[0], "-test.run=TestParse") + cmd = exec.Command(orig[0], "-test.run=TestParse") cmd.Env = append(os.Environ(), "TEST_VERSION=1") err = cmd.Run() if err != nil { t.Fatalf(err.Error()) } - // Reset os.Args - os.Args = originalArgs } // TestFindConfigFile tests if findConfigFile() correctly finds existing and From 6b078858481ccae632115cd7182d43bc65d579d7 Mon Sep 17 00:00:00 2001 From: "Sascha L. Teichmann" Date: Mon, 25 Sep 2023 21:19:41 +0200 Subject: [PATCH 19/19] Fix copyright headers. Make staticcheck happy. --- internal/certs/certs_test.go | 8 ++++++++ internal/filter/filter_test.go | 1 - internal/models/models_test.go | 11 +++++------ internal/options/options.go | 4 ++-- internal/options/options_test.go | 9 ++++++++- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/internal/certs/certs_test.go b/internal/certs/certs_test.go index 46323d03..e9a36cd0 100644 --- a/internal/certs/certs_test.go +++ b/internal/certs/certs_test.go @@ -1,3 +1,11 @@ +// This file is Free Software under the MIT License +// without warranty, see README.md and LICENSES/MIT.txt for details. +// +// SPDX-License-Identifier: MIT +// +// SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) +// Software-Engineering: 2023 Intevation GmbH + package certs import "testing" diff --git a/internal/filter/filter_test.go b/internal/filter/filter_test.go index 0ac3438c..14bcc59c 100644 --- a/internal/filter/filter_test.go +++ b/internal/filter/filter_test.go @@ -6,7 +6,6 @@ // SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) // Software-Engineering: 2023 Intevation GmbH -// Package filter implements helps to filter advisories. package filter import ( diff --git a/internal/models/models_test.go b/internal/models/models_test.go index a01157f1..7da674f4 100644 --- a/internal/models/models_test.go +++ b/internal/models/models_test.go @@ -6,7 +6,6 @@ // SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) // Software-Engineering: 2023 Intevation GmbH -// Package models contains helper models used in the tools internally. package models import ( @@ -16,11 +15,11 @@ import ( // TestNewTimeInterval tests the creation of time intervals via NewTimeInterval() func TestNewTimeInterval(t *testing.T) { - var before time.Time - before = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) - var after time.Time - after = time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC) - pseudoTimeRange := TimeRange{before, after} + var ( + before = time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC) + after = time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC) + pseudoTimeRange = TimeRange{before, after} + ) if NewTimeInterval(after, before) != pseudoTimeRange { t.Errorf("Failure: Couldn't generate timerange.") } diff --git a/internal/options/options.go b/internal/options/options.go index bead99dc..713e81a6 100644 --- a/internal/options/options.go +++ b/internal/options/options.go @@ -3,8 +3,8 @@ // // SPDX-License-Identifier: MIT // -// SPDX-FileCopyrightText: 2022 German Federal Office for Information Security (BSI) -// Software-Engineering: 2022 Intevation GmbH +// SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) +// Software-Engineering: 2023 Intevation GmbH // Package options contains helpers to handle command line options and config files. package options diff --git a/internal/options/options_test.go b/internal/options/options_test.go index 7e59a487..3feba934 100644 --- a/internal/options/options_test.go +++ b/internal/options/options_test.go @@ -1,4 +1,11 @@ -// Package options contains helpers to handle command line options and config files. +// This file is Free Software under the MIT License +// without warranty, see README.md and LICENSES/MIT.txt for details. +// +// SPDX-License-Identifier: MIT +// +// SPDX-FileCopyrightText: 2023 German Federal Office for Information Security (BSI) +// Software-Engineering: 2023 Intevation GmbH + package options import (