Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 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 | |
Zhomart Mukhamejanov | da7e237 | 2018-05-01 12:50:55 -0700 | [diff] [blame] | 19 | import static com.example.android.systemupdatersample.util.PackageFiles.PAYLOAD_BINARY_FILE_NAME; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 20 | |
| 21 | import static org.junit.Assert.assertArrayEquals; |
| 22 | import static org.junit.Assert.assertEquals; |
| 23 | |
| 24 | import android.content.Context; |
koushik panuganti | d5c7fb5 | 2018-12-12 10:39:44 -0800 | [diff] [blame] | 25 | |
| 26 | import androidx.test.InstrumentationRegistry; |
| 27 | import androidx.test.filters.SmallTest; |
| 28 | import androidx.test.runner.AndroidJUnit4; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 29 | |
| 30 | import com.example.android.systemupdatersample.PayloadSpec; |
Zhomart Mukhamejanov | 96eb59e | 2018-05-04 12:17:01 -0700 | [diff] [blame] | 31 | import com.example.android.systemupdatersample.tests.R; |
Zhomart Mukhamejanov | e606f6d | 2018-05-02 20:47:05 -0700 | [diff] [blame] | 32 | import com.google.common.base.Charsets; |
| 33 | import com.google.common.io.Files; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 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; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 42 | import java.io.IOException; |
Zhomart Mukhamejanov | 96eb59e | 2018-05-04 12:17:01 -0700 | [diff] [blame] | 43 | import java.nio.file.Paths; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 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"; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 53 | |
| 54 | private File mTestDir; |
| 55 | |
Zhomart Mukhamejanov | e606f6d | 2018-05-02 20:47:05 -0700 | [diff] [blame] | 56 | private Context mTargetContext; |
Zhomart Mukhamejanov | 96eb59e | 2018-05-04 12:17:01 -0700 | [diff] [blame] | 57 | private Context mTestContext; |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 58 | |
Zhomart Mukhamejanov | b0361ff | 2018-05-18 10:22:13 -0700 | [diff] [blame] | 59 | private PayloadSpecs mPayloadSpecs; |
| 60 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 61 | @Rule |
| 62 | public final ExpectedException thrown = ExpectedException.none(); |
| 63 | |
| 64 | @Before |
| 65 | public void setUp() { |
Zhomart Mukhamejanov | e606f6d | 2018-05-02 20:47:05 -0700 | [diff] [blame] | 66 | mTargetContext = InstrumentationRegistry.getTargetContext(); |
Zhomart Mukhamejanov | 96eb59e | 2018-05-04 12:17:01 -0700 | [diff] [blame] | 67 | mTestContext = InstrumentationRegistry.getContext(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 68 | |
Zhomart Mukhamejanov | 8a6a86a | 2018-06-07 10:55:16 -0700 | [diff] [blame] | 69 | mTestDir = mTargetContext.getCacheDir(); |
Zhomart Mukhamejanov | b0361ff | 2018-05-18 10:22:13 -0700 | [diff] [blame] | 70 | mPayloadSpecs = new PayloadSpecs(); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void forNonStreaming_works() throws Exception { |
Zhomart Mukhamejanov | 96eb59e | 2018-05-04 12:17:01 -0700 | [diff] [blame] | 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()); |
Zhomart Mukhamejanov | b0361ff | 2018-05-18 10:22:13 -0700 | [diff] [blame] | 82 | PayloadSpec spec = mPayloadSpecs.forNonStreaming(packageFile); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 83 | |
| 84 | assertEquals("correct url", "file://" + packageFile.getAbsolutePath(), spec.getUrl()); |
| 85 | assertEquals("correct payload offset", |
| 86 | 30 + PAYLOAD_BINARY_FILE_NAME.length(), spec.getOffset()); |
Zhomart Mukhamejanov | 96eb59e | 2018-05-04 12:17:01 -0700 | [diff] [blame] | 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)); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 92 | } |
| 93 | |
| 94 | @Test |
| 95 | public void forNonStreaming_IOException() throws Exception { |
| 96 | thrown.expect(IOException.class); |
Zhomart Mukhamejanov | b0361ff | 2018-05-18 10:22:13 -0700 | [diff] [blame] | 97 | mPayloadSpecs.forNonStreaming(new File("/fake/news.zip")); |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 98 | } |
| 99 | |
Zhomart Mukhamejanov | e606f6d | 2018-05-02 20:47:05 -0700 | [diff] [blame] | 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 | |
Zhomart Mukhamejanov | b0361ff | 2018-05-18 10:22:13 -0700 | [diff] [blame] | 107 | PayloadSpec spec = mPayloadSpecs.forStreaming(url, offset, size, propertiesFile); |
Zhomart Mukhamejanov | e606f6d | 2018-05-02 20:47:05 -0700 | [diff] [blame] | 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 | |
Zhomart Mukhamejanov | e606f6d | 2018-05-02 20:47:05 -0700 | [diff] [blame] | 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 | |
Zhomart Mukhamejanov | f4d280c | 2018-04-17 13:20:22 -0700 | [diff] [blame] | 121 | } |