Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
openanolis
dragonwell8_jdk
提交
11ae5e94
D
dragonwell8_jdk
项目概览
openanolis
/
dragonwell8_jdk
通知
4
Star
2
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
dragonwell8_jdk
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
11ae5e94
编写于
10月 22, 2013
作者:
M
mullan
浏览文件
操作
浏览文件
下载
差异文件
Merge
上级
e39b83ea
aae73e93
变更
2
显示空白变更内容
内联
并排
Showing
2 changed file
with
310 addition
and
290 deletion
+310
-290
test/java/net/Authenticator/B4769350.java
test/java/net/Authenticator/B4769350.java
+310
-182
test/java/net/NetworkInterface/MemLeakTest.java
test/java/net/NetworkInterface/MemLeakTest.java
+0
-108
未找到文件。
test/java/net/Authenticator/B4769350.java
浏览文件 @
11ae5e94
/*
* Copyright (c) 2002, 201
2
, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2002, 201
3
, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
...
...
@@ -23,9 +23,7 @@
/**
* @test
* @bug 4769350
* @library ../../../sun/net/www/httptest/
* @build HttpCallback TestHttpServer ClosedChannelList HttpTransaction AbstractCallback
* @bug 4769350 8017779
* @run main/othervm B4769350 server
* @run main/othervm B4769350 proxy
* @summary proxy authentication username and password caching only works in serial case
...
...
@@ -34,8 +32,17 @@
* tests may already have invoked the HTTP handler.
*/
import
com.sun.net.httpserver.HttpExchange
;
import
com.sun.net.httpserver.HttpHandler
;
import
com.sun.net.httpserver.HttpServer
;
import
java.io.*
;
import
java.net.*
;
import
java.util.concurrent.BrokenBarrierException
;
import
java.util.concurrent.CountDownLatch
;
import
java.util.concurrent.CyclicBarrier
;
import
java.util.concurrent.Executor
;
import
java.util.concurrent.ExecutorService
;
import
java.util.concurrent.Executors
;
public
class
B4769350
{
...
...
@@ -43,8 +50,7 @@ public class B4769350 {
static
boolean
error
=
false
;
static
void
read
(
InputStream
is
)
throws
IOException
{
int
c
;
while
((
c
=
is
.
read
())
!=
-
1
)
{
while
(
is
.
read
()
!=
-
1
)
{
//System.out.write (c);
}
}
...
...
@@ -64,8 +70,8 @@ public class B4769350 {
try
{
URI
u
=
new
URI
(
"http"
,
authority
,
path
,
null
,
null
);
URL
url
=
u
.
toURL
();
URLConnection
urlc
=
url
.
openConnection
();
InputStream
is
=
urlc
.
getInputStream
();
URLConnection
urlc
=
url
.
openConnection
();
InputStream
is
=
urlc
.
getInputStream
();
read
(
is
);
is
.
close
();
}
catch
(
URISyntaxException
e
)
{
...
...
@@ -73,7 +79,8 @@ public class B4769350 {
error
=
true
;
}
catch
(
IOException
e
)
{
if
(!
allowerror
)
{
System
.
out
.
println
(
Thread
.
currentThread
().
getName
()
+
" "
+
e
);
System
.
out
.
println
(
Thread
.
currentThread
().
getName
()
+
" "
+
e
);
e
.
printStackTrace
();
error
=
true
;
}
...
...
@@ -81,55 +88,58 @@ public class B4769350 {
}
}
static
class
CallBack
extends
AbstractCallback
{
class
Server
implements
AutoCloseable
{
HttpServer
server
;
Executor
executor
;
CyclicBarrier
t1Cond1
;
CyclicBarrier
t1Cond2
;
void
errorReply
(
HttpTransaction
req
,
String
reply
)
throws
IOException
{
req
.
addResponseHeader
(
"Connection"
,
"close"
);
req
.
addResponseHeader
(
"WWW-Authenticate"
,
reply
);
req
.
sendResponse
(
401
,
"Unauthorized"
);
req
.
orderlyClose
();
public
String
getAddress
()
{
return
server
.
getAddress
().
getHostName
();
}
void
proxyReply
(
HttpTransaction
req
,
String
reply
)
throws
IOException
{
req
.
addResponseHeader
(
"Proxy-Authenticate"
,
reply
);
req
.
sendResponse
(
407
,
"Proxy Authentication Required"
);
}
void
okReply
(
HttpTransaction
req
)
throws
IOException
{
req
.
addResponseHeader
(
"Connection"
,
"close"
);
req
.
setResponseEntityBody
(
"Hello ."
);
req
.
sendResponse
(
200
,
"Ok"
);
req
.
orderlyClose
();
}
public
void
startServer
()
{
InetSocketAddress
addr
=
new
InetSocketAddress
(
0
);
public
void
request
(
HttpTransaction
req
,
int
count
)
{
try
{
URI
uri
=
req
.
getRequestURI
();
String
path
=
uri
.
getPath
();
if
(
path
.
endsWith
(
"/t1a"
))
{
doT1a
(
req
,
count
);
}
else
if
(
path
.
endsWith
(
"/t1b"
))
{
doT1b
(
req
,
count
);
}
else
if
(
path
.
endsWith
(
"/t1c"
))
{
doT1c
(
req
,
count
);
}
else
if
(
path
.
endsWith
(
"/t1d"
))
{
doT1d
(
req
,
count
);
}
else
if
(
path
.
endsWith
(
"/t2a"
))
{
doT2a
(
req
,
count
);
}
else
if
(
path
.
endsWith
(
"/t2b"
))
{
doT2b
(
req
,
count
);
}
else
if
(
path
.
endsWith
(
"/t3a"
))
{
doT3a
(
req
,
count
);
}
else
if
(
path
.
endsWith
(
"/t3b"
))
{
doT3bc
(
req
,
count
);
}
else
if
(
path
.
endsWith
(
"/t3c"
))
{
doT3bc
(
req
,
count
);
}
else
{
System
.
out
.
println
(
"unexpected request URI"
);
}
}
catch
(
IOException
e
)
{
e
.
printStackTrace
();
}
server
=
HttpServer
.
create
(
addr
,
0
);
}
catch
(
IOException
ioe
)
{
throw
new
RuntimeException
(
"Server could not be created"
);
}
executor
=
Executors
.
newFixedThreadPool
(
10
);
server
.
setExecutor
(
executor
);
server
.
createContext
(
"/test/realm1/t1a"
,
new
AuthenticationHandlerT1a
()
);
server
.
createContext
(
"/test/realm2/t1b"
,
new
AuthenticationHandlerT1b
());
server
.
createContext
(
"/test/realm1/t1c"
,
new
AuthenticationHandlerT1c
());
server
.
createContext
(
"/test/realm2/t1d"
,
new
AuthenticationHandlerT1d
());
server
.
createContext
(
"/test/realm3/t2a"
,
new
AuthenticationHandlerT2a
());
server
.
createContext
(
"/test/realm3/t2b"
,
new
AuthenticationHandlerT2b
());
server
.
createContext
(
"/test/realm4/t3a"
,
new
AuthenticationHandlerT3a
());
server
.
createContext
(
"/test/realm4/t3b"
,
new
AuthenticationHandlerT3bc
());
server
.
createContext
(
"/test/realm4/t3c"
,
new
AuthenticationHandlerT3bc
());
t1Cond1
=
new
CyclicBarrier
(
2
);
t1Cond2
=
new
CyclicBarrier
(
2
);
server
.
start
();
}
public
int
getPort
()
{
return
server
.
getAddress
().
getPort
();
}
public
void
close
()
{
if
(
executor
!=
null
)
((
ExecutorService
)
executor
).
shutdownNow
();
if
(
server
!=
null
)
server
.
stop
(
0
);
}
/* T1 tests the client by sending 4 requests to 2 different realms
...
...
@@ -138,154 +148,276 @@ public class B4769350 {
* the second requests should be executed without calling the authenticator.
* The test succeeds if the authenticator was only called twice.
*/
void
doT1a
(
HttpTransaction
req
,
int
count
)
throws
IOException
{
switch
(
count
)
{
class
AuthenticationHandlerT1a
implements
HttpHandler
{
volatile
int
count
=
-
1
;
@Override
public
void
handle
(
HttpExchange
exchange
)
throws
IOException
{
count
++;
try
{
switch
(
count
)
{
case
0
:
errorReply
(
req
,
"Basic realm=\"realm1\""
);
TestHttpServer
.
rendezvous
(
"one"
,
2
);
AuthenticationHandler
.
errorReply
(
exchange
,
"Basic realm=\"realm1\""
);
break
;
case
1
:
TestHttpServer
.
waitForCondition
(
"cond2"
);
okReply
(
req
);
t1Cond1
.
await
();
t1cond2latch
.
await
();
AuthenticationHandler
.
okReply
(
exchange
);
break
;
default
:
System
.
out
.
println
(
"Unexpected request"
);
}
}
catch
(
InterruptedException
|
BrokenBarrierException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
class
AuthenticationHandlerT1b
implements
HttpHandler
{
volatile
int
count
=
-
1
;
void
doT1b
(
HttpTransaction
req
,
int
count
)
throws
IOException
{
switch
(
count
)
{
@Override
public
void
handle
(
HttpExchange
exchange
)
throws
IOException
{
count
++;
try
{
switch
(
count
)
{
case
0
:
errorReply
(
req
,
"Basic realm=\"realm2\""
);
TestHttpServer
.
rendezvous
(
"one"
,
2
);
TestHttpServer
.
setCondition
(
"cond1"
);
AuthenticationHandler
.
errorReply
(
exchange
,
"Basic realm=\"realm2\""
);
break
;
case
1
:
TestHttpServer
.
waitForCondition
(
"cond2"
);
okReply
(
req
);
t1Cond1
.
await
();
t1cond1latch
.
countDown
();
t1cond2latch
.
await
();
AuthenticationHandler
.
okReply
(
exchange
);
break
;
default
:
System
.
out
.
println
(
"Unexpected request"
);
}
}
catch
(
InterruptedException
|
BrokenBarrierException
e
)
{
throw
new
RuntimeException
(
e
);
}
}
}
void
doT1c
(
HttpTransaction
req
,
int
count
)
throws
IOException
{
switch
(
count
)
{
class
AuthenticationHandlerT1c
implements
HttpHandler
{
volatile
int
count
=
-
1
;
@Override
public
void
handle
(
HttpExchange
exchange
)
throws
IOException
{
count
++;
switch
(
count
)
{
case
0
:
errorReply
(
req
,
"Basic realm=\"realm1\""
);
TestHttpServer
.
rendezvous
(
"two"
,
2
);
AuthenticationHandler
.
errorReply
(
exchange
,
"Basic realm=\"realm1\""
);
try
{
t1Cond2
.
await
();
}
catch
(
InterruptedException
|
BrokenBarrierException
e
)
{
throw
new
RuntimeException
(
e
);
}
break
;
case
1
:
okReply
(
req
);
AuthenticationHandler
.
okReply
(
exchange
);
break
;
default
:
System
.
out
.
println
(
"Unexpected request"
);
}
}
}
class
AuthenticationHandlerT1d
implements
HttpHandler
{
volatile
int
count
=
-
1
;
void
doT1d
(
HttpTransaction
req
,
int
count
)
throws
IOException
{
switch
(
count
)
{
@Override
public
void
handle
(
HttpExchange
exchange
)
throws
IOException
{
count
++;
switch
(
count
)
{
case
0
:
errorReply
(
req
,
"Basic realm=\"realm2\""
);
TestHttpServer
.
rendezvous
(
"two"
,
2
);
TestHttpServer
.
setCondition
(
"cond2"
);
AuthenticationHandler
.
errorReply
(
exchange
,
"Basic realm=\"realm2\""
);
try
{
t1Cond2
.
await
();
}
catch
(
InterruptedException
|
BrokenBarrierException
e
)
{
throw
new
RuntimeException
(
e
);
}
t1cond2latch
.
countDown
();
break
;
case
1
:
okReply
(
req
);
AuthenticationHandler
.
okReply
(
exchange
);
break
;
default
:
System
.
out
.
println
(
"Unexpected request"
);
}
}
}
/* T2 tests to check that if initial authentication fails, the second will
* succeed, and the authenticator is called twice
*/
void
doT2a
(
HttpTransaction
req
,
int
count
)
throws
IOException
{
/* This will be called several times */
class
AuthenticationHandlerT2a
implements
HttpHandler
{
volatile
int
count
=
-
1
;
@Override
public
void
handle
(
HttpExchange
exchange
)
throws
IOException
{
count
++;
if
(
count
==
1
)
{
TestHttpServer
.
setCondition
(
"T2cond1"
);
t2condlatch
.
countDown
();
}
AuthenticationHandler
.
errorReply
(
exchange
,
"Basic realm=\"realm3\""
);
}
errorReply
(
req
,
"Basic realm=\"realm3\""
);
}
void
doT2b
(
HttpTransaction
req
,
int
count
)
throws
IOException
{
switch
(
count
)
{
class
AuthenticationHandlerT2b
implements
HttpHandler
{
volatile
int
count
=
-
1
;
@Override
public
void
handle
(
HttpExchange
exchange
)
throws
IOException
{
count
++;
switch
(
count
)
{
case
0
:
errorReply
(
req
,
"Basic realm=\"realm3\""
);
AuthenticationHandler
.
errorReply
(
exchange
,
"Basic realm=\"realm3\""
);
break
;
case
1
:
okReply
(
req
);
AuthenticationHandler
.
okReply
(
exchange
);
break
;
default
:
System
.
out
.
println
(
"Unexpected request"
);
}
}
}
/* T3 tests proxy and server authentication. three threads request same
* resource at same time. Authenticator should be called once for server
* and once for proxy
*/
void
doT3a
(
HttpTransaction
req
,
int
count
)
throws
IOException
{
switch
(
count
)
{
class
AuthenticationHandlerT3a
implements
HttpHandler
{
volatile
int
count
=
-
1
;
@Override
public
void
handle
(
HttpExchange
exchange
)
throws
IOException
{
count
++;
switch
(
count
)
{
case
0
:
proxyReply
(
req
,
"Basic realm=\"proxy\""
);
TestHttpServer
.
setCondition
(
"T3cond1
"
);
AuthenticationHandler
.
proxyReply
(
exchange
,
"Basic realm=\"proxy\"
"
);
break
;
case
1
:
errorReply
(
req
,
"Basic realm=\"realm4\""
);
t3cond1
.
countDown
();
AuthenticationHandler
.
errorReply
(
exchange
,
"Basic realm=\"realm4\""
);
break
;
case
2
:
okReply
(
req
);
AuthenticationHandler
.
okReply
(
exchange
);
break
;
default
:
System
.
out
.
println
(
"Unexpected request"
);
}
}
}
class
AuthenticationHandlerT3bc
implements
HttpHandler
{
volatile
int
count
=
-
1
;
void
doT3bc
(
HttpTransaction
req
,
int
count
)
throws
IOException
{
switch
(
count
)
{
@Override
public
void
handle
(
HttpExchange
exchange
)
throws
IOException
{
count
++;
switch
(
count
)
{
case
0
:
proxyReply
(
req
,
"Basic realm=\"proxy\""
);
AuthenticationHandler
.
proxyReply
(
exchange
,
"Basic realm=\"proxy\""
);
break
;
case
1
:
okReply
(
req
);
AuthenticationHandler
.
okReply
(
exchange
);
break
;
default
:
System
.
out
.
println
(
"Unexpected request"
);
}
}
};
}
}
static
class
AuthenticationHandler
{
static
void
errorReply
(
HttpExchange
exchange
,
String
reply
)
throws
IOException
{
exchange
.
getResponseHeaders
().
add
(
"Connection"
,
"close"
);
exchange
.
getResponseHeaders
().
add
(
"WWW-Authenticate"
,
reply
);
exchange
.
sendResponseHeaders
(
401
,
0
);
exchange
.
close
();
}
static
void
proxyReply
(
HttpExchange
exchange
,
String
reply
)
throws
IOException
{
exchange
.
getResponseHeaders
().
add
(
"Proxy-Authenticate"
,
reply
);
exchange
.
sendResponseHeaders
(
407
,
0
);
}
static
void
okReply
(
HttpExchange
exchange
)
throws
IOException
{
exchange
.
getResponseHeaders
().
add
(
"Connection"
,
"close"
);
String
response
=
"Hello ."
;
exchange
.
sendResponseHeaders
(
200
,
response
.
getBytes
().
length
);
OutputStream
os
=
exchange
.
getResponseBody
();
os
.
write
(
response
.
getBytes
());
os
.
close
();
exchange
.
close
();
}
}
static
TestHttp
Server
server
;
static
Server
server
;
static
MyAuthenticator
auth
=
new
MyAuthenticator
();
static
int
redirects
=
4
;
static
Client
c1
,
c2
,
c3
,
c4
,
c5
,
c6
,
c7
,
c8
,
c9
;
static
void
doServerTests
(
String
authority
)
throws
Exception
{
static
CountDownLatch
t1cond1latch
;
static
CountDownLatch
t1cond2latch
;
static
CountDownLatch
t2condlatch
;
static
CountDownLatch
t3cond1
;
static
void
doServerTests
(
String
authority
,
Server
server
)
throws
Exception
{
System
.
out
.
println
(
"Doing Server tests"
);
System
.
out
.
println
(
"T1"
);
c1
=
new
Client
(
authority
,
"/test/realm1/t1a"
,
false
);
c2
=
new
Client
(
authority
,
"/test/realm2/t1b"
,
false
);
c3
=
new
Client
(
authority
,
"/test/realm1/t1c"
,
false
);
c4
=
new
Client
(
authority
,
"/test/realm2/t1d"
,
false
);
t1cond1latch
=
new
CountDownLatch
(
1
);
t1cond2latch
=
new
CountDownLatch
(
1
);
c1
.
start
();
c2
.
start
();
TestHttpServer
.
waitForCondition
(
"cond1"
);
t1cond1latch
.
await
(
);
c3
.
start
();
c4
.
start
();
c1
.
join
();
c2
.
join
();
c3
.
join
();
c4
.
join
();
int
f
=
auth
.
getCount
();
if
(
f
!=
2
)
{
except
(
"Authenticator was called "
+
f
+
" times. Should be 2"
);
except
(
"Authenticator was called "
+
f
+
" times. Should be 2"
,
server
);
}
if
(
error
)
{
except
(
"error occurred"
);
except
(
"error occurred"
,
server
);
}
auth
.
resetCount
();
...
...
@@ -293,73 +425,71 @@ public class B4769350 {
c5
=
new
Client
(
authority
,
"/test/realm3/t2a"
,
true
);
c6
=
new
Client
(
authority
,
"/test/realm3/t2b"
,
false
);
t2condlatch
=
new
CountDownLatch
(
1
);
c5
.
start
();
TestHttpServer
.
waitForCondition
(
"T2cond1"
);
t2condlatch
.
await
(
);
c6
.
start
();
c5
.
join
();
c6
.
join
();
f
=
auth
.
getCount
();
if
(
f
!=
redirects
+
1
)
{
except
(
"Authenticator was called "
+
f
+
" times. Should be: "
+
redirects
+
1
);
except
(
"Authenticator was called "
+
f
+
" times. Should be: "
+
redirects
+
1
,
server
);
}
if
(
error
)
{
except
(
"error occurred"
);
except
(
"error occurred"
,
server
);
}
}
static
void
doProxyTests
(
String
authority
)
throws
Exception
{
static
void
doProxyTests
(
String
authority
,
Server
server
)
throws
Exception
{
System
.
out
.
println
(
"Doing Proxy tests"
);
c7
=
new
Client
(
authority
,
"/test/realm4/t3a"
,
false
);
c8
=
new
Client
(
authority
,
"/test/realm4/t3b"
,
false
);
c9
=
new
Client
(
authority
,
"/test/realm4/t3c"
,
false
);
t3cond1
=
new
CountDownLatch
(
1
);
c7
.
start
();
TestHttpServer
.
waitForCondition
(
"T3cond1"
);
t3cond1
.
await
(
);
c8
.
start
();
c9
.
start
();
c7
.
join
();
c8
.
join
();
c9
.
join
();
int
f
=
auth
.
getCount
();
if
(
f
!=
2
)
{
except
(
"Authenticator was called "
+
f
+
" times. Should be: "
+
2
);
except
(
"Authenticator was called "
+
f
+
" times. Should be: "
+
2
,
server
);
}
if
(
error
)
{
except
(
"error occurred"
);
except
(
"error occurred"
,
server
);
}
}
public
static
void
main
(
String
[]
args
)
throws
Exception
{
new
B4769350
().
runTest
(
args
[
0
].
equals
(
"proxy"
));
}
public
void
runTest
(
boolean
proxy
)
throws
Exception
{
System
.
setProperty
(
"http.maxRedirects"
,
Integer
.
toString
(
redirects
));
System
.
setProperty
(
"http.auth.serializeRequests"
,
"true"
);
Authenticator
.
setDefault
(
auth
);
boolean
proxy
=
args
[
0
].
equals
(
"proxy"
);
try
{
server
=
new
TestHttpServer
(
new
CallBack
(),
10
,
1
,
0
);
System
.
out
.
println
(
"Server: listening on port: "
+
server
.
getLocal
Port
());
try
(
Server
server
=
new
Server
())
{
server
.
startServer
();
System
.
out
.
println
(
"Server: listening on port: "
+
server
.
get
Port
());
if
(
proxy
)
{
System
.
setProperty
(
"http.proxyHost"
,
"localhost"
);
System
.
setProperty
(
"http.proxyPort"
,
Integer
.
toString
(
server
.
getLocalPort
()));
doProxyTests
(
"www.foo.com"
);
System
.
setProperty
(
"http.proxyPort"
,
Integer
.
toString
(
server
.
getPort
()));
doProxyTests
(
"www.foo.com"
,
server
);
}
else
{
doServerTests
(
"localhost:"
+
server
.
getLocalPort
());
}
server
.
terminate
();
}
catch
(
Exception
e
)
{
if
(
server
!=
null
)
{
server
.
terminate
();
}
throw
e
;
doServerTests
(
"localhost:"
+
server
.
getPort
(),
server
);
}
}
static
void
pause
(
int
millis
)
{
try
{
Thread
.
sleep
(
millis
);
}
catch
(
InterruptedException
e
)
{}
}
public
static
void
except
(
String
s
)
{
server
.
terminat
e
();
public
static
void
except
(
String
s
,
Server
server
)
{
server
.
clos
e
();
throw
new
RuntimeException
(
s
);
}
...
...
@@ -368,13 +498,10 @@ public class B4769350 {
super
();
}
int
count
=
0
;
volatile
int
count
=
0
;
@Override
public
PasswordAuthentication
getPasswordAuthentication
()
{
//System.out.println ("Authenticator called: " + getRequestingPrompt());
//try {
//Thread.sleep (1000);
//} catch (InterruptedException e) {}
PasswordAuthentication
pw
;
pw
=
new
PasswordAuthentication
(
"user"
,
"pass1"
.
toCharArray
());
count
++;
...
...
@@ -386,7 +513,8 @@ public class B4769350 {
}
public
int
getCount
()
{
return
(
count
)
;
return
count
;
}
}
}
test/java/net/NetworkInterface/MemLeakTest.java
已删除
100644 → 0
浏览文件 @
e39b83ea
/*
* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/* @test
* @bug 8022584
* @summary Some NetworkInterface methods can leak native memory
* @run main/othervm/timeout=700 MemLeakTest
*/
/* Note: the test can cause a memory leak that's why othervm option is required
*/
import
java.io.BufferedReader
;
import
java.io.FileReader
;
import
java.net.NetworkInterface
;
import
java.net.SocketException
;
import
java.util.Collection
;
import
java.util.Collections
;
public
class
MemLeakTest
{
/**
* Memory leak is assumed, if application consumes more than specified amount of memory during its execution.
* The number is given in Kb.
*/
private
static
final
long
MEM_LEAK_THRESHOLD
=
32
*
1024
;
// 32Mb
public
static
void
main
(
String
[]
args
)
throws
Exception
{
if
(!
System
.
getProperty
(
"os.name"
).
equals
(
"Linux"
))
{
System
.
out
.
println
(
"Test only runs on Linux"
);
return
;
}
// warm up
accessNetInterfaces
(
3
);
long
vMemBefore
=
getVMemSize
();
accessNetInterfaces
(
500_000
);
long
vMemAfter
=
getVMemSize
();
long
vMemDelta
=
vMemAfter
-
vMemBefore
;
if
(
vMemDelta
>
MEM_LEAK_THRESHOLD
)
{
throw
new
Exception
(
"FAIL: Virtual memory usage increased by "
+
vMemDelta
+
"Kb "
+
"(greater than "
+
MEM_LEAK_THRESHOLD
+
"Kb)"
);
}
System
.
out
.
println
(
"PASS: Virtual memory usage increased by "
+
vMemDelta
+
"Kb "
+
"(not greater than "
+
MEM_LEAK_THRESHOLD
+
"Kb)"
);
}
private
static
void
accessNetInterfaces
(
int
times
)
{
try
{
Collection
<
NetworkInterface
>
interfaces
=
Collections
.
list
(
NetworkInterface
.
getNetworkInterfaces
());
for
(
int
i
=
0
;
i
!=
times
;
++
i
)
{
for
(
NetworkInterface
netInterface
:
interfaces
)
{
netInterface
.
getMTU
();
netInterface
.
isLoopback
();
netInterface
.
isUp
();
netInterface
.
isPointToPoint
();
netInterface
.
supportsMulticast
();
}
}
}
catch
(
SocketException
ignore
)
{}
}
/**
* Returns size of virtual memory allocated to the process in Kb.
* Linux specific. On other platforms and in case of any errors returns 0.
*/
private
static
long
getVMemSize
()
{
// Refer to the Linux proc(5) man page for details about /proc/self/stat file
//
// In short, this file contains status information about the current process
// written in one line. The fields are separated with spaces.
// The 23rd field is defined as 'vsize %lu Virtual memory size in bytes'
try
(
FileReader
fileReader
=
new
FileReader
(
"/proc/self/stat"
);
BufferedReader
bufferedReader
=
new
BufferedReader
(
fileReader
))
{
String
line
=
bufferedReader
.
readLine
();
return
Long
.
parseLong
(
line
.
split
(
" "
)[
22
])
/
1024
;
}
catch
(
Exception
ignore
)
{}
return
0
;
}
}
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录