am e49838e6: CTS tests for major:minor of /dev/random and /dev/urandom.
* commit 'e49838e6ad878cbd1397ff1e8b6984558d518ecf':
CTS tests for major:minor of /dev/random and /dev/urandom.
diff --git a/tests/tests/security/jni/Android.mk b/tests/tests/security/jni/Android.mk
index 5821ec0..06172c5 100644
--- a/tests/tests/security/jni/Android.mk
+++ b/tests/tests/security/jni/Android.mk
@@ -24,7 +24,8 @@
LOCAL_SRC_FILES := \
CtsSecurityJniOnLoad.cpp \
android_security_cts_CharDeviceTest.cpp \
- android_security_cts_NativeCodeTest.cpp
+ android_security_cts_LinuxRngTest.cpp \
+ android_security_cts_NativeCodeTest.cpp \
LOCAL_C_INCLUDES := $(JNI_H_INCLUDE)
diff --git a/tests/tests/security/jni/CtsSecurityJniOnLoad.cpp b/tests/tests/security/jni/CtsSecurityJniOnLoad.cpp
index 7244fc2..7577eef 100644
--- a/tests/tests/security/jni/CtsSecurityJniOnLoad.cpp
+++ b/tests/tests/security/jni/CtsSecurityJniOnLoad.cpp
@@ -18,6 +18,7 @@
#include <stdio.h>
extern int register_android_security_cts_CharDeviceTest(JNIEnv*);
+extern int register_android_security_cts_LinuxRngTest(JNIEnv*);
extern int register_android_security_cts_NativeCodeTest(JNIEnv*);
jint JNI_OnLoad(JavaVM *vm, void *reserved) {
@@ -31,6 +32,10 @@
return JNI_ERR;
}
+ if (register_android_security_cts_LinuxRngTest(env)) {
+ return JNI_ERR;
+ }
+
if (register_android_security_cts_NativeCodeTest(env)) {
return JNI_ERR;
}
diff --git a/tests/tests/security/jni/android_security_cts_LinuxRngTest.cpp b/tests/tests/security/jni/android_security_cts_LinuxRngTest.cpp
new file mode 100644
index 0000000..671226b
--- /dev/null
+++ b/tests/tests/security/jni/android_security_cts_LinuxRngTest.cpp
@@ -0,0 +1,107 @@
+/*
+ * Copyright (C) 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <errno.h>
+#include <jni.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <sys/stat.h>
+
+/*
+ * Native methods used by
+ * cts/tests/tests/permission/src/android/security/cts/LinuxRngTest.java
+ */
+
+static void throwIOException(JNIEnv* env, const char *format, ...) {
+ va_list ap;
+ va_start(ap, format);
+
+ char *message;
+ vasprintf(&message, format, ap);
+
+ va_end(ap);
+
+ jclass cls = env->FindClass("java/io/IOException");
+ env->ThrowNew(cls, message);
+
+ free(message);
+}
+
+jint android_security_cts_LinuxRngTest_getCharDeviceMajor(JNIEnv* env,
+ jobject thiz, jstring name)
+{
+ const char* nameStr = env->GetStringUTFChars(name, NULL);
+
+ jint result = -1;
+ struct stat st;
+ if (stat(nameStr, &st) == -1) {
+ throwIOException(env, "Failed to stat %s: %s", nameStr, strerror(errno));
+ goto ret;
+ }
+
+ if (!S_ISCHR(st.st_mode)) {
+ throwIOException(env, "%s is not a character device: mode is 0%o", nameStr, st.st_mode);
+ goto ret;
+ }
+
+ result = major(st.st_rdev);
+
+ret:
+ if (nameStr != NULL) {
+ env->ReleaseStringUTFChars(name, nameStr);
+ }
+ return result;
+}
+
+jint android_security_cts_LinuxRngTest_getCharDeviceMinor(JNIEnv* env,
+ jobject thiz, jstring name)
+{
+ const char* nameStr = env->GetStringUTFChars(name, NULL);
+
+ jint result = -1;
+ struct stat st;
+ if (stat(nameStr, &st) == -1) {
+ throwIOException(env, "Failed to stat %s: %s", nameStr, strerror(errno));
+ goto ret;
+ }
+
+ if (!S_ISCHR(st.st_mode)) {
+ throwIOException(env, "%s is not a character device: mode is 0%o", nameStr, st.st_mode);
+ goto ret;
+ }
+
+ result = minor(st.st_rdev);
+
+ret:
+ if (nameStr != NULL) {
+ env->ReleaseStringUTFChars(name, nameStr);
+ }
+ return result;
+}
+
+static JNINativeMethod gMethods[] = {
+ { "getCharDeviceMajor", "(Ljava/lang/String;)I",
+ (void *) android_security_cts_LinuxRngTest_getCharDeviceMajor },
+ { "getCharDeviceMinor", "(Ljava/lang/String;)I",
+ (void *) android_security_cts_LinuxRngTest_getCharDeviceMinor },
+};
+
+int register_android_security_cts_LinuxRngTest(JNIEnv* env)
+{
+ jclass clazz = env->FindClass("android/security/cts/LinuxRngTest");
+ return env->RegisterNatives(clazz, gMethods,
+ sizeof(gMethods) / sizeof(JNINativeMethod));
+}
diff --git a/tests/tests/security/src/android/security/cts/LinuxRngTest.java b/tests/tests/security/src/android/security/cts/LinuxRngTest.java
new file mode 100644
index 0000000..6bc5fd3
--- /dev/null
+++ b/tests/tests/security/src/android/security/cts/LinuxRngTest.java
@@ -0,0 +1,42 @@
+/*
+ * Copyright 2013 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package android.security.cts;
+
+import junit.framework.TestCase;
+
+import java.io.IOException;
+
+public class LinuxRngTest extends TestCase {
+ static {
+ System.loadLibrary("ctssecurity_jni");
+ }
+
+ public void testDevRandomMajorMinor() throws Exception {
+ // Based on Linux kernel's drivers/char/random.c
+ assertEquals("/dev/random major", 1, getCharDeviceMajor("/dev/random"));
+ assertEquals("/dev/random minor", 8, getCharDeviceMinor("/dev/random"));
+ }
+
+ public void testDevUrandomMajorMinor() throws Exception {
+ // Based on Linux kernel's drivers/char/random.c
+ assertEquals("/dev/urandom major", 1, getCharDeviceMajor("/dev/urandom"));
+ assertEquals("/dev/urandom minor", 9, getCharDeviceMinor("/dev/urandom"));
+ }
+
+ public static native int getCharDeviceMajor(String file) throws IOException;
+ public static native int getCharDeviceMinor(String file) throws IOException;
+}