blob: 29bb70a66c27db5b7a4ce29dea348dddf533f527 [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
Kenny Rootfa59afb2010-07-12 09:02:18 -070025#include <fcntl.h>
26
Kenny Rootb94a9a62010-06-01 10:34:29 -070027namespace android {
28
29#define TEST_FILENAME "/test.obb"
30
31class ObbFileTest : public testing::Test {
32protected:
33 sp<ObbFile> mObbFile;
34 char* mExternalStorage;
35 char* mFileName;
36
37 virtual void SetUp() {
38 mObbFile = new ObbFile();
39 mExternalStorage = getenv("EXTERNAL_STORAGE");
40
41 const int totalLen = strlen(mExternalStorage) + strlen(TEST_FILENAME) + 1;
42 mFileName = new char[totalLen];
43 snprintf(mFileName, totalLen, "%s%s", mExternalStorage, TEST_FILENAME);
Kenny Rootfa59afb2010-07-12 09:02:18 -070044
45 int fd = ::open(mFileName, O_CREAT | O_TRUNC);
46 if (fd < 0) {
47 FAIL() << "Couldn't create " << mFileName << " for tests";
48 }
Kenny Rootb94a9a62010-06-01 10:34:29 -070049 }
50
51 virtual void TearDown() {
52 }
53};
54
55TEST_F(ObbFileTest, ReadFailure) {
Kenny Rootfa59afb2010-07-12 09:02:18 -070056 EXPECT_FALSE(mObbFile->readFrom(-1))
57 << "No failure on invalid file descriptor";
Kenny Rootb94a9a62010-06-01 10:34:29 -070058}
59
60TEST_F(ObbFileTest, WriteThenRead) {
61 const char* packageName = "com.example.obbfile";
62 const int32_t versionNum = 1;
63
64 mObbFile->setPackageName(String8(packageName));
65 mObbFile->setVersion(versionNum);
66
67 EXPECT_TRUE(mObbFile->writeTo(mFileName))
68 << "couldn't write to fake .obb file";
69
70 mObbFile = new ObbFile();
71
72 EXPECT_TRUE(mObbFile->readFrom(mFileName))
73 << "couldn't read from fake .obb file";
74
75 EXPECT_EQ(versionNum, mObbFile->getVersion())
Kenny Rootfa59afb2010-07-12 09:02:18 -070076 << "version didn't come out the same as it went in";
Kenny Rootb94a9a62010-06-01 10:34:29 -070077 const char* currentPackageName = mObbFile->getPackageName().string();
78 EXPECT_STREQ(packageName, currentPackageName)
Kenny Rootfa59afb2010-07-12 09:02:18 -070079 << "package name didn't come out the same as it went in";
Kenny Rootb94a9a62010-06-01 10:34:29 -070080}
81
82}