Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Automatically provide a resource reader for tests #20718

Merged
merged 1 commit into from May 18, 2018
Merged
Changes from all commits
Commits
File filter...
Filter file types
Jump to…
Jump to file
Failed to load files.

Always

Just for now

Automatically provide a resource reader for tests

  • Loading branch information
paulrouget committed Apr 30, 2018
commit e02a23b2f6b8b1d8386f46e73068bdd48a6811f5

Some generated files are not rendered by default. Learn more.

@@ -14,3 +14,6 @@ ipc-channel = "0.10"
regex = "0.2"
serde = "1.0"
embedder_traits = { path = "../embedder_traits" }

[dev-dependencies]
embedder_traits = { path = "../embedder_traits", features = ["tests"] }
@@ -26,6 +26,7 @@ url = "1.2"

[dev-dependencies]
env_logger = "0.5"
embedder_traits = { path = "../embedder_traits", features = ["tests"] }

[target.'cfg(all(unix, not(target_os = "macos"), not(target_os = "ios"), not(target_os = "android")))'.dependencies]
xdg = "2.0"
@@ -2,10 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

extern crate embedder_traits;
extern crate servo_config;

use embedder_traits::resources::register_resources_for_tests;
use servo_config::opts::{parse_url_or_filename, parse_pref_from_command_line};
use servo_config::prefs::{PrefValue, PREFS};
use std::path::Path;
@@ -75,7 +73,6 @@ fn test_argument_parsing_special() {

#[test]
fn test_parse_pref_from_command_line() {
register_resources_for_tests();
// Test with boolean values.
parse_pref_from_command_line("testtrue=true");
assert_eq!(*PREFS.get("testtrue"), PrefValue::Boolean(true));
@@ -2,10 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

extern crate embedder_traits;
extern crate servo_config;

use embedder_traits::resources::register_resources_for_tests;
use servo_config::basedir;
use servo_config::prefs::{PREFS, PrefValue, read_prefs};
use std::fs::{self, File};
@@ -28,7 +26,6 @@ fn test_create_pref() {

#[test]
fn test_get_set_reset_extend() {
register_resources_for_tests();
let json_str = "{\
\"layout.writing-mode.enabled\": true,\
\"extra.stuff\": false,\
@@ -43,3 +43,6 @@ webrender_api = {git = "https://github.com/servo/webrender", features = ["ipc"]}

[target.'cfg(all(not(target_os = "windows"), not(target_os = "ios")))'.dependencies]
gaol = {git = "https://github.com/servo/gaol"}

[dev-dependencies]
embedder_traits = { path = "../embedder_traits", features = ["tests"] }
@@ -9,5 +9,8 @@ publish = false
name = "embedder_traits"
path = "lib.rs"

[features]
tests = []

[dependencies]
lazy_static = "1"
@@ -2,14 +2,18 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::env;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
use std::sync::{Once, ONCE_INIT, RwLock};
use std::sync::RwLock;

lazy_static! {
static ref RES: RwLock<Option<Box<ResourceReaderMethods + Sync + Send>>> = RwLock::new(None);
static ref RES: RwLock<Option<Box<ResourceReaderMethods + Sync + Send>>> = RwLock::new({
#[cfg(not(feature = "tests"))] {
None
}
#[cfg(feature = "tests")] {
Some(resources_for_tests())
}
});
}

pub fn set(reader: Box<ResourceReaderMethods + Sync + Send>) {
@@ -53,45 +57,45 @@ pub trait ResourceReaderMethods {
fn sandbox_access_files_dirs(&self) -> Vec<PathBuf>;
}

static INIT: Once = ONCE_INIT;

pub fn register_resources_for_tests() {
INIT.call_once(|| {
struct ResourceReader;
impl ResourceReaderMethods for ResourceReader {
fn sandbox_access_files(&self) -> Vec<PathBuf> { vec![] }
fn sandbox_access_files_dirs(&self) -> Vec<PathBuf> { vec![] }
fn read(&self, file: Resource) -> Vec<u8> {
let file = match file {
Resource::Preferences => "prefs.json",
Resource::BluetoothBlocklist => "gatt_blocklist.txt",
Resource::DomainList => "public_domains.txt",
Resource::HstsPreloadList => "hsts_preload.json",
Resource::SSLCertificates => "certs",
Resource::BadCertHTML => "badcert.html",
Resource::NetErrorHTML => "neterror.html",
Resource::UserAgentCSS => "user-agent.css",
Resource::ServoCSS => "servo.css",
Resource::PresentationalHintsCSS => "presentational-hints.css",
Resource::QuirksModeCSS => "quirks-mode.css",
Resource::RippyPNG => "rippy.png",
};
let mut path = env::current_exe().unwrap();
path = path.canonicalize().unwrap();
while path.pop() {
path.push("resources");
if path.is_dir() {
break;
}
path.pop();
#[cfg(feature = "tests")]
fn resources_for_tests() -> Box<ResourceReaderMethods + Sync + Send> {
use std::env;
use std::fs::File;
use std::io::Read;
struct ResourceReader;
impl ResourceReaderMethods for ResourceReader {
fn sandbox_access_files(&self) -> Vec<PathBuf> { vec![] }
fn sandbox_access_files_dirs(&self) -> Vec<PathBuf> { vec![] }
fn read(&self, file: Resource) -> Vec<u8> {
let file = match file {
Resource::Preferences => "prefs.json",
Resource::BluetoothBlocklist => "gatt_blocklist.txt",
Resource::DomainList => "public_domains.txt",
Resource::HstsPreloadList => "hsts_preload.json",
Resource::SSLCertificates => "certs",
Resource::BadCertHTML => "badcert.html",
Resource::NetErrorHTML => "neterror.html",
Resource::UserAgentCSS => "user-agent.css",
Resource::ServoCSS => "servo.css",
Resource::PresentationalHintsCSS => "presentational-hints.css",
Resource::QuirksModeCSS => "quirks-mode.css",
Resource::RippyPNG => "rippy.png",
};
let mut path = env::current_exe().unwrap();
path = path.canonicalize().unwrap();
while path.pop() {
path.push("resources");
if path.is_dir() {
break;
}
path.push(file);
let mut buffer = vec![];
File::open(path).expect(&format!("Can't find file: {}", file))
.read_to_end(&mut buffer).expect("Can't read file");
buffer
path.pop();
}
path.push(file);
let mut buffer = vec![];
File::open(path).expect(&format!("Can't find file: {}", file))
.read_to_end(&mut buffer).expect("Can't read file");
buffer
}
set(Box::new(ResourceReader));
});
}
Box::new(ResourceReader)
}
@@ -51,3 +51,6 @@ servo_url = {path = "../url"}
style = {path = "../style"}
style_traits = {path = "../style_traits"}
webrender_api = {git = "https://github.com/servo/webrender", features = ["ipc"]}

[dev-dependencies]
embedder_traits = { path = "../embedder_traits", features = ["tests"] }
@@ -49,6 +49,9 @@ url = "1.2"
uuid = {version = "0.6", features = ["v4"]}
webrender_api = {git = "https://github.com/servo/webrender", features = ["ipc"]}

[dev-dependencies]
embedder_traits = { path = "../embedder_traits", features = ["tests"] }

[[test]]
name = "main"
path = "tests/main.rs"
@@ -3,7 +3,6 @@
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use cookie_rs;
use embedder_traits::resources::register_resources_for_tests;
use hyper::header::{Header, SetCookie};
use net::cookie::Cookie;
use net::cookie_storage::CookieStorage;
@@ -57,7 +56,6 @@ fn test_default_path() {
#[test]
fn fn_cookie_constructor() {
use net_traits::CookieSource;
register_resources_for_tests();

let url = &ServoUrl::parse("http://example.com/foo").unwrap();

@@ -104,7 +102,6 @@ fn fn_cookie_constructor() {

#[test]
fn test_cookie_secure_prefix() {
register_resources_for_tests();
let url = &ServoUrl::parse("https://example.com").unwrap();
let cookie = cookie_rs::Cookie::parse("__Secure-SID=12345").unwrap();
assert!(Cookie::new_wrapped(cookie, url, CookieSource::HTTP).is_none());
@@ -132,7 +129,6 @@ fn test_cookie_secure_prefix() {

#[test]
fn test_cookie_host_prefix() {
register_resources_for_tests();
let url = &ServoUrl::parse("https://example.com").unwrap();
let cookie = cookie_rs::Cookie::parse("__Host-SID=12345").unwrap();
assert!(Cookie::new_wrapped(cookie, url, CookieSource::HTTP).is_none());
@@ -186,7 +182,6 @@ fn delay_to_ensure_different_timestamp() {}
#[test]
fn test_sort_order() {
use std::cmp::Ordering;
register_resources_for_tests();

let url = &ServoUrl::parse("http://example.com/foo").unwrap();
let a_wrapped = cookie_rs::Cookie::parse("baz=bar; Path=/foo/bar/").unwrap();
@@ -206,7 +201,6 @@ fn test_sort_order() {

fn add_cookie_to_storage(storage: &mut CookieStorage, url: &ServoUrl, cookie_str: &str)
{
register_resources_for_tests();
let source = CookieSource::HTTP;
let cookie = cookie_rs::Cookie::parse(cookie_str.to_owned()).unwrap();
let cookie = Cookie::new_wrapped(cookie, url, source).unwrap();
@@ -215,7 +209,6 @@ fn add_cookie_to_storage(storage: &mut CookieStorage, url: &ServoUrl, cookie_str

#[test]
fn test_insecure_cookies_cannot_evict_secure_cookie() {
register_resources_for_tests();
let mut storage = CookieStorage::new(5);
let secure_url = ServoUrl::parse("https://home.example.org:8888/cookie-parser?0001").unwrap();
let source = CookieSource::HTTP;
@@ -252,7 +245,6 @@ fn test_insecure_cookies_cannot_evict_secure_cookie() {

#[test]
fn test_secure_cookies_eviction() {
register_resources_for_tests();
let mut storage = CookieStorage::new(5);
let url = ServoUrl::parse("https://home.example.org:8888/cookie-parser?0001").unwrap();
let source = CookieSource::HTTP;
@@ -288,7 +280,6 @@ fn test_secure_cookies_eviction() {

#[test]
fn test_secure_cookies_eviction_non_http_source() {
register_resources_for_tests();
let mut storage = CookieStorage::new(5);
let url = ServoUrl::parse("https://home.example.org:8888/cookie-parser?0001").unwrap();
let source = CookieSource::NonHTTP;
@@ -350,7 +341,6 @@ fn add_retrieve_cookies(set_location: &str,

#[test]
fn test_cookie_eviction_expired() {
register_resources_for_tests();
let mut vec = Vec::new();
for i in 1..6 {
let st = format!("extra{}=bar; Secure; expires=Sun, 18-Apr-2000 21:06:29 GMT",
@@ -366,7 +356,6 @@ fn test_cookie_eviction_expired() {

#[test]
fn test_cookie_eviction_all_secure_one_nonsecure() {
register_resources_for_tests();
let mut vec = Vec::new();
for i in 1..5 {
let st = format!("extra{}=bar; Secure; expires=Sun, 18-Apr-2026 21:06:29 GMT",
@@ -383,7 +372,6 @@ fn test_cookie_eviction_all_secure_one_nonsecure() {

#[test]
fn test_cookie_eviction_all_secure_new_nonsecure() {
register_resources_for_tests();
let mut vec = Vec::new();
for i in 1..6 {
let st = format!("extra{}=bar; Secure; expires=Sun, 18-Apr-2026 21:06:29 GMT",
@@ -399,7 +387,6 @@ fn test_cookie_eviction_all_secure_new_nonsecure() {

#[test]
fn test_cookie_eviction_all_nonsecure_new_secure() {
register_resources_for_tests();
let mut vec = Vec::new();
for i in 1..6 {
let st = format!("extra{}=bar; expires=Sun, 18-Apr-2026 21:06:29 GMT", i);
@@ -414,7 +401,6 @@ fn test_cookie_eviction_all_nonsecure_new_secure() {

#[test]
fn test_cookie_eviction_all_nonsecure_new_nonsecure() {
register_resources_for_tests();
let mut vec = Vec::new();
for i in 1..6 {
let st = format!("extra{}=bar; expires=Sun, 18-Apr-2026 21:06:29 GMT", i);
ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.