Skip to content

Commit 1c319ab

Browse files
committed
[test] add native module buffer case
1 parent fe744dd commit 1c319ab

File tree

6 files changed

+171
-0
lines changed

6 files changed

+171
-0
lines changed
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"targets": [
3+
{
4+
"target_name": "buffer_example",
5+
"sources": [ "buffer_example.cpp" ],
6+
"include_dirs" : ["<!(node -e \"require('nan')\")"]
7+
}
8+
]
9+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <nan.h>
2+
#include <stdio.h>
3+
using namespace Nan;
4+
using namespace v8;
5+
6+
void buffer_delete_callback(char* data, void* the_vector) {
7+
fputs("DELETE CALLBACK\n", stderr);
8+
}
9+
10+
NAN_METHOD(rotate) {
11+
char* buffer = (char*) node::Buffer::Data(info[0]->ToObject());
12+
size_t size = node::Buffer::Length(info[0]);
13+
unsigned int rot = info[1]->Uint32Value();
14+
15+
char * retval = new char[size];
16+
for(unsigned int i = 0; i < size; i++ ) {
17+
retval[i] = buffer[i] - rot;
18+
buffer[i] += rot;
19+
}
20+
21+
//info.GetReturnValue().Set(Nan::NewBuffer(retval, size, buffer_delete_callback, nullptr).ToLocalChecked());
22+
info.GetReturnValue().Set(Nan::NewBuffer(retval, size).ToLocalChecked());
23+
}
24+
25+
NAN_MODULE_INIT(Init) {
26+
Nan::Set(target, New<String>("rotate").ToLocalChecked(),
27+
GetFunction(New<FunctionTemplate>(rotate)).ToLocalChecked());
28+
}
29+
30+
NODE_MODULE(buffer_example, Init)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<html>
2+
<body>
3+
<script src="index.js"></script>
4+
<h1 id='result'>result pending...</h1>
5+
<script>
6+
document.getElementById('result').innerHTML = result.toString('ascii');
7+
</script>
8+
</body>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
'use strict';
2+
const addon = require('./build/Release/buffer_example');
3+
4+
const buffer = Buffer.from("ABC");
5+
6+
for (var i = 0; i < 1000; i++) {
7+
var b = Buffer.from("CDE");
8+
addon.rotate(b, i);
9+
}
10+
11+
// synchronous, rotates each character by +13
12+
var result = addon.rotate(buffer, 13);
13+
14+
console.log(buffer.toString('ascii'));
15+
console.log(result.toString('ascii'));
16+
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "buffer_example",
3+
"main": "index.html",
4+
"version": "0.0.1",
5+
"private": true,
6+
"gypfile": true,
7+
"scripts": {
8+
"start": "node index.js"
9+
},
10+
"dependencies": {
11+
"nan": "*"
12+
}
13+
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
import time
2+
import os
3+
import shutil
4+
import subprocess
5+
import platform
6+
from subprocess import Popen, PIPE
7+
import sys
8+
9+
from selenium import webdriver
10+
from selenium.webdriver.chrome.options import Options
11+
chrome_options = Options()
12+
testdir = os.path.dirname(os.path.abspath(__file__))
13+
chrome_options.add_argument("nwapp=" + testdir)
14+
nw_tools = os.path.normpath(os.path.join(testdir, os.pardir, os.pardir, os.pardir, 'tools'))
15+
sys.path.append(nw_tools)
16+
import getnwversion
17+
import getnwisrelease
18+
19+
def find_executable(executable, path=None):
20+
"""Find if 'executable' can be run. Looks for it in 'path'
21+
(string that lists directories separated by 'os.pathsep';
22+
defaults to os.environ['PATH']). Checks for all executable
23+
extensions. Returns full path or None if no command is found.
24+
"""
25+
if path is None:
26+
path = os.environ['PATH']
27+
paths = path.split(os.pathsep)
28+
extlist = ['']
29+
if os.name == 'os2':
30+
(base, ext) = os.path.splitext(executable)
31+
# executable files on OS/2 can have an arbitrary extension, but
32+
# .exe is automatically appended if no dot is present in the name
33+
if not ext:
34+
executable = executable + ".exe"
35+
elif sys.platform == 'win32':
36+
pathext = os.environ['PATHEXT'].lower().split(os.pathsep)
37+
(base, ext) = os.path.splitext(executable)
38+
if ext.lower() not in pathext:
39+
extlist = pathext
40+
for ext in extlist:
41+
execname = executable + ext
42+
if os.path.isfile(execname):
43+
return execname
44+
else:
45+
for p in paths:
46+
f = os.path.join(p, execname)
47+
if os.path.isfile(f):
48+
return f
49+
else:
50+
return None
51+
52+
os.chdir(testdir)
53+
54+
header_path = os.path.abspath("../../../tmp/node")
55+
56+
nw_version = getnwversion.nw_version
57+
if getnwisrelease.release == 0:
58+
nw_version += getnwisrelease.postfix
59+
60+
arch = ''
61+
_arch = platform.architecture()[0]
62+
if _arch == '64bit':
63+
arch = 'x64'
64+
elif _arch == '32bit':
65+
arch = 'ia32'
66+
else:
67+
print 'Unsupported arch: ' + _arch
68+
exit(-1)
69+
70+
nw_gyp_path = find_executable('nw-gyp')
71+
npm_path = find_executable('npm')
72+
73+
print "nw_gyp: ", nw_gyp_path
74+
print "npm_path: ", npm_path
75+
print "header path: ", header_path
76+
77+
npm_env = {'npm_config_nodedir': header_path, 'npm_config_target': nw_version,
78+
'npm_config_arch': arch, 'npm_config_target_arch': arch,
79+
'npm_config_runtime': 'node-webkit', 'npm_config_build_from_source': "true",
80+
'npm_config_node_gyp': nw_gyp_path, 'PATH': os.getenv('PATH')}
81+
82+
proc = Popen([npm_path, 'install'], stdout=PIPE, stderr=PIPE, env=npm_env)
83+
out, err = proc.communicate()
84+
print out
85+
print err
86+
assert(proc.returncode == 0)
87+
88+
driver = webdriver.Chrome(executable_path=os.environ['CHROMEDRIVER'], chrome_options=chrome_options)
89+
try:
90+
print driver.current_url
91+
result = driver.find_element_by_id('result').get_attribute('innerHTML')
92+
print result
93+
assert("456" == result)
94+
finally:
95+
driver.quit()

0 commit comments

Comments
 (0)