Back to Repositories

Validating Binary Module Compilation and Security in NW.js

This test suite validates the binary module compilation and loading functionality in NW.js, focusing on secure module compilation and runtime verification. It tests the nwjc compiler’s module transformation and ensures proper loading of compiled binary modules.

Test Coverage Overview

The test suite provides comprehensive coverage of NW.js binary module compilation and loading:

  • Binary file generation and verification
  • Module compilation using nwjc compiler
  • Security checks for content obfuscation
  • Runtime loading and execution validation
  • Cross-platform compatibility testing

Implementation Analysis

The testing approach combines system-level and browser-based verification using Python and Selenium WebDriver. It implements a multi-stage validation process that includes file system operations, binary analysis, and runtime execution verification through Chrome DevTools Protocol.

  • Python subprocess handling for compiler interaction
  • Platform-specific binary analysis
  • Selenium WebDriver for runtime verification
  • Async wait patterns for UI state confirmation

Technical Details

  • Testing Framework: Python with Selenium WebDriver
  • Key Tools: nwjc compiler, strings utility (Linux)
  • Dependencies: Chrome WebDriver, custom nw_util library
  • Configuration: Chrome options with nwapp path setting
  • Platform Support: Cross-platform with OS-specific handling

Best Practices Demonstrated

The test implementation showcases robust testing practices for native module handling and security verification. It demonstrates proper resource cleanup, platform-specific considerations, and thorough validation of both compilation and runtime behavior.

  • Proper test cleanup and setup
  • Cross-platform compatibility handling
  • Security-focused validation
  • Explicit assertion checks
  • Resource management patterns

nwjs/nwJs

test/sanity/module-bin/test.py

            
import time
import os
import shutil
import subprocess
import platform
from subprocess import Popen, PIPE
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from nw_util import *

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
chrome_options = Options()
testdir = os.path.dirname(os.path.abspath(__file__))
chrome_options.add_argument("nwapp=" + testdir)
binfile = os.path.join(testdir, "lib.bin")
nwjc = os.path.join(os.path.dirname(os.environ['CHROMEDRIVER']), "nwjc.exe" if os.name == "nt" else "nwjc")
os.chdir(testdir)
try:
  os.remove(binfile)
except:
  pass
assert(False == os.path.isfile(binfile))
subprocess.call([nwjc, "--nw-module", "lib2.js", "lib.bin"])
assert(os.path.isfile(binfile))

if platform.system() == 'Linux':
    proc = Popen(['strings', 'lib.bin'], stdout=PIPE, stderr=PIPE)
    out, err = proc.communicate()
    ret = out.decode()
    print(ret)
    assert("42" not in ret)
    assert("foo" not in ret)

driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
try:
    print(driver.current_url)
    result = wait_for_element_id_content(driver, 'result', '52')
    print(result)
    result2 = wait_for_element_id_content(driver, 'source', 'native code')
    print(result2)
    assert("{ [native code] }" in result2)
finally:
    driver.quit()