Skip to content

Commit

Permalink
Fixed serious bug in asn_table_to_trie introduced after removing tidy…
Browse files Browse the repository at this point in the history
…verse deps (Fixes #36)
  • Loading branch information
hrbrmstr committed Sep 30, 2018
1 parent 20282cd commit 7e53389
Show file tree
Hide file tree
Showing 11 changed files with 62 additions and 6 deletions.
1 change: 1 addition & 0 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ Imports:
stats,
AsioHeaders,
stringi,
readr,
triebeard
Encoding: UTF-8
LinkingTo: BH, Rcpp, AsioHeaders
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ import(stringi)
import(triebeard)
import(utils)
importFrom(Rcpp,sourceCpp)
importFrom(readr,read_tsv)
useDynLib(iptools)
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
iptools 0.6.1
=============
* Fixed serious bug in `ips_in_cidrs()` introduced after removing tidyverse deps
* Fixed serious bug in `asn_table_to_trie()` introduced after removing
tidyverse deps (Fixes #36)
* Added tests & examples for all CIDR ops
* Added test data file for ASN trie ops

iptools 0.6.0
=============
Expand Down
23 changes: 17 additions & 6 deletions R/cidr.r
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,21 @@
#'
#' @param asn_table_file filename of dat file (can be gzip'd)
#' @export
#' @examples
#' asn_table_to_trie(system.file("test", "rib.tst.gz", package="iptools"))
asn_table_to_trie <- function(asn_table_file) {

read.csv(
readr::read_tsv(
file = asn_table_file,
sep = "\t",
comment.char = ";",
col.names = c("cidr", "asn")
comment = ";",
col_names = c("cidr", "asn")
) -> rip

cidr_split <- stri_split_fixed(rip$cidr, "/", 2, simplify = TRUE)

ip <- cidr_split[,1]
mask <- cidr_split[,1]
prefix = stri_sub(ip_to_binary_string(ip), 1, mask)
mask <- cidr_split[,2]
prefix <- stri_sub(ip_to_binary_string(ip), 1, mask)

triebeard::trie(prefix, rip$asn)

Expand All @@ -26,6 +27,9 @@ asn_table_to_trie <- function(asn_table_file) {
#' @param cidr_trie trie created with \code{asn_table_to_trie()}
#' @param ip character vector or numeric vector of IPv4 addresses
#' @export
#' @examples
#' tbl <- asn_table_to_trie(system.file("test", "rib.tst.gz", package="iptools"))
#' ip_to_asn(tbl, "5.192.0.1")
ip_to_asn <- function(cidr_trie, ip) {

if (inherits(ip, "numeric")) {
Expand All @@ -45,6 +49,11 @@ ip_to_asn <- function(cidr_trie, ip) {
#' @note auto-appends \code{/32} if a bare IPv4 is detected
#' @return \code{data_frame} with \code{ips} column and a logical \code{in_cdir} column
#' @export
#' @examples
#' ips_in_cidrs(
#' c("4.3.2.1", "1.2.3.4", "1.20.113.10", "5.190.145.5"),
#' c("5.190.144.0/21", "1.20.113.0/24")
#' )
ips_in_cidrs <- function(ips, cidrs) {

cidrs[!stri_detect_fixed(cidrs, "/")] <- sprintf("%s/32", cidrs[!stri_detect_fixed(cidrs, "/")])
Expand Down Expand Up @@ -75,6 +84,8 @@ ips_in_cidrs <- function(ips, cidrs) {
#'
#' @param cidrs character vector of IPv4 CIDRs
#' @export
#' @examples
#' host_count("1.52.0.0/14")
host_count <- function(cidrs) {

is_cidr <- stri_detect_fixed(cidrs, "/")
Expand Down
1 change: 1 addition & 0 deletions R/iptools-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,5 @@
#' @import utils
#' @import stats
#' @import AsioHeaders
#' @importFrom readr read_tsv
NULL
Binary file added inst/test/rib.tst.gz
Binary file not shown.
3 changes: 3 additions & 0 deletions man/asn_table_to_trie.Rd

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

3 changes: 3 additions & 0 deletions man/host_count.Rd

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

4 changes: 4 additions & 0 deletions man/ip_to_asn.Rd

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

6 changes: 6 additions & 0 deletions man/ips_in_cidrs.Rd

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

22 changes: 22 additions & 0 deletions tests/testthat/test-cidr-ops.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
context("Test CIDR Operations")

test_that("asn table ops work",{

asn_table_to_trie(
system.file("test", "rib.tst.gz", package="iptools")
) -> asntbl

expect_equal(dim(asntbl), 9994)

expect_equal(ip_to_asn(asntbl, "5.192.0.1"), 5384)

ips_in_cidrs(
c("4.3.2.1", "1.2.3.4", "1.20.113.10", "5.190.145.5"),
c("5.190.144.0/21", "1.20.113.0/24")
) -> res

expect_equal(res$in_cidr, c(FALSE, FALSE, TRUE, TRUE))

expect_equal(host_count("1.52.0.0/14"), 262144)

})

0 comments on commit 7e53389

Please sign in to comment.