1 /* 2 * Copyright (C) 2018 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 package com.example.android.systemupdatersample.util; 18 19 import static com.example.android.systemupdatersample.util.PackageFiles.PAYLOAD_BINARY_FILE_NAME; 20 21 import static org.junit.Assert.assertArrayEquals; 22 import static org.junit.Assert.assertEquals; 23 24 import android.content.Context; 25 26 import androidx.test.InstrumentationRegistry; 27 import androidx.test.filters.SmallTest; 28 import androidx.test.runner.AndroidJUnit4; 29 30 import com.example.android.systemupdatersample.PayloadSpec; 31 import com.example.android.systemupdatersample.tests.R; 32 import com.google.common.base.Charsets; 33 import com.google.common.io.Files; 34 35 import org.junit.Before; 36 import org.junit.Rule; 37 import org.junit.Test; 38 import org.junit.rules.ExpectedException; 39 import org.junit.runner.RunWith; 40 41 import java.io.File; 42 import java.io.IOException; 43 import java.nio.file.Paths; 44 45 /** 46 * Tests if PayloadSpecs parses update package zip file correctly. 47 */ 48 @RunWith(AndroidJUnit4.class) 49 @SmallTest 50 public class PayloadSpecsTest { 51 52 private static final String PROPERTIES_CONTENTS = "k1=val1\nkey2=val2"; 53 54 private File mTestDir; 55 56 private Context mTargetContext; 57 private Context mTestContext; 58 59 private PayloadSpecs mPayloadSpecs; 60 61 @Rule 62 public final ExpectedException thrown = ExpectedException.none(); 63 64 @Before 65 public void setUp() { 66 mTargetContext = InstrumentationRegistry.getTargetContext(); 67 mTestContext = InstrumentationRegistry.getContext(); 68 69 mTestDir = mTargetContext.getCacheDir(); 70 mPayloadSpecs = new PayloadSpecs(); 71 } 72 73 @Test 74 public void forNonStreaming_works() throws Exception { 75 // Prepare the target file 76 File packageFile = Paths 77 .get(mTargetContext.getCacheDir().getAbsolutePath(), "ota.zip") 78 .toFile(); 79 java.nio.file.Files.deleteIfExists(packageFile.toPath()); 80 java.nio.file.Files.copy(mTestContext.getResources().openRawResource(R.raw.ota_002_package), 81 packageFile.toPath()); 82 PayloadSpec spec = mPayloadSpecs.forNonStreaming(packageFile); 83 84 assertEquals("correct url", "file://" + packageFile.getAbsolutePath(), spec.getUrl()); 85 assertEquals("correct payload offset", 86 30 + PAYLOAD_BINARY_FILE_NAME.length(), spec.getOffset()); 87 assertEquals("correct payload size", 1392, spec.getSize()); 88 assertEquals(4, spec.getProperties().size()); 89 assertEquals( 90 "FILE_HASH=sEAK/NMbU7GGe01xt55FsPafIPk8IYyBOAd6SiDpiMs=", 91 spec.getProperties().get(0)); 92 } 93 94 @Test 95 public void forNonStreaming_IOException() throws Exception { 96 thrown.expect(IOException.class); 97 mPayloadSpecs.forNonStreaming(new File("/fake/news.zip")); 98 } 99 100 @Test 101 public void forStreaming_works() throws Exception { 102 String url = "http://a.com/b.zip"; 103 long offset = 45; 104 long size = 200; 105 File propertiesFile = createMockPropertiesFile(); 106 107 PayloadSpec spec = mPayloadSpecs.forStreaming(url, offset, size, propertiesFile); 108 assertEquals("same url", url, spec.getUrl()); 109 assertEquals("same offset", offset, spec.getOffset()); 110 assertEquals("same size", size, spec.getSize()); 111 assertArrayEquals("correct properties", 112 new String[]{"k1=val1", "key2=val2"}, spec.getProperties().toArray(new String[0])); 113 } 114 115 private File createMockPropertiesFile() throws IOException { 116 File propertiesFile = new File(mTestDir, PackageFiles.PAYLOAD_PROPERTIES_FILE_NAME); 117 Files.asCharSink(propertiesFile, Charsets.UTF_8).write(PROPERTIES_CONTENTS); 118 return propertiesFile; 119 } 120 121 } 122