<commit_before>/*
 * Copyright (c) 2011 - 2013 United ID.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.unitedid.yhsm;

import org.testng.annotations.*;
import org.unitedid.yhsm.internal.CommandHandler;
import org.unitedid.yhsm.utility.Utils;

public class ConfigureHSM extends SetupCommon {

    @BeforeTest
    public void setUp() throws Exception {
        super.setUp();
    }

    @AfterTest
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Test
    public void testConfigureHSM() throws Exception {
        System.out.println("Exiting HSM monitor mode (requires YubiHSM in 'debug' mode)");
        hsm.exitMonitorDebugMode();
        System.out.println("Configuring YubiHSM for test suite (" + hsm.getInfo().getSystemInfo() + ")");
        hsm.drainData();
        System.out.println(runCommand("sysinfo", true));
        if (hsm.getInfo().getMajorVersion() == 0) {
            System.out.println(runCommand("hsm ffffffff\r\r2f6af1e667456bb94528e7987344515b\ryes", true));
        } else {
            char esc = 0x1b;
            System.out.println(runCommand("hsm ffffffff\r" + configPassPhrase + "\r" + adminYubikey + "\r\r" + hsmPassPhrase + "\ryes", true));
            System.out.println(runCommand("dbload\r00001," + adminYubikey + ",f0f1f2f3f4f5," + duplicateStr("4d", 16) + ",\r" + esc, false));
        }
        System.out.println(runCommand("sysinfo", true));
        hsm.drainData();
        addKeys();
        System.out.println(runCommand("keylist", true));
        if (hsm.getInfo().getMajorVersion() != 0)
            System.out.println(runCommand("keycommit", true));
        System.out.println(runCommand("dblist", true));
        deviceHandler.write("exit\r".getBytes());
        Thread.sleep(50);
        hsm.drainData();
        CommandHandler.reset(deviceHandler);
    }

    private void addKeys() throws Exception {

        for (int i = 0; i <= 30; i++ ) {
            int flags = 1 << i;
            String key = new String();
            key = duplicateStr(String.format("%02x", i + 1), 32);
            addKey(flags, i + 1, key);
        }

        addKey(0x200, 0x1000, duplicateStr("06", 32));<commit_msg>Fixed test config for hsm password phrase, version <1.0.x<commit_after>/*
 * Copyright (c) 2011 - 2013 United ID.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.unitedid.yhsm;

import org.testng.annotations.*;
import org.unitedid.yhsm.internal.CommandHandler;
import org.unitedid.yhsm.utility.Utils;

public class ConfigureHSM extends SetupCommon {

    @BeforeTest
    public void setUp() throws Exception {
        super.setUp();
    }

    @AfterTest
    public void tearDown() throws Exception {
        super.tearDown();
    }

    @Test
    public void testConfigureHSM() throws Exception {
        System.out.println("Exiting HSM monitor mode (requires YubiHSM in 'debug' mode)");
        hsm.exitMonitorDebugMode();
        System.out.println("Configuring YubiHSM for test suite (" + hsm.getInfo().getSystemInfo() + ")");
        hsm.drainData();
        System.out.println(runCommand("sysinfo", true));
        if (hsm.getInfo().getMajorVersion() == 0) {
            System.out.println(runCommand("hsm ffffffff\r\r" + hsmPassPhrase + "\ryes", true));
        } else {
            char esc = 0x1b;
            System.out.println(runCommand("hsm ffffffff\r" + configPassPhrase + "\r" + adminYubikey + "\r\r" + hsmPassPhrase + "\ryes", true));
            System.out.println(runCommand("dbload\r00001," + adminYubikey + ",f0f1f2f3f4f5," + duplicateStr("4d", 16) + ",\r" + esc, false));
        }
        System.out.println(runCommand("sysinfo", true));
        hsm.drainData();
        addKeys();
        System.out.println(runCommand("keylist", true));
        if (hsm.getInfo().getMajorVersion() != 0)
            System.out.println(runCommand("keycommit", true));
        System.out.println(runCommand("dblist", true));
        deviceHandler.write("exit\r".getBytes());
        Thread.sleep(50);
        hsm.drainData();
        CommandHandler.reset(deviceHandler);
    }

    private void addKeys() throws Exception {

        for (int i = 0; i <= 30; i++ ) {
            int flags = 1 << i;
            String key = new String();
            key = duplicateStr(String.format("%02x", i + 1), 32);
            addKey(flags, i + 1, key);
        }

        addKey(0x200, 0x1000, duplicateStr("06", 32));
{'fraction_non_alphanumeric': 0.13169027036968917, 'fraction_numerical': 0.025013794371896266, 'mean_word_length': 2.837685250529287, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 4, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5929923', 'n_tokens_mistral': 1745, 'n_tokens_neox': 1642, 'n_words': 437}