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     char* mExternalStorage;
     38     char* mFileName;
     39 
     40     virtual void SetUp() {
     41         mObbFile = new ObbFile();
     42         mExternalStorage = getenv("EXTERNAL_STORAGE");
     43 
     44         const int totalLen = strlen(mExternalStorage) + strlen(TEST_FILENAME) + 1;
     45         mFileName = new char[totalLen];
     46         snprintf(mFileName, totalLen, "%s%s", mExternalStorage, TEST_FILENAME);
     47 
     48         int fd = ::open(mFileName, O_CREAT | O_TRUNC, S_IRUSR | S_IWUSR);
     49         if (fd < 0) {
     50             FAIL() << "Couldn't create " << mFileName << " for tests";
     51         }
     52     }
     53 
     54     virtual void TearDown() {
     55     }
     56 };
     57 
     58 TEST_F(ObbFileTest, ReadFailure) {
     59     EXPECT_FALSE(mObbFile->readFrom(-1))
     60             << "No failure on invalid file descriptor";
     61 }
     62 
     63 TEST_F(ObbFileTest, WriteThenRead) {
     64     const char* packageName = "com.example.obbfile";
     65     const int32_t versionNum = 1;
     66 
     67     mObbFile->setPackageName(String8(packageName));
     68     mObbFile->setVersion(versionNum);
     69 #define SALT_SIZE 8
     70     unsigned char salt[SALT_SIZE] = {0x01, 0x10, 0x55, 0xAA, 0xFF, 0x00, 0x5A, 0xA5};
     71     EXPECT_TRUE(mObbFile->setSalt(salt, SALT_SIZE))
     72             << "Salt should be successfully set";
     73 
     74     EXPECT_TRUE(mObbFile->writeTo(mFileName))
     75             << "couldn't write to fake .obb file";
     76 
     77     mObbFile = new ObbFile();
     78 
     79     EXPECT_TRUE(mObbFile->readFrom(mFileName))
     80             << "couldn't read from fake .obb file";
     81 
     82     EXPECT_EQ(versionNum, mObbFile->getVersion())
     83             << "version didn't come out the same as it went in";
     84     const char* currentPackageName = mObbFile->getPackageName().string();
     85     EXPECT_STREQ(packageName, currentPackageName)
     86             << "package name didn't come out the same as it went in";
     87 
     88     size_t saltLen;
     89     const unsigned char* newSalt = mObbFile->getSalt(&saltLen);
     90 
     91     EXPECT_EQ(sizeof(salt), saltLen)
     92             << "salt sizes were not the same";
     93 
     94     for (int i = 0; i < sizeof(salt); i++) {
     95         EXPECT_EQ(salt[i], newSalt[i])
     96                 << "salt character " << i << " should be equal";
     97     }
     98     EXPECT_TRUE(memcmp(newSalt, salt, sizeof(salt)) == 0)
     99             << "salts should be the same";
    100 }
    101 
    102 }
    103