Skip to content

Commit

Permalink
unit tests for token handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mcalmer committed Dec 18, 2024
1 parent 45bc7d5 commit 35326cb
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2449,6 +2449,38 @@ protected SCCClient getSCCClient(ContentSyncSource source) throws SCCClientExcep
csm.updateRepositoriesPayg();
}

@Test
public void testGetTokenFromUrl() {
class CSM extends ContentSyncManager {
public Optional<String> getTokenFromURLTest(String url) {
return getTokenFromURL(url);
}
}
Map<String, Optional<String>> urlMap = Map.of("http://my.host.top/?os=RHEL",
Optional.empty(),
"http://my.host.top/?__token__=exp=1990353599~acl=/repo/$RCE/SLE11-WebYaST-SP2-Updates/sle-11-i586/*" +
"~hmac=984b6b366f696884d0e3ac619af3a41b2d678eeec523135cc70921c541e5ec60",
Optional.of("__token__=exp=1990353599~acl=/repo/$RCE/SLE11-WebYaST-SP2-Updates/sle-11-i586/*" +
"~hmac=984b6b366f696884d0e3ac619af3a41b2d678eeec523135cc70921c541e5ec60"),
"http://my.host.top/?NoaBh00JIaJwSozVS2BK1G6x27JmfPxfKiiMZBlZ4SD1x3S_VUt7805g_G4XB0ShvcKDO4A5uhzv" +
"o74HNzCEAYhMxG8dIw0ZIMla3FzxXCKR5gUaW6PeLjCHG4LrgoXa3zG7KPyy8OQMSAni9F1bs2fqOjKqgQ",
Optional.of("NoaBh00JIaJwSozVS2BK1G6x27JmfPxfKiiMZBlZ4SD1x3S_VUt7805g_G4XB0ShvcKDO4A5uhzv" +
"o74HNzCEAYhMxG8dIw0ZIMla3FzxXCKR5gUaW6PeLjCHG4LrgoXa3zG7KPyy8OQMSAni9F1bs2fqOjKqgQ"),
"http://my.host.top/",
Optional.empty(),
"http://my.host.top/?susetk=exp=1990353599~acl=/repo/$RCE/SLE11-WebYaST-SP2-Updates/sle-11-i586/*" +
"~hmac=984b6b366f696884d0e3ac619af3a41b2d678eeec523135cc70921c541e5ec60",
Optional.of("susetk=exp=1990353599~acl=/repo/$RCE/SLE11-WebYaST-SP2-Updates/sle-11-i586/*" +
"~hmac=984b6b366f696884d0e3ac619af3a41b2d678eeec523135cc70921c541e5ec60"),
"http://my.host.top/?hmac=984b6b366f696884d0e3ac619af3a41b2d678eeec523135cc70921c541e5ec60",
Optional.empty(),
"http://my.host.top/?exp=18976547",
Optional.empty());

CSM csm = new CSM();
urlMap.forEach((url, result) -> assertEquals(result, csm.getTokenFromURLTest(url)));
}

/**
* {@inheritDoc}
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,12 @@
*/
package com.redhat.rhn.manager.content.test;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.redhat.rhn.common.conf.Config;
import com.redhat.rhn.manager.content.ContentSyncException;
import com.redhat.rhn.manager.content.ContentSyncManager;
import com.redhat.rhn.manager.content.MgrSyncUtils;
import com.redhat.rhn.testing.BaseTestCaseWithUser;
Expand All @@ -27,9 +30,12 @@
import org.junit.jupiter.api.Test;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Map;


public class MgrSyncUtilsTest extends BaseTestCaseWithUser {
Expand Down Expand Up @@ -163,4 +169,62 @@ public void testurlToFSPathLegacyQuoteNormalize2() throws Exception {
assertContains(opath.toString(), expected.toString());
assertFalse(opath.toString().contains("%"), "Decoding error: " + opath);
}

@Test
public void testTokens() {
Map<String, Boolean> tokenMap = Map.of("os=RHEL", false,
"__token__=exp=1990353599~acl=/repo/$RCE/SLE11-WebYaST-SP2-Updates/sle-11-i586/*~" +
"hmac=984b6b366f696884d0e3ac619af3a41b2d678eeec523135cc70921c541e5ec60", true,
"NoaBh00JIaJwSozVS2BK1G6x27JmfPxfKiiMZBlZ4SD1x3S_VUt7805g_G4XB0ShvcKDO4A5uhzvo74HNzCEAYh" +
"MxG8dIw0ZIMla3FzxXCKR5gUaW6PeLjCHG4LrgoXa3zG7KPyy8OQMSAni9F1bs2fqOjKqgQ", true,
"", false,
"susetk=exp=1990353599~acl=/repo/$RCE/SLE11-WebYaST-SP2-Updates/sle-11-i586/*~" +
"hmac=984b6b366f696884d0e3ac619af3a41b2d678eeec523135cc70921c541e5ec60", true,
"hmac=984b6b366f696884d0e3ac619af3a41b2d678eeec523135cc70921c541e5ec60", false,
"exp=18976547", false);
tokenMap.forEach((token, result) -> assertEquals(result, MgrSyncUtils.isAuthToken(token)));

assertThrows(ContentSyncException.class, () -> MgrSyncUtils.isAuthToken("exp=1234556&hmac=adf284875ee"));
}

@Test
public void testUrlToPath() throws IOException {
Path path = new File("/tmp/mirror").toPath();
URI fsPath = MgrSyncUtils.urlToFSPath("http://mirrors.fedoraproject.org/mirrorlist?repo=epel-7&arch=x86_64",
"epel7", path);
assertEquals("/tmp/mirror/mirrors.fedoraproject.org/mirrorlist/arch/x86_64/repo/epel-7", fsPath.getPath());
fsPath = MgrSyncUtils.urlToFSPath("http://localhost/pub/repositories/empty/",
"empty", path);
assertEquals("http://localhost/pub/repositories/empty/", fsPath.toString());
fsPath = MgrSyncUtils.urlToFSPath("http://external.domain.top/myrepo?387465284375628347568347565723567",
"myrepo", path);
assertEquals("/tmp/mirror/external.domain.top/myrepo", fsPath.getPath());
fsPath = MgrSyncUtils.urlToFSPath("http://external.domain.top/myrepo2?exttok=exp=1990353599~acl=*~" +
"hmac=984b6b366f696884d0e3ac619af3a41b2d678eeec523135cc70921c541e5ec60",
"myrepo", path);
assertEquals("/tmp/mirror/external.domain.top/myrepo2", fsPath.getPath());
fsPath = MgrSyncUtils.urlToFSPath("http://dl.suse.com/myrepo3?exttok=exp=1990353599~acl=*~" +
"hmac=984b6b366f696884d0e3ac619af3a41b2d678eeec523135cc70921c541e5ec60",
"myrepo", path);
assertEquals("/tmp/mirror/myrepo3", fsPath.getPath());
try {
File tempLocalRepo = Paths.get(path.toString(), "repo", "RPMMD", "driverrepo").toFile();
tempLocalRepo.delete();
tempLocalRepo.mkdirs();
fsPath = MgrSyncUtils.urlToFSPath("http://external.domain.top/myrepo4",
"driverrepo", path);
assertEquals(tempLocalRepo.getPath() + "/", fsPath.getPath());

tempLocalRepo = Paths.get(path.toString(), "mydriverrepo").toFile();
tempLocalRepo.delete();
tempLocalRepo.mkdirs();
fsPath = MgrSyncUtils.urlToFSPath("http://other.domain.top/mydriverrepo",
"otherdriverrepo", path);
assertEquals(tempLocalRepo.getPath() + "/", fsPath.getPath());
}
finally {
FileUtils.deleteDirectory(path.toFile());
}

}
}

0 comments on commit 35326cb

Please sign in to comment.