text:
// Dstl (c) Crown Copyright 2017
// Modified by NCA (c) Crown Copyright 2017
package uk.gov.dstl.baleen.collectionreaders;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashSet;
import java.util.List;
import java.util.Properties;
import java.util.Set;
import java.util.StringJoiner;

import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Header;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.MethodNotSupportedException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.uima.UimaContext;
import org.apache.uima.collection.CollectionException;
import org.apache.uima.fit.descriptor.ConfigurationParameter;
import org.apache.uima.jcas.JCas;
import org.apache.uima.resource.ResourceInitializationException;

import uk.gov.dstl.baleen.types.metadata.Metadata;
import uk.gov.dstl.baleen.uima.BaleenCollectionReader;

/**
 * Connects to a specified mail server, treating each message and/or it's attachments as documents.
 *
 *
Optionally, messages can be deleted or marked as read after it has been retrieved. Be aware
 * that this happens at the point the message is retrieved, and as such if the pipeline fails at a
 * later point then the message will still have been deleted/marked read.
 */
public class EmailReader extends BaleenCollectionReader {
  /**
   * The mail server URL
   *
   * @baleen.config localhost
   */
  public static final String PARAM_SERVER = "server";

  @ConfigurationParameter(name = PARAM_SERVER, defaultValue = "localhost")
  private String server;

  /**
   * The mail server port
   *
   * @baleen.config 110
   */
  public static final String PARAM_PORT = "port";

  @ConfigurationParameter(name = PARAM_PORT, defaultValue = "110")
  private Integer port;

  /**
   * The e-mail address (user) to check
   *
   * @baleen.config baleen@localhost
   */
  public static final String PARAM_USER = "username";

  @ConfigurationParameter(name = PARAM_USER, defaultValue = "baleen@localhost")
  private String user;

  /**
   * The password for the account
   *
   * @baleen.config
   */
  public static final String PARAM_PASS = "password";

  @ConfigurationParameter(name = PARAM_PASS, defaultValue = "")
  private String pass;

  /**
   * Connection protocol (e.g. pop3 or imap)
   *
   *
Must be a valid JavaMail protocol - pop3 and imap are supported by default, other protocols
   * will require the relevant libraries to be on the classpath.
   *
   * @baleen.config pop3
   */
  public static final String PARAM_PROTOCOL = "protocol";

  @ConfigurationParameter(name = PARAM_PROTOCOL, defaultValue = "pop3")
  private String protocol;

  /**
   * The name of the inbox on the server which we will monitor
   *
   * @baleen.config INBOX
   */
  public static final String PARAM_INBOX = "inbox";

  @ConfigurationParameter(name = PARAM_INBOX, defaultValue = "INBOX")
  private String inbox;

  /**
   * How minimum wait in seconds between checking for new messages
   *
   * @baleen.config 120
   */
  public static final String PARAM_WAIT = "wait";

  @ConfigurationParameter(name = PARAM_WAIT, defaultValue = "120")
  private Integer wait;

  /**
   * Should a message be deleted after it has been processed
   *
   * @baleen.config false
   */
  public static final String PARAM_DELETE_EMAIL = "deleteEmails";

  @ConfigurationParameter(name = PARAM_DELETE_EMAIL, defaultValue = "false")
  private Boolean deleteEmailsAfterProcessing;

  /**
   * Should an attachment be deleted after it has been processed
   *
   * @baleen.config false
   */
  public static final String PARAM_DELETE_ATTACHMENT = "deleteAttachments";

  @ConfigurationParameter(name = PARAM_DELETE_ATTACHMENT, defaultValue = "false")
  private Boolean deleteAttachmentsAfterProcessing;

  /**
   * The folder in which to save attachments
   *
   * @baleen.config Current directory
   */
  public static final String PARAM_FOLDER = "attachmentFolder";

  @ConfigurationParameter(name = PARAM_FOLDER, defaultValue = "")
  private String folder;

  /**
   * Choose whether to process just message content, just attachments, or both.
   *
   *
Valid options are: content, attachments, both
   *
   * @baleen.config both
   */
  public static final String PARAM_PROCESS = "process";

  @ConfigurationParameter(name = PARAM_PROCESS, defaultValue = BOTH)
  private String process;

  public static final String BOTH = "both";
  public static final String CONTENT = "content";
  public static final String ATTACHMENTS = "attachments";

  private Long lastCheck = 0L;

  private Store store;
  private Folder inboxFolder;

  private List<Message> messageQueue = new ArrayList<>();
  private List<File> attachmentQueue = new ArrayList<>();

  private Set<String> alreadyProcessed = new HashSet<>();

  @Override
  protected void doInitialize(UimaContext context) throws ResourceInitializationException {
    validateParams();

    Authenticator authenticator =
        new Authenticator() {
          @Override
          protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, pass);
          }
        };

    Properties prop = new Properties();
    prop.put("mail.store.protocol", protocol);
    prop.put("mail.host", server);
    prop.put("mail." + protocol + ".port", port);
    prop.put("mail.user", user);

    Session session = Session.getInstance(prop, authenticator);
    try {
      store = session.getStore();
    } catch (NoSuchProviderException e) {
      throw new ResourceInitializationException(e);
    }

    try {
      store.connect();
      inboxFolder = store.getFolder(inbox);
      reopenConnection();
    } catch (MessagingException me) {
      throw new ResourceInitializationException(me);
    }
  }

  @Override
  protected void doGetNext(JCas jCas) throws IOException, CollectionException {
    try {
      reopenConnection();
    } catch (MessagingException me) {
      throw new IOException("Unable to reconnect to mail server", me);
    }

    try {
      if (CONTENT.equalsIgnoreCase(process)) {
        processMessage(jCas, messageQueue.remove(0), Collections.emptyList());
      } else if (ATTACHMENTS.equalsIgnoreCase(process)) {
        if (attachmentQueue.isEmpty()) {
          attachmentQueue.addAll(saveAttachments(messageQueue.remove(0)));
        }

        processAttachment(jCas, attachmentQueue.remove(0));
      } else { // Both
        if (!attachmentQueue.isEmpty()) {
          processAttachment(jCas, attachmentQueue.remove(0));
        } else {
          Message msg = messageQueue.remove(0);

          List<File> attachments = saveAttachments(msg);
          attachmentQueue.addAll(attachments);

          processMessage(jCas, msg, attachments);
        }
      }
    } catch (MessagingException me) {
      throw new IOException("Unable to process message or attachment", me);
    }
  }

  @Override
  protected void doClose() throws IOException {
    try {
      inboxFolder.close(true);
      store.close();
    } catch (MessagingException me) {
      throw new IOException(me);
    }
  }

  @Override
  public boolean doHasNext() throws IOException, CollectionException {
    try {
      reopenConnection();
    } catch (MessagingException me) {
      throw new IOException("Unable to reconnect to mail server", me);
    }

    if (!attachmentQueue.isEmpty() || !messageQueue.isEmpty()) {
      return true;
    }

    try {
      tryExpunge();
    } catch (MessagingException me) {
      throw new IOException("Unable to expunge (delete) messages", me);
    }

    if (lastCheck + wait * 1000 > System.currentTimeMillis()) {
      return false;
    }

    lastCheck = System.currentTimeMillis();

    try {
      for (Message msg : inboxFolder.getMessages()) {
        String uid = generateUniqueId(msg);
        if (!alreadyProcessed.add(uid)) {
          continue;
        }

        if (!ATTACHMENTS.equalsIgnoreCase(process) || hasAttachments(msg)) {
          messageQueue.add(msg);
        }
      }
    } catch (MessagingException me) {
      throw new IOException("Unable to check for messages", me);
    }

    return !messageQueue.isEmpty();
  }

  private String generateUniqueId(Message msg) throws MessagingException {
    String sentDate = "NOSD";
    String receivedDate = "NORD";

    if (msg.getSentDate() != null) {
      sentDate = String.valueOf(msg.getSentDate().toInstant().toEpochMilli());
    }
    if (msg.getReceivedDate() != null) {
      receivedDate = String.valueOf(msg.getReceivedDate().toInstant().toEpochMilli());
    }

    String sender = getAddress(msg.getFrom()[0]);

    return joinStrings(msg.getSubject(), sender, sentDate, receivedDate);
  }

  private String joinStrings(String... strings) {
    StringJoiner sj = new StringJoiner("_");
    for (String s : strings) {
      if (s != null) {
        sj.add(s);
      }
    }

    return sj.toString();
  }

  private void processMessage(JCas jCas, Message msg, List<File> attachments)
      throws MessagingException, IOException {
    String content = getContent(msg);

    String subject = msg.getSubject();
    String sender = getAddress(msg.getFrom()[0]);

    InputStream is = IOUtils.toInputStream(content, Charset.defaultCharset());
    extractContent(is, "mailto:" + sender + "#" + subject, jCas);

    addMetadata(jCas, "sender", sender);
    addMetadata(jCas, "subject", subject);

    addAddressesMetadata(msg.getRecipients(RecipientType.TO), jCas, "toRecipient");
    addAddressesMetadata(msg.getRecipients(RecipientType.CC), jCas, "ccRecipient");

    for (String attachment : getAttachments(msg)) { // We don't use the attachments list here,
      // because we want to add the list of
      // attachments to the metadata regardless of whether we've saved them or not
      addMetadata(jCas, "attachment", attachment);
    }
    if (!attachments.isEmpty()) {
      for (File attachment : attachments) {
        addMetadata(jCas, "attachmentSaveLocation", attachment.getAbsolutePath());
      }
    }

    Enumeration headers = msg.getAllHeaders();
    while (headers.hasMoreElements()) {
      Header header = headers.nextElement();
      addMetadata(jCas, header.getName(), header.getValue());
    }

    // Delete message?
    if (deleteEmailsAfterProcessing) {
      try {
        msg.setFlag(Flags.Flag.DELETED, true);
      } catch (MessagingException me) {
        getMonitor().error("Unable to delete message", me);
      }

      try {
        alreadyProcessed.remove(generateUniqueId(msg)); // We can save memory by removing messages
        // we've deleted on the server
      } catch (MessagingException me) {
        getMonitor().warn("Unable to re-generate unique ID for message to remove from memory", me);
      }
    }
  }

  private void processAttachment(JCas jCas, File attachment) throws IOException {
    try (InputStream is = new FileInputStream(attachment); ) {
      extractContent(is, attachment.getAbsolutePath(), jCas);
    }

    // Delete attachment?
    if (deleteAttachmentsAfterProcessing) {
      try {
        Files.delete(attachment.toPath());
      } catch (IOException ioe) {
        getMonitor().error("Unable to delete attachment", ioe);
      }
    }
  }

  private String getContent(Message msg) throws IOException, MessagingException {
    Object messageContentObject = msg.getContent();
    if (messageContentObject instanceof Multipart) {
      Multipart multipart = (Multipart) msg.getContent();

      // Loop over the parts of the email
      for (int i = 0; i < multipart.getCount(); i++) {
        // Retrieve the next part
        Part part = multipart.getBodyPart(i);

        if (!Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())
            && StringUtils.isBlank(part.getFileName())) {
          return part.getContent().toString();
        }
      }
    } else {
      return msg.getContent().toString().trim();
    }

    return "";
  }

  private Boolean hasAttachments(Message msg) throws MessagingException, IOException {
    if (msg.isMimeType("multipart/mixed")) {
      Multipart mp = (Multipart) msg.getContent();
      if (mp.getCount() > 1) {
        return true;
      }
    }

    return false;
  }

  private List<String> getAttachments(Message msg) throws MessagingException, IOException {
    Object messageContentObject = msg.getContent();

    List<String> attachments = new ArrayList<>();

    if (messageContentObject instanceof Multipart) {
      Multipart multipart = (Multipart) msg.getContent();

      for (int i = 0; i < multipart.getCount(); i++) {
        Part part = multipart.getBodyPart(i);

        if (!Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())
            && StringUtils.isBlank(part.getFileName())) {
          continue;
        }

        attachments.add(part.getFileName());
      }
    }

    return attachments;
  }

  private List<File> saveAttachments(Message msg) throws MessagingException, IOException {
    Object messageContentObject = msg.getContent();

    List<File> attachmentLocations = new ArrayList<>();

    // Determine email type
    if (messageContentObject instanceof Multipart) {
      // Retrieve the Multipart object from the message
      Multipart multipart = (Multipart) msg.getContent();

      // Loop over the parts of the email
      for (int i = 0; i < multipart.getCount(); i++) {
        // Retrieve the next part
        Part part = multipart.getBodyPart(i);

        if (!Part.ATTACHMENT.equalsIgnoreCase(part.getDisposition())
            && StringUtils.isBlank(part.getFileName())) {
          continue;
        }
        String fileName = part.getFileName();

        File destFile = new File(folder, fileName);

        int append = 0;
        while (destFile.exists()) {
          append++;
          destFile = new File(folder, fileName + "." + append);
        }
        if (append != 0) {
          getMonitor()
              .info(
                  "File with the same name already exists in {} - attachment will be saved as {}",
                  fileName,
                  destFile.getName());
        }

        writeFileToDisk(destFile, part);

        attachmentLocations.add(destFile);
      }
    }

    return attachmentLocations;
  }

  private void writeFileToDisk(File destFile, Part part) throws IOException, MessagingException {
    try (FileOutputStream output = new FileOutputStream(destFile);
        InputStream input = part.getInputStream()) {

      byte[] buffer = new byte[4096];
      int byteRead;

      while ((byteRead = input.read(buffer)) != -1) {
        output.write(buffer, 0, byteRead);
      }
    } catch (IOException ex) {
      throw new IOException("Unable to save attachment", ex);
    }
  }

  private void validateParams() {
    if (folder == null || folder.isEmpty()) {
      folder = System.getProperty("user.dir");
    }

    if (process.isEmpty()
        || !(BOTH.equalsIgnoreCase(process)
            || CONTENT.equalsIgnoreCase(process)
            || ATTACHMENTS.equalsIgnoreCase(process))) {
      process = BOTH;
    }
  }

  private void addMetadata(JCas jCas, String key, String value) {
    Metadata md = new Metadata(jCas);
    md.setKey(key);
    md.setValue(value);
    getSupport().add(md);
  }

  private void tryExpunge() throws MessagingException {
    try {
      inboxFolder.expunge();
    } catch (MethodNotSupportedException mnse) {
      getMonitor()
          .debug(
              "Expunge method not supported (e.g. POP3) - closing and reopening folder - {}",
              mnse.getMessage());

      inboxFolder.close(true);
      reopenConnection();
    }
  }

  private void reopenConnection() throws MessagingException {
    if (!inboxFolder.isOpen()) {
      inboxFolder.open(Folder.READ_WRITE);
    }
  }

  private String getAddress(Address addr) {
    String address = ((InternetAddress) addr).getPersonal();
    if (address == null) {
      address = ((InternetAddress) addr).getAddress();
    }

    return address;
  }

  private void addAddressesMetadata(Address[] addresses, JCas jCas, String key) {
    if (addresses != null) {
      for (Address addr : addresses) {
        addMetadata(jCas, key, getAddress(addr));
      }
    }
  }
}


text:
<?php

class SimplePie_Copyright
{
    var $url;
    var $label;

    // Constructor, used to input the data
    function SimplePie_Copyright($url = null, $label = null)
    {
        $this->url = $url;
        $this->label = $label;
    }

    function __toString()
    {
        // There is no $this->data here
        return md5(serialize($this));
    }

    function get_url()
    {
        if ($this->url !== null) {
            return $this->url;
        } else {
            return null;
        }
    }

    function get_attribution()
    {
        if ($this->label !== null) {
            return $this->label;
        } else {
            return null;
        }
    }
}

?>

redpajama_set_name: RedPajamaGithub


text:
import scrapy
import json

from locations.items import GeojsonPointItem

HEADERS = {'Content-Type': 'application/json'}

class LovesSpider(scrapy.Spider):
    name = "loves"
    allowed_domains = ["www.loves.com"]
    download_delay = 0.2

    def start_requests(self):
        payload = json.dumps({"StoreTypes":[],"Amenities":[],"Restaurants":[],"FoodConcepts":[],"State":"All","City":"All","Highway":"All"})
        yield scrapy.Request(
            "https://www.loves.com/api/sitecore/StoreSearch/SearchStores",
            method='POST',
            body=payload,
            headers=HEADERS
        )

    def parse(self, response):
        stores = json.loads(response.body_as_unicode())
        for store in stores[0]['Points']:
            yield GeojsonPointItem(
                name=store['Name'],
                ref=store['SiteId'],
                addr_full=store['Address1'],
                city=store['City'],
                state=store['State'],
                postcode=store['Zip'],
                phone=store['PhoneNumber'],
                lat=float(store['Latitude']),
                lon=float(store['Longitude']),
            )


text:
package aos.t4f.xsd.model.jaxb;

import java.io.InputStream;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.namespace.QName;

import org.junit.Test;

/**
 *
 */
public class T4FJaxbTest {

	/**
	 *
	 * @throws JAXBException
	 */
	@Test
	public void testMarshall() throws JAXBException {

		JAXBContext jc = JAXBContext.newInstance(PurchaseOrderType.class.getPackage().getName());
		ObjectFactory factory = new ObjectFactory();

		PurchaseOrderType type = factory.createPurchaseOrderType();
		type.setComment("comment");
		Items items = factory.createItems();
		Items.Item o = factory.createItemsItem();
		o.setPartNum("test");
		o.setComment("part comment");
		o.setProductName("productname");
		o.setQuantity(2);

		items.getItem().add(o);
		type.setItems(items);

		JAXBElement<PurchaseOrderType> element = factory.createPurchaseOrder(type);
		Marshaller m = jc.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.marshal(element, System.out);

	}

	/**
	 *
	 * @throws JAXBException
	 */
	@Test
	public void testUnMarshall() throws JAXBException {
		JAXBContext jc = JAXBContext.newInstance(PurchaseOrderType.class.getPackage().getName());
		Unmarshaller u = jc.createUnmarshaller();
		InputStream is = T4FJaxbTest.class.getClassLoader().getResourceAsStream("aos/t4f/xsd/model/jaxb/purchase-order.xml");
		JAXBElement<PurchaseOrderType> doc = (JAXBElement<PurchaseOrderType>) u.unmarshal(T4FJaxbTest.class.getClassLoader().getResourceAsStream("aos/t4f/xsd/model/jaxb/purchase-order.xml"));
		Marshaller m = jc.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.marshal(doc, System.out);
	}

	/**
	 *
	 * @throws JAXBException
	 */
	@Test
	public void testUnMarshallWithoutXmlRoot() throws JAXBException {
		JAXBContext jc = JAXBContext.newInstance(PurchaseOrderType.class.getPackage().getName());
		Unmarshaller u = jc.createUnmarshaller();
		InputStream is = T4FJaxbTest.class.getClassLoader().getResourceAsStream("aos/t4f/xsd/model/jaxb/purchase-order.xml");
		JAXBElement<PurchaseOrderType> doc = (JAXBElement<PurchaseOrderType>) u.unmarshal(T4FJaxbTest.class.getClassLoader().getResourceAsStream("aos/t4f/xsd/model/jaxb/purchase-order.xml"));
		Marshaller m = jc.createMarshaller();
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
		m.marshal(new JAXBElement(new QName("uri","local"), doc.getClass(), doc), System.out);
	}

}


text:
    <title>La maison du domaine</title>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />

		Министерство культуры
Российской Федерации
Портал Культура.рф
Проект «Образы России»

Le musée-réserve naturel de Pouchkine (Zakharkovo)

La maison du domaine

Pour le 200ième anniversaire du poète a été reconstruite la maison du domaine selon le projet de N.Е.Kartachova et V.V.Zoubarev à compte de la région Odintsovksyi.
La maison est en bois de deux étages, avec le deuxième étage entresolé, avec le fronton et les colonnes, le belvédère, le placement d'enfilade des pièces.
Le domaine avec la maison a été acquis par la grand-mère du poète Maria Alekseevna Gannibal en 1804 qu'elle a été propriétaire jusqu'en 1810. Ici sont passées les années d'enfance de A.S.Pouchkin.

Культура.рф © Портал культурного наследия России


text:
@interface DCReferenceKeyParser : NSObject

@property(nonatomic, readonly) NSString *splitToken;

+ (DCReferenceKeyParser *) parserForToken: (NSString *) splitToken;
- (NSString *) splitKeyAndMakeCamelcased: (NSString *) key;

@end


text:
const expect = require('chai').expect;
const sinon = require('sinon');
const dry = require('../src/dry-parser.js');

describe('dry-parser', () => {
    let sandbox;

    beforeEach(() => {
        sandbox = sinon.sandbox.create();
        sandbox.stub(console, 'warn');
    });

    afterEach(() => {
        sandbox.restore();
    });

    describe('function(parse)', () => {
        describe('single-object', () => {
            it('should parse bindings of a single object', () => {
                const results = dry.parse({
                    assets: 'assets',
                    sass: '{assets}/sass'
                });

                expect(results).to.deep.equal({
                    assets: 'assets',
                    sass: 'assets/sass'
                });
            });

            it('should parse nested bindings of a single object', () => {
                const results = dry.parse({
                    dir: {
                        node: 'node_modules'
                    },
                    lib: {
                        angular: '{dir.node}/angular/angular.js'
                    }
                });

                expect(results).to.deep.equal({
                    dir: {
                        node: 'node_modules'
                    },
                    lib: {
                        angular: 'node_modules/angular/angular.js'
                    }
                });
            });

            it('should support whatever ordering is provided', () => {
                const results = dry.parse({
                    lodash: '{node}/lodash',
                    object: '{lodash}/object',
                    node: 'node_modules'
                });

                expect(results).to.deep.equal({
                    lodash: 'node_modules/lodash',
                    object: 'node_modules/lodash/object',
                    node: 'node_modules'
                });
            });

            it('should parse bindings within an array', () => {
                const results = dry.parse({
                    libraries: [
                        'angular'
                    ],
                    someValue: '{libraries.0}'
                });

                expect(results).to.deep.equal({
                    libraries: [
                        'angular'
                    ],
                    someValue: 'angular'
                });
            });

            it('should retain non-string types', () => {
                const results = dry.parse({
                    bool: true,
                    int: 1
                });

                expect(results).to.deep.equal({
                    bool: true,
                    int: 1
                });
            });

            it('should log a warning when passed an invalid config', () => {
                const results = dry.parse({
                    invalid: '{dir.invalidKey}/less'
                });

                expect(results).to.deep.equal({
                    invalid: '{dir.invalidKey}/less'
                });
                expect(console.warn.callCount).to.equal(1);
                expect(console.warn.calledWith('The following key doesn\'t map to any values; ', 'dir.invalidKey')).to.equal(true);
            });
        });

        describe('bi-parsing', () => {
            it('should parse bindings between two objects', () => {
                const results = dry.parse({
                    sass: '{assets}/sass'
                }, {
                    assets: 'assets'
                });

                expect(results).to.deep.equal({
                    sass: 'assets/sass'
                });
            });
        });
    });
});


text:
package com.jcraft.jsch;

import org.apache.airavata.gfac.core.authentication.GSIAuthenticationInfo;
import org.globus.gsi.gssapi.GSSConstants;
import org.ietf.jgss.GSSException;
import org.ietf.jgss.Oid;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/* -*-mode:java; c-basic-offset:2; indent-tabs-mode:nil -*- */
/*
 * Copyright(c)2004,2005,2006 ymnk, JCraft,Inc. All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 * 1. Redistributions of source code must retain the above copyright notice,
 * this list of conditions and the following disclaimer. 2. Redistributions in
 * binary form must reproduce the above copyright notice, this list of
 * conditions and the following disclaimer in the documentation and/or other
 * materials provided with the distribution. 3. The names of the authors may not
 * be used to endorse or promote products derived from this software without
 * specific prior written permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND
 * ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL JCRAFT, INC. OR ANY CONTRIBUTORS TO THIS
 * SOFTWARE BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
 * OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION)HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT(INCLUDING NEGLIGENCE OR OTHERWISE)ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

/**
 * This class now supports two mappings to the gssapi-with-mic method: x509
 * (preferred) and krb5.
 *
 * @author Al Rossi
 * @author Jeff Overbey
 */
public class UserAuthGSSAPIWithMICGSSCredentials extends UserAuth {

    private static final int SSH_MSG_USERAUTH_GSSAPI_RESPONSE = 60;
    private static final int SSH_MSG_USERAUTH_GSSAPI_TOKEN = 61;
    // private static final int SSH_MSG_USERAUTH_GSSAPI_EXCHANGE_COMPLETE = 63;
    private static final int SSH_MSG_USERAUTH_GSSAPI_ERROR = 64;
    private static final int SSH_MSG_USERAUTH_GSSAPI_ERRTOK = 65;
    private static final int SSH_MSG_USERAUTH_GSSAPI_MIC = 66;

    // this is the preferred order
    private static String[] supportedMethods = { "gssapi-with-mic.x509",
            "gssapi-with-mic.krb5" };
    private static byte[][] supportedOids;

    static {
        try {
            supportedOids = new byte[][] {
                    GSSConstants.MECH_OID.getDER(),
                    new Oid("1.2.840.113554.1.2.2").getDER() };
        } catch (GSSException gsse) {
            gsse.printStackTrace();
        }
    }

    @Override
    public boolean start(Session session) throws Exception {

        // this.userinfo = userinfo;
        Packet packet = session.packet;
        Buffer buf = session.buf;
        final String username = session.username;
        byte[] _username = Util.str2byte(username);

        // checkForSupportedOIDs
        List methods = new ArrayList();
        boolean found = false;
        for (int i = 0; i < supportedOids.length; i++) {
            found = found
                    || checkForSupportedOIDs(methods, packet, buf, i,
                    _username, session);
        }

        if (!found)
            return false;

        // logger.debug( "supported methods " + methods );

        boolean success = false;
        for (Iterator it = methods.iterator(); it.hasNext();) {
            String method = (String) it.next();
            success = tryMethod(username, _username, method, session, packet,
                    buf);
            if (success)
                break;
        }
        return success;

    }

    private boolean checkForSupportedOIDs(List methods, Packet packet,
                                          Buffer buf, int index, byte[] _username, Session session)
            throws Exception {
        packet.reset();

        // byte SSH_MSG_USERAUTH_REQUEST(50)
        // string user name(in ISO-10646 UTF-8 encoding)
        // string service name(in US-ASCII)
        // string "gssapi"(US-ASCII)
        // uint32 n, the number of OIDs client supports
        // string[n] mechanism OIDS
        buf.putByte((byte) SSH_MSG_USERAUTH_REQUEST);
        buf.putString(_username);
        buf.putString("ssh-connection".getBytes());
        buf.putString("gssapi-with-mic".getBytes());
        buf.putInt(1);
        buf.putString(supportedOids[index]);
        session.write(packet);

        while (true) {
            buf = session.read(buf);

            if (buf.buffer[5] == SSH_MSG_USERAUTH_FAILURE) {
                return false;
            }

            if (buf.buffer[5] == SSH_MSG_USERAUTH_GSSAPI_RESPONSE) {
                buf.getInt();
                buf.getByte();
                buf.getByte();
                byte[] message = buf.getString();
                // logger.debug( "OID " + supportedOids[index] );
                if (Util.array_equals(message, supportedOids[index])) {
                    methods.add(supportedMethods[index]);
                    // logger.debug( "OID MATCH, method is " + methods );
                    return true;
                }
            }

            if (buf.buffer[5] == SSH_MSG_USERAUTH_BANNER) {
                buf.getInt();
                buf.getByte();
                buf.getByte();
                byte[] _message = buf.getString();
                buf.getString();
                String message = Util.byte2str(_message);
                if (userinfo != null) {
                    userinfo.showMessage(message);
                }
                continue;
            }
            return false;
        }
    }

    private boolean tryMethod(String username, byte[] _username, String method,
                              Session session, Packet packet, Buffer buf) throws Exception {
        GSSContext context = null;
        try {
            Class c = Class.forName(session.getConfig(method));
            context = (GSSContext) (c.newInstance());

        } catch (Exception e) {
            // logger.error( "could not instantiate GSSContext", e );
            return false;
        }

        // Get the credentials and set them
        // Not a good way, but we dont have any choice
        if (session instanceof ExtendedSession) {
            GSIAuthenticationInfo authenticationInfo = ((ExtendedSession) session).getAuthenticationInfo();

            if (context instanceof GSSContextX509) {
                ((GSSContextX509) context).setCredential(authenticationInfo.getCredentials());
            }
        }

        // logger.debug( "GOT CONTEXT: " + context );

        // FIXME
        // if ( userinfo instanceof IX509UserInfo ) {
        // if ( context instanceof GSSContextX509 ) {
        // GSSCredential credential = ( ( IX509UserInfo )userinfo
        // ).getCredential();
        // logger.debug( "user info credential = " + credential );
        // ( ( GSSContextX509 )context ).setCredential( credential );
        // }
        // }

        try {
            context.create(username, session.host);
        } catch (JSchException e) {
            // logger.error( "context creation failed", e );
            return false;
        }

        byte[] token = new byte[0];

        while (!context.isEstablished()) {
            try {
                token = context.init(token, 0, token.length);
            } catch (JSchException e) {
                // logger.error( "context initialization failed", e );
                // TODO
                // ERRTOK should be sent?
                // byte SSH_MSG_USERAUTH_GSSAPI_ERRTOK
                // string error token
                return false;
            }

            if (token != null) {
                packet.reset();
                buf.putByte((byte) SSH_MSG_USERAUTH_GSSAPI_TOKEN);
                buf.putString(token);
                session.write(packet);
            }

            if (!context.isEstablished()) {
                buf = session.read(buf);

                if (buf.buffer[5] == SSH_MSG_USERAUTH_GSSAPI_ERROR) {
                    // uint32 major_status
                    // uint32 minor_status
                    // string message
                    // string language tag
                    buf = session.read(buf);
                } else if (buf.buffer[5] == SSH_MSG_USERAUTH_GSSAPI_ERRTOK) {
                    buf = session.read(buf);
                }

                if (buf.buffer[5] == SSH_MSG_USERAUTH_FAILURE) {
                    return false;
                }

                buf.getInt();
                buf.getByte();
                buf.getByte();
                token = buf.getString();
            }
        }

        Buffer mbuf = new Buffer();
        // string session identifier
        // byte SSH_MSG_USERAUTH_REQUEST
        // string user name
        // string service
        // string "gssapi-with-mic"
        mbuf.putString(session.getSessionId());
        mbuf.putByte((byte) SSH_MSG_USERAUTH_REQUEST);
        mbuf.putString(_username);
        mbuf.putString("ssh-connection".getBytes());
        mbuf.putString("gssapi-with-mic".getBytes());

        byte[] mic = context.getMIC(mbuf.buffer, 0, mbuf.getLength());

        if (mic == null) { // there was an error in the getMIC call
            return false;
        }

        packet.reset();
        buf.putByte((byte) SSH_MSG_USERAUTH_GSSAPI_MIC);
        buf.putString(mic);
        session.write(packet);

        context.dispose();

        buf = session.read(buf);
        if (buf.buffer[5] == SSH_MSG_USERAUTH_SUCCESS) {
            return true;
        }
        if (buf.buffer[5] == SSH_MSG_USERAUTH_FAILURE) {
            buf.getInt();
            buf.getByte();
            buf.getByte();
            byte[] foo = buf.getString();
            int partial_success = buf.getByte();
            if (partial_success != 0) {
                throw new JSchPartialAuthException(new String(foo));
            }
        }
        return false;
    }
}


text:
export * from './cluster.service';
export * from './task.matcher';
export * from './filter/clusterFilter.service';


text:
package msgraph

import "context"

// CartToClassAssociationRequestBuilder is request builder for CartToClassAssociation
type CartToClassAssociationRequestBuilder struct{ BaseRequestBuilder }

// Request returns CartToClassAssociationRequest
func (b *CartToClassAssociationRequestBuilder) Request() *CartToClassAssociationRequest {
	return &CartToClassAssociationRequest{
		BaseRequest: BaseRequest{baseURL: b.baseURL, client: b.client},
	}
}

// CartToClassAssociationRequest is request for CartToClassAssociation
type CartToClassAssociationRequest struct{ BaseRequest }

// Get performs GET request for CartToClassAssociation
func (r *CartToClassAssociationRequest) Get(ctx context.Context) (resObj *CartToClassAssociation, err error) {
	var query string
	if r.query != nil {
		query = "?" + r.query.Encode()
	}
	err = r.JSONRequest(ctx, "GET", query, nil, &resObj)
	return
}

// Update performs PATCH request for CartToClassAssociation
func (r *CartToClassAssociationRequest) Update(ctx context.Context, reqObj *CartToClassAssociation) error {
	return r.JSONRequest(ctx, "PATCH", "", reqObj, nil)
}

// Delete performs DELETE request for CartToClassAssociation
func (r *CartToClassAssociationRequest) Delete(ctx context.Context) error {
	return r.JSONRequest(ctx, "DELETE", "", nil, nil)
}


text:
package net.jxse.OSGi.Services;

import java.util.logging.Logger;
import net.jxse.configuration.JxsePeerConfiguration;
import net.jxta.configuration.JxtaConfigurationException;
import net.jxta.platform.NetworkManager;

/**
 * This API class declares the contract from which an instance
 * of the {@code NetworkManager} service can be obtained from OSGi.
 *
 * <p>This JXSE OSGi service should be made available when
 * the OSGi framework is started and removed when the framework is
 * stopped. In other words, it should always be available while the
 * Jxse OSGi framework is active.
 */
public abstract class JxseOSGiNetworkManagerService extends JxseOSGiService {

    /**
     *  Logger
     */
    private final static Logger LOG = Logger.getLogger(JxseOSGiNetworkManagerService.class.getName());

    /**
     * Set the peer configuration if the {@code NetworkManager} is not in a started status, else
     * throws a {@code JxtaConfigurationException}.
     *
     * @param config The peer configuration.
     * @throws JxtaConfigurationException if the {@code NetworkManager} is in a started status.
     */
    public abstract void setPeerConfiguration(JxsePeerConfiguration config) throws JxtaConfigurationException;

    /**
     * Returns a copy of the peer configuration or {@code null} if not available.
     *
     * @return a peer configuration or {@code null}.
     */
    public abstract JxsePeerConfiguration getPeerConfigurationCopy();

    /**
     * Returns the current instance of the {@code NetworkManager} if it is in a started status, else
     * creates a new instance and configures it with the current peer configuration.
     *
     * @return instance of a NetworkManager.
     * @throws Exception if an issue is encountered while retrieving the {@code NetworkManager}.
     */
    public abstract NetworkManager getConfiguredNetworkManager() throws Exception;

}


text:
package hamt64

import (
	"fmt"
	"strings"

	"github.com/lleo/go-hamt-key"
)

type collisionLeaf struct {
	kvs []key.KeyVal
}

func newCollisionLeaf(kvs []key.KeyVal) *collisionLeaf {
	leaf := new(collisionLeaf)
	leaf.kvs = append(leaf.kvs, kvs...)

	return leaf
}

func (l collisionLeaf) Hash60() key.HashVal60 {
	// valid because ALL l.kvs[*].Key.Hash60() MUST be the same key.HashVal60
	return l.kvs[0].Key.Hash60()
}

func (l collisionLeaf) String() string {
	var kvstrs = make([]string, len(l.kvs))
	for i := 0; i < len(l.kvs); i++ {
		kvstrs[i] = l.kvs[i].String()
	}
	var jkvstr = strings.Join(kvstrs, ",")

	return fmt.Sprintf("collisionLeaf{kvs:[]key.KeyVal{%s}}", jkvstr)
}

func (l collisionLeaf) get(key key.Key) (interface{}, bool) {
	for i := 0; i < len(l.kvs); i++ {
		if l.kvs[i].Key.Equals(key) {
			return l.kvs[i].Val, true
		}
	}
	return nil, false
}

func (l collisionLeaf) copy() *collisionLeaf {
	var nl = new(collisionLeaf)

	// keep key.KeyVal containers, only this splice is new
	nl.kvs = append(nl.kvs, l.kvs...)

	return nl
}

// put insertes a new key,val pair into the leaf node, and returns a new leaf
// and a bool representing if the new leaf is bigger (ie accumulated key/val pair).
func (l collisionLeaf) put(key_ key.Key, val interface{}) (leafI, bool) {
	var nl = l.copy()

	// check if key_ is exact match of current key
	// if exact match create new key.KeyVal container and update Val
	// and return new leaf & bool
	for i := 0; i < len(l.kvs); i++ {
		if nl.kvs[i].Key.Equals(key_) { // Key.Equal() checks equal-by-value

			// new key.KeyVal container, and keep the old l.kvs[i].Key object.
			nl.kvs[i] = key.KeyVal{l.kvs[i].Key, val}

			return nl, false // key,val was not added, merely replaced Val
		}
	}

	nl.kvs = append(nl.kvs, key.KeyVal{key_, val})
	return nl, true // key_,val was added
}

// del method searches current list of key.KeyVal objects, if key_ found
// remove matching key.KeyVal container, and return a new leafI, the removed
// value, and a bool indicating if the key_ was found&removed.
func (l collisionLeaf) del(key_ key.Key) (leafI, interface{}, bool) {

	if len(l.kvs) == 2 {
		// exhaustive search
		// if key_ found new leaf will be a flatLeaf.
		if l.kvs[0].Key.Equals(key_) {
			return newFlatLeaf(l.kvs[1].Key, l.kvs[1].Val), l.kvs[0].Val, true
		}
		if l.kvs[1].Key.Equals(key_) {
			return newFlatLeaf(l.kvs[0].Key, l.kvs[0].Val), l.kvs[1].Val, true
		}

		// key_ not found, hence no deletion occured
		return nil, nil, false
	}

	var nl = l.copy()

	for i := 0; i < len(l.kvs); i++ {
		if l.kvs[i].Key.Equals(key_) {
			var retVal = l.kvs[i].Val

			// removing the i'th element of a slice; wiki/SliceTricks "Delete"
			nl.kvs = append(nl.kvs[:i], nl.kvs[i+1:]...)

			return nl, retVal, true
		}
	}

	return nil, nil, false
}

func (l collisionLeaf) keyVals() []key.KeyVal {
	return l.kvs
}


text:
namespace password_manager {

class LogRouter;

// This interface is used by the password management code to receive and display
// logs about progress of actions like saving a password.
class LogManager {
 public:
  virtual ~LogManager() = default;

  // This method is called by a LogRouter, after the LogManager registers with
  // one. If |router_can_be_used| is true, logs can be sent to LogRouter after
  // the return from OnLogRouterAvailabilityChanged and will reach at least one
  // LogReceiver instance. If |router_can_be_used| is false, no logs should be
  // sent to the LogRouter.
  virtual void OnLogRouterAvailabilityChanged(bool router_can_be_used) = 0;

  // The owner of the LogManager can call this to start or end suspending the
  // logging, by setting |suspended| to true or false, respectively.
  virtual void SetSuspended(bool suspended) = 0;

  // Forward |text| for display to the LogRouter (if registered with one).
  virtual void LogSavePasswordProgress(const std::string& text) const = 0;

  // Returns true if logs recorded via LogSavePasswordProgress will be
  // displayed, and false otherwise.
  virtual bool IsLoggingActive() const = 0;

  // Returns the production code implementation of LogManager. If |log_router|
  // is null, the manager will do nothing. |notification_callback| will be
  // called every time the activity status of logging changes.
  static std::unique_ptr<LogManager> Create(
      LogRouter* log_router,
      base::Closure notification_callback);
};

}  // namespace password_manager

#endif  // COMPONENTS_PASSWORD_MANAGER_CORE_BROWSER_LOG_MANAGER_H_


text:
using System;
using System.Runtime.InteropServices;
using System.Text;

namespace USIEngineOnAWS
{
    // from http://qiita.com/y_minowa/items/685db9926dec0d6b711b

    /// <summary>
    /// iniファイル取り扱いのためのユーティリティクラス
    /// </summary>
    class InifileUtils
    {
        /// <summary>
        /// iniファイルのパスを保持
        /// </summary>
        private String filePath { get; set; }

        internal static class NativeMethods
        {
            // ==========================================================
            [DllImport("KERNEL32.DLL", CharSet = CharSet.Unicode)]
            public static extern uint
            GetPrivateProfileString(string lpAppName,
            string lpKeyName, string lpDefault,
            StringBuilder lpReturnedString, uint nSize,
            string lpFileName);

            [DllImport("KERNEL32.DLL", CharSet = CharSet.Unicode)]
            public static extern uint
                GetPrivateProfileInt(string lpAppName,
                string lpKeyName, int nDefault, string lpFileName);

            [DllImport("kernel32.dll", CharSet = CharSet.Unicode)]
            public static extern int WritePrivateProfileString(
                string lpApplicationName,
                string lpKeyName,
                string lpstring,
                string lpFileName);
            // ==========================================================
        }

        /// <summary>
        /// コンストラクタ(デフォルト)
        /// </summary>
        public InifileUtils()
        {
            this.filePath = AppDomain.CurrentDomain.BaseDirectory + "hogehoge.ini";
        }

        /// <summary>
        /// コンストラクタ(fileパスを指定する場合)
        /// </summary>
        /// <param name="filePath">iniファイルパス</param>
        public InifileUtils(String filePath)
        {
            this.filePath = filePath;
        }

        /// <summary>
        /// iniファイル中のセクションのキーを指定して、文字列を返す
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public String getValueString(String section, String key)
        {
            StringBuilder sb = new StringBuilder(1024);

            NativeMethods.GetPrivateProfileString(
                section,
                key,
                "",
                sb,
                Convert.ToUInt32(sb.Capacity),
                filePath);

            return sb.ToString();
        }

        /// <summary>
        /// iniファイル中のセクションのキーを指定して、整数値を返す
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        public int getValueInt(String section, String key)
        {
            return (int)NativeMethods.GetPrivateProfileInt(section, key, 0, filePath);
        }

        /// <summary>
        /// 指定したセクション、キーに数値を書き込む
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="val"></param>
        public void setValue(String section, String key, int val)
        {
            setValue(section, key, val.ToString());
        }

        /// <summary>
        /// 指定したセクション、キーに文字列を書き込む
        /// </summary>
        /// <param name="section"></param>
        /// <param name="key"></param>
        /// <param name="val"></param>
        public void setValue(String section, String key, String val)
        {
            NativeMethods.WritePrivateProfileString(section, key, val, filePath);
        }
    }
}


text:
import Route from '@ember/routing/route';
import { get, set } from '@ember/object';
import { inject as service } from '@ember/service';
import { on } from '@ember/object/evented';
import { hash } from 'rsvp';
import C from 'ui/utils/constants';

export default Route.extend({
  session: service(),
  store:   service(),

  model() {
    const store = get(this, 'store');

    return hash({
      apps: store.find('app', null, { forceReload: true }).then((apps) => {
        const out = [];
        const projectApp = apps.findBy('name', 'project-monitoring');

        if ( projectApp ) {
          out.push(projectApp);
        }

        return out;
      })
    });
  },

  setDefaultRoute: on('activate', function() {
    set(this, `session.${ C.SESSION.PROJECT_ROUTE }`, 'authenticated.project.monitoring.project-setting');
  }),
});


text:
require_relative "spec_helper"

describe "openstack-identity::server" do
  before { identity_stubs }
  describe "suse" do
    before do
      @chef_run = ::ChefSpec::ChefRunner.new ::OPENSUSE_OPTS
      @chef_run.converge "openstack-identity::server"
    end

    it "installs mysql python packages" do
      expect(@chef_run).to install_package "python-mysql"
    end

    it "installs postgresql python packages if explicitly told" do
      chef_run = ::ChefSpec::ChefRunner.new ::OPENSUSE_OPTS do |n|
        n.set["openstack"]["db"]["identity"]["db_type"] = "postgresql"
      end
      chef_run.converge "openstack-identity::server"

      expect(chef_run).to install_package "python-psycopg2"
    end

    it "installs memcache python packages" do
      expect(@chef_run).to install_package "python-python-memcached"
    end

    it "installs keystone packages" do
      expect(@chef_run).to upgrade_package "openstack-keystone"
    end

    it "starts keystone on boot" do
      expect(@chef_run).to set_service_to_start_on_boot "openstack-keystone"
    end

    describe "/etc/keystone" do
      before do
        @dir = @chef_run.directory "/etc/keystone"
      end

      it "has proper owner" do
        expect(@dir).to be_owned_by "openstack-keystone", "openstack-keystone"
      end
    end

    describe "/etc/keystone/ssl" do
      before do
        opts = ::OPENSUSE_OPTS.merge(:evaluate_guards => true)
        chef_run = ::ChefSpec::ChefRunner.new opts do |n|
          n.set["openstack"]["auth"]["strategy"] = "pki"
        end
        chef_run.converge "openstack-identity::server"
        @dir = chef_run.directory "/etc/keystone/ssl"
      end

      it "has proper owner" do
        expect(@dir).
          to be_owned_by "openstack-keystone", "openstack-keystone"
      end
    end

    it "deletes keystone.db" do
      expect(@chef_run).to delete_file "/var/lib/keystone/keystone.db"
    end

    it "runs pki setup" do
      opts = ::OPENSUSE_OPTS.merge(:evaluate_guards => true)
      chef_run = ::ChefSpec::ChefRunner.new opts do |n|
        n.set["openstack"]["auth"]["strategy"] = "pki"
      end
      chef_run.converge "openstack-identity::server"
      cmd = "keystone-manage pki_setup"

      expect(chef_run).to execute_command(cmd).with(
        :user => "openstack-keystone"
      )
    end

    describe "keystone.conf" do
      before do
        @template = @chef_run.template "/etc/keystone/keystone.conf"
      end

      it "has proper owner" do
        expect(@template).
          to be_owned_by "openstack-keystone", "openstack-keystone"
      end

      it "template contents" do
        pending "TODO: implement"
      end
    end

    describe "default_catalog.templates" do
      before do
        opts = ::OPENSUSE_OPTS.merge(:evaluate_guards => true)
        chef_run = ::ChefSpec::ChefRunner.new opts do |n|
          n.set["openstack"]["identity"]["catalog"]["backend"] = "templated"
        end
        chef_run.converge "openstack-identity::server"
        @template = chef_run.
          template "/etc/keystone/default_catalog.templates"
      end

      it "has proper owner" do
        expect(@template).
          to be_owned_by "openstack-keystone", "openstack-keystone"
      end

      it "template contents" do
        pending "TODO: implement"
      end
    end
  end
end


text:
/**
 * Created Mar 10, 2008
 */
package com.crawljax.util;

import junit.framework.TestCase;

import org.apache.commons.lang.StringUtils;

/**
 * Test the edit distance algorithm.
 *
 * @author mesbah
 * @version $Id: EditDistanceTest.java 297 2010-04-02 08:51:33Z frankgroeneveld $
 */
public class EditDistanceTest extends TestCase {

	/**
	 * Check if threshold calculation works.
	 */
	public void testGetThreshold() {
		String x = "<form>bl</form>";
		String y = "<form>blabla</form>";
		double p = 0.8;

		double expected = (2 * Math.max(x.length(), y.length()) * (1 - p));

		assertEquals(expected, EditDistance.getThreshold(x, y, p));
	}

	/**
	 * Check if clone detection algorithm works correctly.
	 */
	public void testIsClone() {
		String x = "<form>BL</form>";
		String y = "<form>blabla</form>";

		System.out.println(StringUtils.getLevenshteinDistance(x, y) + " Thesh: "
		        + EditDistance.getThreshold(x, y, 0.7));
		assertTrue(EditDistance.isClone(x, y, 0.0));
		assertTrue(EditDistance.isClone(x, y, 0.5));
		assertTrue(EditDistance.isClone(x, y, 0.7));
		assertTrue(EditDistance.isClone(x, y, 0.75));
		assertTrue(EditDistance.isClone(x, y, 0.84));
		assertFalse(EditDistance.isClone(x, y, 0.89));
		assertFalse(EditDistance.isClone(x, y, 0.893));
		assertFalse(EditDistance.isClone(x, y, 0.9));
		assertFalse(EditDistance.isClone(x, y, 1));

		boolean arg = false;

		try {
			EditDistance.isClone(x, y, -2);
		} catch (IllegalArgumentException e) {
			arg = true;
		}

		assertTrue(arg);

		arg = false;

		try {
			EditDistance.isClone(x, y, 2);
		} catch (IllegalArgumentException e) {
			arg = true;
		}

		assertTrue(arg);
	}
}


text:
CREATE TABLE [dbo].[ServerStatus] (
    [Offline]     BIT      CONSTRAINT [DF_ServerStatus_Offline] DEFAULT (0) NOT NULL,
    [SignupsOpen] INT      CONSTRAINT [DF_ServerStatus_SignupsOpen] DEFAULT (1) NOT NULL,
    [NotStarted]  BIT      CONSTRAINT [DF_ServerStatus_NotStarted] DEFAULT (1) NOT NULL,
    [StartDate]   DATETIME NULL,
    [EndGame]     BIT      NULL,
    [EndDate]     DATETIME NULL,
    [MaxPlayers]  INT      NULL
);


text:
class CreateOutputs < ActiveRecord::Migration[4.2]
  def change
    create_table :outputs do |t|
      t.text :text
      t.references :trigger, null: false, index: true, foreign_key: {
        on_delete: :restrict, on_update: :restrict
      }
      t.references :run, null: false, index: true, foreign_key: {
        on_delete: :restrict, on_update: :restrict
      }

      t.timestamps null: false
    end

    add_index :outputs, :created_at
  end
end


text:
<!DOCTYPE html>

<meta charset=UTF-8>
<title>ImageOptim Help</title>
<link rel="stylesheet" href="../../style.css" type="text/css" />
<meta name="AppleTitle" content="ImageOptim Help" />
<meta name="AppleIcon" content="../../ImageOptim.icns" />
<meta name="AppleFont" content="Lucida Grande,Helvetica,Arial" />
<meta name="description" content="How to use ImageOptim" />

            ImageOptim

            How to use

Drag'n'drop PNG or JPEG files or directories onto main table view in ImageOptim's window.

Wait until it finishes.

Profit! (from smaller files)

            Files will be overwritten with smaller versions. Unoptimized versions will be moved to Trash.

            You can also drop files onto ImageOptim's Dock icon.

            Learn how to change Preferences.

            Icons in file table

                    Image has been optimized successfully.

                    Everything went fine, but image couldn't be optimized any further.

                    Optimization of this file is in progress.

                    File is queued to be optimized after other files are finished.

                    There was an error during optimization. You'll find explanation by running Console.app.

            How does it work

            ImageOptim is a front-end (GUI) for these applications:

Zopfli

OptiPNG

PNGCrush

AdvPNG

JpegOptim

JpegTran

JpegRescan

and PNGOUT.

            ImageOptim runs them and automatically selects the smallest file.


text:
@interface NOCSExampleViewController : UIViewController

@end


text:
<?php

namespace KJSenchaTestAsset\Direct\Form;

use KJSencha\Annotation as Ext;

/**
 * Demonstrates uploading a file
 *
 * Mark the methods which handles a upload with Ext\Formhandler annotation
 */
class Upload
{
    /**
     * Upload File example
     *
     * Simply return the filenames which have been uploaded
     *
     * @Ext\Formhandler
     * @return array
     */
    public function uploadFile()
    {
        $files = array();

        foreach ($_FILES as $file) {
            $files[] = $file['name'];
        }

        return array(
            'success' => true,
            'msg' => 'File(s) succesfully uploaded:
' . implode('
', $files),
        );
    }

    /**
     * Upload File example
     *
     * Simply return the filenames which have been uploaded
     *
     * @Ext\Formhandler
     * @return array
     */
    public function emptyUpload()
    {
        return array();
    }
}


text:
// -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 4; tab-width: 4; -*-
// vim: set shiftwidth=4 softtabstop=4 expandtab:

/*
 *  Description:
 *    A class that allows creating an Splus named vector from c++.
 *
 */
#include "R_NamedVectorBase.h"
#include "util.h"

// #include <string.h>

using std::vector;
using std::string;

using namespace eolts;

/*
 * R "named" class
 */

R_NamedVectorBase::R_NamedVectorBase(int type, size_t length):
    _obj(0),_type(type),_length(length),_pindx(-1),_names(0)
{
    PROTECT_WITH_INDEX(_obj = Rf_allocVector(type,_length),&_pindx);
    _names = Rf_allocVector(STRSXP,_length);
    Rf_setAttrib(_obj,R_NamesSymbol,_names);
}

R_NamedVectorBase::R_NamedVectorBase(int type, SEXP obj):
    _obj(obj),_type(type),_length(0),_pindx(-1),_names(0)
{

    if (TYPEOF(obj) != type)
        PROTECT_WITH_INDEX(_obj = Rf_coerceVector(_obj,type),&_pindx);

    _length = Rf_xlength(_obj);
    _names = Rf_getAttrib(_obj,R_NamesSymbol);
    if (!_names || !Rf_isString(_names)) {
        _names = Rf_allocVector(STRSXP,_length);
        Rf_setAttrib(_obj,R_NamesSymbol,_names);
    }

    if ((size_t)Rf_xlength(_names) != _length) {
        Rf_warning("length of names of R_NamedVector is not equal to length of vector");
        _names = Rf_xlengthgets(_names,_length);
        Rf_setAttrib(_obj,R_NamesSymbol,_names);
    }
}

R_NamedVectorBase::~R_NamedVectorBase() {
#ifdef DEBUG
    Rprintf("~R_NamedVectorBase\n");
#endif
    if (_pindx >= 0) UNPROTECT(1);

}

void R_NamedVectorBase::setLength(size_t length)
{
    if (_length != length) {
        if (_pindx >= 0)
            REPROTECT(_obj = Rf_xlengthgets(_obj,length),_pindx);
        else
            PROTECT_WITH_INDEX(_obj = Rf_xlengthgets(_obj,length),&_pindx);
        _length = length;

        _names = Rf_xlengthgets(_names,_length);
        Rf_setAttrib(_obj,R_NamesSymbol,_names);
    }
}

vector<string> R_NamedVectorBase::getNames()
{
    vector<string> names;
    for (size_t i = 0; i < (size_t)Rf_xlength(_names); i++) {
        SEXP dn = STRING_ELT(_names,i);
        names.push_back(CHAR(dn));
    }
    return names;
}
void R_NamedVectorBase::setNames(const vector<string>& names)
{
    if ((size_t)Rf_xlength(_names) != _length) {
        Rprintf("Rf_xlength(_names)=%td, _length=%zd\n",
                Rf_xlength(_names),_length);
        Rf_error("bad length in R_NamedVectorBase::setNames");
    }

    if (_length != names.size())
        Rf_warning("bad length in R_NamedVectorBase::setNames");

    size_t i;
    for (i = 0; i < names.size() && i < _length; i++)
        SET_STRING_ELT(_names,i,Rf_mkChar(names[i].c_str()));
    for ( ; i < _length; i++)
        SET_STRING_ELT(_names,i,NA_STRING);
}


text:
module cethSpecs
end

require 'spec_helper'
require 'uri'

describe Seth::ceth do
  before(:each) do
    Seth::Log.logger = Logger.new(StringIO.new)

    Seth::Config[:node_name]  = "webmonkey.example.com"

    # Prevent gratuitous code reloading:
    Seth::ceth.stub(:load_commands)
    @ceth = Seth::ceth.new
    @ceth.ui.stub(:puts)
    @ceth.ui.stub(:print)
    Seth::Log.stub(:init)
    Seth::Log.stub(:level)
    [:debug, :info, :warn, :error, :crit].each do |level_sym|
      Seth::Log.stub(level_sym)
    end
    Seth::ceth.stub(:puts)
    @stdout = StringIO.new
  end

  describe "selecting a config file" do
    context "when the current working dir is inside a symlinked directory" do
      before do
        Seth::ceth.reset_config_path!
        # pwd according to your shell is /home/someuser/prod/seth-repo, but
        # seth-repo is a symlink to /home/someuser/codes/seth-repo
        if Seth::Platform.windows?
          ENV.should_receive(:[]).with("CD").and_return("/home/someuser/prod/seth-repo")
        else
          ENV.should_receive(:[]).with("PWD").and_return("/home/someuser/prod/seth-repo")
        end

        Dir.stub(:pwd).and_return("/home/someuser/codes/seth-repo")
      end

      after do
        Seth::ceth.reset_config_path!
      end

      it "loads the config from the non-dereferenced directory path" do
        File.should_receive(:exist?).with("/home/someuser/prod/seth-repo/.seth").and_return(false)
        File.should_receive(:exist?).with("/home/someuser/prod/.seth").and_return(true)
        File.should_receive(:directory?).with("/home/someuser/prod/.seth").and_return(true)
        Seth::ceth.seth_config_dir.should == "/home/someuser/prod/.seth"
      end
    end
  end

  describe "after loading a subcommand" do
    before do
      Seth::ceth.reset_subcommands!

      if cethSpecs.const_defined?(:TestNameMapping)
        cethSpecs.send(:remove_const, :TestNameMapping)
      end

      if cethSpecs.const_defined?(:TestExplicitCategory)
        cethSpecs.send(:remove_const, :TestExplicitCategory)
      end

      Kernel.load(File.join(seth_SPEC_DATA, 'ceth_subcommand', 'test_name_mapping.rb'))
      Kernel.load(File.join(seth_SPEC_DATA, 'ceth_subcommand', 'test_explicit_category.rb'))
    end

    it "has a category based on its name" do
      cethSpecs::TestNameMapping.subcommand_category.should == 'test'
    end

    it "has an explictly defined category if set" do
      cethSpecs::TestExplicitCategory.subcommand_category.should == 'cookbook site'
    end

    it "can reference the subcommand by its snake cased name" do
      Seth::ceth.subcommands['test_name_mapping'].should equal(cethSpecs::TestNameMapping)
    end

    it "lists subcommands by category" do
      Seth::ceth.subcommands_by_category['test'].should include('test_name_mapping')
    end

    it "lists subcommands by category when the subcommands have explicit categories" do
      Seth::ceth.subcommands_by_category['cookbook site'].should include('test_explicit_category')
    end

    it "has empty dependency_loader list by default" do
      cethSpecs::TestNameMapping.dependency_loaders.should be_empty
    end
  end

  describe "after loading all subcommands" do
    before do
      Seth::ceth.reset_subcommands!
      Seth::ceth.load_commands
    end

    it "references a subcommand class by its snake cased name" do
      class SuperAwesomeCommand < Seth::ceth
      end

      Seth::ceth.load_commands

      Seth::ceth.subcommands.should have_key("super_awesome_command")
      Seth::ceth.subcommands["super_awesome_command"].should == SuperAwesomeCommand
    end

    it "guesses a category from a given ARGV" do
      Seth::ceth.subcommands_by_category["cookbook"] << :cookbook
      Seth::ceth.subcommands_by_category["cookbook site"] << :cookbook_site
      Seth::ceth.guess_category(%w{cookbook foo bar baz}).should == 'cookbook'
      Seth::ceth.guess_category(%w{cookbook site foo bar baz}).should == 'cookbook site'
      Seth::ceth.guess_category(%w{cookbook site --help}).should == 'cookbook site'
    end

    it "finds a subcommand class based on ARGV" do
      Seth::ceth.subcommands["cookbook_site_vendor"] = :CookbookSiteVendor
      Seth::ceth.subcommands["cookbook"] = :Cookbook
      Seth::ceth.subcommand_class_from(%w{cookbook site vendor --help foo bar baz}).should == :CookbookSiteVendor
    end

  end

  describe "the headers include X-Remote-Request-Id" do

    let(:headers) {{"Accept"=>"application/json",
                    "Accept-Encoding"=>"gzip;q=1.0,deflate;q=0.6,identity;q=0.3",
                    'X-Seth-Version' => seth::VERSION,
                    "Host"=>"api.opscode.piab",
                    "X-REMOTE-REQUEST-ID"=>request_id}}

    let(:request_id) {"1234"}

    let(:request_mock) { {} }

    let(:rest) do
      Net::HTTP.stub(:new).and_return(http_client)
      Seth::RequestID.instance.stub(:request_id).and_return(request_id)
      Seth::Config.stub(:seth_server_url).and_return("https://api.opscode.piab")
      command = Seth::ceth.run(%w{test yourself})
      rest = command.noauth_rest
      rest
    end

    let!(:http_client) do
      http_client = Net::HTTP.new(url.host, url.port)
      http_client.stub(:request).and_yield(http_response).and_return(http_response)
      http_client
    end

    let(:url) { URI.parse("https://api.opscode.piab") }

    let(:http_response) do
      http_response = Net::HTTPSuccess.new("1.1", "200", "successful rest req")
      http_response.stub(:read_body)
      http_response.stub(:body).and_return(body)
      http_response["Content-Length"] = body.bytesize.to_s
      http_response
    end

    let(:body) { "ninja" }

    before(:each) do
      Seth::Config[:seth_server_url] = "https://api.opscode.piab"
      if cethSpecs.const_defined?(:TestYourself)
        cethSpecs.send :remove_const, :TestYourself
      end
      Kernel.load(File.join(seth_SPEC_DATA, 'ceth_subcommand', 'test_yourself.rb'))
      Seth::ceth.subcommands.each { |name, klass| seth::ceth.subcommands.delete(name) unless klass.kind_of?(Class) }
    end

    it "confirms that the headers include X-Remote-Request-Id" do
      Net::HTTP::Get.should_receive(:new).with("/monkey", headers).and_return(request_mock)
      rest.get_rest("monkey")
    end
  end

  describe "when running a command" do
    before(:each) do
      if cethSpecs.const_defined?(:TestYourself)
        cethSpecs.send :remove_const, :TestYourself
      end
      Kernel.load(File.join(seth_SPEC_DATA, 'ceth_subcommand', 'test_yourself.rb'))
      Seth::ceth.subcommands.each { |name, klass| seth::ceth.subcommands.delete(name) unless klass.kind_of?(Class) }
    end

    it "merges the global ceth CLI options" do
      extra_opts = {}
      extra_opts[:editor] = {:long=>"--editor EDITOR",
                             :description=>"Set the editor to use for interactive commands",
                             :short=>"-e EDITOR",
                             :default=>"/usr/bin/vim"}

      # there is special hackery to return the subcommand instance going on here.
      command = Seth::ceth.run(%w{test yourself}, extra_opts)
      editor_opts = command.options[:editor]
      editor_opts[:long].should         == "--editor EDITOR"
      editor_opts[:description].should  == "Set the editor to use for interactive commands"
      editor_opts[:short].should        == "-e EDITOR"
      editor_opts[:default].should      == "/usr/bin/vim"
    end

    it "creates an instance of the subcommand and runs it" do
      command = Seth::ceth.run(%w{test yourself})
      command.should be_an_instance_of(cethSpecs::TestYourself)
      command.ran.should be_true
    end

    it "passes the command specific args to the subcommand" do
      command = Seth::ceth.run(%w{test yourself with some args})
      command.name_args.should == %w{with some args}
    end

    it "excludes the command name from the name args when parts are joined with underscores" do
      command = Seth::ceth.run(%w{test_yourself with some args})
      command.name_args.should == %w{with some args}
    end

    it "exits if no subcommand matches the CLI args" do
      Seth::ceth.ui.stub(:stdout).and_return(@stdout)
      Seth::ceth.ui.should_receive(:fatal)
      lambda {Seth::ceth.run(%w{fuuu uuuu fuuuu})}.should raise_error(SystemExit) { |e| e.status.should_not == 0 }
    end

    it "loads lazy dependencies" do
      command = Seth::ceth.run(%w{test yourself})
      cethSpecs::TestYourself.test_deps_loaded.should be_true
    end

    it "loads lazy dependencies from multiple deps calls" do
      other_deps_loaded = false
      cethSpecs::TestYourself.class_eval do
        deps { other_deps_loaded = true }
      end
      command = Seth::ceth.run(%w{test yourself})
      cethSpecs::TestYourself.test_deps_loaded.should be_true
      other_deps_loaded.should be_true
    end

    describe "merging configuration options" do
      before do
        cethSpecs::TestYourself.option(:opt_with_default,
                                        :short => "-D VALUE",
                                        :default => "default-value")
      end

      it "prefers the default value if no config or command line value is present" do
        ceth_command = cethSpecs::TestYourself.new([]) #empty argv
        ceth_command.configure_seth
        ceth_command.config[:opt_with_default].should == "default-value"
      end

      it "prefers a value in Seth::Config[:ceth] to the default" do
        Seth::Config[:ceth][:opt_with_default] = "from-ceth-config"
        ceth_command = cethSpecs::TestYourself.new([]) #empty argv
        ceth_command.configure_seth
        ceth_command.config[:opt_with_default].should == "from-ceth-config"
      end

      it "prefers a value from command line over Seth::Config and the default" do
        Seth::Config[:ceth][:opt_with_default] = "from-ceth-config"
        ceth_command = cethSpecs::TestYourself.new(["-D", "from-cli"])
        ceth_command.configure_seth
        ceth_command.config[:opt_with_default].should == "from-cli"
      end
    end

  end

  describe "when first created" do
    before do
      unless cethSpecs.const_defined?(:TestYourself)
        Kernel.load(File.join(seth_SPEC_DATA, 'ceth_subcommand', 'test_yourself.rb'))
      end
      @ceth = cethSpecs::TestYourself.new(%w{with some args -s scrogramming})
    end

    it "it parses the options passed to it" do
      @ceth.config[:scro].should == 'scrogramming'
    end

    it "extracts its command specific args from the full arg list" do
      @ceth.name_args.should == %w{with some args}
    end

    it "does not have lazy dependencies loaded" do
      @ceth.class.test_deps_loaded.should_not be_true
    end
  end

  describe "when formatting exceptions" do
    before do
      @stdout, @stderr, @stdin = StringIO.new, StringIO.new, StringIO.new
      @ceth.ui = Seth::ceth::UI.new(@stdout, @stderr, @stdin, {})
      @ceth.should_receive(:exit).with(100)
    end

    it "formats 401s nicely" do
      response = Net::HTTPUnauthorized.new("1.1", "401", "Unauthorized")
      response.instance_variable_set(:@read, true) # I hate you, net/http.
      response.stub(:body).and_return(Seth::JSONCompat.to_json(:error => "y u no syncronize your clock?"))
      @ceth.stub(:run).and_raise(Net::HTTPServerException.new("401 Unauthorized", response))
      @ceth.run_with_pretty_exceptions
      @stderr.string.should match(/ERROR: Failed to authenticate to/)
      @stdout.string.should match(/Response:  y u no syncronize your clock\?/)
    end

    it "formats 403s nicely" do
      response = Net::HTTPForbidden.new("1.1", "403", "Forbidden")
      response.instance_variable_set(:@read, true) # I hate you, net/http.
      response.stub(:body).and_return(Seth::JSONCompat.to_json(:error => "y u no administrator"))
      @ceth.stub(:run).and_raise(Net::HTTPServerException.new("403 Forbidden", response))
      @ceth.stub(:username).and_return("sadpanda")
      @ceth.run_with_pretty_exceptions
      @stderr.string.should match(%r[ERROR: You authenticated successfully to http.+ as sadpanda but you are not authorized for this action])
      @stdout.string.should match(%r[Response:  y u no administrator])
    end

    it "formats 400s nicely" do
      response = Net::HTTPBadRequest.new("1.1", "400", "Bad Request")
      response.instance_variable_set(:@read, true) # I hate you, net/http.
      response.stub(:body).and_return(Seth::JSONCompat.to_json(:error => "y u search wrong"))
      @ceth.stub(:run).and_raise(Net::HTTPServerException.new("400 Bad Request", response))
      @ceth.run_with_pretty_exceptions
      @stderr.string.should match(%r[ERROR: The data in your request was invalid])
      @stdout.string.should match(%r[Response: y u search wrong])
    end

    it "formats 404s nicely" do
      response = Net::HTTPNotFound.new("1.1", "404", "Not Found")
      response.instance_variable_set(:@read, true) # I hate you, net/http.
      response.stub(:body).and_return(Seth::JSONCompat.to_json(:error => "nothing to see here"))
      @ceth.stub(:run).and_raise(Net::HTTPServerException.new("404 Not Found", response))
      @ceth.run_with_pretty_exceptions
      @stderr.string.should match(%r[ERROR: The object you are looking for could not be found])
      @stdout.string.should match(%r[Response: nothing to see here])
    end

    it "formats 500s nicely" do
      response = Net::HTTPInternalServerError.new("1.1", "500", "Internal Server Error")
      response.instance_variable_set(:@read, true) # I hate you, net/http.
      response.stub(:body).and_return(Seth::JSONCompat.to_json(:error => "sad trombone"))
      @ceth.stub(:run).and_raise(Net::HTTPFatalError.new("500 Internal Server Error", response))
      @ceth.run_with_pretty_exceptions
      @stderr.string.should match(%r[ERROR: internal server error])
      @stdout.string.should match(%r[Response: sad trombone])
    end

    it "formats 502s nicely" do
      response = Net::HTTPBadGateway.new("1.1", "502", "Bad Gateway")
      response.instance_variable_set(:@read, true) # I hate you, net/http.
      response.stub(:body).and_return(Seth::JSONCompat.to_json(:error => "sadder trombone"))
      @ceth.stub(:run).and_raise(Net::HTTPFatalError.new("502 Bad Gateway", response))
      @ceth.run_with_pretty_exceptions
      @stderr.string.should match(%r[ERROR: bad gateway])
      @stdout.string.should match(%r[Response: sadder trombone])
    end

    it "formats 503s nicely" do
      response = Net::HTTPServiceUnavailable.new("1.1", "503", "Service Unavailable")
      response.instance_variable_set(:@read, true) # I hate you, net/http.
      response.stub(:body).and_return(Seth::JSONCompat.to_json(:error => "saddest trombone"))
      @ceth.stub(:run).and_raise(Net::HTTPFatalError.new("503 Service Unavailable", response))
      @ceth.run_with_pretty_exceptions
      @stderr.string.should match(%r[ERROR: Service temporarily unavailable])
      @stdout.string.should match(%r[Response: saddest trombone])
    end

    it "formats other HTTP errors nicely" do
      response = Net::HTTPPaymentRequired.new("1.1", "402", "Payment Required")
      response.instance_variable_set(:@read, true) # I hate you, net/http.
      response.stub(:body).and_return(Seth::JSONCompat.to_json(:error => "nobugfixtillyoubuy"))
      @ceth.stub(:run).and_raise(Net::HTTPServerException.new("402 Payment Required", response))
      @ceth.run_with_pretty_exceptions
      @stderr.string.should match(%r[ERROR: Payment Required])
      @stdout.string.should match(%r[Response: nobugfixtillyoubuy])
    end

    it "formats NameError and NoMethodError nicely" do
      @ceth.stub(:run).and_raise(NameError.new("Undefined constant FUUU"))
      @ceth.run_with_pretty_exceptions
      @stderr.string.should match(%r[ERROR: ceth encountered an unexpected error])
      @stdout.string.should match(%r[This may be a bug in the 'ceth' ceth command or plugin])
      @stdout.string.should match(%r[Exception: NameError: Undefined constant FUUU])
    end

    it "formats missing private key errors nicely" do
      @ceth.stub(:run).and_raise(Seth::Exceptions::PrivateKeyMissing.new('key not there'))
      @ceth.stub(:api_key).and_return("/home/root/.seth/no-key-here.pem")
      @ceth.run_with_pretty_exceptions
      @stderr.string.should match(%r[ERROR: Your private key could not be loaded from /home/root/.seth/no-key-here.pem])
      @stdout.string.should match(%r[Check your configuration file and ensure that your private key is readable])
    end

    it "formats connection refused errors nicely" do
      @ceth.stub(:run).and_raise(Errno::ECONNREFUSED.new('y u no shut up'))
      @ceth.run_with_pretty_exceptions
      # Errno::ECONNREFUSED message differs by platform
      # *nix = Errno::ECONNREFUSED: Connection refused
      # win32: Errno::ECONNREFUSED: No connection could be made because the target machine actively refused it.
      @stderr.string.should match(%r[ERROR: Network Error: .* - y u no shut up])
      @stdout.string.should match(%r[Check your ceth configuration and network settings])
    end
  end

end


text:
// Automatically generated from TypeScript type definitions provided by
// DefinitelyTyped (https://github.com/DefinitelyTyped/DefinitelyTyped),
// which is licensed under the MIT license; see file DefinitelyTyped-LICENSE
// in parent directory.
// Type definitions for Node.js 10.5.x
// Project: http://nodejs.org/
// Definitions by: Microsoft TypeScript <http://typescriptlang.org>
//                 DefinitelyTyped <https://github.com/DefinitelyTyped/DefinitelyTyped>
//                 Parambir Singh <https://github.com/parambirs>
//                 Christian Vaagland Tellnes <https://github.com/tellnes>
//                 Wilco Bakker <https://github.com/WilcoBakker>
//                 Nicolas Voigt <https://github.com/octo-sniffle>
//                 Chigozirim C. <https://github.com/smac89>
//                 Flarna <https://github.com/Flarna>
//                 Mariusz Wiktorczyk <https://github.com/mwiktorczyk>
//                 wwwy3y3 <https://github.com/wwwy3y3>
//                 Deividas Bakanas <https://github.com/DeividasBakanas>
//                 Kelvin Jin <https://github.com/kjin>
//                 Alvis HT Tang <https://github.com/alvis>
//                 Sebastian Silbermann <https://github.com/eps1lon>
//                 Hannes Magnusson <https://github.com/Hannes-Magnusson-CK>
//                 Alberto Schiabel <https://github.com/jkomyno>
//                 Klaus Meinhardt <https://github.com/ajafff>
//                 Huw <https://github.com/hoo29>
//                 Nicolas Even <https://github.com/n-e>
//                 Bruno Scheufler <https://github.com/brunoscheufler>
//                 Mohsen Azimi <https://github.com/mohsen1>
//                 Hoàng Văn Khải <https://github.com/KSXGitHub>
//                 Alexander T. <https://github.com/a-tarasyuk>
//                 Lishude <https://github.com/islishude>
//                 Andrew Makarov <https://github.com/r3nya>
//                 Zane Hannan AU <https://github.com/ZaneHannanAU>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped

/**
 * @externs
 * @fileoverview Definitions for module "crypto"
 */

var crypto = {};

/**
 * @interface
 */
crypto.Certificate = function() {};

/**
 * @param {(string|Buffer)} spkac
 * @return {Buffer}
 */
crypto.Certificate.prototype.exportChallenge = function(spkac) {};

/**
 * @param {(string|Buffer)} spkac
 * @return {Buffer}
 */
crypto.Certificate.prototype.exportPublicKey = function(spkac) {};

/**
 * @param {Buffer} spkac
 * @return {boolean}
 */
crypto.Certificate.prototype.verifySpkac = function(spkac) {};

/**
 * @return {crypto.Certificate}
 */
crypto.Certificate = function() {};

/**
 * @return {crypto.Certificate}
 * @constructor
 */
crypto.Certificate = function() {};

/**
 * @type {boolean}
 */
crypto.fips;

/**
 * @interface
 */
crypto.CredentialDetails = function() {};

/**
 * @type {string}
 */
crypto.CredentialDetails.prototype.pfx;

/**
 * @type {string}
 */
crypto.CredentialDetails.prototype.key;

/**
 * @type {string}
 */
crypto.CredentialDetails.prototype.passphrase;

/**
 * @type {string}
 */
crypto.CredentialDetails.prototype.cert;

/**
 * @type {(string|Array<string>)}
 */
crypto.CredentialDetails.prototype.ca;

/**
 * @type {(string|Array<string>)}
 */
crypto.CredentialDetails.prototype.crl;

/**
 * @type {string}
 */
crypto.CredentialDetails.prototype.ciphers;

/**
 * @interface
 */
crypto.Credentials = function() {};

/**
 * @type {*}
 */
crypto.Credentials.prototype.context;

/**
 * @param {crypto.CredentialDetails} details
 * @return {crypto.Credentials}
 */
crypto.createCredentials = function(details) {};

/**
 * @param {string} algorithm
 * @return {crypto.Hash}
 */
crypto.createHash = function(algorithm) {};

/**
 * @param {string} algorithm
 * @param {(string|Buffer)} key
 * @return {crypto.Hmac}
 */
crypto.createHmac = function(algorithm, key) {};

/**
 * @interface
 * @extends {NodeJS.ReadWriteStream}
 */
crypto.Hash = function() {};

/**
 * @param {(string|Buffer)} data
 * @return {crypto.Hash}
 */
crypto.Hash.prototype.update = function(data) {};

/**
 * @param {(string|Buffer)} data
 * @param {(string)} input_encoding
 * @return {crypto.Hash}
 */
crypto.Hash.prototype.update = function(data, input_encoding) {};

/**
 * @return {Buffer}
 */
crypto.Hash.prototype.digest = function() {};

/**
 * @param {(string)} encoding
 * @return {string}
 */
crypto.Hash.prototype.digest = function(encoding) {};

/**
 * @interface
 * @extends {NodeJS.ReadWriteStream}
 */
crypto.Hmac = function() {};

/**
 * @param {(string|Buffer)} data
 * @return {crypto.Hmac}
 */
crypto.Hmac.prototype.update = function(data) {};

/**
 * @param {(string|Buffer)} data
 * @param {(string)} input_encoding
 * @return {crypto.Hmac}
 */
crypto.Hmac.prototype.update = function(data, input_encoding) {};

/**
 * @return {Buffer}
 */
crypto.Hmac.prototype.digest = function() {};

/**
 * @param {(string)} encoding
 * @return {string}
 */
crypto.Hmac.prototype.digest = function(encoding) {};

/**
 * @param {string} algorithm
 * @param {*} password
 * @return {crypto.Cipher}
 */
crypto.createCipher = function(algorithm, password) {};

/**
 * @param {string} algorithm
 * @param {*} key
 * @param {*} iv
 * @return {crypto.Cipher}
 */
crypto.createCipheriv = function(algorithm, key, iv) {};

/**
 * @interface
 * @extends {NodeJS.ReadWriteStream}
 */
crypto.Cipher = function() {};

/**
 * @param {Buffer} data
 * @return {Buffer}
 */
crypto.Cipher.prototype.update = function(data) {};

/**
 * @param {string} data
 * @param {(string)} input_encoding
 * @return {Buffer}
 */
crypto.Cipher.prototype.update = function(data, input_encoding) {};

/**
 * @param {Buffer} data
 * @param {*} input_encoding
 * @param {(string)} output_encoding
 * @return {string}
 */
crypto.Cipher.prototype.update = function(data, input_encoding, output_encoding) {};

/**
 * @param {string} data
 * @param {(string)} input_encoding
 * @param {(string)} output_encoding
 * @return {string}
 */
crypto.Cipher.prototype.update = function(data, input_encoding, output_encoding) {};

/**
 * @return {Buffer}
 */
crypto.Cipher.prototype.final = function() {};

/**
 * @param {string} output_encoding
 * @return {string}
 */
crypto.Cipher.prototype.final = function(output_encoding) {};

/**
 * @param {boolean=} auto_padding
 * @return {void}
 */
crypto.Cipher.prototype.setAutoPadding = function(auto_padding) {};

/**
 * @return {Buffer}
 */
crypto.Cipher.prototype.getAuthTag = function() {};

/**
 * @param {Buffer} buffer
 * @return {void}
 */
crypto.Cipher.prototype.setAAD = function(buffer) {};

/**
 * @param {string} algorithm
 * @param {*} password
 * @return {crypto.Decipher}
 */
crypto.createDecipher = function(algorithm, password) {};

/**
 * @param {string} algorithm
 * @param {*} key
 * @param {*} iv
 * @return {crypto.Decipher}
 */
crypto.createDecipheriv = function(algorithm, key, iv) {};

/**
 * @interface
 * @extends {NodeJS.ReadWriteStream}
 */
crypto.Decipher = function() {};

/**
 * @param {Buffer} data
 * @return {Buffer}
 */
crypto.Decipher.prototype.update = function(data) {};

/**
 * @param {string} data
 * @param {(string)} input_encoding
 * @return {Buffer}
 */
crypto.Decipher.prototype.update = function(data, input_encoding) {};

/**
 * @param {Buffer} data
 * @param {*} input_encoding
 * @param {(string)} output_encoding
 * @return {string}
 */
crypto.Decipher.prototype.update = function(data, input_encoding, output_encoding) {};

/**
 * @param {string} data
 * @param {(string)} input_encoding
 * @param {(string)} output_encoding
 * @return {string}
 */
crypto.Decipher.prototype.update = function(data, input_encoding, output_encoding) {};

/**
 * @return {Buffer}
 */
crypto.Decipher.prototype.final = function() {};

/**
 * @param {string} output_encoding
 * @return {string}
 */
crypto.Decipher.prototype.final = function(output_encoding) {};

/**
 * @param {boolean=} auto_padding
 * @return {void}
 */
crypto.Decipher.prototype.setAutoPadding = function(auto_padding) {};

/**
 * @param {Buffer} tag
 * @return {void}
 */
crypto.Decipher.prototype.setAuthTag = function(tag) {};

/**
 * @param {Buffer} buffer
 * @return {void}
 */
crypto.Decipher.prototype.setAAD = function(buffer) {};

/**
 * @param {string} algorithm
 * @return {crypto.Signer}
 */
crypto.createSign = function(algorithm) {};

/**
 * @interface
 * @extends {NodeJS.WritableStream}
 */
crypto.Signer = function() {};

/**
 * @param {(string|Buffer)} data
 * @return {crypto.Signer}
 */
crypto.Signer.prototype.update = function(data) {};

/**
 * @param {(string|Buffer)} data
 * @param {(string)} input_encoding
 * @return {crypto.Signer}
 */
crypto.Signer.prototype.update = function(data, input_encoding) {};

/**
 * @param {(string|{key: string, passphrase: string})} private_key
 * @return {Buffer}
 */
crypto.Signer.prototype.sign = function(private_key) {};

/**
 * @param {(string|{key: string, passphrase: string})} private_key
 * @param {(string)} output_format
 * @return {string}
 */
crypto.Signer.prototype.sign = function(private_key, output_format) {};

/**
 * @param {string} algorith
 * @return {crypto.Verify}
 */
crypto.createVerify = function(algorith) {};

/**
 * @interface
 * @extends {NodeJS.WritableStream}
 */
crypto.Verify = function() {};

/**
 * @param {(string|Buffer)} data
 * @return {crypto.Verify}
 */
crypto.Verify.prototype.update = function(data) {};

/**
 * @param {(string|Buffer)} data
 * @param {(string)} input_encoding
 * @return {crypto.Verify}
 */
crypto.Verify.prototype.update = function(data, input_encoding) {};

/**
 * @param {string} object
 * @param {Buffer} signature
 * @return {boolean}
 */
crypto.Verify.prototype.verify = function(object, signature) {};

/**
 * @param {string} object
 * @param {string} signature
 * @param {(string)} signature_format
 * @return {boolean}
 */
crypto.Verify.prototype.verify = function(object, signature, signature_format) {};

/**
 * @param {number} prime_length
 * @param {number=} generator
 * @return {crypto.DiffieHellman}
 */
crypto.createDiffieHellman = function(prime_length, generator) {};

/**
 * @param {Buffer} prime
 * @return {crypto.DiffieHellman}
 */
crypto.createDiffieHellman = function(prime) {};

/**
 * @param {string} prime
 * @param {(string)} prime_encoding
 * @return {crypto.DiffieHellman}
 */
crypto.createDiffieHellman = function(prime, prime_encoding) {};

/**
 * @param {string} prime
 * @param {(string)} prime_encoding
 * @param {(number|Buffer)} generator
 * @return {crypto.DiffieHellman}
 */
crypto.createDiffieHellman = function(prime, prime_encoding, generator) {};

/**
 * @param {string} prime
 * @param {(string)} prime_encoding
 * @param {string} generator
 * @param {(string)} generator_encoding
 * @return {crypto.DiffieHellman}
 */
crypto.createDiffieHellman = function(prime, prime_encoding, generator, generator_encoding) {};

/**
 * @interface
 */
crypto.DiffieHellman = function() {};

/**
 * @return {Buffer}
 */
crypto.DiffieHellman.prototype.generateKeys = function() {};

/**
 * @param {(string)} encoding
 * @return {string}
 */
crypto.DiffieHellman.prototype.generateKeys = function(encoding) {};

/**
 * @param {Buffer} other_public_key
 * @return {Buffer}
 */
crypto.DiffieHellman.prototype.computeSecret = function(other_public_key) {};

/**
 * @param {string} other_public_key
 * @param {(string)} input_encoding
 * @return {Buffer}
 */
crypto.DiffieHellman.prototype.computeSecret = function(other_public_key, input_encoding) {};

/**
 * @param {string} other_public_key
 * @param {(string)} input_encoding
 * @param {(string)} output_encoding
 * @return {string}
 */
crypto.DiffieHellman.prototype.computeSecret = function(other_public_key, input_encoding, output_encoding) {};

/**
 * @return {Buffer}
 */
crypto.DiffieHellman.prototype.getPrime = function() {};

/**
 * @param {(string)} encoding
 * @return {string}
 */
crypto.DiffieHellman.prototype.getPrime = function(encoding) {};

/**
 * @return {Buffer}
 */
crypto.DiffieHellman.prototype.getGenerator = function() {};

/**
 * @param {(string)} encoding
 * @return {string}
 */
crypto.DiffieHellman.prototype.getGenerator = function(encoding) {};

/**
 * @return {Buffer}
 */
crypto.DiffieHellman.prototype.getPublicKey = function() {};

/**
 * @param {(string)} encoding
 * @return {string}
 */
crypto.DiffieHellman.prototype.getPublicKey = function(encoding) {};

/**
 * @return {Buffer}
 */
crypto.DiffieHellman.prototype.getPrivateKey = function() {};

/**
 * @param {(string)} encoding
 * @return {string}
 */
crypto.DiffieHellman.prototype.getPrivateKey = function(encoding) {};

/**
 * @param {Buffer} public_key
 * @return {void}
 */
crypto.DiffieHellman.prototype.setPublicKey = function(public_key) {};

/**
 * @param {string} public_key
 * @param {string} encoding
 * @return {void}
 */
crypto.DiffieHellman.prototype.setPublicKey = function(public_key, encoding) {};

/**
 * @param {Buffer} private_key
 * @return {void}
 */
crypto.DiffieHellman.prototype.setPrivateKey = function(private_key) {};

/**
 * @param {string} private_key
 * @param {string} encoding
 * @return {void}
 */
crypto.DiffieHellman.prototype.setPrivateKey = function(private_key, encoding) {};

/**
 * @type {number}
 */
crypto.DiffieHellman.prototype.verifyError;

/**
 * @param {string} group_name
 * @return {crypto.DiffieHellman}
 */
crypto.getDiffieHellman = function(group_name) {};

/**
 * @param {(string|Buffer)} password
 * @param {(string|Buffer)} salt
 * @param {number} iterations
 * @param {number} keylen
 * @param {string} digest
 * @param {(function(Error, Buffer): *)} callback
 * @return {void}
 */
crypto.pbkdf2 = function(password, salt, iterations, keylen, digest, callback) {};

/**
 * @param {(string|Buffer)} password
 * @param {(string|Buffer)} salt
 * @param {number} iterations
 * @param {number} keylen
 * @param {string} digest
 * @return {Buffer}
 */
crypto.pbkdf2Sync = function(password, salt, iterations, keylen, digest) {};

/**
 * @param {number} size
 * @return {Buffer}
 */
crypto.randomBytes = function(size) {};

/**
 * @param {number} size
 * @param {(function(Error, Buffer): void)} callback
 * @return {void}
 */
crypto.randomBytes = function(size, callback) {};

/**
 * @param {number} size
 * @return {Buffer}
 */
crypto.pseudoRandomBytes = function(size) {};

/**
 * @param {number} size
 * @param {(function(Error, Buffer): void)} callback
 * @return {void}
 */
crypto.pseudoRandomBytes = function(size, callback) {};

/**
 * @interface
 */
crypto.RsaPublicKey = function() {};

/**
 * @type {string}
 */
crypto.RsaPublicKey.prototype.key;

/**
 * @type {number}
 */
crypto.RsaPublicKey.prototype.padding;

/**
 * @interface
 */
crypto.RsaPrivateKey = function() {};

/**
 * @type {string}
 */
crypto.RsaPrivateKey.prototype.key;

/**
 * @type {string}
 */
crypto.RsaPrivateKey.prototype.passphrase;

/**
 * @type {number}
 */
crypto.RsaPrivateKey.prototype.padding;

/**
 * @param {(string|crypto.RsaPublicKey)} public_key
 * @param {Buffer} buffer
 * @return {Buffer}
 */
crypto.publicEncrypt = function(public_key, buffer) {};

/**
 * @param {(string|crypto.RsaPrivateKey)} private_key
 * @param {Buffer} buffer
 * @return {Buffer}
 */
crypto.privateDecrypt = function(private_key, buffer) {};

/**
 * @param {(string|crypto.RsaPrivateKey)} private_key
 * @param {Buffer} buffer
 * @return {Buffer}
 */
crypto.privateEncrypt = function(private_key, buffer) {};

/**
 * @param {(string|crypto.RsaPublicKey)} public_key
 * @param {Buffer} buffer
 * @return {Buffer}
 */
crypto.publicDecrypt = function(public_key, buffer) {};

/**
 * @return {Array<string>}
 */
crypto.getCiphers = function() {};

/**
 * @return {Array<string>}
 */
crypto.getCurves = function() {};

/**
 * @return {Array<string>}
 */
crypto.getHashes = function() {};

/**
 * @interface
 */
crypto.ECDH = function() {};

/**
 * @return {Buffer}
 */
crypto.ECDH.prototype.generateKeys = function() {};

/**
 * @param {(string)} encoding
 * @return {string}
 */
crypto.ECDH.prototype.generateKeys = function(encoding) {};

/**
 * @param {(string)} encoding
 * @param {(string)} format
 * @return {string}
 */
crypto.ECDH.prototype.generateKeys = function(encoding, format) {};

/**
 * @param {Buffer} other_public_key
 * @return {Buffer}
 */
crypto.ECDH.prototype.computeSecret = function(other_public_key) {};

/**
 * @param {string} other_public_key
 * @param {(string)} input_encoding
 * @return {Buffer}
 */
crypto.ECDH.prototype.computeSecret = function(other_public_key, input_encoding) {};

/**
 * @param {string} other_public_key
 * @param {(string)} input_encoding
 * @param {(string)} output_encoding
 * @return {string}
 */
crypto.ECDH.prototype.computeSecret = function(other_public_key, input_encoding, output_encoding) {};

/**
 * @return {Buffer}
 */
crypto.ECDH.prototype.getPrivateKey = function() {};

/**
 * @param {(string)} encoding
 * @return {string}
 */
crypto.ECDH.prototype.getPrivateKey = function(encoding) {};

/**
 * @return {Buffer}
 */
crypto.ECDH.prototype.getPublicKey = function() {};

/**
 * @param {(string)} encoding
 * @return {string}
 */
crypto.ECDH.prototype.getPublicKey = function(encoding) {};

/**
 * @param {(string)} encoding
 * @param {(string)} format
 * @return {string}
 */
crypto.ECDH.prototype.getPublicKey = function(encoding, format) {};

/**
 * @param {Buffer} private_key
 * @return {void}
 */
crypto.ECDH.prototype.setPrivateKey = function(private_key) {};

/**
 * @param {string} private_key
 * @param {(string)} encoding
 * @return {void}
 */
crypto.ECDH.prototype.setPrivateKey = function(private_key, encoding) {};

/**
 * @param {string} curve_name
 * @return {crypto.ECDH}
 */
crypto.createECDH = function(curve_name) {};

/**
 * @type {string}
 */
crypto.DEFAULT_ENCODING;

module.exports.Certificate = crypto.Certificate;

module.exports.Certificate = crypto.Certificate;

module.exports.fips = crypto.fips;

module.exports.CredentialDetails = crypto.CredentialDetails;

module.exports.Credentials = crypto.Credentials;

module.exports.createCredentials = crypto.createCredentials;

module.exports.createHash = crypto.createHash;

module.exports.createHmac = crypto.createHmac;

module.exports.Hash = crypto.Hash;

module.exports.Hmac = crypto.Hmac;

module.exports.createCipher = crypto.createCipher;

module.exports.createCipheriv = crypto.createCipheriv;

module.exports.Cipher = crypto.Cipher;

module.exports.createDecipher = crypto.createDecipher;

module.exports.createDecipheriv = crypto.createDecipheriv;

module.exports.Decipher = crypto.Decipher;

module.exports.createSign = crypto.createSign;

module.exports.Signer = crypto.Signer;

module.exports.createVerify = crypto.createVerify;

module.exports.Verify = crypto.Verify;

module.exports.createDiffieHellman = crypto.createDiffieHellman;

module.exports.createDiffieHellman = crypto.createDiffieHellman;

module.exports.createDiffieHellman = crypto.createDiffieHellman;

module.exports.createDiffieHellman = crypto.createDiffieHellman;

module.exports.createDiffieHellman = crypto.createDiffieHellman;

module.exports.DiffieHellman = crypto.DiffieHellman;

module.exports.getDiffieHellman = crypto.getDiffieHellman;

module.exports.pbkdf2 = crypto.pbkdf2;

module.exports.pbkdf2Sync = crypto.pbkdf2Sync;

module.exports.randomBytes = crypto.randomBytes;

module.exports.randomBytes = crypto.randomBytes;

module.exports.pseudoRandomBytes = crypto.pseudoRandomBytes;

module.exports.pseudoRandomBytes = crypto.pseudoRandomBytes;

module.exports.RsaPublicKey = crypto.RsaPublicKey;

module.exports.RsaPrivateKey = crypto.RsaPrivateKey;

module.exports.publicEncrypt = crypto.publicEncrypt;

module.exports.privateDecrypt = crypto.privateDecrypt;

module.exports.privateEncrypt = crypto.privateEncrypt;

module.exports.publicDecrypt = crypto.publicDecrypt;

module.exports.getCiphers = crypto.getCiphers;

module.exports.getCurves = crypto.getCurves;

module.exports.getHashes = crypto.getHashes;

module.exports.ECDH = crypto.ECDH;

module.exports.createECDH = crypto.createECDH;

module.exports.DEFAULT_ENCODING = crypto.DEFAULT_ENCODING;

/**
 * @interface
 * @extends {crypto.Signer}
 */
crypto.Sign = function() {};

/**
 * @param {number} size
 * @param {(function(Error, Buffer): *)=} callback
 * @return {void}
 */
crypto.rng = function(size, callback) {};

/**
 * @param {number} size
 * @param {(function(Error, Buffer): *)=} callback
 * @return {void}
 */
crypto.prng = function(size, callback) {};

/**
 * @interface
 * @extends {NodeJS.ReadWriteStream}
 */
crypto.Decipher = function() {};

/**
 * @param {string} output_encoding
 * @return {(string|Buffer)}
 */
crypto.Decipher.prototype.finaltol = function(output_encoding) {};

/**
 * @interface
 */
crypto.ECDH = function() {};

/**
 * @param {Buffer} private_key
 * @return {void}
 */
crypto.ECDH.prototype.setPublicKey = function(private_key) {};

/**
 * @param {string} private_key
 * @param {string} encoding
 * @return {void}
 */
crypto.ECDH.prototype.setPublicKey = function(private_key, encoding) {};

module.exports.Sign = crypto.Sign;

module.exports.rng = crypto.rng;

module.exports.prng = crypto.prng;

module.exports.Decipher = crypto.Decipher;

module.exports.ECDH = crypto.ECDH;