| Kenny Root | b94a9a6 | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 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 <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 Root | fa59afb | 2010-07-12 09:02:18 -0700 | [diff] [blame] | 25 | #include <fcntl.h> | 
| Kenny Root | 95a6889 | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 26 | #include <string.h> | 
| Kenny Root | fa59afb | 2010-07-12 09:02:18 -0700 | [diff] [blame] | 27 |  | 
| Kenny Root | b94a9a6 | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 28 | namespace android { | 
|  | 29 |  | 
|  | 30 | #define TEST_FILENAME "/test.obb" | 
|  | 31 |  | 
|  | 32 | class ObbFileTest : public testing::Test { | 
|  | 33 | protected: | 
|  | 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); | 
| Kenny Root | fa59afb | 2010-07-12 09:02:18 -0700 | [diff] [blame] | 45 |  | 
|  | 46 | int fd = ::open(mFileName, O_CREAT | O_TRUNC); | 
|  | 47 | if (fd < 0) { | 
|  | 48 | FAIL() << "Couldn't create " << mFileName << " for tests"; | 
|  | 49 | } | 
| Kenny Root | b94a9a6 | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 50 | } | 
|  | 51 |  | 
|  | 52 | virtual void TearDown() { | 
|  | 53 | } | 
|  | 54 | }; | 
|  | 55 |  | 
|  | 56 | TEST_F(ObbFileTest, ReadFailure) { | 
| Kenny Root | fa59afb | 2010-07-12 09:02:18 -0700 | [diff] [blame] | 57 | EXPECT_FALSE(mObbFile->readFrom(-1)) | 
|  | 58 | << "No failure on invalid file descriptor"; | 
| Kenny Root | b94a9a6 | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 59 | } | 
|  | 60 |  | 
|  | 61 | TEST_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); | 
| Kenny Root | 95a6889 | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 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"; | 
| Kenny Root | b94a9a6 | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 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()) | 
| Kenny Root | fa59afb | 2010-07-12 09:02:18 -0700 | [diff] [blame] | 81 | << "version didn't come out the same as it went in"; | 
| Kenny Root | b94a9a6 | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 82 | const char* currentPackageName = mObbFile->getPackageName().string(); | 
|  | 83 | EXPECT_STREQ(packageName, currentPackageName) | 
| Kenny Root | fa59afb | 2010-07-12 09:02:18 -0700 | [diff] [blame] | 84 | << "package name didn't come out the same as it went in"; | 
| Kenny Root | 95a6889 | 2010-10-13 15:00:07 -0700 | [diff] [blame] | 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"; | 
| Kenny Root | b94a9a6 | 2010-06-01 10:34:29 -0700 | [diff] [blame] | 98 | } | 
|  | 99 |  | 
|  | 100 | } |