|
| 1 | +// This file is part of arduino-cli. |
| 2 | +// |
| 3 | +// Copyright 2020 ARDUINO SA (http://www.arduino.cc/) |
| 4 | +// |
| 5 | +// This software is released under the GNU General Public License version 3, |
| 6 | +// which covers the main part of arduino-cli. |
| 7 | +// The terms of this license can be found at: |
| 8 | +// https://www.gnu.org/licenses/gpl-3.0.en.html |
| 9 | +// |
| 10 | +// You can be released from the requirements of the above licenses by purchasing |
| 11 | +// a commercial license. Buying such a license is mandatory if you want to |
| 12 | +// modify or otherwise use the software for commercial activities involving the |
| 13 | +// Arduino software without disclosing the source code of your own applications. |
| 14 | +// To purchase a commercial license, send an email to license@arduino.cc. |
| 15 | + |
| 16 | +package compile |
| 17 | + |
| 18 | +import ( |
| 19 | + "testing" |
| 20 | + |
| 21 | + properties "github.com/arduino/go-properties-orderedmap" |
| 22 | + "github.com/stretchr/testify/require" |
| 23 | +) |
| 24 | + |
| 25 | +func TestReplaceSecurityKeys(t *testing.T) { |
| 26 | + propsWithDefaultKeys := properties.NewFromHashmap(map[string]string{ |
| 27 | + "tools.toolname.keys.path": "/default-keys-path", |
| 28 | + "tools.toolname.sign.name": "default-signing-key.pem", |
| 29 | + "tools.toolname.encrypt.name": "default-encrypt-key.pem", |
| 30 | + }) |
| 31 | + newKeysPath := "/new-keys-path" |
| 32 | + newSignKeyName := "new-signing-key.pem" |
| 33 | + newEncryptKeyName := "new-encrypt-key.pem" |
| 34 | + goldProps := properties.NewFromHashmap(map[string]string{ |
| 35 | + "tools.toolname.keys.path": newKeysPath, |
| 36 | + "tools.toolname.sign.name": newSignKeyName, |
| 37 | + "tools.toolname.encrypt.name": newEncryptKeyName, |
| 38 | + }) |
| 39 | + |
| 40 | + ReplaceSecurityKeys(propsWithDefaultKeys, newKeysPath, newSignKeyName, newEncryptKeyName) |
| 41 | + require.True(t, goldProps.Equals(propsWithDefaultKeys)) |
| 42 | +} |
| 43 | + |
| 44 | +func TestReplaceSecurityKeysEmpty(t *testing.T) { |
| 45 | + propsWithNoKeys := properties.NewFromHashmap(map[string]string{}) |
| 46 | + goldProps := properties.NewFromHashmap(map[string]string{}) |
| 47 | + newKeysPath := "/new-keys-path" |
| 48 | + newSignKeyName := "new-signing-key.pem" |
| 49 | + newEncryptKeyName := "new-encrypt-key.pem" |
| 50 | + |
| 51 | + // No error should be returned since the properties map is empty |
| 52 | + ReplaceSecurityKeys(propsWithNoKeys, newKeysPath, newSignKeyName, newEncryptKeyName) |
| 53 | + require.True(t, goldProps.Equals(propsWithNoKeys)) |
| 54 | +} |
| 55 | + |
| 56 | +func TestReplaceSecurityKeysNothingToReplace(t *testing.T) { |
| 57 | + propsWithDifferentKeys := properties.NewFromHashmap(map[string]string{ |
| 58 | + "tools.openocd.path": "{runtime.tools.openocd.path}", |
| 59 | + "tools.openocd.cmd": "bin/openocd", |
| 60 | + "tools.openocd.cmd.windows": "bin/openocd.exe", |
| 61 | + }) |
| 62 | + goldProps := propsWithDifferentKeys.Clone() |
| 63 | + newKeysPath := "/new-keys-path" |
| 64 | + newSignKeyName := "new-signing-key.pem" |
| 65 | + newEncryptKeyName := "new-encrypt-key.pem" |
| 66 | + |
| 67 | + // No error should be returned since there are no keys in the properties map |
| 68 | + ReplaceSecurityKeys(propsWithDifferentKeys, newKeysPath, newSignKeyName, newEncryptKeyName) |
| 69 | + require.True(t, goldProps.Equals(propsWithDifferentKeys)) |
| 70 | +} |
0 commit comments