Skip to content

Commit

Permalink
add tests; clean up
Browse files Browse the repository at this point in the history
  • Loading branch information
Phineas committed Oct 28, 2021
1 parent f95f793 commit 44eefee
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 107 deletions.
18 changes: 0 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 2 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
[package]
authors = ["Phineas Walton <[email protected]>"]
name = "domain-lookup-tree"
version = "0.1.0"
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
generational-arena = "0.2"
readme="./README.md"
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# domain-lookup-tree

## Overview

DomainLookupTree is a data structure which provides efficient domain name lookup matching with support for wildcard entries.

Requirements for this implementation:
Expand All @@ -17,9 +19,11 @@ If, when performing a lookup, the domain contains segments deeper than the wildc
It's good to keep in mind that, when traversing the tree, domain names are sorted by top level to infinite n-level, or in simpler terms, in reverse. This means that if "google.com" is looked up in the tree, it would split by ".", reverse the vector, then first perform a root node lookup for "com", and so on.

Walking down the tree - the story of a lookup:
Let's say have a RuleTree with an entry ".giggl.app" which means that the tree looks like this:
Let's say have a DomainLookupTree with an entry ".giggl.app" which means that the tree looks like this:

app
app
└── giggl [wildcard]

A rule lookup for "canary.giggl.app" is requested. First, "app" is matched, but it's not a wildcard, so it's ignored. We now check the decendants of "app" for "giggl" - it matches, and it's a wildcard match, so we store it within the context of the lookup. This lookup will now 100% return a match, even if it isn't absolute. Anyway, we now check the decendants of "giggl" for "canary", though it doesn't exist, and the traversal ends. Now, we didn't have an absolute match, but we did have a wildcard match earlier on for ".giggl.app", so we successfully return the result ".giggl.app" from the lookup function.

## Usage
6 changes: 2 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,21 +72,19 @@ impl Node {
}

impl DomainLookupTree {
pub fn new(minimum_level: usize) -> DomainLookupTree {
pub fn new() -> DomainLookupTree {
DomainLookupTree {
nodes: Default::default(),
minimum_level,
minimum_level: 0,
}
}

// For inserting an item into the tree, we need to make sure that t
pub fn insert(&mut self, domain: &str) {
let is_wildcard = domain.starts_with(".");
let segments = domain_to_rseg(domain);
let n_segments = segments.len();

let mut head = &mut self.nodes;
// let mut fqdn = String::new();
for (i, segment) in segments.iter().copied().enumerate() {
let node = head
.entry(segment.to_owned())
Expand Down
17 changes: 0 additions & 17 deletions src/main.rs

This file was deleted.

61 changes: 0 additions & 61 deletions src/old.rs

This file was deleted.

57 changes: 57 additions & 0 deletions tests/tests.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
extern crate domain_lookup_tree;

use domain_lookup_tree::DomainLookupTree;

#[test]
fn matches_wildcard_upper_level() {
let mut tree = DomainLookupTree::new();

tree.insert(".test.com");

assert_eq!(tree.lookup("123.test.com"), Some(".test.com".to_string()))
}

#[test]
fn matches_wildcard_direct() {
let mut tree = DomainLookupTree::new();
tree.insert(".test.com");
assert_eq!(tree.lookup("test.com"), Some(".test.com".to_string()))
}

#[test]
fn does_not_match_noninserted() {
let mut tree = DomainLookupTree::new();
tree.insert(".test.com");
assert_eq!(tree.lookup("google.com"), None)
}

#[test]
fn matches_direct() {
let mut tree = DomainLookupTree::new();
tree.insert("test.com");
assert_eq!(tree.lookup("test.com"), Some("test.com".to_string()))
}

#[test]
fn matches_wildcard_n_upper_level() {
let mut tree = DomainLookupTree::new();
tree.insert(".test.com");

assert_eq!(
tree.lookup("a.b.c.123.test.com"),
Some(".test.com".to_string())
)
}

#[test]
fn matches_multiple_inserts_under_common_gtld() {
let mut tree = DomainLookupTree::new();
tree.insert(".test.com");
tree.insert("google.com");
tree.insert("abc.com");
tree.insert("phineas.io");

assert_eq!(tree.lookup("google.com"), Some("google.com".to_string()));
assert_eq!(tree.lookup("phineas.io"), Some("phineas.io".to_string()));
assert_eq!(tree.lookup("test.com"), Some(".test.com".to_string()))
}

0 comments on commit 44eefee

Please sign in to comment.