Skip to content

Commit 2f3a66e

Browse files
committed
added more tests and some refactors
1 parent 9fe554e commit 2f3a66e

File tree

6 files changed

+129
-14
lines changed

6 files changed

+129
-14
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "ip2location"
3-
version = "0.3.2"
3+
version = "0.3.3"
44
authors = ["MARIRS <marirs@gmail.com>"]
55
description = "Find geo information & proxy information based on the given IP using IP2Location BIN databases"
66
keywords = ["ip2location", "geoip", "geolocation", "ip", "proxy"]

README.md

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,64 @@ cargo t -v
3535
ip2location = "0.3.2"
3636
```
3737

38+
### Example
39+
```rust
40+
use crate::{error, Record, DB};
41+
42+
const IPV4BIN: &str = "data/IP2LOCATION-LITE-DB1.BIN";
43+
const IPV6BIN: &str = "data/IP2LOCATION-LITE-DB1.IPV6.BIN";
44+
const IP2PROXYBIN: &str = "data/IP2PROXY-IP-COUNTRY.BIN";
45+
46+
// Lookup an IP v4 in the IP2Location V6 BIN Database
47+
fn ip_lookup_in_ipv6bin() -> Result<(), error::Error> {
48+
let mut db = DB::from_file(IPV6BIN)?;
49+
let record = db.ip_lookup("43.224.159.155".parse().unwrap())?;
50+
let record = if let Record::LocationDb(rec) = record {
51+
Some(rec)
52+
} else {
53+
None
54+
};
55+
assert!(record.is_some());
56+
let record = record.unwrap();
57+
assert!(!record.country.is_none());
58+
assert_eq!(record.country.clone().unwrap().short_name, "IN");
59+
assert_eq!(record.country.unwrap().long_name, "India");
60+
Ok(())
61+
}
62+
63+
// Lookup an IP v4 in the IP2Location V4 BIN Database
64+
fn ip_lookup_in_ipv4bin() -> Result<(), error::Error> {
65+
let mut db = DB::from_file(IPV4BIN)?;
66+
let record = db.ip_lookup("43.224.159.155".parse().unwrap())?;
67+
let record = if let Record::LocationDb(rec) = record {
68+
Some(rec)
69+
} else {
70+
None
71+
};
72+
assert!(record.is_some());
73+
let record = record.unwrap();
74+
assert!(!record.country.is_none());
75+
assert_eq!(record.country.clone().unwrap().short_name, "IN");
76+
assert_eq!(record.country.unwrap().long_name, "India");
77+
Ok(())
78+
}
79+
80+
// Lookup an IP in the Proxy Database
81+
fn ip_lookup_in_proxy_bin() -> Result<(), error::Error> {
82+
let mut db = DB::from_file(IP2PROXYBIN)?;
83+
let record = db.ip_lookup("1.1.1.1".parse().unwrap())?;
84+
let record = if let Record::ProxyDb(rec) = record {
85+
Some(rec)
86+
} else {
87+
None
88+
};
89+
assert!(record.is_some());
90+
let record = record.unwrap();
91+
assert!(!record.country.is_none());
92+
Ok(())
93+
}
94+
```
95+
3896
### Executing the Example
3997
```bash
4098
cargo b --example

src/common.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,12 @@ impl DB {
175175
//!
176176
//! let mut db = DB::from_file_mmap("data/IP2PROXY-IP-COUNTRY.BIN").unwrap();
177177
//!```
178+
if !path.as_ref().exists() {
179+
return Err(Error::IoError(
180+
"Error opening DB file: No such file or directory".to_string(),
181+
));
182+
}
183+
178184
if let Ok(location_db) = LocationDB::from_file_mmap(&path) {
179185
Ok(DB::LocationDb(location_db))
180186
} else if let Ok(proxy_db) = ProxyDB::from_file_mmap(&path) {

src/ip2location/db.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,16 @@ impl LocationDB {
6666
//!
6767
//! let mut db = LocationDB::from_file("data/IP2LOCATION-LITE-DB1.BIN").unwrap();
6868
//!```
69+
if !path.as_ref().exists() {
70+
return Err(Error::IoError(
71+
"Error opening DB file: No such file or directory".to_string(),
72+
));
73+
}
74+
6975
let db = File::open(&path)?;
70-
let mut ss = Self::new(Source::File(path.as_ref().to_path_buf(), db));
71-
ss.read_header()?;
72-
Ok(ss)
76+
let mut ldb = Self::new(Source::File(path.as_ref().to_path_buf(), db));
77+
ldb.read_header()?;
78+
Ok(ldb)
7379
}
7480

7581
pub fn from_file_mmap<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
@@ -83,11 +89,17 @@ impl LocationDB {
8389
//!
8490
//! let mut db = DB::from_file_mmap("data/IP2LOCATION-LITE-DB1.BIN").unwrap();
8591
//!```
92+
if !path.as_ref().exists() {
93+
return Err(Error::IoError(
94+
"Error opening DB file: No such file or directory".to_string(),
95+
));
96+
}
97+
8698
let db = File::open(&path)?;
8799
let mm = unsafe { Mmap::map(&db) }?;
88-
let mut ss = Self::new(Source::Mmap(path.as_ref().to_path_buf(), mm));
89-
ss.read_header()?;
90-
Ok(ss)
100+
let mut ldb = Self::new(Source::Mmap(path.as_ref().to_path_buf(), mm));
101+
ldb.read_header()?;
102+
Ok(ldb)
91103
}
92104

93105
pub fn print_db_info(&self) {

src/ip2proxy/db.rs

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -65,10 +65,16 @@ impl ProxyDB {
6565
//!
6666
//! let mut db = DB::from_file("data/IP2PROXY-IP-COUNTRY.BIN").unwrap();
6767
//!```
68+
if !path.as_ref().exists() {
69+
return Err(Error::IoError(
70+
"Error opening DB file: No such file or directory".to_string(),
71+
));
72+
}
73+
6874
let db = File::open(&path)?;
69-
let mut ss = Self::new(Source::File(path.as_ref().to_path_buf(), db));
70-
ss.read_header()?;
71-
Ok(ss)
75+
let mut pdb = Self::new(Source::File(path.as_ref().to_path_buf(), db));
76+
pdb.read_header()?;
77+
Ok(pdb)
7278
}
7379

7480
pub fn from_file_mmap<P: AsRef<Path>>(path: P) -> Result<Self, Error> {
@@ -82,11 +88,17 @@ impl ProxyDB {
8288
//!
8389
//! let mut db = DB::from_file_mmap("data/IP2PROXY-IP-COUNTRY.BIN").unwrap();
8490
//!```
91+
if !path.as_ref().exists() {
92+
return Err(Error::IoError(
93+
"Error opening DB file: No such file or directory".to_string(),
94+
));
95+
}
96+
8597
let db = File::open(&path)?;
8698
let mm = unsafe { Mmap::map(&db) }?;
87-
let mut ss = Self::new(Source::Mmap(path.as_ref().to_path_buf(), mm));
88-
ss.read_header()?;
89-
Ok(ss)
99+
let mut pdb = Self::new(Source::Mmap(path.as_ref().to_path_buf(), mm));
100+
pdb.read_header()?;
101+
Ok(pdb)
90102
}
91103

92104
pub fn ip_lookup(&mut self, ip: IpAddr) -> Result<ProxyRecord, Error> {

src/tests/tests_lib.rs

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::{error, Record, DB};
22

33
const IPV4BIN: &str = "data/IP2LOCATION-LITE-DB1.BIN";
44
const IPV6BIN: &str = "data/IP2LOCATION-LITE-DB1.IPV6.BIN";
5+
const IP2PROXYBIN: &str = "data/IP2PROXY-IP-COUNTRY.BIN";
56

67
#[test]
78
fn test_ipv4_lookup_in_ipv4bin() -> Result<(), error::Error> {
@@ -106,7 +107,7 @@ fn test_ipv6_lookup_using_mmap() -> Result<(), error::Error> {
106107
}
107108

108109
#[test]
109-
fn test_err_filenotfound() -> Result<(), error::Error> {
110+
fn test_err_filenotfound_location() -> Result<(), error::Error> {
110111
let db = DB::from_file("nonexistant.bin");
111112
assert!(db.is_err());
112113
let result = &db.unwrap_err();
@@ -115,3 +116,29 @@ fn test_err_filenotfound() -> Result<(), error::Error> {
115116
assert_eq!(result, expected);
116117
Ok(())
117118
}
119+
120+
#[test]
121+
fn test_err_filenotfound_mmap_location() -> Result<(), error::Error> {
122+
let db = DB::from_file_mmap("nonexistant.bin");
123+
assert!(db.is_err());
124+
let result = &db.unwrap_err();
125+
let expected =
126+
&error::Error::IoError("Error opening DB file: No such file or directory".to_string());
127+
assert_eq!(result, expected);
128+
Ok(())
129+
}
130+
131+
#[test]
132+
fn test_ip_lookup_in_proxy_bin() -> Result<(), error::Error> {
133+
let mut db = DB::from_file(IP2PROXYBIN)?;
134+
let record = db.ip_lookup("1.1.1.1".parse().unwrap())?;
135+
let record = if let Record::ProxyDb(rec) = record {
136+
Some(rec)
137+
} else {
138+
None
139+
};
140+
assert!(record.is_some());
141+
let record = record.unwrap();
142+
assert!(!record.country.is_none());
143+
Ok(())
144+
}

0 commit comments

Comments
 (0)