summaryrefslogtreecommitdiff
path: root/tests/net/jni/UidRangeTest.cpp
blob: 7941731a07f33bd5c7a6cacdac1df42169fdd0b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
/*
 * Copyright (C) 2016 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 <memory>

#include <binder/Parcel.h>

#include "UidRangeTest.h"

using android::net::UidRange;

extern "C"
JNIEXPORT jbyteArray Java_android_net_UidRangeTest_readAndWriteNative(JNIEnv* env, jclass,
        jbyteArray inParcel) {
    const UidRange range = unmarshall(env, inParcel);
    return marshall(env, range);
}

extern "C"
JNIEXPORT jint Java_android_net_UidRangeTest_getStart(JNIEnv* env, jclass, jbyteArray inParcel) {
    const UidRange range = unmarshall(env, inParcel);
    return range.getStart();
}

extern "C"
JNIEXPORT jint Java_android_net_UidRangeTest_getStop(JNIEnv* env, jclass, jbyteArray inParcel) {
    const UidRange range = unmarshall(env, inParcel);
    return range.getStop();
}


/**
 * Reads exactly one UidRange from 'parcelData' assuming that it is a Parcel. Any bytes afterward
 * are ignored.
 */
UidRange unmarshall(JNIEnv* env, jbyteArray parcelData) {
    const int length = env->GetArrayLength(parcelData);

    std::unique_ptr<uint8_t> bytes(new uint8_t[length]);
    env->GetByteArrayRegion(parcelData, 0, length, reinterpret_cast<jbyte*>(bytes.get()));

    android::Parcel p;
    p.setData(bytes.get(), length);

    UidRange range;
    range.readFromParcel(&p);
    return range;
}

/**
 * Creates a Java byte[] array and writes the contents of 'range' to it as a Parcel containing
 * exactly one object.
 *
 * Every UidRange maps to a unique parcel object, so both 'marshall(e, unmarshall(e, x))' and
 * 'unmarshall(e, marshall(e, x))' should be fixed points.
 */
jbyteArray marshall(JNIEnv* env, const UidRange& range) {
    android::Parcel p;
    range.writeToParcel(&p);
    const int length = p.dataSize();

    jbyteArray parcelData = env->NewByteArray(length);
    env->SetByteArrayRegion(parcelData, 0, length, reinterpret_cast<const jbyte*>(p.data()));

    return parcelData;
}