blob: 05aaf0870a14dc6b648254e0438876d02969ae5b [file] [log] [blame]
Kenny Rootb94a9a62010-06-01 10:34:29 -07001/*
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 <utils/Log.h>
19#include <utils/ObbFile.h>
20#include <utils/RefBase.h>
21#include <utils/String8.h>
22
23#include <gtest/gtest.h>
24
25namespace android {
26
27#define TEST_FILENAME "/test.obb"
28
29class ObbFileTest : public testing::Test {
30protected:
31 sp<ObbFile> mObbFile;
32 char* mExternalStorage;
33 char* mFileName;
34
35 virtual void SetUp() {
36 mObbFile = new ObbFile();
37 mExternalStorage = getenv("EXTERNAL_STORAGE");
38
39 const int totalLen = strlen(mExternalStorage) + strlen(TEST_FILENAME) + 1;
40 mFileName = new char[totalLen];
41 snprintf(mFileName, totalLen, "%s%s", mExternalStorage, TEST_FILENAME);
42 }
43
44 virtual void TearDown() {
45 }
46};
47
48TEST_F(ObbFileTest, ReadFailure) {
49 EXPECT_FALSE(mObbFile->readFrom(-1))
50 << "No failure on invalid file descriptor";
51}
52
53TEST_F(ObbFileTest, WriteThenRead) {
54 const char* packageName = "com.example.obbfile";
55 const int32_t versionNum = 1;
56
57 mObbFile->setPackageName(String8(packageName));
58 mObbFile->setVersion(versionNum);
59
60 EXPECT_TRUE(mObbFile->writeTo(mFileName))
61 << "couldn't write to fake .obb file";
62
63 mObbFile = new ObbFile();
64
65 EXPECT_TRUE(mObbFile->readFrom(mFileName))
66 << "couldn't read from fake .obb file";
67
68 EXPECT_EQ(versionNum, mObbFile->getVersion())
69 << "version didn't come out the same as it went in";
70 const char* currentPackageName = mObbFile->getPackageName().string();
71 EXPECT_STREQ(packageName, currentPackageName)
72 << "package name didn't come out the same as it went in";
73}
74
75}