Java 卡单元测试

发布于 2024-08-23 08:39:18 字数 154 浏览 6 评论 0原文

我想在某种模拟/模拟环境中运行我的 Java Card 应用程序,以便能够对它们运行 JUnit(或任何其他单元测试框架)测试。有谁知道这样的工具吗?我更喜欢一些开源项目。

I'd like to run my Java Card applications in some kind of emulated/simulated environment to be able to run JUnit (or any other unit test framework) tests on them. Does anyone know of such a tool? I'd prefer some open source project.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

箹锭⒈辈孓 2024-08-30 08:39:18

为此,我们的团队专门开发了 jCardSim:开源 JavaCard 模拟器 - http://code.google.com /p/jcardsim/

//1. create simulator
Simulator simulator = new Simulator();
//2. install applet
simulator.installApplet(appletAID, HelloWorldApplet.class);
//3. select applet
simulator.selectApplet(appletAID);
//4. send apdu
ResponseAPDU response = simulator.transmitCommand(new CommandAPDU(0x01, 0x01, 0x00,   0x00));
//5. check response
assertEquals(0x9000, response.getSW());

单元测试示例:http://code.google.com/p/jcardsim/source/browse/trunk/src/test/java/com/licel/jcardsim/base/SimulatorTest.java

它完全模拟真实NXP 芯片 JavaCard。尝试使用它。
欢迎任何意见和想法!如果您分享该项目的链接,我们将不胜感激!

Special for this purpose our team developed jCardSim: open-source JavaCard Simulator - http://code.google.com/p/jcardsim/.

//1. create simulator
Simulator simulator = new Simulator();
//2. install applet
simulator.installApplet(appletAID, HelloWorldApplet.class);
//3. select applet
simulator.selectApplet(appletAID);
//4. send apdu
ResponseAPDU response = simulator.transmitCommand(new CommandAPDU(0x01, 0x01, 0x00,   0x00));
//5. check response
assertEquals(0x9000, response.getSW());

Unit-test example : http://code.google.com/p/jcardsim/source/browse/trunk/src/test/java/com/licel/jcardsim/base/SimulatorTest.java

It's fully emulate the real NXP-chip JavaCard. Try use it.
Any comments and ideas are welcome! We will be grateful if you share link to the project!

天赋异禀 2024-08-30 08:39:18

请注意,执行此操作的一种方法是在 Java SE 中创建 JUnit 测试,并使用 APDU 与 Java Card 中的类进行通信。显然这与直接在 Java Card 中测试类不同,但乞丐不能是选择者。一般来说,您需要创建一个小程序来为您执行一些转换。这是 Mikhail 的 jCardSim 答案 中使用的方法 - 它当然也可以在真实的卡上使用,但是您将失去所有代码覆盖率以及可能的调试选项。

另一种方法(我有一个专有的、可行的解决方案)是在卡上实现单元测试,并从 Java SE 的 JUnit 测试框架中调用它们。这是一项繁重的工作;我必须生成自己的 List 实现才能使其正常工作。不过这是值得的;它允许我使用 Java Card 中定义的对象和类型。据我所知,没有开源替代方案。

可以选择将 JUnit 测试框架添加到 jCardSim 并直接测试类,因为完整的 JRE 在 Applet 编译期间可用。请注意,jCardSim 还不是功能齐全的 Java 卡(例如,没有按照 API 中指定的方式重置内存、没有事务支持等),因此在测试 Java 卡特定代码时请注意这一点。

Note that one method of doing this is to create JUnit tests in Java SE, and use APDU's to communicate with the classes in Java Card. Obviously this is not the same as directly testing classes in Java Card, but beggars cannot be choosers. In general you would need to create an Applet that does some conversions for you. This is the method that is used in the jCardSim answer from Mikhail - it could of course be used on a real card too, but you would lose any code coverage and - possibly - debugging options.

Another method (that I've got a proprietary, working solution for) is to implement unit tests on the card and call them from a JUnit test framework from Java SE. This is a lot of work; I've had to generate my own List implementation for this to work. It was worth it though; it let me use Java Card defined objects and types from within Java Card. There is no open source alternative for this as far as I know.

It could be an option to add a JUnit test framework to jCardSim and test classes directly, as the complete JRE is available during compilation of the Applet. Note that jCardSim is not a fully functioning Java Card yet (e.g. no reset of memory as specified in the API, no transaction support etc. etc.) so be aware of this when testing Java Card specific code.

神也荒唐 2024-08-30 08:39:18

不了解 Java Card,但是,乍一看,它似乎与任何其他 JavaME 平台没有太大区别(我所说的不同是指将它们与 J2SE 等进行比较)。因此,您可以使用任何普通的测试环境,例如 JUnit 测试环境 Eclipse/Netbeans 使开发变得更容易。您可能需要 powermock 来模拟一些特定于平台的内容。希望这有帮助。

Netbeans 的一个提示:您可以考虑单独的项目进行测试(这取决于您的应用程序项目)。在这种情况下,您可以使用最新 Java 玩具的功能对您的 UT 进行编码。

Don't know the Java Card, however, at first glance, it seems not much different than any other JavaME platforms (by different I mean comparing them to e.g. J2SE). As such you may use any ordinary e.g. JUnit test environment with Eclipse/Netbeans to make development little bit easier. You may need powermock to mock some platform specific stuff. Hope this helps.

One tip for Netbeans: you may consider separate project for tests (that depends on your application project). In that case you can have your UTs coded with the power of latest Java toys.

独自唱情﹋歌 2024-08-30 08:39:18

Java 卡小程序是简单的 Java 代码片段。所以,JUnit 没问题。像测试常规 Java 类一样测试它们。

但问题是我从未见过 javacard.* 和 javacardx.* 包中类的 java 实现。如果你能得到它们——问题就解决了。

如果您想测试小程序本身如何工作(APDU 发送/接收),那么 Java Card SDK 中有 cref.exe。这是一个javacard模拟器。

Java card applets are simple pieces of Java code. So, JUnit is fine. Test them like you test regular java classes.

But the problem is that I never saw java implementations of classes in javacard.* and javacardx.* packages. If you will get them - the problem is solved.

If you want to test how applet itself works (APDU send/receive), then there is cref.exe in the Java Card SDK. It's a javacard emulator.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文