Home | History | Annotate | Download | only in tests
      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 <sys/types.h>
     26 #include <sys/stat.h>
     27 #include <fcntl.h>
     28 #include <string.h>
     29 
     30 namespace android {
     31 
     32 #define TEST_FILENAME "/test.obb"
     33 
     34 class ObbFileTest : public testing::Test {
     35 protected:
     36     sp<ObbFile> mObbFile;
     37     String8 mFileName;
     38 
     39     virtual void SetUp() {
     40         mObbFile = new ObbFile();
     41         char* externalStorage = getenv("EXTERNAL_STORAGE");
     42 
     43         mFileName.append(externalStorage);
     44         mFileName.append(TEST_FILENAME);
     45 
     46         int fd = ::open(mFileName.string(), O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
     47         if (fd < 0) {
     48             FAIL() << "Couldn't create " << mFileName.string() << " for tests";
     49         }
     50     }
     51 
     52     virtual void TearDown() {
     53     }
     54 };
     55 
     56 TEST_F(ObbFileTest, ReadFailure) {
     57     EXPECT_FALSE(mObbFile->readFrom(-1))
     58             << "No failure on invalid file descriptor";
     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);
     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.string()))
     73             << "couldn't write to fake .obb file";
     74 
     75     mObbFile = new ObbFile();
     76 
     77     EXPECT_TRUE(mObbFile->readFrom(mFileName.string()))
     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 (size_t 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