ObbFile_test.cpp revision 83c64e6b624a876436d2ef5d2f173b10407e27b4
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "ObbFile_test"
18#include <androidfw/ObbFile.h>
19#include <utils/Log.h>
20#include <utils/RefBase.h>
21#include <utils/String8.h>
22
23#include <gtest/gtest.h>
24
25#include <fcntl.h>
26#include <string.h>
27
28namespace android {
29
30#define TEST_FILENAME "/test.obb"
31
32class ObbFileTest : public testing::Test {
33protected:
34    sp<ObbFile> mObbFile;
35    char* mExternalStorage;
36    char* mFileName;
37
38    virtual void SetUp() {
39        mObbFile = new ObbFile();
40        mExternalStorage = getenv("EXTERNAL_STORAGE");
41
42        const int totalLen = strlen(mExternalStorage) + strlen(TEST_FILENAME) + 1;
43        mFileName = new char[totalLen];
44        snprintf(mFileName, totalLen, "%s%s", mExternalStorage, TEST_FILENAME);
45
46        int fd = ::open(mFileName, O_CREAT | O_TRUNC);
47        if (fd < 0) {
48            FAIL() << "Couldn't create " << mFileName << " for tests";
49        }
50    }
51
52    virtual void TearDown() {
53    }
54};
55
56TEST_F(ObbFileTest, ReadFailure) {
57    EXPECT_FALSE(mObbFile->readFrom(-1))
58            << "No failure on invalid file descriptor";
59}
60
61TEST_F(ObbFileTest, WriteThenRead) {
62    const char* packageName = "com.example.obbfile";
63    const int32_t versionNum = 1;
64
65    mObbFile->setPackageName(String8(packageName));
66    mObbFile->setVersion(versionNum);
67#define SALT_SIZE 8
68    unsigned char salt[SALT_SIZE] = {0x01, 0x10, 0x55, 0xAA, 0xFF, 0x00, 0x5A, 0xA5};
69    EXPECT_TRUE(mObbFile->setSalt(salt, SALT_SIZE))
70            << "Salt should be successfully set";
71
72    EXPECT_TRUE(mObbFile->writeTo(mFileName))
73            << "couldn't write to fake .obb file";
74
75    mObbFile = new ObbFile();
76
77    EXPECT_TRUE(mObbFile->readFrom(mFileName))
78            << "couldn't read from fake .obb file";
79
80    EXPECT_EQ(versionNum, mObbFile->getVersion())
81            << "version didn't come out the same as it went in";
82    const char* currentPackageName = mObbFile->getPackageName().string();
83    EXPECT_STREQ(packageName, currentPackageName)
84            << "package name didn't come out the same as it went in";
85
86    size_t saltLen;
87    const unsigned char* newSalt = mObbFile->getSalt(&saltLen);
88
89    EXPECT_EQ(sizeof(salt), saltLen)
90            << "salt sizes were not the same";
91
92    for (int i = 0; i < sizeof(salt); i++) {
93        EXPECT_EQ(salt[i], newSalt[i])
94                << "salt character " << i << " should be equal";
95    }
96    EXPECT_TRUE(memcmp(newSalt, salt, sizeof(salt)) == 0)
97            << "salts should be the same";
98}
99
100}
101