From 428d9f280dfb369c2d7cebc0920dc8ed89a36c6a Mon Sep 17 00:00:00 2001 From: Patrick Date: Fri, 6 Dec 2024 19:30:26 +0000 Subject: [PATCH] Added tests for tracking if the version was properly registered --- director/director_test.go | 147 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 147 insertions(+) diff --git a/director/director_test.go b/director/director_test.go index 8ea01a203..551052aaf 100644 --- a/director/director_test.go +++ b/director/director_test.go @@ -874,6 +874,153 @@ func TestDirectorRegistration(t *testing.T) { assert.False(t, getAd.DisableDirectorTest) teardown() }) + + t.Run("origin-advertise-with-version-and-ua-test", func(t *testing.T) { + c, r, w := setupContext() + pKey, token, _ := generateToken() + publicKey, err := jwk.PublicKeyOf(pKey) + assert.NoError(t, err, "Error creating public key from private key") + setupJwksCache(t, "/foo/bar", publicKey) + + isurl := url.URL{} + isurl.Path = ts.URL + + ad := server_structs.OriginAdvertiseV2{ + BrokerURL: "https://broker-url.org", + DataURL: "https://or-url.org", + Name: "test", + Namespaces: []server_structs.NamespaceAdV2{{ + Path: "/foo/bar", + Issuer: []server_structs.TokenIssuer{{IssuerUrl: isurl}}, + }}, + Version: "7.0.0", + } + + jsonad, err := json.Marshal(ad) + assert.NoError(t, err, "Error marshalling OriginAdvertise") + + setupRequest(c, r, jsonad, token, server_structs.OriginType) + + r.ServeHTTP(w, c.Request) + + assert.Equal(t, 200, w.Result().StatusCode, "Expected status code of 200") + + get := serverAds.Get("https://or-url.org") + getAd := get.Value() + require.NotNil(t, get, "Coudln't find server in the director cache.") + assert.Equal(t, ad.Version, getAd.Version) + teardown() + + }) + t.Run("origin-advertise-with-ua-version-test", func(t *testing.T) { + c, r, w := setupContext() + pKey, token, _ := generateToken() + publicKey, err := jwk.PublicKeyOf(pKey) + assert.NoError(t, err, "Error creating public key from private key") + setupJwksCache(t, "/foo/bar", publicKey) + + isurl := url.URL{} + isurl.Path = ts.URL + + ad := server_structs.OriginAdvertiseV2{ + BrokerURL: "https://broker-url.org", + DataURL: "https://or-url.org", + Name: "test", + Namespaces: []server_structs.NamespaceAdV2{{ + Path: "/foo/bar", + Issuer: []server_structs.TokenIssuer{{IssuerUrl: isurl}}, + }}, + } + + jsonad, err := json.Marshal(ad) + assert.NoError(t, err, "Error marshalling OriginAdvertise") + + setupRequest(c, r, jsonad, token, server_structs.OriginType) + + r.ServeHTTP(w, c.Request) + + assert.Equal(t, 200, w.Result().StatusCode, "Expected status code of 200") + + get := serverAds.Get("https://or-url.org") + getAd := get.Value() + require.NotNil(t, get, "Coudln't find server in the director cache.") + assert.Equal(t, "7.0.0", getAd.Version) + teardown() + }) + + t.Run("origin-advertise-with-no-version-info-test", func(t *testing.T) { + c, r, w := setupContext() + pKey, token, _ := generateToken() + publicKey, err := jwk.PublicKeyOf(pKey) + assert.NoError(t, err, "Error creating public key from private key") + setupJwksCache(t, "/foo/bar", publicKey) + + isurl := url.URL{} + isurl.Path = ts.URL + + ad := server_structs.OriginAdvertiseV2{ + BrokerURL: "https://broker-url.org", + DataURL: "https://or-url.org", + Name: "test", + Namespaces: []server_structs.NamespaceAdV2{{ + Path: "/foo/bar", + Issuer: []server_structs.TokenIssuer{{IssuerUrl: isurl}}, + }}, + } + + jsonad, err := json.Marshal(ad) + assert.NoError(t, err, "Error marshalling OriginAdvertise") + + setupRequest(c, r, jsonad, token, server_structs.OriginType) + // set header so that it doesn't have any version info + c.Request.Header.Set("User-Agent", "fake-curl") + + r.ServeHTTP(w, c.Request) + + assert.Equal(t, 200, w.Result().StatusCode, "Expected status code of 200") + + get := serverAds.Get("https://or-url.org") + getAd := get.Value() + require.NotNil(t, get, "Coudln't find server in the director cache.") + assert.Equal(t, "unknown", getAd.Version) + teardown() + }) + + t.Run("origin-advertise-with-old-ad-test", func(t *testing.T) { + c, r, w := setupContext() + pKey, token, _ := generateToken() + publicKey, err := jwk.PublicKeyOf(pKey) + assert.NoError(t, err, "Error creating public key from private key") + setupJwksCache(t, "/foo/bar", publicKey) + + isurl := url.URL{} + isurl.Path = ts.URL + + ad := server_structs.OriginAdvertiseV1{ + Name: "test", + URL: "https://or-url.org", + Namespaces: []server_structs.NamespaceAdV1{{ + Path: "/foo/bar", + Issuer: isurl, + }}, + } + + jsonad, err := json.Marshal(ad) + assert.NoError(t, err, "Error marshalling OriginAdvertise") + + setupRequest(c, r, jsonad, token, server_structs.OriginType) + + r.ServeHTTP(w, c.Request) + + assert.Equal(t, 200, w.Result().StatusCode, "Expected status code of 200") + + get := serverAds.Get("https://or-url.org") + getAd := get.Value() + require.NotNil(t, get, "Coudln't find server in the director cache.") + assert.Equal(t, "7.0.0", getAd.Version) + teardown() + + }) } func TestGetAuthzEscaped(t *testing.T) {